Search code examples
python-2.7cdf

Cumulative distribution function (hypergeometric)


I need to find out how to calculate the 'hypergeometric cdf':

I know how the function looks like and how it works but I have a few problems tipping the function into python:

def hypergeometricCDF(N,K,n,x):
"""
Call:
    p = hypergeometricCDF(N,K,n,x)
Input argument:
    N: integer
    K: integer
    n: integer
    x: integer
Output argument:
    p: float
Example:
    hypergeometricCDF(120,34,12,7)
    =>
    0.995786
"""
f=sum(range(x+1))
p = log_binomial_coeff(N-K,n-f) + log_binomial_coeff(K,f) - log_binomial_coeff(N,n)
return(p)

The problem is, how do I integrate the sum function from i to x? I tried it with sum(range(x+1)) but it won't work.


Solution

  • Try this:

    def hypergeometricCDF(N,K,n,x):
    """
    Call:
        p = hypergeometricCDF(N,K,n,x)
    Input argument:
        N: integer
        K: integer
        n: integer
        x: integer
    Output argument:
        p: float
    Example:
        hypergeometricCDF(120,34,12,7)
        =>
        0.995786
    """
    k = arange(x+1)
    p = sum(exp(log_hypergeometricPMF(N,K,n,k)))
    return(p)
    

    log_hypergeometricPMF is defined on top of the file ;)