need help with stats-cdf-binomial or any binomial expansion function

Posted on 2007-08-07
Last Modified: 2008-01-09
I am trying to do binomial expansions in PHP (example: the chance an event with 30% probability will occur 3 out of 5 times is 13%).

There is a formula (in the PECL stat library) that does binomial expansions. It is:

It is not documented, and so far I cannot figure out what the variables stand for by plugging in numbers. For example,

echo(stats_cdf_binomial(3,5,0.3,1) . '<br />');

will return a value but it isn't the right one, and juggling the numbers around so far hasn't worked.

Is there somewhere I can find out how to use this function? There is no documentation file in the library itself that I can find, or anywhere else.

I tried making my own binomial expansion function, which is just formula 1 on this page:

however my problem here is that for large values of n, n! is to large to calculate. (I need to use values of n > 500.) Also, the exponent terms can be too big for certain values of n and k. Presumably there is some way to get around this problem, but I don't know how to do it.

To solve this problem, I either need to know how to use stats-cdf-binomial (assuming it can handle large n and k) or I need another function to calculate binomial expansions. My current function to handle them (works fine for low n) is:

function bin_dist($this_many,$out_of_this_many,$success_probability) { // calculates binomial distribution

$probability = fact($out_of_this_many)*(pow($success_probability,$this_many))*(pow(1-$success_probability,$out_of_this_many-$this_many))/(fact($out_of_this_many - $this_many)*fact($this_many));

return $probability;

Question by:bitt3n
    LVL 48

    Accepted Solution

    From the source:

                  Cumulative Distribution Function
                             BINomial distribution


         Calculates any one parameter of the binomial
         distribution given values for the others.


         WHICH --> Integer indicating which of the next four argument
                   values is to be calculated from the others.
                   Legal range: 1..4
                   iwhich = 1 : Calculate P and Q from S,XN,PR and OMPR
                   iwhich = 2 : Calculate S from P,Q,XN,PR and OMPR
                   iwhich = 3 : Calculate XN from P,Q,S,PR and OMPR
                   iwhich = 4 : Calculate PR and OMPR from P,Q,S and XN

         P <--> The cumulation from 0 to S of the binomial distribution.
                (Probablility of S or fewer successes in XN trials each
                with probability of success PR.)
                Input range: [0,1].

         Q <--> 1-P.
                Input range: [0, 1].
                P + Q = 1.0.

         S <--> The number of successes observed.
                Input range: [0, XN]
                Search range: [0, XN]

         XN  <--> The number of binomial trials.
                  Input range: (0, +infinity).
                  Search range: [1E-100, 1E100]

         PR  <--> The probability of success in each binomial trial.
                  Input range: [0,1].
                  Search range: [0,1]

         OMPR  <--> 1-PR
                  Input range: [0,1].
                  Search range: [0,1]
                  PR + OMPR = 1.0

         STATUS <-- 0 if calculation completed correctly
                   -I if input parameter number I is out of range
                    1 if answer appears to be lower than lowest
                      search bound
                    2 if answer appears to be higher than greatest
                      search bound
                    3 if P + Q .ne. 1
                    4 if PR + OMPR .ne. 1

         BOUND <-- Undefined if STATUS is 0

                   Bound exceeded by parameter number I if STATUS
                   is negative.

                   Lower search bound if STATUS is 1.

    And here what the parameters mean depending on the which:
        if (which < 4)  {
            pr = arg3;
            ompr = 1.0 - pr;
        } else {
            xn = arg3;

        if (which < 3) {
            xn = arg2;
        } else {
            sn = arg2;

        if (which == 1) {
            sn = arg1;
        } else {
            p = arg1;
            q = 1.0 - p;

    For large number to use in php use the extenion

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now