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

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<c and it ==0 otherwise, where Gamma is the usual math gamma function and Beta is described at reference.wolfram.com/mathematica/ref/Beta.html. That might help you make a little progress. Commented Dec 26, 2013 at 23:18

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Where n,p and k stand for? Sorry I am a little confused :D
n and p are the parameters of binomial function, while k is your "30", try to look here: en.wikipedia.org/wiki/…
I believe you need i <= k in the for loop.
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

Comments