コード例 #1
0
ファイル: rpnGeometry.py プロジェクト: flawr/rpn
def getNSphereRadius( n, k ):
    if real_int( k ) < 3:
        raise ValueError( 'the number of dimensions must be at least 3' )

    if not isinstance( n, RPNMeasurement ):
        return RPNMeasurement( n, 'meter' )

    dimensions = n.getDimensions( )

    if dimensions == { 'length' : 1 }:
        return n
    elif dimensions == { 'length' : int( k - 1 ) }:
        m2 = n.convertValue( RPNMeasurement( 1, [ { 'meter' : int( k - 1 ) } ] ) )

        result = root( fdiv( fmul( m2, gamma( fdiv( k, 2 ) ) ),
                             fmul( 2, power( pi, fdiv( k, 2 ) ) ) ), fsub( k, 1 ) )

        return RPNMeasurement( result, [ { 'meter' : 1 } ] )
    elif dimensions == { 'length' : int( k ) }:
        m3 = n.convertValue( RPNMeasurement( 1, [ { 'meter' : int( k ) } ] ) )

        result = root( fmul( fdiv( gamma( fadd( fdiv( k, 2 ), 1 ) ),
                                   power( pi, fdiv( k, 2 ) ) ),
                             m3 ), k )

        return RPNMeasurement( result, [ { 'meter' : 1 } ] )
    else:
        raise ValueError( 'incompatible measurement type for computing the radius: ' +
                          str( dimensions ) )
コード例 #2
0
	def plotAdjustedHistograms(self, bs = 50, sbs = 30, quality = 1):
		bins, avs, stds = self.getBinnedData(quality, bs, length=True)
		rv = scipy.stats.lognorm
		nbin = lambda k, p, r: mpmath.gamma(k + r)/(mpmath.gamma(k+1)*mpmath.gamma(r))*np.power(1-p, r)*np.power(p, k)
		nbin_npy = np.frompyfunc(nbin, 3, 1)
		results = []
		for bn in range(bs):
			if len(bins[bn]) == 0:
				continue
			start = time.clock()
			data = np.clip(bins[bn], 0.01, 1e10)*2000/128.12
			av = np.average(data)
			va = np.var(data)
			_, bx, _ = plt.hist(data, bins=sbs, label=str(bn)+":"+str(bn/float(bs)), normed = True)
			x = np.linspace(np.min(bx), np.max(bx), 100)
			shape, _, scale = rv.fit(data, floc=0)
			plt.plot(x, rv.pdf(x, shape, scale=scale), 'k--', linewidth=1.5, label = 'lognormal')
			plt.plot(x, mlab.normpdf(x, av, np.sqrt(va)), 'r:', linewidth=1.5, label = 'normal')
			r = (av*av)/(va-av)
			p = (va-av)/(va)
			ssq = lambda params: -np.sum(np.log(nbin_npy(data, params[0], params[1]).astype('float64')))
			res = minimize(ssq, np.array([p,r]), method='nelder-mead')
			#print p,r,res
			plt.plot(x, nbin_npy(x, res.x[0], res.x[1]).astype('float64'), "g-", linewidth = 2, label='nbin')
			plt.title(str(res.x[0]) + ", " + str(res.x[1]))
			results.append([bn, res.x[0], res.x[1]])
			plt.legend()
			plt.savefig("results/clognormal"+str(bn)+".pdf")
			plt.clf()
			print bn, "done in", time.clock() - start
		results = np.array(results)
		np.save("results/nbinParams.npy", results)
コード例 #3
0
def D(k, w):
    """
    Henning
    """

    two_lambda_j_prime = 2 * (4 - 1.5) * pow(k, 2)
    h2f3 = hyp2f3(1, 0.5, 0.5 - 4, 1 + w, 1 - w, two_lambda_j_prime)

    print(two_lambda_j_prime)
    print(int(h2f3))

    result = 1
    result -= h2f3

    print(result)

    third = sqrt(pi) * w
    third *= gamma(4 + 1) * gamma(0.5 - 4)
    third /= csc(pi * w)
    third /= gamma(4 + 1.5 + w) * gamma(4 + 1.5 - w)
    third *= pow(two_lambda_j_prime, 4 + 0.5)
    third *= hyp1f2(4 + 1, 4 + 1.5 + w, 4 + 1.5 - w, two_lambda_j_prime)

    print(int(third))

    result += third

    print(result)

    lambda_kappa_j_p2 = (4 - 1.5) / ((pow(5.099, 2) - 1) * (4 - 0.5))
    print(lambda_kappa_j_p2 * pow(k, 2))

    result /= pow(k, 2) * lambda_kappa_j_p2

    return result
