jesobreira

Binomial PHP

Feb 20th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.56 KB | None | 0 0
  1. <?php
  2.  
  3. // if GMP extension is not enabled
  4. // we must create a gmp_fact function
  5. if(!function_exists('gmp_fact')) {
  6.     function gmp_fact($x) {
  7.         $result = 1;
  8.         for($i = $x; $i > 1; $i--) {
  9.             $result *= $x;
  10.             $x--;
  11.         }
  12.         return $result;
  13.     }
  14. }
  15.  
  16. $varn = 5;
  17. $varx = 1;
  18. $varp = 0.15;
  19. $varq = 0.85;
  20.  
  21. // calculate n combination of x
  22. $comb = gmp_fact($varn)/(gmp_fact($varx)*gmp_fact($varn-$varx));
  23.  
  24. // calculate probability
  25. $prob = $comb*pow($varp, $varx)*pow($varq, $varn-$varx);
  26.  
  27. // result
  28. echo $prob;
Add Comment
Please, Sign In to add comment