[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2007-08-07
Medium Priority
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: http://usphp.com/manual/en/function.stats-cdf-binomial.php

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: http://en.wikipedia.org/wiki/Binomial_expansion

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

hernst42 earned 2000 total points
ID: 19651897
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 http://www.php.net/bc extenion

Author Comment

ID: 19696972

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 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