コード例 #4
0
	def plotCorrectedHistogramsLogNormalForBins(self, bs = 50, sbs = 30, quality = 1):
		bins, avs, stds = self.getBinnedData(quality, bs, length=True)
		rv = scipy.stats.lognorm
		nbin = lambda k, p, r: mpmath.gamma(k + r)/(mpmath.gamma(k+1)*mpmath.gamma(r))*np.power(1-p, r)*np.power(p, k)
		nbin_npy = np.frompyfunc(nbin, 3, 1)
		for bn in range(bs):
			if len(bins[bn]) == 0:
				continue
			_, bx, _ = plt.hist(bins[bn], bins=sbs, label=str(bn)+":"+str(bn/float(bs)), normed = True)
			#mu = np.log(np.power(avs[bn],2)/np.sqrt(np.power(avs[bn],2) + np.power(stds[bn],2)))
			#sig = np.log(np.sqrt(np.power(avs[bn],2) + np.power(stds[bn],2))/avs[bn])
			data = np.clip(bins[bn], 0.01, 1e10)
			x = np.linspace(np.min(bx), np.max(bx), 100)
			shape, _, scale = rv.fit(data, floc=0)
			plt.plot(x, rv.pdf(x, shape, scale=scale), 'k--', linewidth=1.5, label = 'lognormal')
			plt.plot(x, mlab.normpdf(x, avs[bn], stds[bn]), 'r:', linewidth=1.5, label = 'normal')
			if(bn % 10 == 0):
				r = (avs[bn]*avs[bn])/(stds[bn]*stds[bn]-avs[bn])
				p = (stds[bn]*stds[bn]-avs[bn])/(stds[bn]*stds[bn])
				ssq = lambda params: -np.sum(np.log(nbin_npy(np.array(bins[bn]), params[0], params[1]).astype('float64')))
				res = minimize(ssq, np.array([p,r]), method='nelder-mead')
				print p,r,res
				plt.plot(x, nbin_npy(x, res.x[0], res.x[1]).astype('float64'), "g-", linewidth = 2, label='nbin')
			plt.legend()
			plt.savefig("results/clognormal"+str(bn)+".pdf")
			plt.clf()
コード例 #5
0
ファイル: rpnGeometry.py プロジェクト: GaelicGrime/rpn
def getKSphereRadius(n, k):
    if k < 3:
        raise ValueError('the number of dimensions must be at least 3')

    if not isinstance(n, RPNMeasurement):
        return RPNMeasurement(n, 'meter')

    dimensions = n.getDimensions()

    if dimensions == {'length': 1}:
        return n

    if dimensions == {'length': int(k - 1)}:
        area = n.convertValue(RPNMeasurement(1, [{'meter': int(k - 1)}]))

        result = root(
            fdiv(fmul(area, gamma(fdiv(k, 2))), fmul(2, power(pi, fdiv(k,
                                                                       2)))),
            fsub(k, 1))

        return RPNMeasurement(result, [{'meter': 1}])

    if dimensions == {'length': int(k)}:
        volume = n.convertValue(RPNMeasurement(1, [{'meter': int(k)}]))

        result = root(
            fmul(fdiv(gamma(fadd(fdiv(k, 2), 1)), power(pi, fdiv(k, 2))),
                 volume), k)

        return RPNMeasurement(result, [{'meter': 1}])

    raise ValueError(
        'incompatible measurement type for computing the radius: ' +
        str(dimensions))
コード例 #6
0
 def B_01(Non, Noff, alpha):
     Ntot = Non + Noff
     gam = (1 + 2 * Noff) * power(alpha, (0.5 + Ntot)) * gamma(0.5 + Ntot)
     delta = ((2 * power((1 + alpha), Ntot)) * gamma(1 + Ntot) *
              hyp2f1(0.5 + Noff, 1 + Ntot, 1.5 + Noff, (-1 / alpha)))
     c1_c2 = sqrt(pi) / (2 * atan(1 / sqrt(alpha)))
     return gam / (c1_c2 * delta)
コード例 #7
0
def U_Kummer_mpmath(a, x):
    factor = np.sqrt(np.pi) * 2**(-0.25 - 1 / 2. * a)
    factor_2 = np.sqrt(np.pi) * 2**(0.25 - 1 / 2. * a) * x
    first_term = factor * mpmath.hyp1f1(
        0.5 * a + 0.25, 0.5, 0.5 * x**2) / mpmath.gamma(0.75 + 0.5 * a)
    second_term = factor_2 * mpmath.hyp1f1(
        0.5 * a + 0.75, 1.5, 0.5 * x**2) / mpmath.gamma(0.25 + 0.5 * a)
    return first_term + second_term
コード例 #8
0
def I(eta1, eta2, k1, k2, l):

    Il = 0.25 * (4. * k1 * k2 / (k1 - k2)**2)**(l + 1) * math.exp(
        math.pi * abs(eta1 - eta2) / 2.) * abs(
            gamma(l + 1 + 1j * eta1) *
            gamma(l + 1 + 1j * eta2)) / gamma(2. * l + 2)

    return Il * G(eta1, eta2, k1, k2, l)
コード例 #9
0
def U_Kummer_fortran(a, x):
    factor = np.sqrt(np.pi) * 2**(-0.25 - 1 / 2. * a)
    factor_2 = np.sqrt(np.pi) * 2**(0.25 - 1 / 2. * a) * x
    first_term = factor * fortran_functions.kummers_function(
        0.5 * a + 0.25, 0.5, 0.5 * x**2) / mpmath.gamma(0.75 + 0.5 * a)
    second_term = factor_2 * fortran_functions.kummers_function(
        0.5 * a + 0.75, 1.5, 0.5 * x**2) / mpmath.gamma(0.25 + 0.5 * a)
    return first_term + second_term
コード例 #10
0
 def eXtmp(x, g, l):
     if (l > 0) & (l < 1):
         return ((l**(g * l - 1) * (1 - l)**(g *
                                             (1 - l) - 1) * g**(g / 2 - 1) /
                  (mm.gamma(g * l) * mm.gamma(g * (1 - l)))) *
                 (x + np.sqrt(g))**g * mm.exp(-np.sqrt(g) * x - g))
     else:
         return (g**(g / 2) * (x + np.sqrt(g))**g *
                 mm.exp(-np.sqrt(g) * x - g) / mm.gamma(g))
