MathPHP\Probability\Combinatorics::combinations PHP Метод

combinations() публичный статический Метод

Without repetition: (n) n! nCk = ( ) = ---------- (k) (n - k)!k! With repetition: (n) (n + k - 1)! nC'k = ( ) = ------------ (k) (n - 1)!k! http://mathworld.wolfram.com/BinomialCoefficient.html
public static combinations ( integer $n, integer $k, boolean $repetition = false ) : integer
$n integer
$k integer
$repetition boolean Whether to do n choose k with or without repetitions
Результат integer number of possible combinations of n objects taken k at a time
    public static function combinations(int $n, int $k, bool $repetition = false)
    {
        if ($n < 0) {
            throw new Exception\OutOfBoundsException('Cannot compute negative combinations.');
        }
        if ($k > $n) {
            throw new Exception\OutOfBoundsException('k cannot be larger than n.');
        }
        // nC'k with repetition
        if ($repetition) {
            $⟮n + k − 1⟯! = self::factorial($n + $k - 1);
            $⟮n − 1⟯!k! = self::factorial($n - 1) * self::factorial($k);
            return $⟮n + k − 1⟯! / $⟮n − 1⟯!k!;
        }
        // nCk without repetition
        $n! = self::factorial($n);
        $⟮n − k⟯!k! = self::factorial($n - $k) * self::factorial($k);
        return $n! / $⟮n − k⟯!k!;
    }

Usage Example

Пример #1
0
 /**
  * Binomial distribution - probability mass function
  * https://en.wikipedia.org/wiki/Binomial_distribution
  *
  * P(X = r) = nCr pʳ (1 - p)ⁿ⁻ʳ
  *
  * @param  int   $n number of events
  * @param  int   $r number of successful events
  * @param  float $p probability of success
  *
  * @return float
  */
 public static function PMF(int $n, int $r, float $p) : float
 {
     Support::checkLimits(self::LIMITS, ['n' => $n, 'r' => $r, 'p' => $p]);
     $nCr = Combinatorics::combinations($n, $r);
     $pʳ = pow($p, $r);
     $⟮1 − p⟯ⁿ⁻ʳ = pow(1 - $p, $n - $r);
     return $nCr * $pʳ * $⟮1 − p⟯ⁿ⁻ʳ;
 }
All Usage Examples Of MathPHP\Probability\Combinatorics::combinations