# tablepress/3.4/libraries/vendor/PhpSpreadsheet/Calculation/Statistical/Distributions/HyperGeometric.php

TablePress – Tables in WordPress made easy, version 3.4. 76 lines.

- Page: https://pluginprobe.com/plugins/tablepress/3.4/code/libraries/vendor/PhpSpreadsheet/Calculation/Statistical/Distributions/HyperGeometric.php
- Raw: https://pluginprobe.com/plugins/tablepress/3.4/raw/libraries/vendor/PhpSpreadsheet/Calculation/Statistical/Distributions/HyperGeometric.php
- Modified: 2025-08-26T04:06:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/tablepress/3.4/code/libraries/vendor/PhpSpreadsheet/Calculation/Statistical/Distributions/HyperGeometric.php#L10-L20`.

```php
<?php

namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions;

use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception;
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Combinations;

class HyperGeometric
{
	use ArrayEnabled;

	/**
	 * HYPGEOMDIST.
	 *
	 * Returns the hypergeometric distribution. HYPGEOMDIST returns the probability of a given number of
	 * sample successes, given the sample size, population successes, and population size.
	 *
	 * @param mixed $sampleSuccesses Integer number of successes in the sample
	 *                      Or can be an array of values
	 * @param mixed $sampleNumber Integer size of the sample
	 *                      Or can be an array of values
	 * @param mixed $populationSuccesses Integer number of successes in the population
	 *                      Or can be an array of values
	 * @param mixed $populationNumber Integer population size
	 *                      Or can be an array of values
	 *
	 * @return array<mixed>|float|string If an array of numbers is passed as an argument, then the returned result will also be an array
	 *            with the same dimensions
	 */
	public static function distribution($sampleSuccesses, $sampleNumber, $populationSuccesses, $populationNumber)
	{
		if (
			is_array($sampleSuccesses) || is_array($sampleNumber)
			|| is_array($populationSuccesses) || is_array($populationNumber)
		) {
			return self::evaluateArrayArguments(
				[self::class, __FUNCTION__],
				$sampleSuccesses,
				$sampleNumber,
				$populationSuccesses,
				$populationNumber
			);
		}

		try {
			$sampleSuccesses = DistributionValidations::validateInt($sampleSuccesses);
			$sampleNumber = DistributionValidations::validateInt($sampleNumber);
			$populationSuccesses = DistributionValidations::validateInt($populationSuccesses);
			$populationNumber = DistributionValidations::validateInt($populationNumber);
		} catch (Exception $e) {
			return $e->getMessage();
		}

		if (($sampleSuccesses < 0) || ($sampleSuccesses > $sampleNumber) || ($sampleSuccesses > $populationSuccesses)) {
			return ExcelError::NAN();
		}
		if (($sampleNumber <= 0) || ($sampleNumber > $populationNumber)) {
			return ExcelError::NAN();
		}
		if (($populationSuccesses <= 0) || ($populationSuccesses > $populationNumber)) {
			return ExcelError::NAN();
		}

		$successesPopulationAndSample = (float) Combinations::withoutRepetition($populationSuccesses, $sampleSuccesses);
		$numbersPopulationAndSample = (float) Combinations::withoutRepetition($populationNumber, $sampleNumber);
		$adjustedPopulationAndSample = (float) Combinations::withoutRepetition(
			$populationNumber - $populationSuccesses,
			$sampleNumber - $sampleSuccesses
		);

		return $successesPopulationAndSample * $adjustedPopulationAndSample / $numbersPopulationAndSample;
	}
}

```