コード例 #11
0
def mean(k):
    """
    Mean of the chi distribution.
    """
    _validate_k(k)
    with mpmath.extradps(5):
        k = mpmath.mpf(k)
        return mpmath.sqrt(2) * mpmath.gamma((k + 1)/2) / mpmath.gamma(k/2)
    return k
コード例 #12
0
def test_var():
    with mpmath.workdps(50):
        xi = mpmath.mpf('0.25')
        mu = 3
        sigma = 2
        g1 = mpmath.gamma(1 - xi)
        g2 = mpmath.gamma(1 - 2 * xi)
        assert mpmath.almosteq(genextreme.var(xi, mu, sigma),
                               sigma**2 * (g2 - g1**2) / xi**2)
コード例 #13
0
ファイル: maxkappa.py プロジェクト: YuguangTong/qtn-proj
 def b(zc, n, t, k):
     """
 
     """
     tau = mp.sqrt(2*k/ (2*k-3) / t)
     b1 = zc * mp.exp(-zc**2)
     b2 = n * tau * zc * gamma(k+1)/gamma(k-1/2)/k**(3/2)
     b2 /= (1+ (zc * tau)**2 / k)**k
     return b1 + b2
コード例 #14
0
def alt_f(n):
    func=[[0]*(n+1) for i in range(n+1)]
    #func[0]=[mpm.gamma(i+1) for i in range(n+1)]
    func[0]=[1 for i in range(n+1)]
    for k in range(1,n+1):
        for i in range(n+1):
            func[k][i]=sum([mpm.gamma(j+1)*mpm.gamma(1-j+i)*func[k-1][j]/mpm.gamma(i+1) for j in range(i+1)])
    print('table created')
    return func
コード例 #15
0
 def _do_record_query1(vector):
     shape_v = vector.get_shape().as_list()
     dimension = np.array(shape_v).prod()
     beta = np.float(
         (variance * dimension * mp.gamma(dimension / exponents) /
          mp.gamma((dimension + 2) / exponents))**(exponents / 2))
     # print ("beta",type(beta))
     return self.record_sum_query(exponents, l2_norm_bound, dimension,
                                  beta)
コード例 #16
0
ファイル: test_cdflib.py プロジェクト: yishuifengxing/scipy
def _student_t_cdf(df, t, dps=None):
    if dps is None:
        dps = mpmath.mp.dps
    with mpmath.workdps(dps):
        df, t = mpmath.mpf(df), mpmath.mpf(t)
        fac = mpmath.hyp2f1(0.5, 0.5*(df + 1), 1.5, -t**2/df)
        fac *= t*mpmath.gamma(0.5*(df + 1))
        fac /= mpmath.sqrt(mpmath.pi*df)*mpmath.gamma(0.5*df)
        return 0.5 + fac
コード例 #17
0
ファイル: test_cdflib.py プロジェクト: Juanlu001/scipy
def _student_t_cdf(df, t, dps=None):
    if dps is None:
        dps = mpmath.mp.dps
    with mpmath.workdps(dps):
        df, t = mpmath.mpf(df), mpmath.mpf(t)
        fac = mpmath.hyp2f1(0.5, 0.5*(df + 1), 1.5, -t**2/df)
        fac *= t*mpmath.gamma(0.5*(df + 1))
        fac /= mpmath.sqrt(mpmath.pi*df)*mpmath.gamma(0.5*df)
        return 0.5 + fac
コード例 #18
0
ファイル: ztnb_em.py プロジェクト: zj-zhang/CLAM-BleedingEdge
def ztnb_pmf(y, mu, alpha):
    r = 1.0 / alpha
    if y <= 0:
        raise Exception('y must be larger than 0.')
    p = mu / (mu + r + 0.0)
    ztnbin_mpmath = lambda y, p, r: mpmath.gamma(y + r) / (
        mpmath.gamma(y + 1) * mpmath.gamma(r)) * np.power(1 - p, r) * np.power(
            p, y) / (1 - np.power(1 - p, r))
    ztnbin = np.frompyfunc(ztnbin_mpmath, 3, 1)
    return float(ztnbin(y, p, r))
コード例 #19
0
 def avTtmp(x, g, l):
     if (l > 0) & (l < 1):
         return ((mm.gamma(g * l) * mm.gamma(g * (1 - l)) * l**(1 - g * l) *
                  (1 - l)**(1 - g * (1 - l)) * g**(1 - g / 2)) *
                 mm.gammainc(g, a=np.sqrt(g) * x + g, regularized=True) *
                 (x + np.sqrt(g))**(-g) * mm.exp(np.sqrt(g) * x + g))
     else:
         return ((mm.gamma(g) * g**(-g / 2)) *
                 mm.gammainc(g, a=np.sqrt(g) * X[i] + g, regularized=True) *
                 (x + np.sqrt(g))**(-g) * mm.exp(np.sqrt(g) * x + g))
