Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
 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)))
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
 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
Ejemplo n.º 7
0
 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
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
 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)))
Ejemplo n.º 14
0
 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)))
Ejemplo n.º 15
0
 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)
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
 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)
Ejemplo n.º 18
0
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)
Ejemplo n.º 19
0
 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)
Ejemplo n.º 20
0
 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)