tablepress
/
libraries
/
vendor
/
PhpSpreadsheet
/
Calculation
/
Statistical
/
Distributions
/
HyperGeometric.php
HyperGeometric.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/Statistical/Distributions/HyperGeometric.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\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<mixed>|float|string 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($sampleSuccesses, $sampleNumber, $populationSuccesses, $populationNumber) |
| 33 | { |
| 34 | if ( |
| 35 | is_array($sampleSuccesses) || is_array($sampleNumber) |
| 36 | || is_array($populationSuccesses) || is_array($populationNumber) |
| 37 | ) { |
| 38 | return self::evaluateArrayArguments( |
| 39 | [self::class, __FUNCTION__], |
| 40 | $sampleSuccesses, |
| 41 | $sampleNumber, |
| 42 | $populationSuccesses, |
| 43 | $populationNumber |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | try { |
| 48 | $sampleSuccesses = DistributionValidations::validateInt($sampleSuccesses); |
| 49 | $sampleNumber = DistributionValidations::validateInt($sampleNumber); |
| 50 | $populationSuccesses = DistributionValidations::validateInt($populationSuccesses); |
| 51 | $populationNumber = DistributionValidations::validateInt($populationNumber); |
| 52 | } catch (Exception $e) { |
| 53 | return $e->getMessage(); |
| 54 | } |
| 55 | |
| 56 | if (($sampleSuccesses < 0) || ($sampleSuccesses > $sampleNumber) || ($sampleSuccesses > $populationSuccesses)) { |
| 57 | return ExcelError::NAN(); |
| 58 | } |
| 59 | if (($sampleNumber <= 0) || ($sampleNumber > $populationNumber)) { |
| 60 | return ExcelError::NAN(); |
| 61 | } |
| 62 | if (($populationSuccesses <= 0) || ($populationSuccesses > $populationNumber)) { |
| 63 | return ExcelError::NAN(); |
| 64 | } |
| 65 | |
| 66 | $successesPopulationAndSample = (float) Combinations::withoutRepetition($populationSuccesses, $sampleSuccesses); |
| 67 | $numbersPopulationAndSample = (float) Combinations::withoutRepetition($populationNumber, $sampleNumber); |
| 68 | $adjustedPopulationAndSample = (float) Combinations::withoutRepetition( |
| 69 | $populationNumber - $populationSuccesses, |
| 70 | $sampleNumber - $sampleSuccesses |
| 71 | ); |
| 72 | |
| 73 | return $successesPopulationAndSample * $adjustedPopulationAndSample / $numbersPopulationAndSample; |
| 74 | } |
| 75 | } |
| 76 |