コード例 #20
0
 def G(self,s): # Laplace-Transform
   zz = 2*self.v + 2 + s
   mu = sqrt(self.v**2+2*zz)
   a  = mu/2 - self.v/2 - 1
   b  = mu/2 + self.v/2 + 2
   v1 = (2*self.alp)**(-a)*gamma(b)/gamma(mu+1)/(zz*(zz - 2*(1 + self.v)))
   prec = floor(max(log10(abs(v1)),mp.dps))+self.prec
   # additional precision needed for computation of hyp1f1
   with mp.extradps(prec):
     value = hyp1f1(a,mu + 1,self.beta)*v1
   return value
コード例 #21
0
def VMFMeanDirDensity(x, k, p):

    for i in range(0, len(x)):
        if (x[i] < -1.0 or x[i] > 1.0):
            sys.exit('Input of x must be within -1 to 1')
    coeff = float((k / 2)**(p / 2 - 1) * (mpmath.gamma(
        (p - 1) / 2) * mpmath.gamma(1 / 2) * mpmath.besseli(p / 2 - 1, k))
                  **(-1))
    #sp.special.gamma((p-1)/2) becoming infinity
    y = coeff * np.exp(k * x) * np.power((1 - np.square(x)), ((p - 2) / 2))
    return y
コード例 #22
0
def MSD_generic(x0, H, lambd, sigma, t):
    y = np.zeros((len(t), 1))
    for i in range(0, len(t)):
        y[i, 0] = (x0**2) * ((1 - exp(-lambd * t[i]))**2).real + (sigma**2) * (
            t[i]**(2 * H)) * exp(-lambd * t[i]).real + (sigma**2) / (
                2 * (lambd**(2 * H))) * (complex(
                    gamma(2 * H + 1) - gammainc(2 * H + 1, lambd * t[i], inf)
                ) + exp(-2 * 1j * pi * H) * exp(-2 * lambd * t[i]) * complex(
                    gamma(2 * H + 1) - gammainc(2 * H + 1, -lambd * t[i], inf))
                                         ).real
    return y
コード例 #23
0
ファイル: asian.py プロジェクト: daifengqi/PyFENG
    def price_element_real(self, tau, q_value, k):
        nu = self.nu()

        p1 = m.exp(-(nu ** 2 - q_value ** 2) * tau / 2)
        p2 = (q_value * m.gamma((nu + q_value) / 2)) / (
            4 * self.eta_q(eigenval=q_value) * m.gamma(1 + q_value)
        )
        const = (2 * k) ** ((nu + 3) / 2) * m.exp(-1 / (4 * k))
        w1 = m.whitw(-(nu + 3) / 2, q_value / 2, 1 / (2 * k))
        m1 = m.whitm((1 - nu) / 2, q_value / 2, 1 / (2 * self.b))
        return p1 * p2 * const * w1 * m1
コード例 #24
0
    def beta(self, a, b):
        """
        Uses either mpmath's gamma or log-gamma function to compute values of the beta function.
        """

        a, b = mpmath.mpf(a), mpmath.mpf(b)

        if self.use_log: beta = mpmath.exp(mpmath.loggamma(a) + mpmath.loggamma(b) - mpmath.loggamma(a + b))
        else: beta = mpmath.gamma(a) * mpmath.gamma(b) / mpmath.gamma(a + b)

        return beta
コード例 #25
0
def nk(n,A):
    n_k=[]
    for length in range(1,n+1):
        #x=[n**(length-1)*(-1)**i*mpm.gamma(n+1)*A[length-1][i]*mpm.gamma(i+1)/(mpm.gamma(i+length)**2*mpm.gamma(1-i+n)) for i in range(n+1)]
        x=[mpm.gamma(n+1.)*(-1)**i*A[length-1][i]*mpm.gamma(i+1)/(mpm.gamma(i+length)**2*mpm.gamma(2-i-length+n)) 
        for i in range(n-length+2)]
        n_k.append(sum(x))
    knk=sum(k*n_k[k-1] for k in range(1,n+1))
    snk=sum(n_k)
    kavg=knk*1./snk
    a=kavg/n**(1.0/2)
    return n_k, kavg, a
コード例 #26
0
    def probability(self, x):
        '''
          Probability of x with Cummulative Density Function of Beta Geometric Distribution
          Parameter:
            x   = Data value
          Output:
            p   = Calculated Probability
        '''
        beta = lambda a, b: (gamma(a) * gamma(b)) / gamma(a + b)
        p = beta(self.alpha + 1, self.beta + x - 1) / beta(self.alpha, self.beta)

        return float(p)
コード例 #27
0
ファイル: weibull_max.py プロジェクト: WarrenWeckesser/mpsci
def var(k, loc, scale):
    """
    Variance of the Weibull distribution (for maxima).

    This is a three-parameter version of the distribution.  The more typical
    two-parameter version has just the parameters k and scale.
    """
    with mpmath.extradps(5):
        k, loc, scale = _validate_params(k, loc, scale)
        v1 = 1 + 1 / k
        v2 = 1 + 2 / k
        return scale**2 * (mpmath.gamma(v2) - mpmath.gamma(v1)**2)
コード例 #28
0
ファイル: asian.py プロジェクト: daifengqi/PyFENG
    def price_element_imag(self, tau, p_value, k):
        nu = self.nu()

        p1 = m.exp(-(nu ** 2 + p_value ** 2) * tau / 2)
        p2 = (p_value * m.gamma(complex(nu / 2, p_value / 2))) / (
            4
            * self.xi_p(eigenval=p_value)
            * m.gamma(complex(1, p_value))
        )
        const = (2 * k) ** ((nu + 3) / 2) * m.exp(-1 / (4 * k))
        w1 = m.whitw(-(nu + 3) / 2, complex(0, p_value / 2), 1 / (2 * k))
        m1 = m.whitm((1 - nu) / 2, complex(0, p_value / 2), 1 / (2 * self.b))
        return p1 * p2 * const * w1 * m1
