def K_(a): ''' Compute the K function ''' # res = B_(a[0]+a[1], a[2]+a[3]) - (B_(a[0], a[2]) + B_(a[1],a[3])) return mpmath.beta(a[0]+a[1], a[2]+a[3])/(mpmath.beta(a[0], a[2]) * mpmath.beta(a[1],a[3]))
def beta_binomial(k,n,a,b,multi_precission=False): """ Beta binomial function, returning the probability of k successes in n trials (given a p distribution beta of barameters a and b), and supporting multiprecission output. Parameters ---------- k : int, ndarray Successes. n : int, ndarray Trials. a, b : int,ndarray Parameters of the beta distribution. multi_precission : bool, optional Whether or not to use multiprecision floating-point output (default: False). Returns ------- p : int,ndarray Probability of k successes in n trials. Examples -------- >>> n = 80000 >>> k = 40000 >>> mp_comb(n, k) 7.0802212521852e+24079 """ if multi_precission: import mpmath as mp p = mp_comb(n,k) * mp.beta(k+a, n-k+b) / mp.beta(a,b) else: from scipy.special import beta from scipy.misc import comb p = comb(n,k) * beta(k+a, n-k+b) / beta(a,b) return p
def pdf_bb_ratio(a1, a2, b1, b2, w): lnA = mpmath.log(mpmath.beta(a1, b1)) + mpmath.log(mpmath.beta(a2, b2)) def pdf_calc(wi): if wi < 0: print('Ratio below Zero! Not reasonable!') exit(1) elif wi == 0: resulti = 0 elif wi < 1: resulti = mpmath.exp( mpmath.log(mpmath.beta(a1 + a2, b2)) + (a1 - 1.0) * mpmath.log(wi) + log_hyper_2F1(a1 + a2, 1 - b1, a1 + a2 + b2, wi) - lnA) else: resulti = mpmath.exp( mpmath.log(mpmath.beta(a1 + a2, b1)) - (1.0 + a2) * mpmath.log(wi) + log_hyper_2F1(a1 + a2, 1 - b2, a1 + a2 + b1, (1 / wi)) - lnA) return resulti if isinstance(w, int) or isinstance(w, float) or isinstance(w, mpmath.mpf): result = pdf_calc(w) else: result = np.zeros(len(w)) for i in range(len(w)): wi = w[i] result[i] = pdf_calc(wi) return result
def test_beta(): np.random.seed(1234) b = np.r_[np.logspace(-200, 200, 4), np.logspace(-10, 10, 4), np.logspace(-1, 1, 4), -1, -2.3, -3, -100.3, -10003.4] a = b ab = np.array(np.broadcast_arrays(a[:, None], b[None, :])).reshape(2, -1).T old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 400 assert_func_equal(sc.beta, lambda a, b: float(mpmath.beta(a, b)), ab, vectorized=False, rtol=1e-10) assert_func_equal( sc.betaln, lambda a, b: float(mpmath.log(abs(mpmath.beta(a, b)))), ab, vectorized=False, rtol=1e-10) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def beta_binomial(k, n, a, b, multi_precission=False): """ Beta binomial function, returning the probability of k successes in n trials (given a p distribution beta of parameters a and b), and supporting multiprecission output. Parameters ---------- k : int, ndarray Successes. n : int, ndarray Trials. a, b : int,ndarray Parameters of the beta distribution. multi_precission : bool, optional Whether or not to use multiprecision floating-point output (default: False). Returns ------- p : int,ndarray Probability of k successes in n trials. Examples -------- >>> n = 80000 >>> k = 40000 >>> mp_comb(n, k) 7.0802212521852e+24079 """ if multi_precission: import mpmath as mp p = mp_comb(n, k) * mp.beta(k + a, n - k + b) / mp.beta(a, b) else: from scipy.special import beta from scipy.misc import comb p = comb(n, k) * beta(k + a, n - k + b) / beta(a, b) return p
def test_beta(): np.random.seed(1234) b = np.r_[np.logspace(-200, 200, 4), np.logspace(-10, 10, 4), np.logspace(-1, 1, 4), -1, -2.3, -3, -100.3, -10003.4] a = b ab = np.array(np.broadcast_arrays(a[:,None], b[None,:])).reshape(2, -1).T old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 400 assert_func_equal(sc.beta, lambda a, b: float(mpmath.beta(a, b)), ab, vectorized=False, rtol=1e-10) assert_func_equal( sc.betaln, lambda a, b: float(mpmath.log(abs(mpmath.beta(a, b)))), ab, vectorized=False, rtol=1e-10) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def func_ppf(x, a0, b0, a1, b1, p): """Function CDF ratio of beta function for root-finding.""" mp.mp.dps = 100 one = mp.mp.one c = mp.beta(a0 + a1, b0) / (mp.beta(a0, b0) * mp.beta(a1, b1)) c *= mp.mpf(x) ** -a1 / a1 f = mp.hyp3f2(a1, a0 + a1, one - b1, a1 + one, a0 + a1 + b0, one / x) return float(one - c * f) - p
def pmf(k, ntotal, ngood, nsample): """ Probability mass function of the hypergeometric distribution. """ _validate(ntotal, ngood, nsample) nbad = ntotal - ngood numer = (ntotal + 1) * mpmath.beta(ntotal - nsample + 1, nsample + 1) denom = ((ngood + 1) * (nbad + 1) * mpmath.beta(k + 1, ngood - k + 1) * mpmath.beta(nsample - k + 1, nbad - nsample + k + 1)) pmf = numer / denom return pmf
def e_ratio(a,b,e,x): # Get S bt2 = mp.beta(a,b-1.0) # Beta function bix = mp.betainc(a,b+1.0,0.0,e) # Incomplete Beta function hf = mp.hyp2f1(1.0,a,a+b-1.0,-1.0) # 2F1, Gauss' hypergeometric function hfre = mp.re(hf) Sval = bix - x*bt2*hfre # Get U c1 = mp.mpc(1.0 + a) c2 = mp.mpc(-b) c3 = mp.mpc(1.0) c4 = mp.mpc(2.0 + a) Uval = mp.appellf1(c1,c2,c3,c4,e,-e) Ure = mp.re(Uval) # Get P & Q Pval = mp.hyp2f1(a+1.0,1.0-b,a+2.0,e) # 2F1, Gauss' hypergeometric function Pre = mp.re(Pval) Qval = mp.hyp2f1(a+1.0,2.0-b,a+2.0,e) # 2F1, Gauss' hypergeometric function Qre = mp.re(Qval) # Get T Tval = ( (e**(1.0+a)) / (1.0+a) )*( 3.0*Pre + 2.0*Qre - Ure ) Tval = Tval + 4.0*Sval # Get Rval (ratio) Rval = 0.25*(1.0-e*e)*( (1.0-e)**(1.0-b) )*( e**(1.0-a) )*Tval return Rval
def beta(a, b): if a < -1e12 or b < -1e12: # Function is defined here only at integers, but due # to loss of precision this is numerically # ill-defined. Don't compare values here. return np.nan return mpmath.beta(a, b)
def e_ratio(a, b, e, x): # Get S bt2 = mp.beta(a, b - 1.0) # Beta function bix = mp.betainc(a, b + 1.0, 0.0, e) # Incomplete Beta function hf = mp.hyp2f1(1.0, a, a + b - 1.0, -1.0) # 2F1, Gauss' hypergeometric function hfre = mp.re(hf) Sval = bix - x * bt2 * hfre # Get U c1 = mp.mpc(1.0 + a) c2 = mp.mpc(-b) c3 = mp.mpc(1.0) c4 = mp.mpc(2.0 + a) Uval = mp.appellf1(c1, c2, c3, c4, e, -e) Ure = mp.re(Uval) # Get P & Q Pval = mp.hyp2f1(a + 1.0, 1.0 - b, a + 2.0, e) # 2F1, Gauss' hypergeometric function Pre = mp.re(Pval) Qval = mp.hyp2f1(a + 1.0, 2.0 - b, a + 2.0, e) # 2F1, Gauss' hypergeometric function Qre = mp.re(Qval) # Get T Tval = ((e**(1.0 + a)) / (1.0 + a)) * (3.0 * Pre + 2.0 * Qre - Ure) Tval = Tval + 4.0 * Sval # Get Rval (ratio) Rval = 0.25 * (1.0 - e * e) * ( (1.0 - e)**(1.0 - b)) * (e**(1.0 - a)) * Tval return Rval
def t_sf(t, df): lhs = 1 / (mp.sqrt(df) * mp.beta(0.5, df / 2)) rhs = (1 + ((mp.mpf(t)**2) / df))**(-(df + 1) / 2) # a = df / 2 # b = 1 / 2 # x = df / ((t ** 2) + df) # I = mp.betainc(a, b, 0, x) return lhs * rhs
def multi_winner_ode(c, y, n, k): w = y[0] #input comes in as an array c = mp.mpf(c) w = mp.mpf(w) num = -2**(-n) * (2-c)**(n-k-1) * c**(k-1) * (2*c - w - 2) * (w-2) denom = (c-w) * mp.beta(k, n-k) * mp.betainc(k, n-k, 0, c/2, regularized=True) return num/denom
def pdf_calc(wi): if wi < 0: print('Ratio below Zero! Not reasonable!') exit(1) elif wi == 0: resulti = 0 elif wi < 1: resulti = mpmath.exp( mpmath.log(mpmath.beta(a1 + a2, b2)) + (a1 - 1.0) * mpmath.log(wi) + log_hyper_2F1(a1 + a2, 1 - b1, a1 + a2 + b2, wi) - lnA) else: resulti = mpmath.exp( mpmath.log(mpmath.beta(a1 + a2, b1)) - (1.0 + a2) * mpmath.log(wi) + log_hyper_2F1(a1 + a2, 1 - b2, a1 + a2 + b1, (1 / wi)) - lnA) return resulti
def mean(c, d, scale): """ Mean of the Burr type XII distribution. """ _validate_params(c, d, scale) with mpmath.extradps(5): c = mpmath.mpf(c) d = mpmath.mpf(d) scale = mpmath.mpf(scale) return d*mpmath.beta(d - 1/c, 1 + 1/c)*scale
def cum_pdf_calc(wi): if wi < 0: print('Ratio below Zero! Not reasonable!') exit(1) elif wi == 0: resulti = 0 elif wi < 1: resulti = mpmath.exp( mpmath.log(mpmath.beta(a1 + a2, b2)) + a1 * mpmath.log(wi) - mpmath.log(a1) + log_hyper_3F2(a1, a1 + a2, 1 - b1, a1 + 1, a1 + a2 + b2, wi) - lnA) else: resulti = 1 - mpmath.exp( mpmath.log(mpmath.beta(a1 + a2, b1)) - a2 * mpmath.log(wi) - mpmath.log(a2) + log_hyper_3F2(a2, a1 + a2, 1 - b2, a2 + 1, a1 + a2 + b1, (1 / wi)) - lnA) return resulti
def beta_pdf(m, s, t): n = m * (1 - m) / (s**2) a = (m * n) b = ((1 - m) * n) # print(n,a,b) num = t**(a - 1) * (1 - t)**(b - 1) den = beta( a, b) ## I believe this works well, based on some tests with Desmos return float(num / den)
def var(c, d, scale): """ Variance of the Burr type XII distribution. """ _validate_params(c, d, scale) with mpmath.extradps(5): c = mpmath.mpf(c) d = mpmath.mpf(d) scale = mpmath.mpf(scale) mu1 = mean(c, d, 1) mu2 = d*mpmath.beta(d - 2/c, 1 + 2/c) return scale**2 * (-mu1**2 + mu2)
def N(self,p,z): a = (z-p)**2 b = (z+p)**2 N = len(self.n) res = np.zeros(N) for n in range(N): beta = mp.beta((n+8)/4,1/2) appell = self.appellf1(1/2,1,1/2,(n+10)/4,(a-1)/a,(1-a)/(b-a)) gauss = self.hyp2f1(1/2,1/2,(n+10)/4,(1-a)/(b-a)) res[n] = (1-a)**(n/4+3/2)/np.sqrt(b - a)*beta*( (z**2-p**2)/a*appell - gauss) return res
def entropy(df): """ Entropy of Student's t distribution. """ if df <= 0: raise ValueError('df must be greater than 0') with mpmath.extradps(5): df = mpmath.mpf(df) h = df/2 h1 = (df + 1)/2 return (h1*(mpmath.digamma(h1) - mpmath.digamma(h)) + mpmath.log(mpmath.sqrt(df)*mpmath.beta(h, 0.5)))
def beta(a, b, nonzero=False): if a < -1e12 or b < -1e12: # Function is defined here only at integers, but due # to loss of precision this is numerically # ill-defined. Don't compare values here. return np.nan if (a < 0 or b < 0) and (abs(float(a + b)) % 1) == 0: # close to a zero of the function: mpmath and scipy # will not round here the same, so the test needs to be # run with an absolute tolerance if nonzero: bad_points.append((float(a), float(b))) return np.nan return mpmath.beta(a, b)
def pdf(x, dfn, dfd): """ Probability density function of the F distribution. `dfn` and `dfd` are the numerator and denominator degrees of freedom, resp. """ if x <= 0: return mpmath.mp.zero with mpmath.mp.extradps(5): x = mpmath.mp.mpf(x) dfn = mpmath.mp.mpf(dfn) dfd = mpmath.mp.mpf(dfd) r = dfn / dfd hdfn = dfn / 2 hdfd = dfd / 2 p = (r**hdfn * x**(hdfn - 1) * (1 + r*x)**(-(hdfn + hdfd)) / mpmath.beta(hdfn, hdfd)) return p
def case_nonld(self,p,z): ld_coef = self.ld_coef n = self.n omega = self.omega if p>0 and 1/2+abs(p-1/2)<z<1+p: return 1 - np.sum( self.N(p,z)*ld_coef/(n+4))/2/np.pi/omega elif 0<p<1/2 and (p<z<1-p or z==1-p): L = p**2*(1-p**2/2-z**2) return 1 - (ld_coef[0]*p**2 + ld_coef[4]*L + 2*np.sum(self.M(p,z,n[1:4])*ld_coef[1:4]/(n[1:4]+4)) )/4/omega elif 0<p<1/2 and z==p: hyp = np.zeros(len(n)) for i in range(len(n)): hyp[i] = self.hyp2f1(1/2,-n[i]/4-1,1,4*p**2) return 1/2 + np.sum( ld_coef/(n+4)*hyp )/2/omega elif p==1/2 and z==1/2: coef = np.zeros(len(n)) for i in range(len(n)): coef[i] = self.gamma(1.5+n[i]/4)/self.gamma(2+n[i]/4) return 1/2 + np.sum( ld_coef/(n+4)*coef )/2/np.sqrt(np.pi)/omega elif p>1/2 and z==p: coef = np.zeros(len(n)) for i in range(len(n)): coef[i] = (mp.beta(0.5,n[i]/4+2) * self.hyp2f1(0.5,0.5,5/2+n[i]/4,1/4/p**2)) return 1/2 + np.sum( ld_coef/(n+4)*coef )/4/p/np.pi/omega elif p>1/2 and abs(1-p)<=z<p: return - np.sum( self.N(p,z)*ld_coef/(n+4))/2/np.pi/omega elif 0<p<1 and 0<z<1/2-abs(p-1/2): L = p**2*(1-p**2/2-z**2) return (ld_coef[0]*(1-p**2)+ld_coef[4]*(0.5-L) - 2*np.sum(self.M(p,z,n[1:4])*ld_coef[1:4]/(n[1:4]+4)) )/4/omega elif 0<p<1 and z==0: return np.sum(ld_coef*(1-p**2)**(n/4+1)/(n+4))/omega elif p>1 and 0<=z<p-1: return 0.
mp.dps = 1000 mp.pretty = True n = 4 j = n-1 # n here is other number of players aka N-1 # k is # number of winners k = 2 H = lambda c, w: (-(1-c)**(j-k) * c**(k-1) * (w-1) * (1-2*c + w))/(2 * mp.beta(k, j+1-k) * (1 - mp.betainc(k, j+1-k, 0, c, regularized=True))) G = lambda c, w: (-(1-c)**(j-k) * c**(k-1) * (w-1) * (1-2*c + w))/(2 * beta(k, j+1-k) * (1 - mp.betainc(k, j+1-k, 0, c, regularized=True))) num = lambda c, w: -(1-c)**(j-k) * c**(k-1) * (w-1) * (1-2*c + w) denom = lambda c,w: 2 * mp.beta(k, j+1-k) * (1 - mp.betainc(k, j+1-k, 0, c, regularized=True)) func = lambda c, w: (2**(-1 - n) * (2 - c)**(-k + n) * c**(-1 + k) * (-4 + 4 * c - 2 * c * w + w**2)) \ / (mp.beta(k, 1 - k + n)* (-1 + mp.betainc( k, 1 - k + n,0, c/2) * (c - w))) print(H(.2,.5)) print(G(.2,.5)) c_end = mp.mpf('0') c_0 = mp.mpf('1.995') w_0 = mp.mpf('1.99') w_c = [w_0] cs = [c_0]
def betaMergerRate(b, k, alpha=alphaDefault): if 1 < k and k <= b: return mp.beta(k-1, b-k+1) / mp.beta(2-alpha, alpha) else: print "wrong parameters in betaMergerRate. b, k = %s, %s"%(str(b), str(k)) return mp.mpf('0')
def pearsonr(x, y, alternative='two-sided'): """ Pearson's correlation coefficient. Returns the correlation coefficient r and the p-value. x and y must be one-dimensional sequences with the same lengths. The function assumes all the values in x and y are finite (no `inf`, no `nan`). Examples -------- >>> from mpsci.stats import pearsonr >>> import mpmath >>> mpmath.mp.dps = 25 >>> x = [1, 2, 3, 5, 8, 10] >>> y = [0.25, 2, 2, 2.5, 2.4, 5.5] Compute the correlation coefficent and p-value. >>> r, p = pearsonr(x, y) >>> r mpf('0.8645211772786436751458124677') >>> p mpf('0.02628844331049414042317641803') Compute a one-sided p-value. The correlation coefficient is the same; only the p-value is different. >>> r, p = pearsonr(x, y, alternative='greater') >>> r mpf('0.8645211772786436751458124677') >>> p mpf('0.01314422165524707021158820901') """ if alternative not in ['two-sided', 'less', 'greater']: raise ValueError("alternative must be 'two-sided', 'less', or " "'greater'.") if len(x) != len(y): raise ValueError('lengths of x and y must be the same.') if all(x[0] == t for t in x[1:]) or all(y[0] == t for t in y[1:]): return mpmath.nan, mpmath.nan if len(x) == 2: return mpmath.sign(x[1] - x[0]) * mpmath.sign(y[1] - y[0]), mpmath.mpf(1) x = [mpmath.mp.mpf(float(t)) for t in x] y = [mpmath.mp.mpf(float(t)) for t in y] xmean = sum(x) / len(x) ymean = sum(y) / len(y) xm = [t - xmean for t in x] ym = [t - ymean for t in y] num = sum(s * t for s, t in zip(xm, ym)) den = mpmath.sqrt(sum(t**2 for t in xm) * sum(t**2 for t in ym)) r = num / den n = len(x) a = mpmath.mpf(float(n)) / 2 - 1 if alternative == 'two-sided': p = 2 * mpmath.betainc(a, a, x2=0.5 * (1 - abs(r))) / mpmath.beta(a, a) elif alternative == 'less': p = mpmath.betainc(a, a, x2=0.5 * (1 + r)) / mpmath.beta(a, a) else: # alternative == 'greater' p = mpmath.betainc(a, a, x2=0.5 * (1 - r)) / mpmath.beta(a, a) return r, p
def fmt(f, inputs, out): # Here we set up output. We hope that refcounting will collect fds # promptly if out is None: out = sys.stdout else: out = open(out, 'w') for xs in inputs: param = ["%.18g" % x for x in xs] sOut = mpmath.nstr(f(*xs), mpmath.mp.dps) print('\t'.join(param + [sOut]), file=out) ## ================================================================ fmt(mpmath.erf, load_inputs('inputs/erf.dat'), 'erf.dat') fmt(mpmath.erf, load_inputs('inputs/erf.dat'), 'erf.dat') fmt(mpmath.erfc, load_inputs('inputs/erfc.dat'), 'erfc.dat') fmt(mpmath.erfc, load_inputs('inputs/erfc-large.dat'), 'erfc-large.dat') fmt(mpmath.loggamma, load_inputs('inputs/loggamma.dat'), 'loggamma.dat') fmt(mpmath.digamma, load_inputs('inputs/digamma.dat'), 'digamma.dat') fmt(mpmath.expm1, load_inputs('inputs/expm1.dat'), 'expm1.dat') fmt(mpmath.log1p, load_inputs('inputs/log1p.dat'), 'log1p.dat') fmt(lambda x: mpmath.log(mpmath.factorial(x)), map(lambda x: (x, ), range(0, 20000)), 'factorial.dat') fmt(lambda a, x: mpmath.gammainc(z=a, a=0, b=x, regularized=True), load_inputs_cartesian('inputs/igamma.dat'), 'igamma.dat') fmt(lambda p, q: mpmath.log(mpmath.beta(p, q)), load_inputs_cartesian('inputs/logbeta.dat'), 'logbeta.dat')
def calc(p, s, f): return mpmath.exp((s - 1) * mpmath.log(p) + mpmath.mpf(f - 1) * mpmath.log(1 - p) - mpmath.log(mpmath.beta(s, f)))
def _H(alpha, m, lmd, d): H2 = mp.beta(m / 2, (lmd + d) / 2) * mp.log(alpha) Q = mp.quad(lambda v: _integrand(v, alpha, m, lmd, d), [0, mp.inf]) H3 = (1 + lmd / m) * Q return H2 + H3