def freq_pdf_old(x, p, n, t, k=8): if x > 0.5: return freq_pdf2(1.0-x, 1.0-p, n, t, k) q = 1.0 - p prob = -util.INF sgn = 1 t4n = t / (4*n) for i in xrange(1, k+1): #term = (p * q * i * (i+1) * (2*i+1) * # hypergeo(1-i,i+2,2,p) * hypergeo(1-i,i+2,2,x) * # exp(-t * i * (i+1) / (4*n))) lcoff = log(p * q * i * (i+1) * (2*i+1)) h1 = hypergeo(1-i,i+2,2,p, i+2) h2 = hypergeo(1-i,i+2,2,x, i+2) sgn2 = util.sign(h1) * util.sign(h2) if sgn2 != 0: term = (lcoff + log(abs(h1)) + log(abs(h2)) + (- i * (i+1) * t4n)) sgn, prob = stats.logadd_sign(sgn, prob, sgn2, term) return sgn * exp(prob)
def hypergeo(a, b, c, z, k=100): """Hypergeometric function""" terms = [0.0] signs = [1.0] for i in xrange(1, k+1): term = float((i+a-1)*(i+b-1)*z)/(i+c-1)/i signs.append(util.sign(term) * signs[-1]) if term == 0.0: break terms.append(log(abs(term)) + terms[i-1]) return sum(s*exp(i) for s, i in zip(signs, terms))
def loghypergeo(a, b, c, z, k=100): """ Hypergeometric function Performs computation in log-space """ terms = [0.0] signs = [1.0] for i in xrange(1, k+1): term = float((i+a-1)*(i+b-1)*z)/(i+c-1)/i signs.append(util.sign(term) * signs[-1]) if term == 0.0: break terms.append(log(abs(term)) + terms[i-1]) sgn = 1 tot = -util.INF for s, t in zip(signs, terms): sgn, tot = stats.logadd_sign(sgn, tot, s, t) return sgn, tot