コード例 #29
0
ファイル: weibull_max.py プロジェクト: WarrenWeckesser/mpsci
def skewness(k, loc, scale):
    """
    Skewness of the Weibull distribution (for maxima).

    This is a three-parameter version of the distribution.  The more typical
    two-parameter version has just the parameters k and scale.
    """
    with mpmath.extradps(5):
        k, loc, scale = _validate_params(k, loc, scale)
        g1 = mpmath.gamma(1 + 1 / k)
        g2 = mpmath.gamma(1 + 2 / k)
        g3 = mpmath.gamma(1 + 3 / k)
        return -(g3 - 3 * g1 * g2 + 2 * g1**3) / mpmath.power(g2 - g1**2, 1.5)
コード例 #30
0
def get_sbar_lam_approx(lam, n, R, a, alpha1, alpha2, beta1, beta2,
                        verbose = 0):
    """Function to compute the expected strength as a function of
       the latent parameter lam approximately. See Appendix of
       the WHSCM paper for details of approximation.

    Args:
        lam (float): The latent parameter lambda of a node.
        n (int): The number of nodes in a graph.
        R (float): The R parameter of the WHSCM.
        a (float): The a parameter of the WHSCM.
        alpha1 (float): The alpha1 parameter of the WHSCM.
        alpha2 (float): The alpha2 parameter of the WHSCM.
        beta1 (float): The beta1 parameter of the WHSCM.
        beta2 (float): The beta2 parameter of the WHSCM.

    Returns:
        sbar_lam_approx (float): The expected strength of a node
                                 with the latent parameter lambda
                                 in the WHSCM.
    """
    lam_c = get_lam_c(a, R, beta1)
    A1 = get_A1_const(alpha1, alpha2, lam_c)
    A2 = get_A2_const(alpha1, alpha2, lam_c, A1)

    if lam >= 1 and lam <= lam_c:
        term1 = (n * A1 * lam**(1.+beta1)) / (np.exp(2*R) * (lam_c)**(alpha1) * a * a * beta1 * (1. + (lam_c / lam)**beta1))
        term2 = -(lam_c**(2.+beta1))
        term3 = ((lam_c**(alpha1))/(1. + lam**beta1)) * (lam**beta1 + lam_c**beta1)
        term4 = (2.+beta1-alpha1)*(lam**beta1 + lam_c**beta1)/(alpha1-2.)
        term5 = (lam_c**alpha1) * special.hyp2f1(1., (alpha1-2.)/beta1, 1. + (alpha1-2.)/beta1, - (lam**beta1))
        term6 = (lam_c**2.) * special.hyp2f1(1., (alpha1-2.)/beta1, 1. + (alpha1-2.)/beta1, - ((lam/lam_c)**beta1))
        term7 = (n * A2 * lam**(beta1) * (lam_c)**(1.-alpha2)) / a
        term8 = (mp.gamma(alpha2-1.)/mp.gamma(alpha2)) * special.hyp2f1(1., alpha2-1., alpha2, - (a*np.exp(2.*R))/(lam**(1.+beta1) * lam_c))
        sbar_lam_approx = term1*(term2 + term3 + term4*(term5 - term6)) + term7*term8

    elif lam > lam_c:
        term1 = (n * A1 * mp.gamma((-beta1+alpha1-1.)/(1.+beta1))) / (a * (lam_c**alpha1) * (1.+beta1) * mp.gamma(alpha1/(1.+beta1)))
        term2 = (lam_c**alpha1) * special.hyp2f1(1., (alpha1/(1.+beta1)) - 1., alpha1/(1.+beta1), - (a*np.exp(2.*R))/lam)
        term3 = (lam_c**(1.+beta1)) * special.hyp2f1(1., (alpha1/(1.+beta1)) - 1., alpha1/(1.+beta1), - (a*np.exp(2.*R))/(lam * lam_c**(1.+beta1)))
        term4 = (n * A2 * (lam**beta2) * (lam_c**(1.+beta1-alpha2-beta2))) / (a*(alpha2-1.))
        term5 = special.hyp2f1(1., (alpha2-1.)/beta2, 1. + (alpha2-1.)/beta2, -(lam/(lam_c))**beta2)
        sbar_lam_approx = term1*(term2 - term3) + term4*term5

    else:
        if verbose == 1:
            print "ERROR (get_sbar_lam_approx): Lambda parameter "+\
                  "has to be between 1 and infinity."
        sbar_lam_approx = None

    return sbar_lam_approx
コード例 #31
0
def t_sf(t, df):
    """
    Student t distribution cumulative density function or survival function

    :param t: t statistic
    :param df: degrees of freedom
    :return: area under the PDF from -inf to t
    """
    t = -mp.fabs(t)
    lhs = mp.gamma((df + 1) / 2) / (mp.sqrt(df * mp.pi) * mp.gamma(df / 2))
    rhs = mp.quad(lambda x: (1 + (x * x) / df)**(-df / 2 - 1 / 2),
                  [-mp.inf, t])
    gc.collect()
    return lhs * rhs
