def gamma(x): return Gamma(x)
def C_mof2Dto1D(r_core, beta): """ gamma in pixel """ return 1. / (beta - 1) * 2 * math.sqrt( np.pi) * r_core * Gamma(beta) / Gamma(beta - 1. / 2)
def levy(self): sigma = (Gamma(1 + self.beta) * sin(pi * self.beta / 2) / (Gamma(1 + self.beta) * self.beta * 2**( (self.beta - 1) / 2)))**(1 / self.beta) return 0.01 * (self.normal(0, 1) * sigma / fabs(self.normal(0, 1))**(1 / self.beta))
def factorial(n): return Gamma(n + 1)
def moffat1d_Flux2Amp(r_core, beta, Flux=1): """ Calculate the (astropy) amplitude of 1d Moffat profile given the core width, power index, and total flux F. Note in astropy unit (x,y) the amplitude should be scaled with 1/sqrt(pi).""" Amp = Flux * Gamma(beta) / (r_core * np.sqrt(np.pi) * Gamma(beta - 1. / 2) ) # Derived scaling factor return Amp
def f1(x): return Gamma(x)
def sedov(t, E0, rho0, g, n=1000, nu=3): """ solve the sedov problem t - the time E0 - the initial energy rho0 - the initial density n - number of points (10000) nu - the dimension g - the polytropic gas gamma """ # the similarity variable v_min = 2.0 / ((nu + 2) * g) v_max = 4.0 / ((nu + 2) * (g + 1)) v = v_min + arange(n) * (v_max - v_min) / (n - 1.0) a = calc_a(g, nu) beta = calc_beta(v, g=g, nu=nu) lbeta = log(beta) r = exp(-a[0] * lbeta[0] - a[2] * lbeta[1] - a[1] * lbeta[2]) rho = ((g + 1.0) / (g - 1.0)) * exp( a[3] * lbeta[1] + a[5] * lbeta[3] + a[4] * lbeta[2] ) p = exp(nu * a[0] * lbeta[0] + (a[5] + 1) * lbeta[3] + (a[4] - 2 * a[1]) * lbeta[2]) u = beta[0] * r * 4.0 / ((g + 1) * (nu + 2)) p *= 8.0 / ((g + 1) * (nu + 2) * (nu + 2)) # we have to take extra care at v=v_min, since this can be a special point. # It is not a singularity, however, the gradients of our variables (wrt v) are. # r -> 0, u -> 0, rho -> 0, p-> constant u[0] = 0.0 rho[0] = 0.0 r[0] = 0.0 p[0] = p[1] # volume of an n-sphere vol = (pi ** (nu / 2.0) / Gamma(nu / 2.0 + 1)) * power(r, nu) # note we choose to evaluate the integral in this way because the # volumes of the first few elements (i.e near v=vmin) are shrinking # very slowly, so we dramatically improve the error convergence by # finding the volumes exactly. This is most important for the # pressure integral, as this is on the order of the volume. # (dimensionless) energy of the model solution de = rho * u * u * 0.5 + p / (g - 1) # integrate (trapezium rule) q = inner(de[1:] + de[:-1], diff(vol)) * 0.5 # the factor to convert to this particular problem fac = (q * (t ** nu) * rho0 / E0) ** (-1.0 / (nu + 2)) # shock speed shock_speed = fac * (2.0 / (nu + 2)) rho_s = ((g + 1) / (g - 1)) * rho0 r_s = shock_speed * t * (nu + 2) / 2.0 p_s = (2.0 * rho0 * shock_speed * shock_speed) / (g + 1) u_s = (2.0 * shock_speed) / (g + 1) r *= fac * t u *= fac p *= fac * fac * rho0 rho *= rho0 return r, p, rho, u, r_s, p_s, rho_s, u_s, shock_speed
def GammaPDF(x,a,b): return b**a * x**(a-1.0) * np.exp(-b*x) / Gamma(a)
var = 0.5 # Controlling the variance of the proposal distribution for phi_. Thus, it is possible to control the acceptance rate of phi_. # Below, the implementation is slightly different due to the parametrisation of the Gamma distribution in Python. for i in tqdm(range(TotIter)): # Proposal distribution for beta (multivariate Normal) betac = np.random.multivariate_normal(list(itertools.chain(*beta_)), mprop, 1).T mu_ = func_mu(beta_) muc = func_mu(betac) # Computing the ratio stated in the Metropolis-Hastings algorithm ratio_beta = ( (-sum(np.log(Gamma(muc * phi_))) - sum(np.log(Gamma( (1 - muc) * phi_))) + sum((muc * phi_ - 1) * np.log(y)) + sum( ((1 - muc) * phi_ - 1) * np.log(1 - y)) - 0.5 * (betac - m).T @ V_1 @ (betac - m)) - (-sum(np.log(Gamma(mu_ * phi_))) - sum(np.log(Gamma( (1 - mu_) * phi_))) + sum((mu_ * phi_ - 1) * np.log(y)) + sum( ((1 - mu_) * phi_ - 1) * np.log(1 - y)) - 0.5 * (beta_ - m).T @ V_1 @ (beta_ - m))) # Accept/reject step for betac (beta candidate) if (np.random.uniform(0, 1, 1) < np.exp(ratio_beta)): beta_ = betac if (i > Burn): j_beta = j_beta + 1 # Proposal distribution for phi
def pmf(n, mu): return np.exp(-mu) * (mu**n) / Gamma(n + 1)
def mass(Rb, rho0, gamma): return (4. * np.pi * rho0 * (Rb * kpc_to_cm)**3 * Gamma(3. - gamma)) * GeV_to_m_sun
def qli_closed_form(l, i): """Closed form solution forq^l_i.""" return (2**l / np.pi * Gamma(0.5 * (1 + i)) * Gamma(l + 0.5 * (1 - i)) / Gamma(l + 1))
def h_pade(u, t, params): assert u != 0 A = np.sqrt(u * (u + 1j) - params['RHO']**2 * u**2) r_minus = -1j * params['RHO'] * u - A gamma0 = 1 gamma1 = -1 gamma2 = 1 + r_minus / 2 / A * Gamma(1 - 2 * params['ALPHA']) / Gamma( 1 - params['ALPHA'])**2 beta0 = -u * (u + 1j) / 2 beta1 = 1j * params['RHO'] * u * Gamma(1) / Gamma(1 + params['ALPHA']) * beta0 beta2 = beta0 ** 2 * Gamma(1) ** 2 / Gamma(1 + params['ALPHA']) ** 2 + \ 1j * params['RHO'] * u * Gamma(1 + params['ALPHA']) / Gamma(1 + 2 * params['ALPHA']) * beta1 # small-time b1 = Gamma(1) / Gamma(1 + params['ALPHA']) * beta0 * params['XI']**1 b2 = Gamma(1 + params['ALPHA']) / Gamma( 1 + 2 * params['ALPHA']) * beta1 * params['XI']**2 b3 = Gamma(1 + 2 * params['ALPHA']) / Gamma( 1 + 3 * params['ALPHA']) * beta2 * params['XI']**3 # large-time g0 = r_minus * gamma0 / A**0 / Gamma(1) g1 = r_minus * gamma1 / A**1 / Gamma(1 - params['ALPHA']) * params['XI']**(-1) g2 = r_minus * gamma2 / A**2 / Gamma(1 - 2 * params['ALPHA']) * params['XI']**(-2) q1 = (b1**2 * g1 - b1 * b2 * g2 + b1 * g0**2 - b2 * g0 * g1 - b3 * g0 * g2 + b3 * g1**2) / (b1**2 * g2 + 2 * b1 * g0 * g1 + b2 * g0 * g2 - b2 * g1**2 + g0**3) q2 = (b1**2 * g0 - b1 * b2 * g1 - b1 * b3 * g2 + b2**2 * g2 + b2 * g0**2 - b3 * g0 * g1) / (b1**2 * g2 + 2 * b1 * g0 * g1 + b2 * g0 * g2 - b2 * g1**2 + g0**3) q3 = (b1**3 + 2 * b1 * b2 * g0 + b1 * b3 * g1 - b2**2 * g1 + b3 * g0**2) / (b1**2 * g2 + 2 * b1 * g0 * g1 + b2 * g0 * g2 - b2 * g1**2 + g0**3) p1 = b1 p2 = (b1**3 * g1 + b1**2 * g0**2 + b1 * b2 * g0 * g1 - b1 * b3 * g0 * g2 + b1 * b3 * g1**2 + b2**2 * g0 * g2 - b2**2 * g1**2 + b2 * g0**3) / (b1**2 * g2 + 2 * b1 * g0 * g1 + b2 * g0 * g2 - b2 * g1**2 + g0**3) p3 = g0 * q3 return (p1 * t + p2 * t**2 + p3 * t**3) / (1 + q1 * t + q2 * t**2 + q3 * t**3)
def v(k): return Gamma(params['ALPHA'] * k + 1) / Gamma(params['ALPHA'] * k - params['ALPHA'] + 1)
def _pdf(self, x, z_0, beta): return beta* (x**2.) * np.exp(-(x/z_0)**beta )/Gamma(3./beta)/z_0**3.
def test_alpha_one_half(self): integral = fracderivative(lambda t: t**(1 / 2), 1, -1 / 2) self.assertAlmostEqual(integral, 1 * Gamma(3 / 2) / Gamma(2), places=3)