tablepress
/
libraries
/
vendor
/
PhpSpreadsheet
/
Calculation
/
Statistical
/
Distributions
/
Poisson.php
Poisson.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/Statistical/Distributions/Poisson.php
| 1 | <?php |
| 2 | |
| 3 | namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions; |
| 4 | |
| 5 | use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 | use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 7 | use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 8 | use TablePress\PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
| 9 | |
| 10 | class Poisson |
| 11 | { |
| 12 | use ArrayEnabled; |
| 13 | |
| 14 | /** |
| 15 | * POISSON. |
| 16 | * |
| 17 | * Returns the Poisson distribution. A common application of the Poisson distribution |
| 18 | * is predicting the number of events over a specific time, such as the number of |
| 19 | * cars arriving at a toll plaza in 1 minute. |
| 20 | * |
| 21 | * @param mixed $value Float value for which we want the probability |
| 22 | * Or can be an array of values |
| 23 | * @param mixed $mean Mean value as a float |
| 24 | * Or can be an array of values |
| 25 | * @param mixed $cumulative Boolean value indicating if we want the cdf (true) or the pdf (false) |
| 26 | * Or can be an array of values |
| 27 | * |
| 28 | * @return array<mixed>|float|string The result, or a string containing an error |
| 29 | * If an array of numbers is passed as an argument, then the returned result will also be an array |
| 30 | * with the same dimensions |
| 31 | */ |
| 32 | public static function distribution($value, $mean, $cumulative) |
| 33 | { |
| 34 | if (is_array($value) || is_array($mean) || is_array($cumulative)) { |
| 35 | return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $mean, $cumulative); |
| 36 | } |
| 37 | |
| 38 | try { |
| 39 | $value = DistributionValidations::validateFloat($value); |
| 40 | $mean = DistributionValidations::validateFloat($mean); |
| 41 | $cumulative = DistributionValidations::validateBool($cumulative); |
| 42 | } catch (Exception $e) { |
| 43 | return $e->getMessage(); |
| 44 | } |
| 45 | |
| 46 | if (($value < 0) || ($mean < 0)) { |
| 47 | return ExcelError::NAN(); |
| 48 | } |
| 49 | |
| 50 | if ($cumulative) { |
| 51 | $summer = 0; |
| 52 | $floor = floor($value); |
| 53 | for ($i = 0; $i <= $floor; ++$i) { |
| 54 | /** @var float $fact */ |
| 55 | $fact = MathTrig\Factorial::fact($i); |
| 56 | $summer += $mean ** $i / $fact; |
| 57 | } |
| 58 | |
| 59 | return exp(0 - $mean) * $summer; |
| 60 | } |
| 61 | /** @var float $fact */ |
| 62 | $fact = MathTrig\Factorial::fact($value); |
| 63 | |
| 64 | return (exp(0 - $mean) * $mean ** $value) / $fact; |
| 65 | } |
| 66 | } |
| 67 |