コード例 #32
0
ファイル: miscmaths.py プロジェクト: fossabot/Halfpipe-1
def t2z_convert(t, nu):
    t = mpf(t)
    nu = mpf(nu)

    z = sqrt(mpf("2")) * erfinv(  # inverse normal cdf
        mpf("2") * t *
        gamma((mpf("1") / mpf("2")) * nu + mpf("1") / mpf("2")) * hyper(
            (mpf("1") / mpf("2"),
             (mpf("1") / mpf("2")) * nu + mpf("1") / mpf("2")),
            (mpf("3") / mpf("2"), ),
            -power(t, mpf("2")) / nu,
        ) / (sqrt(pi) * sqrt(nu) * gamma((mpf("1") / mpf("2")) * nu)))

    return z
コード例 #33
0
 def plotAdjustedHistograms(self, bs=50, sbs=30, quality=1):
     bins, avs, stds = self.getBinnedData(quality, bs, length=True)
     rv = scipy.stats.lognorm
     nbin = lambda k, p, r: mpmath.gamma(k + r) / (mpmath.gamma(
         k + 1) * mpmath.gamma(r)) * np.power(1 - p, r) * np.power(p, k)
     nbin_npy = np.frompyfunc(nbin, 3, 1)
     results = []
     for bn in range(bs):
         if len(bins[bn]) == 0:
             continue
         start = time.clock()
         data = np.clip(bins[bn], 0.01, 1e10) * 2000 / 128.12
         av = np.average(data)
         va = np.var(data)
         _, bx, _ = plt.hist(data,
                             bins=sbs,
                             label=str(bn) + ":" + str(bn / float(bs)),
                             normed=True)
         x = np.linspace(np.min(bx), np.max(bx), 100)
         shape, _, scale = rv.fit(data, floc=0)
         plt.plot(x,
                  rv.pdf(x, shape, scale=scale),
                  'k--',
                  linewidth=1.5,
                  label='lognormal')
         plt.plot(x,
                  mlab.normpdf(x, av, np.sqrt(va)),
                  'r:',
                  linewidth=1.5,
                  label='normal')
         r = (av * av) / (va - av)
         p = (va - av) / (va)
         ssq = lambda params: -np.sum(
             np.log(nbin_npy(data, params[0], params[1]).astype('float64')))
         res = minimize(ssq, np.array([p, r]), method='nelder-mead')
         #print p,r,res
         plt.plot(x,
                  nbin_npy(x, res.x[0], res.x[1]).astype('float64'),
                  "g-",
                  linewidth=2,
                  label='nbin')
         plt.title(str(res.x[0]) + ", " + str(res.x[1]))
         results.append([bn, res.x[0], res.x[1]])
         plt.legend()
         plt.savefig("results/clognormal" + str(bn) + ".pdf")
         plt.clf()
         print bn, "done in", time.clock() - start
     results = np.array(results)
     np.save("results/nbinParams.npy", results)
コード例 #34
0
def Phi(a, x):
    """Calculates Phi(a,x) = exp(x**2/4)*U(a,x), where U(a,x) is the
    parabolic cylinder funciton. Implementation uses the relation to
    kummers function (Eq.19.12.1 and 13.1.32 in Handbook of
    mathematical Functions, Abramowitz and Stegun, 1972, Dover
    Puplications, New York)
    """

    fac1 = np.sqrt(np.pi) * 2**(-0.25 - 1 / 2. * a)
    fac2 = np.sqrt(np.pi) * 2**(0.25 - 1 / 2. * a) * x
    kummer1 = ff.kummers_function(0.5 * a + 0.25, 0.5, 0.5 * x**2)
    first_term = kummer1 / mpmath.gamma(0.75 + 0.5 * a)
    kummer2 = ff.kummers_function(0.5 * a + 0.75, 1.5, 0.5 * x**2)
    second_term = kummer2 / mpmath.gamma(0.25 + 0.5 * a)
    return fac1 * first_term + fac2 * second_term
コード例 #35
0
ファイル: t.py プロジェクト: WarrenWeckesser/mpsci
def sf(x, df):
    """
    Survival function of Student's t distribution.
    """
    if df <= 0:
        raise ValueError('df must be greater than 0')

    with mpmath.extradps(5):
        half = mpmath.mp.one/2
        x = mpmath.mpf(x)
        df = mpmath.mpf(df)
        h = (df + 1) / 2
        p1 = x * mpmath.gamma(h)
        p2 = mpmath.hyp2f1(half, h, 3*half, -x**2/df)
        return half - p1*p2/mpmath.sqrt(mpmath.pi*df)/mpmath.gamma(df/2)
コード例 #36
0
ファイル: noncentral_t_distr.py プロジェクト: dkasak/pacal
def multiprec_pdf(df, mu, x):
    df = mp.mpf(str(df))
    mu = mp.mpf(str(mu))
    x  = mp.mpf(str(x))
    pdf = (mp.exp(-mu**2 / 2) * 2**(-df) * df**(df/2) * mp.gamma(df+1) * \
           ((mp.sqrt(2) * x * mu * (x**2+df)**(-df/2-1) * mp.hyp1f1(df/2+1,1.5,(mu**2 * x**2)/(2 * (x**2+df))))/(mp.gamma((df+1)/2))+((x**2+df)**(-df/2-0.5) * mp.hyp1f1((df+1)/2,0.5,(mu**2 * x**2)/(2 * (x**2+df))))/(mp.gamma(df/2+1))))/(mp.gamma(df/2))
    return pdf
