def lnlhood(self, param): """ This is the function that evaluates the likelihood at each point in NDIM space Parameters ---------- param : Returns ------- """ likeit = 0 #loop over all the ions for ii in range(self.nions): thislike = 0 #parity check : make sure data and models are aligned if (self.data[ii][0] != self.mod_colm_tag[ii]): raise ValueError( 'Mismtach between observables and models. This is a big mistake!!!' ) #now call the interpolator for models given current ion mod_columns = self.interpol[ii](param) #check if upper limit if (self.data[ii][3] == -1): #integrate the upper limit of a Gaussian - cumulative distribution arg = ((self.data[ii][1] - mod_columns) / (np.sqrt(2) * self.data[ii][2]))[0] thislike = mmath.log(0.5 + 0.5 * mmath.erf(arg)) likeit = likeit + float(thislike) #print self.data[ii][0], float(thislike), self.data[ii][1], mod_columns #check if lower limit elif (self.data[ii][3] == -2): #integrate the lower limit of a Gaussian - Q function arg = ((self.data[ii][1] - mod_columns) / (np.sqrt(2) * self.data[ii][2]))[0] thislike = mmath.log(0.5 - 0.5 * mmath.erf(arg)) likeit = likeit + float(thislike) #print self.data[ii][0], float(thislike), self.data[ii][1], mod_columns #if value, just eval Gaussian else: #add the likelihood for this ion thislike = -1 * np.log( np.sqrt(2 * np.pi) * self.data[ii][2]) - (self.data[ii][1] - mod_columns)**2 / ( 2 * self.data[ii][2]**2) #mpmath equivalent #thislike=-1*mmath.log(mmath.npdf(mod_columns[0],mu=self.data[ii][1],sigma=self.data[ii][2])) likeit = likeit + float(thislike) return likeit
def fl0xz(x0, seta2, seff2): xm = x0 / np.sqrt(2. * seta2) tau = lamda1 * seff2 / np.sqrt(2. * seta2) ki = theta * (1. + lamda2 * seff2) / np.sqrt(2. * seta2) intgr1 = erf(tau + ki - xm) intgr2 = erf(tau + ki + xm) return .5 * (intgr1 + intgr2)
def fl0xz(x0, seta2, seff2): xm = x0/np.sqrt(2.*seta2) tau = lamda1*seff2/np.sqrt(2.*seta2) ki = theta*(1.+lamda2*seff2)/np.sqrt(2.*seta2) intgr1 = erf(tau+ki-xm) intgr2 = erf(tau+ki+xm) return .5*(intgr1+intgr2)
def test_bounds_for_Q1(): """ These bounds are from: Jiangping Wang and Dapeng Wu, "Tight bounds for the first order Marcum Q-function" Wireless Communications and Mobile Computing, Volume 12, Issue 4, March 2012, Pages 293-301. """ with mpmath.workdps(50): sqrt2 = mpmath.sqrt(2) sqrthalfpi = mpmath.sqrt(mpmath.pi / 2) for b in [mpmath.mp.mpf(1), mpmath.mp.mpf(10)]: for a in [b / 16, 0.5 * b, 0.875 * b, b]: q1 = marcumq(1, a, b) sinhab = mpmath.sinh(a * b) i0ab = mpmath.besseli(0, a * b) # lower bound when 0 <= a <= b lb = (mpmath.sqrt(mpmath.pi / 8) * b * i0ab / sinhab * (mpmath.erfc((b - a) / sqrt2) - mpmath.erfc( (b + a) / sqrt2))) assert lb < q1 # upper bound when 0 <= a <= b ub = ((i0ab + 3) / (mpmath.exp(a * b) + 3) * ( mpmath.exp(-(b - a)**2 / 2) + a * sqrthalfpi * mpmath.erfc( (b - a) / sqrt2) + 3 * mpmath.exp(-(a**2 + b**2) / 2))) assert q1 < ub, ("marcumq(1, a, b) < ub for a = %s, b = %s" % (a, b)) for a in [mpmath.mp.mpf(1), mpmath.mp.mpf(10)]: for b in [b / 16, 0.5 * b, 0.875 * b, b]: q1 = marcumq(1, a, b) sinhab = mpmath.sinh(a * b) i0ab = mpmath.besseli(0, a * b) # lower bound when 0 <= b <= a lb = (1 - sqrthalfpi * b * i0ab / sinhab * (mpmath.erf(a / sqrt2) - mpmath.erf( (a - b) / sqrt2) / 2 - mpmath.erf( (a + b) / sqrt2) / 2)) assert lb < q1 # upper bound when 0 <= b <= a ub = ( 1 - i0ab / (mpmath.exp(a * b) + 3) * (4 * mpmath.exp(-a**2 / 2) - mpmath.exp(-(b - a)**2 / 2) - 3 * mpmath.exp(-(a**2 + b**2) / 2) + a * sqrthalfpi * (mpmath.erfc(-a / sqrt2) - mpmath.erfc((b - a) / sqrt2)))) assert q1 < ub, ("marcumq(1, a, b) < ub for a = %s, b = %s" % (a, b))
def fq(x0, seta2, seff2): coeff = 1./np.sqrt(np.pi) xm = x0/np.sqrt(2.*seta2) tau = lamda1*seff2/np.sqrt(2.*seta2) psi = lamda2*seff2*x0/np.sqrt(2.*seta2) intgr1 = coeff*(-tau+2*psi+xm)*exp(-(tau+xm)**2) +.5*(1.+2.*(tau-psi)**2)*erfc(tau+xm) intgr2 = coeff*(-tau-2*psi-xm)*exp(-(tau-xm)**2) +.5*(1.+2.*(tau+psi)**2)*erfc(tau-xm) intgr3 = .5*x0**2*(erf(tau+xm)+erf(tau-xm)) return (seta2/(1.+lamda2*seff2)**2)*(intgr1+intgr2)+intgr3
def fmt2_erfc(t, m, low=0): tt = mpmath.sqrt(t) low = mpmath.mpf(low) low2 = low * low f = mpmath.sqrt(mpmath.pi)/2. / tt * (mpmath.erf(tt) - mpmath.erf(low*tt)) e = mpmath.exp(-t) e1 = mpmath.exp(-t*low2) * low b = mpmath.mpf('.5') / t out = [f] for i in range(m): f = b * ((2*i+1) * f - e + e1) e1 *= low2 out.append(f) return np.array(out)
def fq(x0, seta2, seff2): coeff = 1. / np.sqrt(np.pi) xm = x0 / np.sqrt(2. * seta2) tau = lamda1 * seff2 / np.sqrt(2. * seta2) psi = lamda2 * seff2 * x0 / np.sqrt(2. * seta2) intgr1 = coeff * (-tau + 2 * psi + xm) * exp(-(tau + xm)**2) + .5 * ( 1. + 2. * (tau - psi)**2) * erfc(tau + xm) intgr2 = coeff * (-tau - 2 * psi - xm) * exp(-(tau - xm)**2) + .5 * ( 1. + 2. * (tau + psi)**2) * erfc(tau - xm) intgr3 = .5 * x0**2 * (erf(tau + xm) + erf(tau - xm)) return (seta2 / (1. + lamda2 * seff2)**2) * (intgr1 + intgr2) + intgr3
def lnlhood(self, param): """ This is the function that evaluates the likelihood at each point in NDIM space Parameters ---------- param : Returns ------- """ likeit=0 #loop over all the ions for ii in range(self.nions): #parity check : make sure data and models are aligned if(self.data[ii][0] != self.mod_colm_tag[ii]): raise ValueError('Mismtach between observables and models. This is a big mistake!!!') #now call the interpolator for models given current ion mod_columns=self.interpol[ii](param) #check if upper limit if(self.data[ii][3] == -1): #integrate the upper limit of a Gaussian - cumulative distribution arg=((self.data[ii][1]-mod_columns)/(np.sqrt(2)*self.data[ii][2]))[0] thislike=mmath.log(0.5+0.5*mmath.erf(arg)) likeit=likeit+float(thislike) #print self.data[ii][0], float(thislike), self.data[ii][1], mod_columns #check if lower limit elif(self.data[ii][3] == -2): #integrate the lower limit of a Gaussian - Q function arg=((self.data[ii][1]-mod_columns)/(np.sqrt(2)*self.data[ii][2]))[0] thislike=mmath.log(0.5-0.5*mmath.erf(arg)) likeit=likeit+float(thislike) #print self.data[ii][0], float(thislike), self.data[ii][1], mod_columns #if value, just eval Gaussian else: #add the likelihood for this ion thislike=-1*np.log(np.sqrt(2*np.pi)*self.data[ii][2])-(self.data[ii][1]-mod_columns)**2/(2*self.data[ii][2]**2) likeit=likeit+thislike return likeit
def fmt2(t, m, low=None, factor=mpmath.mpf(1)): tt = mpmath.sqrt(t) f = mpmath.pi**.5 / 2. / tt * mpmath.erf(tt) e = mpmath.exp(-t) b = mpmath.mpf('.5') / t out = [f] for i in range(m): f = b * ((2 * i + 1) * f - e) out.append(f) return np.array(out)
def fmt2(t, m, low=None): half = mpmath.mpf('.5') tt = mpmath.sqrt(t) f = mpmath.pi**half / 2 / tt * mpmath.erf(tt) e = mpmath.exp(-t) b = half / t out = [f] for i in range(m): f = b * ((2 * i + 1) * f - e) out.append(f) return np.array(out)
def test_erf_complex(): # need to increase mpmath precision for this test old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 70 x1, y1 = np.meshgrid(np.linspace(-10, 1, 11), np.linspace(-10, 1, 11)) x2, y2 = np.meshgrid(np.logspace(-10, 0.8, 11), np.logspace(-10, 0.8, 11)) points = np.r_[x1.ravel(), x2.ravel()] + 1j * np.r_[y1.ravel(), y2.ravel()] # note that the global accuracy of our complex erf algorithm is limited # roughly to 2e-8 assert_func_equal(sc.erf, lambda x: complex(mpmath.erf(x)), points, vectorized=False, rtol=2e-8) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def test_erf_complex(): # need to increase mpmath precision for this test old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 70 x1, y1 = np.meshgrid(np.linspace(-10, 1, 31), np.linspace(-10, 1, 11)) x2, y2 = np.meshgrid(np.logspace(-80, .8, 31), np.logspace(-80, .8, 11)) points = np.r_[x1.ravel(),x2.ravel()] + 1j*np.r_[y1.ravel(),y2.ravel()] assert_func_equal(sc.erf, lambda x: complex(mpmath.erf(x)), points, vectorized=False, rtol=1e-13) assert_func_equal(sc.erfc, lambda x: complex(mpmath.erfc(x)), points, vectorized=False, rtol=1e-13) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def test_erf_complex(): # need to increase mpmath precision for this test old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 70 x1, y1 = np.meshgrid(np.linspace(-10, 1, 11), np.linspace(-10, 1, 11)) x2, y2 = np.meshgrid(np.logspace(-10, .8, 11), np.logspace(-10, .8, 11)) points = np.r_[x1.ravel(),x2.ravel()] + 1j*np.r_[y1.ravel(),y2.ravel()] # note that the global accuracy of our complex erf algorithm is limited # roughly to 2e-8 assert_func_equal(sc.erf, lambda x: complex(mpmath.erf(x)), points, vectorized=False, rtol=2e-8) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def sf(x, mu=0, sigma=1): """ Survival function of the Lévy distribution. """ if sigma <= 0: raise ValueError('sigma must be positive.') if x <= mu: return mpmath.one with mpmath.extradps(5): x = mpmath.mpf(x) mu = mpmath.mpf(mu) sigma = mpmath.mpf(sigma) arg = mpmath.sqrt(sigma / (2 * (x - mu))) return mpmath.erf(arg)
def test_erf_complex(): # need to increase mpmath precision for this test old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 70 x1, y1 = np.meshgrid(np.linspace(-10, 1, 31), np.linspace(-10, 1, 11)) x2, y2 = np.meshgrid(np.logspace(-80, .8, 31), np.logspace(-80, .8, 11)) points = np.r_[x1.ravel(), x2.ravel()] + 1j * np.r_[y1.ravel(), y2.ravel()] assert_func_equal(sc.erf, lambda x: complex(mpmath.erf(x)), points, vectorized=False, rtol=1e-13) assert_func_equal(sc.erfc, lambda x: complex(mpmath.erfc(x)), points, vectorized=False, rtol=1e-13) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def pval(x): return -mpmath.log10(1 - 0.5 * (1 + mpmath.erf(x / mpmath.sqrt(2))))
def eval(self, z): return mpmath.erf(z)
def Z(x): return (1.j * mp.sqrt(mp.pi) * mp.exp(- x**2) * (1 + mp.erf(1.j * x))) def getGrowthNakata(ky_list, Setup, init = -0.07 -.015j):
for m in range(0, n+1): xn12 = mp.mpf(1.0)/mp.power(x, m+0.5) cgam = mp.gammainc(m+0.5) # "Complete" gamma? # Incomplete gamma via continued fractions and Lentz method lgam = LentzIncGamma(m+0.5, x, maxiter) # put it together F.append(mp.mpf(0.5) * (xn12 * cgam - ex*lgam)) return F X = 0.01 N = 1 maxiter = 500 oldF = BoysValue(N, X) newF = BoysValue_Gamma(N, X, maxiter) sx = mp.sqrt(X) fac = mp.sqrt(mp.pi)/2.0 print("ERF: {}".format(mp.nstr(fac/sx * mp.erf(sx), 16))) print("OLD: {}".format(mp.nstr(oldF[0], 16))) print("NEW: {}".format(mp.nstr(newF[0], 16)))
def test_erf_complex(self): assert_mpmath_equal(sc.erf, lambda z: mpmath.erf(z), [ComplexArg()], n=200)
def test_erf(self): assert_mpmath_equal(sc.erf, lambda z: mpmath.erf(z), [Arg()])
def Erf(x): return(float(mpmath.erf(x)))
def ps0_lp (κ, s): k = σ * κ return (1 - mpmath.exp(k**2/2)/2 * ( mpmath.exp(+κ*x[i]) * mpmath.erfc((b+k)/sqrt2) + mpmath.exp(-κ*x[i]) * (1+mpmath.erf((b-k)/sqrt2)) ) ) / s
def normal_cdf(x, mu=0, sigma=1): return (1 + mpmath.erf((x - mu) / mpmath.sqrt(2) / sigma)) / 2
def pval(x): return float(1 - 0.5 * (1 + mpmath.erf(x / mpmath.sqrt(2))))
def f46(x): # erf return mpmath.erf(x)
""" fun = lambda t: f(t + x) npoints = len(intrv) df = self.__dip__(p, npoints - 1, fun, intrv, eps) if not self.r: warnings.warn("Derivation failed to reach precision goal.", RuntimeWarning) return df if __name__ == "__main__": # function to test f = lambda t: 6 * mp.exp(-6 * t) + t + mp.log(0.2 * t + 4) - 2**t - mp.erf( t) # Real derivatives, for testing f_d = [] f_d.append(lambda t: 1 / (5 * (t / 5 + 4)) - 36 * mp.exp(-6 * t) - 2**t * mp.log(2) - (2 * mp.exp(-t**2)) / mp.pi**(1 / 2) + 1) f_d.append(lambda t: 216 * mp.exp(-6 * t) - 1 / (25 * (t / 5 + 4)**2) - 2** t * mp.log(2)**2 + (4 * t * mp.exp(-t**2)) / mp.pi**(1 / 2)) f_d.append(lambda t: 2.0 / (125 * (t / 5 + 4)**3) - 1296 * mp.exp(-6 * t) + (4 * mp.exp(-t**2)) / mp.pi**(1 / 2) - 2**t * mp.log(2)**3 - (8 * t**2 * mp.exp(-t**2)) / mp.pi**(1 / 2)) f_d.append(lambda t: 7776 * mp.exp(-6 * t) - 6.0 / (625 * (t / 5 + 4)**4) - 2**t * mp.log(2)**4 - (24 * t * mp.exp(-t**2)) / mp.pi** (1 / 2) + (16 * t**3 * mp.exp(-t**2)) / mp.pi**(1 / 2)) nPoints = 100 # points around 0