Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   8  use 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|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 */
  55                  $fact = MathTrig\Factorial::fact($i);
  56                  $summer += $mean ** $i / $fact;
  57              }
  58  
  59              return exp(0 - $mean) * $summer;
  60          }
  61          /** @var float */
  62          $fact = MathTrig\Factorial::fact($value);
  63  
  64          return (exp(0 - $mean) * $mean ** $value) / $fact;
  65      }
  66  }