コード例 #37
0
def make_gamma_vals():
    from mpmath import gamma

    x = [-mpf('0.5'), -mpf('0.01'), mpf('0.01')] + linspace(mpf('0.1'), 10, 100) + [mpf(20), mpf(30)]
    ga = [gamma(val) for val in x]

    return make_special_vals('gamma_vals', ('x', x), ('ga', ga))
コード例 #38
0
ファイル: old2cpDetect.py プロジェクト: ChayaSt/cpdetect
def findGaussianChangePoint( data ):
	
	# the denominator. This is the easy part.
	N = len( data )

	if N<6 : return None # can't find a cp in data this small

	# set up gamma function table
	#for i in range(N):
		

	s2 = mpf(data.var())
	gpart = gamma( mpf(N)/2.0 - 1 )
	denom = (pi**1.5) * mpf((N*s2))**( -N/2.0 + 0.5 ) * gpart

	# the numerator. A little trickier.
	# calc_twostate_weights() already deals with ts<3 and ts>N-2.
	weights=calc_twostate_weights( data )
	if weights is None: return None

	num = 2.0**2.5 * abs(data.mean()) * weights.mean()

	logodds = log( num ) - log( denom ) 	

	print "num:", num, "log num:", log(num), "| denom:", denom, "log denom:", log(denom), "|| log odds:", logodds 
	
	# If there is a change point, then logodds will be greater than 0
	if logodds < 0 : 
		return None
	
	return ( weights.argmax(), logodds ) 
コード例 #39
0
ファイル: matern.py プロジェクト: pennajm/gptools
def matern_function(Xi, Xj, *args):
    r"""Matern covariance function of arbitrary dimension, for use with :py:class:`ArbitraryKernel`.
    
    The Matern kernel has the following hyperparameters, always referenced in
    the order listed:
    
    = ===== ====================================
    0 sigma prefactor
    1 nu    order of kernel
    2 l1    length scale for the first dimension
    3 l2    ...and so on for all dimensions
    = ===== ====================================
    
    The kernel is defined as:
    
    .. math::
    
        k_M = \sigma^2 \frac{2^{1-\nu}}{\Gamma(\nu)}
        \left (\sqrt{2\nu \sum_i\left (\frac{\tau_i^2}{l_i^2}\right )}\right )^\nu
        K_\nu\left(\sqrt{2\nu \sum_i\left(\frac{\tau_i^2}{l_i^2}\right)}\right)
    
    Parameters
    ----------
    Xi, Xj : :py:class:`Array`, :py:class:`mpf`, tuple or scalar float
        Points to evaluate the covariance between. If they are :py:class:`Array`,
        :py:mod:`scipy` functions are used, otherwise :py:mod:`mpmath`
        functions are used.
    *args
        Remaining arguments are the 2+num_dim hyperparameters as defined above.
    """
    num_dim = len(args) - 2
    nu = args[1]
    
    if isinstance(Xi, scipy.ndarray):
        if isinstance(Xi, scipy.matrix):
            Xi = scipy.asarray(Xi, dtype=float)
            Xj = scipy.asarray(Xj, dtype=float)
        
        tau = scipy.asarray(Xi - Xj, dtype=float)
        l_mat = scipy.tile(args[-num_dim:], (tau.shape[0], 1))
        r2l2 = scipy.sum((tau / l_mat)**2, axis=1)
        y = scipy.sqrt(2.0 * nu * r2l2)
        k = 2.0**(1 - nu) / scipy.special.gamma(nu) * y**nu * scipy.special.kv(nu, y)
        k[r2l2 == 0] = 1
    else:
        try:
            tau = [xi - xj for xi, xj in zip(Xi, Xj)]
        except TypeError:
            tau = Xi - Xj
        try:
            r2l2 = sum([(t / l)**2 for t, l in zip(tau, args[2:])])
        except TypeError:
            r2l2 = (tau / args[2])**2
        y = mpmath.sqrt(2.0 * nu * r2l2)
        k = 2.0**(1 - nu) / mpmath.gamma(nu) * y**nu * mpmath.besselk(nu, y)
    k *= args[0]**2.0
    return k
コード例 #40
0
ファイル: recipe-577133.py プロジェクト: jacob-carrier/code
def Hypergeometric1F1(a,b,t,N):
    
#   Initiate the stepsize
    h = 2*pi/N;  
    
    c1 = mpf('0.5017')
    c2 = mpf('0.6407')
    c3 = mpf('0.6122')
    c4 = mpc('0','0.2645')
    
#   The for loop is evaluating the Laplace inversion at each point theta
#   which is based on the trapezoidal rule
    ans = 0.0
    for k in range(N):
      theta = -pi + (k+0.5)*h
      z    = N/t*(c1*theta/tan(c2*theta) - c3 + c4*theta)
      dz   = N/t*(-c1*c2*theta/sin(c2*theta)**2 + c1/tan(c2*theta)+c4)
      ans += exp(z*t)*F(a,b,t,z)*dz
          
    return gamma(b)*t**(1-b)*exp(t)*((h/(2j*pi))*ans)
