1

I have this formula based on mathematica:

2*CDF[BinomialDistribution[100, 1/2], 30] // N

But I have no idea how to convert it in PHP.. This is the original question:

https://math.stackexchange.com/questions/619136/calculate-the-probabilities-of-appearing-even-or-odd-numbers-in-a-predefined-set

Can anyone helping me converting the above formula in a working PHP function? Is this possible?

Thanks

Community
  • 1
  • 1
  • I assume you want to change some of those numbers and calculate the result. CDF[BinomialDistribution[a, b], c]==(Beta[1-b, a-Floor[c], 1+Floor[c]] Gamma[1+a])/(Gamma[a-Floor[c]] Gamma[1+Floor[c]]) when 0<=c<=a and it ==1 when a – Bill Dec 26 '13 at 23:18

2 Answers2

2

You have to implement it from scratch. Unfortunately I don't know PHP syntax, I write you C code which you can easily traduce yourself:

double cdfBinomial(int n ,double p ,int k){
    double sum = 0;
    for(int i = 0; i <= k; i++){
        sum+=combinations(n,i)*pow((double)p,(double)i)*pow((double)(1-p),(double)(n-i));
    }
    return sum;
}

You have also to implement combinations function like done here.

Community
  • 1
  • 1
HAL9000
  • 3,562
  • 3
  • 25
  • 47
1

Here is a demo using HAL9000's code in generic Mathematica form.

First the CFD version:

2*CDF[BinomialDistribution[100, 0.5], 30]

0.0000785014

And now generic form with the auxiliary combinations function:

combinations[n0_, k_] := Module[{n = n0},
  If[k > n, 0,
   r = 1; For[d = 1, d <= k, ++d,
    r *= n--;
    r /= d];
   r]]

n = 100; p = 0.5; k = 30;

sum = 0; For[i = 0, i <= k, i++,
 sum += combinations[n, i]*p^i*(1 - p)^(n - i)];

2*sum

0.0000785014

This page was useful in writing this post: The Binomial Distribution

Chris Degnen
  • 8,443
  • 2
  • 23
  • 40