Exemplo n.º 1
0
Arquivo: libm.py Projeto: zholos/qml
def test_hyper():
    for x in sorted(exparg):
        test("erf", x, N(sp.erf(x)))
    for x in sorted(exparg):
        test("erfc", x, N(sp.erfc(x)))

    gamarg = FiniteSet(*(x+S(1)/12 for x in exparg))
    betarg = ProductSet(gamarg, gamarg)
    for x in sorted(gamarg):
        test("lgamma", x, N(sp.log(abs(sp.gamma(x)))))
    for x in sorted(gamarg):
        test("gamma", x, N(sp.gamma(x)))
    for x, y in sorted(betarg, key=lambda (x, y): (y, x)):
        test("beta", x, y, N(sp.beta(x, y)))

    pgamarg = FiniteSet(S(1)/12, S(1)/3, S(3)/2, 5)
    pgamargp = ProductSet(gamarg & Interval(0, oo, True), pgamarg)
    for a, x in sorted(pgamargp):
        test("pgamma", a, x, N(sp.lowergamma(a, x)))
    for a, x in sorted(pgamargp):
        test("pgammac", a, x, N(sp.uppergamma(a, x)))
    for a, x in sorted(pgamargp):
        test("pgammar", a, x, N(sp.lowergamma(a, x)/sp.gamma(a)))
    for a, x in sorted(pgamargp):
        test("pgammarc", a, x, N(sp.uppergamma(a, x)/sp.gamma(a)))
    for a, x in sorted(pgamargp):
        test("ipgammarc", a, N(sp.uppergamma(a, x)/sp.gamma(a)), x)

    pbetargp = [(a, b, x) for a, b, x in ProductSet(betarg, pgamarg)
                if a > 0 and b > 0 and x < 1]
    pbetargp.sort(key=lambda (a, b, x): (b, a, x))
    for a, b, x in pbetargp:
        test("pbeta", a, b, x, mp.betainc(mpf(a), mpf(b), x2=mpf(x)))
    for a, b, x in pbetargp:
        test("pbetar", a, b, x, mp.betainc(mpf(a), mpf(b), x2=mpf(x),
                                           regularized=True))
    for a, b, x in pbetargp:
        test("ipbetar", a, b, mp.betainc(mpf(a), mpf(b), x2=mpf(x),
                                         regularized=True), x)

    for x in sorted(posarg):
        test("j0", x, N(sp.besselj(0, x)))
    for x in sorted(posarg):
        test("j1", x, N(sp.besselj(1, x)))
    for x in sorted(posarg-FiniteSet(0)):
        test("y0", x, N(sp.bessely(0, x)))
    for x in sorted(posarg-FiniteSet(0)):
        test("y1", x, N(sp.bessely(1, x)))
Exemplo n.º 2
0
def _f_cdf(dfn, dfd, x):
    if x < 0:
        return mpmath.mpf(0)
    dfn, dfd, x = mpmath.mpf(dfn), mpmath.mpf(dfd), mpmath.mpf(x)
    ub = dfn*x/(dfn*x + dfd)
    res = mpmath.betainc(dfn/2, dfd/2, x2=ub, regularized=True)
    return res
Exemplo n.º 3
0
 def test_btdtria(self):
     _assert_inverts(
         sp.btdtria,
         lambda a, b, x: mpmath.betainc(a, b, x2=x, regularized=True),
         0, [ProbArg(), Arg(0, 1e3, inclusive_a=False),
             Arg(0, 1, inclusive_a=False, inclusive_b=False)],
         rtol=1e-6)
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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
Exemplo n.º 6
0
def beta_cdf(x, a, b):
    if x <= S(0):
        return Float('0.0')
    if x >= S(1):
        return Float('1.0')
    mpmath.mp.dps = 500
    result = mpmath.betainc(to_mpmath(a), to_mpmath(b), 0, to_mpmath(x), regularized=True)
    return Float(result)
Exemplo n.º 7
0
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
Exemplo n.º 8
0
 def test_btdtrib(self):
     # Use small values of a or mpmath doesn't converge
     _assert_inverts(
         sp.btdtrib,
         lambda a, b, x: mpmath.betainc(a, b, x2=x, regularized=True),
         1, [Arg(0, 1e2, inclusive_a=False), ProbArg(),
          Arg(0, 1, inclusive_a=False, inclusive_b=False)],
         rtol=1e-7, endpt_atol=[None, 1e-20, 1e-20])
Exemplo n.º 9
0
def cdf(k, p):
    """
    CDF of the log-series distribution.
    """
    p = _validate_p(p)
    if k < 1:
        return _mpm.mp.zero
    with _mpm.extradps(5):
        return 1 + _mpm.betainc(k + 1, 0, 0, p) / _mpm.log1p(-p)
Exemplo n.º 10
0
def _binomial_cdf(k, n, p):
    k, n, p = mpmath.mpf(k), mpmath.mpf(n), mpmath.mpf(p)
    if k <= 0:
        return mpmath.mpf(0)
    elif k >= n:
        return mpmath.mpf(1)

    onemp = mpmath.fsub(1, p, exact=True)
    return mpmath.betainc(n - k, k + 1, x2=onemp, regularized=True)
Exemplo n.º 11
0
def sf(k, p):
    """
    Survival function of the log-series distribution.
    """
    p = _validate_p(p)
    if k < 1:
        return _mpm.mp.one
    with _mpm.extradps(5):
        return -_mpm.betainc(k + 1, 0, 0, p) / _mpm.log1p(-p)