コード例 #41
0
ファイル: rpnGeometry.py プロジェクト: ConceptJunkie/rpn
def getKSphereSurfaceArea( n, k ):
    if real_int( k ) < 3:
        raise ValueError( 'the number of dimensions must be at least 3' )

    if not isinstance( n, RPNMeasurement ):
        return getKSphereSurfaceArea( n, RPNMeasurement( real( n ), 'meter' ) )

    dimensions = n.getDimensions( )

    if dimensions == { 'length' : 1 }:
        m = n.convertValue( RPNMeasurement( 1, [ { 'meter' : 1 } ] ) )

        result = fmul( fdiv( fmul( power( pi, fdiv( k, 2 ) ), 2 ), gamma( fdiv( k, 2 ) ) ), power( m, fsub( k, 1 ) ) )

        return RPNMeasurement( result, [ { 'meter' : int( k - 1 ) } ] )
    elif dimensions == { 'length' : int( k - 1 ) }:
        return n
    elif dimensions == { 'length' : int( k ) }:
        radius = getKSphereRadius( n, k )
        return getKSphereSurfaceArea( radius, k )
    else:
        raise ValueError( 'incompatible measurement type for computing the surface area' )
コード例 #42
0
ファイル: rpnGeometry.py プロジェクト: flawr/rpn
def getNSphereVolume( n, k ):
    if real_int( k ) < 1:
        raise ValueError( 'the number of dimensions must be at least 3' )

    if not isinstance( n, RPNMeasurement ):
        return getNSphereVolume( RPNMeasurement( real( n ), 'meter' ), k )

    dimensions = n.getDimensions( )
    m = n.getValue( )

    if dimensions == { 'length' : 1 }:
        m = n.convertValue( RPNMeasurement( 1, [ { 'meter' : 1 } ] ) )

        result = fmul( fdiv( power( pi, fdiv( k, 2 ) ),
                             gamma( fadd( fdiv( k, 2 ), 1 ) ) ), power( m, k ) )

        return RPNMeasurement( result, [ { 'meter' : k } ] )
    elif dimensions == { 'length' : int( k - 1 ) }:
        radius = getNSphereRadius( n, k )
        return getNSphereVolume( radius, k )
    elif dimensions == { 'length' : int( k ) }:
        return n
    else:
        raise ValueError( 'incompatible measurement type for computing the volume' )
コード例 #43
0
ファイル: detectionScript.py プロジェクト: ChayaSt/cpdetect
from cpDetect import * 
import matplotlib.pyplot as plt
import scipy
from numpy import ones
from mpmath import mpf, gamma
import cPickle

FILE=open( "trajectory-1.dat", "r" )
u = cPickle.Unpickler( FILE )
trajectory = scipy.array( u.load() )

# generate gamma function table
t0=time()
print "generating gamma function table"
gammatable=[-99,-99,-99] # first three points don't make sense
for i in range(3,len(trajectory)+1): gammatable.append( gamma( 0.5*i - 1 ) )
t1=time()
print "took %3.3f seconds" % ( t1-t0 )

cpd = ChangePointDetector( trajectory,findGaussianChangePoint, gammatable )

# the part we want to time
t0 = time()
print "splitting"
cpd.split_init()
t1 = time()
print "took %3.3f seconds" % ( t1-t0 )
cpd.sort()
cpd.showall()
print "found %d change points" % cpd.nchangepoints()
コード例 #44
0
ファイル: old2cpDetect.py プロジェクト: ChayaSt/cpdetect
def calc_alpha( N, x2, s2 ):
	first = mpf(N)**(-N/2.0 + 1.0/2.0)
	second = mpf(s2)**(-N/2.0 + 1.0 )
	third = gamma( mpf(N)/2.0 - 1.0 ) 
	return first*second*third
コード例 #45
0
	def __init__(self, p = 0.1, r = 10):
		nbin_mpmath = lambda k, p, r: mpmath.gamma(k + r)/(mpmath.gamma(k+1)*mpmath.gamma(r))*np.power(1-p, r)*np.power(p, k)
		self.nbin = np.frompyfunc(nbin_mpmath, 3, 1)
		self.p = p
		self.r = r
コード例 #46
0
ファイル: neg_bin.py プロジェクト: jovesus/reg-gen
 def _get_value(self, x, mu, v):
     try:
         return rf(v, x) / gamma(x+1) * ( v/float(v+mu) ) ** v * ( mu/float(v+mu) ) ** x
     except ValueError:
         print(x, mu, v, file=sys.stderr)
         return 1
コード例 #47
0
def wprp_pl_lim(rp,r0,g,rmax):
    return (1/gamma(g/2)) * (h.rp*h.rlim/r0)**-g * gamma((g-1)/2) * \
           (gamma(0.5) * h.rp * h.rlim**g - h.rp**g *h.rlim * gamma(g/2)*\
           hyp2f1A(0.5,(g-1)/2,(g+1)/2,h.rp.value**2/h.rlim.value**2))
コード例 #48
0
def wprp_pl(rp,r0,g):
    return rp*(rp/r0)**-g * gamma(0.5)*gamma((g-1)/2.0)/gamma(g/2.0)
コード例 #49
0
ファイル: test_mpmath.py プロジェクト: 7islands/scipy
 def test_gammainc(self):
     assert_mpmath_equal(sc.gammainc,
                         _exception_to_nan(
                             lambda z, b: mpmath.gammainc(z, b=b)/mpmath.gamma(z)),
                         [Arg(a=0), Arg(a=0)])
コード例 #50
0
 def ftot(t):
     exp4t = mp.exp(4 * t)
     return exp4t * mp.exp(1j * omega * (t + exp4t * mp.gamma(t)))