MathPHP\Functions\Support::checkLimits PHP 메소드

checkLimits() 공개 정적인 메소드

The parameter limits are defined using ISO 31-11 notation. https://en.wikipedia.org/wiki/ISO_31-11 (a,b) = a < x < b [a,b) = a <= x < b (a,b] = a < x <= b [a,b] = a <= x <= b
public static checkLimits ( array $limits, array $params ) : boolean
$limits array Boundary limit definitions for each parameter ['var1' => limit, 'var2' => limit, ...]
$params array Parameters and their value to check against the defined limits ['var1' => value, 'var2' => value, ...]
리턴 boolean True if all parameters are within defined limits
    public static function checkLimits(array $limits, array $params)
    {
        // All parameters should have limit bounds defined
        $undefined_limits = array_diff_key($params, $limits);
        if (!empty($undefined_limits)) {
            throw new Exception\BadParameterException('Parameter without bounds limit defined: ' . print_r($undefined_limits, true));
        }
        foreach ($params as $variable => $value) {
            // Remove the first character: ( or [
            $lower_endpoint = substr($limits[$variable], 0, 1);
            // Remove the last character: ) or ]
            $upper_endpoint = substr($limits[$variable], -1, 1);
            // Set the lower and upper limits: #,#
            list($lower_limit, $upper_limit) = explode(',', substr($limits[$variable], 1, -1));
            // If the lower limit is -∞, we are always in bounds.
            if ($lower_limit != "-∞") {
                switch ($lower_endpoint) {
                    case '(':
                        if ($value <= $lower_limit) {
                            throw new Exception\OutOfBoundsException("{$variable} must be > {$lower_limit}");
                        }
                        break;
                    case '[':
                        if ($value < $lower_limit) {
                            throw new Exception\OutOfBoundsException("{$variable} must be >= {$lower_limit}");
                        }
                        break;
                    default:
                        throw new Exception\BadDataException("Unknown lower endpoint character: {$lower_limit}");
                }
            }
            // If the upper limit is ∞, we are always in bounds.
            if ($upper_limit != "∞") {
                switch ($upper_endpoint) {
                    case ')':
                        if ($value >= $upper_limit) {
                            throw new Exception\OutOfBoundsException("{$variable} must be < {$upper_limit}");
                        }
                        break;
                    case ']':
                        if ($value > $upper_limit) {
                            throw new Exception\OutOfBoundsException("{$variable} must be <= {$upper_limit}");
                        }
                        break;
                    default:
                        throw new Exception\BadDataException("Unknown upper endpoint character: {$upper_endpoint}");
                }
            }
        }
        return true;
    }

Usage Example

예제 #1
0
 /**
  * Cumulative Poisson Probability (lower culmulative distribution) - CDF
  * The probability that the Poisson random variable is greater than some specified lower limit,
  * and less than some specified upper limit.
  *
  *           k  λˣℯ^⁻λ
  * P(k,λ) =  ∑  ------
  *          ₓ₌₀  xᵢ!
  *
  * @param  int   $k events in the interval
  * @param  float $λ average number of successful events per interval
  *
  * @return float The cumulative Poisson probability
  */
 public static function CDF(int $k, float $λ) : float
 {
     Support::checkLimits(self::LIMITS, ['k' => $k, 'λ' => $λ]);
     return array_sum(array_map(function ($k) use($λ) {
         return self::PMF($k, $λ);
     }, range(0, $k)));
 }
All Usage Examples Of MathPHP\Functions\Support::checkLimits