Exemplo n.º 12
0
def sf(x, dfn, dfd):
    """
    Survival function of the F distribution.

    `dfn` and `dfd` are the numerator and denominator degrees of freedom, resp.
    """
    if x <= 0:
        return mpmath.mp.one
    with mpmath.mp.extradps(5):
        x = mpmath.mp.mpf(x)
        dfn = mpmath.mp.mpf(dfn)
        dfd = mpmath.mp.mpf(dfd)
        dfnx = dfn * x
        return mpmath.betainc(dfn/2, dfd/2, x1=dfnx/(dfnx + dfd),
                              regularized=True)
Exemplo n.º 13
0
    def sf(a, b=np.inf, dps=15):
        dps_temp = mp.mp.dps
        mp.mp.dps = dps

        tmp_a = d1*a/d2
        tmp_b = d1*b/d2
        beta_a = tmp_a / (1. + tmp_a)
        beta_b = tmp_b / (1. + tmp_b)
        if b == np.inf:
            beta_b = 1.
        sf = mp.betainc(d1/2., d2/2., 
                        x1=beta_a, x2=beta_b,
                        regularized=True)
        mp.mp.dps = dps_temp
        return sf
Exemplo n.º 14
0
def flex(c,y,n,k):
    j = n-1
    # n here is other number of players aka N-1
    # k is # number of winners
    w = y[0]
    # print(c,w,n,k)
    # num = (1-c)**j * c**k * (-1+w) * (1-2*c+w) * gamma(1+j) * gamma(1-k+j)**2
    # denom = -2 * (1 - c)**k * c * (c - w) * gamma(k) * gamma(1 - k + j) +  2 * (1 - c)**j * c**(1 + k) * (c - w) * gamma(k) * gamma(1+j) * hyp2f1(1, k - j, 1 + k, c/(-1 + c))
    # print(c**(k-1))
    # print(k,j+1-k)
    # print(betainc(c, k, j+1-k))
    # print(betainc(np.float64(.999),2.0,1.0))
    c = mp.mpf(c)
    w = mp.mpf(w)
    num = -(1-c)**(j-k) * c**(k-1) * (w-1) * (1-2*c + w)
    # print(num)
    denom = 2 * beta(k, j+1-k) * (1 - mp.betainc(k, j+1-k, 0, c, regularized=True)) * (c-w)
    # print(num)
    # print(denom)
    return num/denom
Exemplo n.º 15
0
def F_int_func(i, a, b, c, A, x, verbose=False):
	beta_inc = special.betainc(A, i+1, x)
	if not(beta_inc ):
		alt = F_int_func2(i, a, b, c, A, x, verbose=False)
		if verbose:
			print 'rounding error 1', i, beta_inc, special.betaln(A, i+1), mpmath.betainc(A, i+1, x), alt
		return alt

		beta_inc = 10e-320

	liter = math.lgamma(a+i) - math.lgamma(a) \
		  + math.lgamma(b+i) - math.lgamma(b) \
		  + math.lgamma(c) - math.lgamma(c+i) \
		  - math.lgamma(i+1) \
		  + math.log( beta_inc ) \
		  + special.betaln(A, i+1) 
		  
	if liter > 709.:
# 		if verbose:
		print 'rounding error 2', i, 
		liter = 709.
	return math.exp(liter)
Exemplo n.º 16
0
 def test_betainc(self):
     assert_mpmath_equal(sc.betainc,
                         _exception_to_nan(lambda a, b, x: mpmath.betainc(a, b, 0, x, regularized=True)),
                         [Arg(), Arg(), Arg()])
Exemplo n.º 17
0
#!/usr/bin/python
"""
"""

import mpmath as mp
import numpy  as np
import sys


mp.mp.dps = 30

a    = float(sys.argv[1])
b    = float(sys.argv[2])
x_lo = float(sys.argv[3])
n    = int  (sys.argv[4])

for x in np.logspace(x_lo, 0, n) :
    x = mp.mpf(x)
    with mp.extradps(300) :
        y = mp.betainc(a, b, 0, x, regularized=True)
    print x, y
Exemplo n.º 18
0
Arquivo: libm.py Projeto: zholos/qml
 def cdf(d1, d2, x):
     d1, d2, x = map(mpf, (d1, d2, x))
     return mp.betainc(d1/2, d2/2, x2=x/(x+d2/d1), regularized=True)
Exemplo n.º 19
0
Arquivo: libm.py Projeto: zholos/qml
 def cdf(k, x):
     k, x = map(mpf, (k, x))
     return .5 + .5*mp.sign(x)*mp.betainc(k/2, .5, x1=1/(1+x**2/k),
                                          regularized=True)
Exemplo n.º 20
0
        plt.plot(
            x,
            y,
            label=
            f"{name} sample, conversion rate: {y_mode:0.1E} $\pm$ {y_var:0.1E}"
        )
        plt.yticks([])
    plt.legend()
    plt.show()


plot([beta_C, beta_T], names=["Control", "Test"])

from mpmath import betainc

p = betainc(a_T, b_T, 0.003, 1, regularized=True)  #result: 0.48112566853812544

import seaborn as sns
import pandas as pd
import numpy as np
imps_ctrl, convs_ctrl = 16500, 30
imps_test, convs_test = 17000, 50

#here we create the Beta functions for the two sets
a_C, b_C = convs_ctrl + 1, imps_ctrl - convs_ctrl + 1
a_T, b_T = convs_test + 1, imps_test - convs_test + 1

val_C = np.random.beta(a_C, b_C, 1000000)
val_T = np.random.beta(a_T, b_T, 1000000)
vals = np.vstack([val_C, val_T]).T
Exemplo n.º 21
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]
Exemplo n.º 22
0
 def _h(self, c):
     c = np.complex(c)
     f1 = -(-c) ** self.alpha * c ** self.alpha
     f2 = mpmath.betainc(-c, 3 - self.alpha, self.alpha - 2)
     return (f1 * f2).real