/** * Returns the probability mass function * * @param float $x The test value * @param float $p The probability of success per trial * @param int $n The number of trials * @return float The probability */ public static function getPmf($x, $p = 0.5, $n = 1) { return \PHPStats\Stats::combinations($n, $x) * pow($p, $x) * pow(1 - $p, $n - $x); }
/** Returns the cumulative distribution function, the probability of getting the test value or something below it @param float $x The test value @param int $L The population size @param int $m The number of interesting elements in the population @param int $n The number of draws from the population @return float The probability */ static function getCdf($x, $L = 1, $m = 1, $n = 1) { $x = floor($x); $L = floor($L); $m = floor($m); $n = floor($n); if ($L >= 1 && $m >= 0 && $n >= 0) { $sum = 0; for ($i = 0; $i <= $x; $i++) { $sum += \PHPStats\Stats::combinations($m, $i) * \PHPStats\Stats::combinations($L - $m, $n - $i) / \PHPStats\Stats::combinations($L, $n); } return $sum; } else { return 0.0; } }
public function test_combinations() { $this->assertEquals(1, Stats::combinations(1, 1)); $this->assertEquals(2, Stats::combinations(2, 1)); $this->assertEquals(6, Stats::combinations(4, 2)); $this->assertEquals(1, Stats::combinations(5, 5)); $this->assertEquals(56, Stats::combinations(8, 5)); }