Ejemplo n.º 1
0
def nCr(n, r):
    """
    Computes the number of combinations of n things
    taken r at a time.
    """
    # Check boundaries. This may need to be revised.
    if r < 0 or n < 0:
        return 0
    if (n <= 0 or r == n or r == 0):
        return 1

    # Version 1: using Gmpy
    return int(gmpy.bincoef(n, r))

    # Version 2: using iteration.
    if r > n - r:
        r = n - r
    x = n
    i = n - 1
    for d in range(2, r + 1):
        x = (x * i) / d
        i = i - 1
    return x

    # Version 3: using recursion
    if n < 2 or r == 0 or r >= n:
        return 1
    return nCr(n - 1, r) + nCr(n - 1, r - 1)
Ejemplo n.º 2
0
def nCr(n, r):
    """
    Computes the number of combinations of n things
    taken r at a time.
    """
    # Check boundaries. This may need to be revised.
    if r < 0 or n < 0:
        return 0
    if (n <= 0 or r == n or r == 0):
        return 1

    # Version 1: using Gmpy
    return int(gmpy.bincoef(n, r))

    # Version 2: using iteration.
    if r > n - r:
        r = n - r
    x = n
    i = n - 1
    for d in range(2, r + 1):
        x = (x * i) / d
        i = i - 1
    return x

    # Version 3: using recursion
    if n < 2 or r == 0 or r >= n:
        return 1
    return nCr(n - 1, r) + nCr(n - 1, r - 1)
Ejemplo n.º 3
0
def bernoulli(n):
    """
    Returns recursively the nth bernoulli number as a fraction.
    """
    if n <= 0: return 1
    if n == 1: return gmpy.mpq(-1,2)
    if (n%2) == 1:  return 0
    return -gmpy.mpq(sum(map(lambda x:gmpy.bincoef(n+1, x)*bernoulli(x), range(n))), n+1)
Ejemplo n.º 4
0
def bernoulli(n):
    """
    Returns recursively the nth bernoulli number as a fraction.
    """
    if n <= 0: return 1
    if n == 1: return gmpy.mpq(-1, 2)
    if (n % 2) == 1: return 0
    return -gmpy.mpq(
        sum(map(lambda x: gmpy.bincoef(n + 1, x) * bernoulli(x), range(n))),
        n + 1)
Ejemplo n.º 5
0
 def iterate(rest, n_rest):
     if rest and n_rest > 0:
         for k in range(n_rest + 1):
             for coeff, next in iterate(rest[1:], n_rest - k):
                 if k == 0:
                     this_factor = []
                 else:
                     this_factor = [Expression('Power', rest[0], Integer(k))]
                 yield (bincoef(n_rest, k) * coeff, this_factor + next)
     elif n_rest == 0:
         yield (mpz(1), [])
Ejemplo n.º 6
0
 def iterate(rest, n_rest):
     if rest and n_rest > 0:
         for k in range(n_rest + 1):
             for coeff, next in iterate(
                     rest[1:], n_rest - k):
                 if k == 0:
                     this_factor = []
                 else:
                     this_factor = [
                         Expression('Power', rest[0],
                                    Integer(k))
                     ]
                 yield (bincoef(n_rest, k) * coeff,
                        this_factor + next)
     elif n_rest == 0:
         yield (mpz(1), [])
Ejemplo n.º 7
0
def cov_from_P_prob(k, N, P_probs):
#------------------------------------------------------------------------------ 
    """
    Returns the k-th order of covariance among N spike trains with given P_prob
    """
    import gmpy
    
    assert k <= N, "you can only calculate up to N-th order of covariace"
    assert len(P_probs) <= N+1, "Invalid P_prob"
    
    P_probs = np.array(P_probs)/sum(P_probs)
    mean = sum([1.0*i*P_prob for i,P_prob in enumerate(P_probs)])/N
    
    total_cov = 0
    for i in xrange(k+1):
        raw_corr = raw_corr_from_P_prob(k-i, N, P_probs)
        contribution = ((-mean)**i)*gmpy.bincoef(k,i)*raw_corr 
        total_cov += contribution 
        #print raw_corr, total_cov, contribution
    
    return float(total_cov) 
Ejemplo n.º 8
0
def CalculateProbability(a, b, c, d):
    return mpq(
        bincoef(a + b, a) * bincoef(c + d, c), bincoef(a + b + c + d, a + c))
Ejemplo n.º 9
0
def CalculateProbability(a, b, c, d):
    return mpq(bincoef(a + b, a) * bincoef(c + d, c), bincoef(a + b + c + d, a + c))
Ejemplo n.º 10
0
def prob(people):
    return 1.0 - float(factorial(people)*bincoef(365,people))/(365**people)
Ejemplo n.º 11
0
    def apply_exact(self, n, k, evaluation):
        'Binomial[n_Integer, k_Integer]'

        if k.value < 0:
            return Integer(0)
        return Number.from_mp(bincoef(n.value, k.value))
Ejemplo n.º 12
0
 def apply_exact(self, n, k, evaluation):
     'Binomial[n_Integer, k_Integer]'
     
     if k.value < 0:
         return Integer(0)
     return Number.from_mp(bincoef(n.value, k.value))