def _stats(self, mu): var = mu tmp = np.asarray(mu) mu_nonzero = tmp > 0 g1 = _lazywhere(mu_nonzero, (tmp,), lambda x: sqrt(1.0/x), np.inf) g2 = _lazywhere(mu_nonzero, (tmp,), lambda x: 1.0/x, np.inf) return mu, var, g1, g2
def _stats(self, mu): var = mu tmp = np.asarray(mu) mu_nonzero = tmp > 0 g1 = _lazywhere(mu_nonzero, (tmp, ), lambda x: sqrt(1.0 / x), np.inf) g2 = _lazywhere(mu_nonzero, (tmp, ), lambda x: 1.0 / x, np.inf) return mu, var, g1, g2
def _fixed_point_helper(func, x0, args, xtol, maxiter, use_accel): p0 = x0 for i in range(maxiter): p1 = func(p0, *args) if use_accel: p2 = func(p1, *args) d = p2 - 2.0 * p1 + p0 p = _lazywhere(d != 0, (p0, p1, d), f=_del2, fillvalue=p2) else: p = p1 relerr = _lazywhere(p0 != 0, (p, p0), f=_relerr, fillvalue=p) if np.all(np.abs(relerr) < xtol): return p p0 = p msg = "Failed to converge after %d iterations, value is %s" % (maxiter, p) raise RuntimeError(msg)
def _logpmf(self, x, mu, alpha, p, w): s, p = self.convert_params(mu, alpha, p) return _lazywhere(x != 0, (x, s, p, w), (lambda x, s, p, w: np.log(1. - w) + nbinom.logpmf(x, s, p)), np.log(w + (1. - w) * nbinom.pmf(x, s, p)))
def _fixed_point_helper(func, x0, args, xtol, maxiter, use_accel): p0 = x0 for i in range(maxiter): p1 = func(p0, *args) if use_accel: p2 = func(p1, *args) d = p2 - 2.0 * p1 + p0 p = _lazywhere(d != 0, (p0, p1, d), f=_del2, fillvalue=p2) else: p = p1 relerr = _lazywhere(p0 != 0, (p, p0), f=_relerr, fillvalue=p) if np.all(np.abs(relerr) < xtol): return p p0 = p msg = "Failed to converge after %d iterations, value is %s" % (maxiter, p) raise RuntimeError(msg)
def alphanot1func(alpha, beta, TH, aTH, bTH, cosTH, tanTH, W): res = _lazywhere( beta == 0, (alpha, beta, TH, aTH, bTH, cosTH, tanTH, W), beta0func, f2=otherwise, ) return res
def _logpmf(self, k, M, n, r): cond = ((r == 0) & (k == 0)) result = _lazywhere(~cond, (k, M, n, r), lambda k, M, n, r: (-betaln(k+1, r) + betaln(k+r, 1) - betaln(n-k+1, M-r-n+1) + betaln(M-r-k+1, 1) + betaln(n+1, M-n+1) - betaln(M+1, 1)), fillvalue=0.0) return result
def crystalball(x,N,beta,m,mu,sig): """ Scipy's crystalball function (doesn't exist in python2.) https://github.com/scipy/scipy/blob/59cabc8/scipy/stats/_continuous_distns.py#L5798 """ from scipy._lib._util import _lazywhere x = (x - mu)/sig rhs = lambda x, beta, m: np.exp(-x**2 / 2) lhs = lambda x, beta, m: (m/beta)**m * np.exp(-beta**2 / 2.0) * (m/beta - beta - x)**(-m) return N * _lazywhere(np.atleast_1d(x > -beta), (x, beta, m), f=rhs, f2=lhs)
def _logcdf(self, x, n, p): k = floor(x) cdf = self._cdf(k, n, p) cond = cdf > 0.5 def f1(k, n, p): return np.log1p(-special.betainc(k + 1, n, 1 - p)) def f2(k, n, p): return np.log(cdf) with np.errstate(divide='ignore'): return _lazywhere(cond, (x, n, p), f=f1, f2=f2)
def _rvs_Z1(alpha, beta, size=None, random_state=None): """Simulate random variables using Nolan's methods as detailed in [NO]. """ def alpha1func(alpha, beta, TH, aTH, bTH, cosTH, tanTH, W): return (2 / np.pi * ((np.pi / 2 + bTH) * tanTH - beta * np.log( (np.pi / 2 * W * cosTH) / (np.pi / 2 + bTH)))) def beta0func(alpha, beta, TH, aTH, bTH, cosTH, tanTH, W): return (W / (cosTH / np.tan(aTH) + np.sin(TH)) * ((np.cos(aTH) + np.sin(aTH) * tanTH) / W)**(1.0 / alpha)) def otherwise(alpha, beta, TH, aTH, bTH, cosTH, tanTH, W): # alpha is not 1 and beta is not 0 val0 = beta * np.tan(np.pi * alpha / 2) th0 = np.arctan(val0) / alpha val3 = W / (cosTH / np.tan(alpha * (th0 + TH)) + np.sin(TH)) res3 = val3 * ( (np.cos(aTH) + np.sin(aTH) * tanTH - val0 * (np.sin(aTH) - np.cos(aTH) * tanTH)) / W)**(1.0 / alpha) return res3 def alphanot1func(alpha, beta, TH, aTH, bTH, cosTH, tanTH, W): res = _lazywhere( beta == 0, (alpha, beta, TH, aTH, bTH, cosTH, tanTH, W), beta0func, f2=otherwise, ) return res alpha = np.broadcast_to(alpha, size) beta = np.broadcast_to(beta, size) TH = uniform.rvs(loc=-np.pi / 2.0, scale=np.pi, size=size, random_state=random_state) W = expon.rvs(size=size, random_state=random_state) aTH = alpha * TH bTH = beta * TH cosTH = np.cos(TH) tanTH = np.tan(TH) res = _lazywhere( alpha == 1, (alpha, beta, TH, aTH, bTH, cosTH, tanTH, W), alpha1func, f2=alphanot1func, ) return res
def _fixed_point_helper(func, x0, args, xtol, maxiter, use_accel): """Helper function from scipy optimize""" p0 = x0 for i in range(maxiter): p1 = func(p0, *args) if np.all(np.abs(p1-p0) < xtol/2.): return p1 if use_accel and i < maxiter//2: p2 = func(p1, *args) d = p2 - 2.0 * p1 + p0 p = _lazywhere(d != 0, (p0, p1, d), f=_del2, fillvalue=p2) else: p = p1 p0 = p msg = "Failed to converge after %d iterations, value is %s" % (maxiter, p) raise RuntimeError(msg)
def fixedPoint(function, x0, args, tol, maxit): p0 = x0 for i in range(maxit): #Iterar hasta el máximo de iteraciones p1 = function(p0, *args) #Vector resultante p=p1 #Variable temporal fval = _lazywhere(p0 != 0, (p, p0), f=sp_helper, fillvalue=p) #Funcion aux. if(np.all(np.abs(fval) < tol)): # Condición de convergencia return p p0 = p msg = "Failed to converge after %d iterations, value is %s" % (maxit, p) raise RuntimeError(msg)
def _logpmf(self, x, mu, alpha, p, w): return _lazywhere(x != 0, (x, mu, alpha, p, w), (lambda x, mu, alpha, p, w: np.log(1. - w) + genpoisson_p.logpmf(x, mu, alpha, p)), np.log(w + (1. - w) * genpoisson_p.pmf(x, mu, alpha, p)))
def _logpmf(self, x, mu, w): return _lazywhere(x != 0, (x, mu, w), (lambda x, mu, w: np.log(1. - w) + x * np.log(mu) - gammaln(x + 1.) - mu), np.log(w + (1. - w) * np.exp(-mu)))
def _cdf(self, x, a): k = floor(x) f = lambda k, a: 1.0 - exp(-a * k) / (exp(a) + 1) f2 = lambda k, a: exp(a * (k+1)) / (exp(a) + 1) return _lazywhere(k >= 0, (k, a), f=f, f2=f2)
def series_density(y, mu, p, phi): density = _lazywhere(np.array(y) > 0, (y, mu, p, phi), f=density_otherwise, f2=density_at_zero) return density
def _munp(self, n, a): return _lazywhere( a > n + 1, (a, n), lambda a, n: special.zeta(a - n, 1) / special.zeta(a, 1), np.inf)
def _gen_harmonic(n, a): """Generalized harmonic number""" n, a = np.broadcast_arrays(n, a) return _lazywhere(a > 1, (n, a), f=_gen_harmonic_gt1, f2=_gen_harmonic_leq1)
def _cdf(self, x, a): k = floor(x) f = lambda k, a: 1.0 - exp(-a * k) / (exp(a) + 1) f2 = lambda k, a: exp(a * (k + 1)) / (exp(a) + 1) return _lazywhere(k >= 0, (k, a), f=f, f2=f2)
def _munp(self, n, a): return _lazywhere( a > n + 1, (a, n), lambda a, n: special.zeta(a - n, 1) / special.zeta(a, 1), np.inf)