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\Combinations;
   9  
  10  class HyperGeometric
  11  {
  12      use ArrayEnabled;
  13  
  14      /**
  15       * HYPGEOMDIST.
  16       *
  17       * Returns the hypergeometric distribution. HYPGEOMDIST returns the probability of a given number of
  18       * sample successes, given the sample size, population successes, and population size.
  19       *
  20       * @param mixed $sampleSuccesses Integer number of successes in the sample
  21       *                      Or can be an array of values
  22       * @param mixed $sampleNumber Integer size of the sample
  23       *                      Or can be an array of values
  24       * @param mixed $populationSuccesses Integer number of successes in the population
  25       *                      Or can be an array of values
  26       * @param mixed $populationNumber Integer population size
  27       *                      Or can be an array of values
  28       *
  29       * @return array|float|string
  30       *         If an array of numbers is passed as an argument, then the returned result will also be an array
  31       *            with the same dimensions
  32       */
  33      public static function distribution($sampleSuccesses, $sampleNumber, $populationSuccesses, $populationNumber)
  34      {
  35          if (
  36              is_array($sampleSuccesses) || is_array($sampleNumber) ||
  37              is_array($populationSuccesses) || is_array($populationNumber)
  38          ) {
  39              return self::evaluateArrayArguments(
  40                  [self::class, __FUNCTION__],
  41                  $sampleSuccesses,
  42                  $sampleNumber,
  43                  $populationSuccesses,
  44                  $populationNumber
  45              );
  46          }
  47  
  48          try {
  49              $sampleSuccesses = DistributionValidations::validateInt($sampleSuccesses);
  50              $sampleNumber = DistributionValidations::validateInt($sampleNumber);
  51              $populationSuccesses = DistributionValidations::validateInt($populationSuccesses);
  52              $populationNumber = DistributionValidations::validateInt($populationNumber);
  53          } catch (Exception $e) {
  54              return $e->getMessage();
  55          }
  56  
  57          if (($sampleSuccesses < 0) || ($sampleSuccesses > $sampleNumber) || ($sampleSuccesses > $populationSuccesses)) {
  58              return ExcelError::NAN();
  59          }
  60          if (($sampleNumber <= 0) || ($sampleNumber > $populationNumber)) {
  61              return ExcelError::NAN();
  62          }
  63          if (($populationSuccesses <= 0) || ($populationSuccesses > $populationNumber)) {
  64              return ExcelError::NAN();
  65          }
  66  
  67          $successesPopulationAndSample = (float) Combinations::withoutRepetition($populationSuccesses, $sampleSuccesses);
  68          $numbersPopulationAndSample = (float) Combinations::withoutRepetition($populationNumber, $sampleNumber);
  69          $adjustedPopulationAndSample = (float) Combinations::withoutRepetition(
  70              $populationNumber - $populationSuccesses,
  71              $sampleNumber - $sampleSuccesses
  72          );
  73  
  74          return $successesPopulationAndSample * $adjustedPopulationAndSample / $numbersPopulationAndSample;
  75      }
  76  }