• Facebook
  • Twitter
  • Reddit
  • StumbleUpon
  • Digg
  • email

All Samples(2536)  |  Call(2398)  |  Derive(0)  |  Import(138)
where(condition, [x, y])

Return elements, either from `x` or `y`, depending on `condition`.

If only `condition` is given, return ``condition.nonzero()``.

Parameters
----------
condition : array_like, bool
    When True, yield `x`, otherwise yield `y`.
x, y : array_like, optional
    Values from which to choose. `x` and `y` need to have the same
    shape as `condition`.

Returns
-------
out : ndarray or tuple of ndarrays
    If both `x` and `y` are specified, the output array contains
    elements of `x` where `condition` is True, and elements from
    `y` elsewhere.

    If only `condition` is given, return the tuple
    ``condition.nonzero()``, the indices where `condition` is True.

See Also
--------
nonzero, choose

Notes
-----
If `x` and `y` are given and input arrays are 1-D, `where` is
equivalent to::

    [xv if c else yv for (c,xv,yv) in zip(condition,x,y)]

Examples
--------
>>> np.where([[True, False], [True, True]],
...          [[1, 2], [3, 4]],
...          [[9, 8], [7, 6]])
array([[1, 8],
       [3, 4]])

>>> np.where([[0, 1], [1, 0]])
(array([0, 1]), array([1, 0]))

>>> x = np.arange(9.).reshape(3, 3)
>>> np.where( x > 5 )
(array([2, 2, 2]), array([0, 1, 2]))
>>> x[np.where( x > 3.0 )]               # Note: result is 1D.
array([ 4.,  5.,  6.,  7.,  8.])
>>> np.where(x < 5, x, -1)               # Note: broadcasting.
array([[ 0.,  1.,  2.],
       [ 3.,  4., -1.],
       [-1., -1., -1.]])

src/m/a/matplotlib-HEAD/matplotlib/examples/pylab_examples/barcode_demo.py   matplotlib(Download)
from matplotlib.pyplot import figure, show, cm
from numpy import where
from numpy.random import rand
 
# the bar
x = where(rand(500)>0.7, 1.0, 0.0)
 

src/m/a/matplotlib-HEAD/examples/pylab_examples/barcode_demo.py   matplotlib(Download)
from matplotlib.pyplot import figure, show, cm
from numpy import where
from numpy.random import rand
 
# the bar
x = where(rand(500)>0.7, 1.0, 0.0)
 

src/m/a/Matplotlib--JJ-s-dev-HEAD/examples/pylab_examples/barcode_demo.py   Matplotlib--JJ-s-dev(Download)
from matplotlib.pyplot import figure, show, cm
from numpy import where
from numpy.random import rand
 
# the bar
x = where(rand(500)>0.7, 1.0, 0.0)
 

src/n/e/NeuroTools-0.1.0/examples/retina/benchmark_retina.py   NeuroTools(Download)
    y = params['position'][1]
    r2 = x**2 + y**2
    r = numpy.sqrt(r2)
    id_center = [int(k) for k in numpy.where( r2 < N_ret**2)[0]]
 
    # mean activity accross kernelseeds as a function of SNR
    for i_exp, experiment in enumerate(p.iter_inner()):
 
############# MAKING FIGURE ############################
from NeuroTools.plotting import pylab_params
from numpy import zeros, where, arange
 
pylab.close('all')
pylab.ioff() #pylab.ion() #

src/p/y/PyProp-HEAD/examples/vector/optimalcontrol/example.py   PyProp(Download)
#import system modules
import sys
import os
from numpy import conj, size
from numpy import where as nwhere
import pylab
from numpy import fft
	spectrumSize = size(controlSpectrum)
	freq = freq[spectrumSize/2:]
	absSpectrum = abs(controlSpectrum[spectrumSize/2:])
	I = nwhere(freq > freqCutoff)[0][0]
	subplots[1].plot(freq[:I], absSpectrum[:I], label="Control spectrum")
	subplots[1].set_xlabel("Frequency (a.u.)")
	subplots[1].legend(loc="best")

src/s/c/scipy-HEAD/scipy/stats/distributions.py   scipy(Download)
from scipy.special import gammaln as gamln
 
import inspect
from numpy import alltrue, where, arange, putmask, \
     ravel, take, ones, sum, shape, product, repeat, reshape, \
     zeros, floor, logical_and, log, sqrt, exp, arctanh, tan, sin, arcsin, \
     arctan, tanh, ndarray, cos, cosh, sinh, newaxis, array, log1p, expm1
    def _cdf_skip(self, x, a, b):
        # remove for now: special.hyp2f1 is incorrect for large a
        x = where(x==1.0, 1.0-1e-6,x)
        return pow(x,a)*special.hyp2f1(a+b,a,1+a,-x)/a/special.beta(a,b)
    def _munp(self, n, a, b):
        if (n == 1.0):
            return where(b > 1, a/(b-1.0), inf)
        elif (n == 2.0):
            return where(b > 2, a*(a+1.0)/((b-2.0)*(b-1.0)), inf)
        elif (n == 3.0):
            return where(b > 3, a*(a+1.0)*(a+2.0)/((b-3.0)*(b-2.0)*(b-1.0)),
                         inf)
        elif (n == 4.0):
            return where(b > 4,
    def _rvs(self, a):
        u = random(size=self._size)
        return (gamma.rvs(a,size=self._size)*where(u>=0.5,1,-1))
    def _pdf(self, x, a):
        ax = abs(x)
        return 1.0/(2*special.gamma(a))*ax**(a-1.0) * exp(-ax)
    def _logpdf(self, x, a):
        ax = abs(x)
        return (a-1.0)*log(ax) - ax - log(2) - gamln(a)
    def _cdf(self, x, a):
        fac = 0.5*special.gammainc(a,abs(x))
        return where(x>0,0.5+fac,0.5-fac)
    def _sf(self, x, a):
        fac = 0.5*special.gammainc(a,abs(x))
        #return where(x>0,0.5-0.5*fac,0.5+0.5*fac)
        return where(x>0,0.5-fac,0.5+fac)
    def _ppf(self, q, a):
        fac = special.gammainccinv(a,1-abs(2*q-1))
        return where(q>0.5, fac, -fac)
    def _rvs(self, c):
        u = random(size=self._size)
        return weibull_min.rvs(c, size=self._size)*(where(u>=0.5,1,-1))
    def _pdf(self, x, c):
        ax = abs(x)
        Px = c/2.0*ax**(c-1.0)*exp(-ax**c)
        return Px
    def _logpdf(self, x, c):
        ax = abs(x)
        return log(c) - log(2.0) + (c-1.0)*log(ax) - ax**c
    def _cdf(self, x, c):
        Cx1 = 0.5*exp(-abs(x)**c)
        return where(x > 0, 1-Cx1, Cx1)
    def _ppf_skip(self, q, c):
        fac = where(q<=0.5,2*q,2*q-1)
        fac = pow(arr(log(1.0/fac)),1.0/c)
        return where(q>0.5,fac,-fac)
    def _stats(self, c):
        var = gam(1+2.0/c)
        return 0.0, var, 0.0, gam(1+4.0/c)/var
    def _stats(self, dfn, dfd):
        v2 = arr(dfd*1.0)
        v1 = arr(dfn*1.0)
        mu = where (v2 > 2, v2 / arr(v2 - 2), inf)
        mu2 = 2*v2*v2*(v2+v1-2)/(v1*(v2-2)**2 * (v2-4))
        mu2 = where(v2 > 4, mu2, inf)
        g1 = 2*(v2+2*v1-2)/(v2-6)*sqrt((2*v2-4)/(v1*(v2+v1-2)))
        g1 = where(v2 > 6, g1, nan)
        g2 = 3/(2*v2-16)*(8+g1*g1*(v2-6))
        g2 = where(v2 > 8, g2, nan)
    def _argcheck(self, c):
        c = arr(c)
        self.b = where(c < 0, 1.0/abs(c), inf)
        return where(c==0, 0, 1)
    def _pdf(self, x, c):
        Px = pow(1+c*x,arr(-1.0-1.0/c))
        return Px
    def _munp(self, n, c):
        k = arange(0,n+1)
        val = (-1.0/c)**n * sum(comb(n,k)*(-1)**k / (1.0-c*k),axis=0)
        return where(c*n < 1, val, inf)
    def _entropy(self, c):
        if (c > 0):
            return 1+c
    def _argcheck(self, c):
        min = np.minimum
        max = np.maximum
        sml = floatinfo.machar.xmin
        #self.b = where(c > 0, 1.0 / c,inf)
        #self.a = where(c < 0, 1.0 / c, -inf)
        self.b = where(c > 0, 1.0 / max(c, sml),inf)
        self.a = where(c < 0, 1.0 / min(c,-sml), -inf)
        return where(abs(c)==inf, 0, 1) #True #(c!=0)
        ##        return p2
        cx = c*x
 
        logex2 = where((c==0)*(x==x),0.0,log1p(-cx))
        logpex2 = where((c==0)*(x==x),-x,logex2/c)
        pex2 = exp(logpex2)
        # % Handle special cases
        logpdf = where((cx==1) | (cx==-inf),-inf,-pex2+logpex2-logex2)
    def _cdf(self, x, c):
        #return exp(-pow(1-c*x,1.0/c))
        loglogcdf = where((c==0)*(x==x),-x,log1p(-c*x)/c)
        return exp(-exp(loglogcdf))
 
    def _ppf(self, q, c):
        #return 1.0/c*(1.-(-log(q))**c)
        x = -log(-log(q))
        return where((c==0)*(x==x),x,-expm1(-c*x)/c)
        g2 = g(2)
        g3 = g(3);
        g4 = g(4)
        g2mg12 = where(abs(c)<1e-7,(c*pi)**2.0/6.0,g2-g1**2.0)
        gam2k = where(abs(c)<1e-7,pi**2.0/6.0, expm1(gamln(2.0*c+1.0)-2*gamln(c+1.0))/c**2.0);
        eps = 1e-14
        gamk = where(abs(c)<eps,-_EULER,expm1(gamln(c+1))/c)
 
        m = where(c<-1.0,nan,-gamk)
        v = where(c<-0.5,nan,g1**2.0*gam2k)
 
        #% skewness
        sk1 = where(c<-1./3,nan,np.sign(c)*(-g3+(g2+2*g2mg12)*g1)/((g2mg12)**(3./2.)));
        sk = where(abs(c)<=eps**0.29,12*sqrt(6)*_ZETA3/pi**3,sk1)
        sk = where(abs(c)<=eps**0.29,12*sqrt(6)*_ZETA3/pi**3,sk1)
 
        #% The kurtosis is:
        ku1 = where(c<-1./4,nan,(g4+(-4*g3+3*(g2+g2mg12)*g1)*g1)/((g2mg12)**2))
        ku = where(abs(c)<=(eps)**0.23,12.0/5.0,ku1-3.0)
        return m,v,sk,ku
 
 
    def _munp(self, n, c):
        k = arange(0,n+1)
        vals = 1.0/c**n * sum(comb(n,k) * (-1)**k * special.gamma(c*k + 1),axis=0)
        return where(c*n > -1, vals, inf)
    def _cdf(self, x, a, c):
        val = special.gammainc(a,x**c)
        cond = c + 0*val
        return where(cond>0,val,1-val)
    def _ppf(self, q, a, c):
        val1 = special.gammaincinv(a,q)
        val2 = special.gammaincinv(a,1.0-q)
        ic = 1.0/c
        cond = c+0*val1
        return where(cond > 0,val1**ic,val2**ic)
    def _cdf(self, x):
        return where(x > 0, 1.0-0.5*exp(-x), 0.5*exp(x))
    def _ppf(self, q):
        return where(q > 0.5, -log(2*(1-q)), log(2*q))
    def _stats(self):
        return 0, 2, 0, 3
    def _entropy(self):
    def _pdf(self, x, c):
        cd2 = c/2.0
        c = where(x < 1, c, -c)
        return cd2*x**(c-1)
    def _cdf(self, x, c):
        return where(x < 1, 0.5*x**c, 1-0.5*x**(-c))
    def _ppf(self, q, c):
        return where(q < 0.5, (2.0*q)**(1.0/c), (2*(1.0-q))**(-1.0/c))
    def _stats(self, dfn, dfd, nc):
        mu = where(dfd <= 2, inf, dfd / (dfd-2.0)*(1+nc*1.0/dfn))
        mu2 = where(dfd <=4, inf, 2*(dfd*1.0/dfn)**2.0 * \
                    ((dfn+nc/2.0)**2.0 + (dfn+nc)*(dfd-2.0)) / \
                    ((dfd-2.0)**2.0 * (dfd-4.0)))
        return mu, mu2, None, None
ncf = ncf_gen(a=0.0, name='ncf', longname="A non-central F distribution",
    def _stats(self, df):
        mu2 = where(df > 2, df / (df-2.0), inf)
        g1 = where(df > 3, 0.0, nan)
        g2 = where(df > 4, 6.0/(df-4.0), nan)
        return 0, mu2, g1, g2
t = t_gen(name='t',longname="Student's T",
          shapes="df", extradoc="""
    def _pdf(self, x, c):
        return where(x < c, 2*x/c, 2*(1-x)/(1-c))
    def _cdf(self, x, c):
        return where(x < c, x*x/c, (x*x-2*x+c)/(c-1))
    def _ppf(self, q, c):
        return where(q < c, sqrt(c*q), 1-sqrt((1-c)*(1-q)))
    def _stats(self, c):
    def _pdf(self, x, lam):
        Fx = arr(special.tklmbda(x,lam))
        Px = Fx**(lam-1.0) + (arr(1-Fx))**(lam-1.0)
        Px = 1.0/arr(Px)
        return where((lam > 0) & (abs(x) < 1.0/lam), Px, 0.0)
    def _cdf(self, x, lam):
        return special.tklmbda(x, lam)
    def _ppf(self, q, lam):
        q = q*1.0
        vals1 = (q**lam - (1-q)**lam)/lam
        vals2 = log(q/(1-q))
        return where((lam == 0)&(q==q), vals2, vals1)
    def _ppf(self, q, c):
        val = (1.0-c)/(1.0+c)
        rcq = 2*arctan(val*tan(pi*q))
        rcmq = 2*pi-2*arctan(val*tan(pi*(1-q)))
        return where(q < 1.0/2, rcq, rcmq)
    def _entropy(self, c):
        return log(2*pi*(1-c*c))
    pk = arr(pk)
    pk = 1.0* pk / sum(pk,axis=0)
    if qk is None:
        vec = where(pk == 0, 0.0, pk*log(pk))
    else:
        qk = arr(qk)
        if len(qk) != len(pk):
            raise ValueError, "qk and pk must have same length."
        qk = 1.0*qk / sum(qk,axis=0)
        # If qk is zero anywhere, then unless pk is zero at those places
        #   too, the relative entropy is infinite.
        if any(take(pk,nonzero(qk==0.0),axis=0)!=0.0, 0):
            return inf
        vec = where (pk == 0, 0.0, -pk*log(pk / qk))
    def _ppf(self, q, n, pr):
        vals = ceil(special.bdtrik(q,n,pr))
        vals1 = vals-1
        temp = special.bdtr(vals1,n,pr)
        return where(temp >= q, vals1, vals)
    def _stats(self, n, pr):
        q = 1.0-pr
    def _entropy(self, n, pr):
        k = r_[0:n+1]
        vals = self._pmf(k,n,pr)
        lvals = where(vals==0,0.0,log(vals))
        return -sum(vals*lvals,axis=0)
binom = binom_gen(name='binom',shapes="n, pr",extradoc="""
 
    def _ppf(self, q, n, pr):
        vals = ceil(special.nbdtrik(q,n,pr))
        vals1 = (vals-1).clip(0.0, np.inf)
        temp = self._cdf(vals1,n,pr)
        return where(temp >= q, vals1, vals)
    def _stats(self, n, pr):
        Q = 1.0 / pr
    def _ppf(self, q, pr):
        vals = ceil(log(1.0-q)/log(1-pr))
        temp = 1.0-(1.0-pr)**(vals-1)
        return where((temp >= q) & (vals > 0), vals-1, vals)
    def _stats(self, pr):
        mu = 1.0/pr
        qr = 1.0-pr
    def _entropy(self, M, n, N):
        k = r_[N-(M-n):min(n,N)+1]
        vals = self.pmf(k,M,n,N)
        lvals = where(vals==0.0,0.0,log(vals))
        return -sum(vals*lvals,axis=0)
hypergeom = hypergeom_gen(name='hypergeom',longname="A hypergeometric",
                          shapes="M, n, N", extradoc="""
    def _ppf(self, q, mu):
        vals = ceil(special.pdtrik(q,mu))
        vals1 = vals-1
        temp = special.pdtr(vals1,mu)
        return where((temp >= q), vals1, vals)
    def _stats(self, mu):
        var = mu
    def _ppf(self, q, lambda_):
        vals = ceil(-1.0/lambda_ * log1p(-q)-1)
        vals1 = (vals-1).clip(self.a, np.inf)
        temp = self._cdf(vals1, lambda_)
        return where(temp >= q, vals1, vals)
    def _stats(self, lambda_):
        mu = 1/(exp(lambda_)-1)
    def _ppf(self, q, lambda_, N):
        qnew = q*(1-exp(-lambda_*N))
        vals = ceil(-1.0/lambda_ * log(1-qnew)-1)
        vals1 = (vals-1).clip(0.0, np.inf)
        temp = self._cdf(vals1, lambda_, N)
        return where(temp >= q, vals1, vals)
    def _stats(self, lambda_, N):
    def _ppf(self, q, min, max):
        vals = ceil(q*(max-min)+min)-1
        vals1 = (vals-1).clip(min, max)
        temp = self._cdf(vals1, min, max)
        return where(temp >= q, vals1, vals)
    def _stats(self, min, max):
        m2, m1 = arr(max), arr(min)
    def _cdf(self, x, a):
        k = floor(x)
        ind = (k >= 0)
        const = exp(a)+1
        return where(ind, 1.0-exp(-a*k)/const, exp(a*(k+1))/const)
    def _ppf(self, q, a):
        const = 1.0/(1+exp(-a))
        cons2 = 1+exp(a)
        ind = q < const
        vals = ceil(where(ind, log(q*cons2)/a-1, -log((1-q)*cons2)/a))
        vals1 = (vals-1)
        temp = self._cdf(vals1, a)
        return where(temp >= q, vals1, vals)
    def _pmf(self, x, mu1, mu2):
        px = np.where(x < 0, ncx2.pdf(2*mu2, 2*(1-x), 2*mu1)*2,
                         ncx2.pdf(2*mu1, 2*(x+1), 2*mu2)*2)
        #ncx2.pdf() returns nan's for extremely low probabilities
        return px
    def _cdf(self, x, mu1, mu2):
        x = np.floor(x)
        px = np.where(x < 0, ncx2.cdf(2*mu2, -2*x, 2*mu1),

src/p/y/PyProp-HEAD/examples/combined/bspline/wavepacket1d/example.py   PyProp(Download)
#Import system modules
import sys
import os
from numpy import double, r_, fft
from numpy import max as nmax
from numpy import max as nmin
from numpy import where as nwhere
 
	k0 = conf.Wavepacket.k0
	k0_trunk = 5 * k0
	trunkIdx = list(nwhere(abs(grid_fft) <= k0_trunk)[0])
 
	rcParams['interactive'] = True
	figure()

src/s/c/scipy-0.8.0/scipy/stats/distributions.py   scipy(Download)
import scipy.integrate
 
import inspect
from numpy import alltrue, where, arange, put, putmask, \
     ravel, take, ones, sum, shape, product, repeat, reshape, \
     zeros, floor, logical_and, log, sqrt, exp, arctanh, tan, sin, arcsin, \
     arctan, tanh, ndarray, cos, cosh, sinh, newaxis, array, log1p, expm1
    def _cdf_skip(self, x, a, b):
        # remove for now: special.hyp2f1 is incorrect for large a
        x = where(x==1.0, 1.0-1e-6,x)
        return pow(x,a)*special.hyp2f1(a+b,a,1+a,-x)/a/special.beta(a,b)
    def _munp(self, n, a, b):
        if (n == 1.0):
            return where(b > 1, a/(b-1.0), inf)
        elif (n == 2.0):
            return where(b > 2, a*(a+1.0)/((b-2.0)*(b-1.0)), inf)
        elif (n == 3.0):
            return where(b > 3, a*(a+1.0)*(a+2.0)/((b-3.0)*(b-2.0)*(b-1.0)),
                         inf)
        elif (n == 4.0):
            return where(b > 4,
    def _rvs(self, a):
        u = random(size=self._size)
        return (gamma.rvs(a,size=self._size)*where(u>=0.5,1,-1))
    def _pdf(self, x, a):
        ax = abs(x)
        return 1.0/(2*special.gamma(a))*ax**(a-1.0) * exp(-ax)
    def _cdf(self, x, a):
        fac = 0.5*special.gammainc(a,abs(x))
        return where(x>0,0.5+fac,0.5-fac)
    def _sf(self, x, a):
        fac = 0.5*special.gammainc(a,abs(x))
        #return where(x>0,0.5-0.5*fac,0.5+0.5*fac)
        return where(x>0,0.5-fac,0.5+fac)
    def _ppf(self, q, a):
        fac = special.gammainccinv(a,1-abs(2*q-1))
        return where(q>0.5, fac, -fac)
    def _stats(self, a):
        mu2 = a*(a+1.0)
        return 0.0, mu2, 0.0, (a+2.0)*(a+3.0)/mu2-3.0
dgamma = dgamma_gen(name='dgamma',longname="A double gamma",
    def _rvs(self, c):
        u = random(size=self._size)
        return weibull_min.rvs(c, size=self._size)*(where(u>=0.5,1,-1))
    def _pdf(self, x, c):
        ax = abs(x)
        Px = c/2.0*ax**(c-1.0)*exp(-ax**c)
        return Px
    def _cdf(self, x, c):
        Cx1 = 0.5*exp(-abs(x)**c)
        return where(x > 0, 1-Cx1, Cx1)
    def _ppf_skip(self, q, c):
        fac = where(q<=0.5,2*q,2*q-1)
        fac = pow(arr(log(1.0/fac)),1.0/c)
        return where(q>0.5,fac,-fac)
    def _stats(self, dfn, dfd):
        v2 = arr(dfd*1.0)
        v1 = arr(dfn*1.0)
        mu = where (v2 > 2, v2 / arr(v2 - 2), inf)
        mu2 = 2*v2*v2*(v2+v1-2)/(v1*(v2-2)**2 * (v2-4))
        mu2 = where(v2 > 4, mu2, inf)
        g1 = 2*(v2+2*v1-2)/(v2-6)*sqrt((2*v2-4)/(v1*(v2+v1-2)))
        g1 = where(v2 > 6, g1, nan)
        g2 = 3/(2*v2-16)*(8+g1*g1*(v2-6))
        g2 = where(v2 > 8, g2, nan)
    def _argcheck(self, c):
        c = arr(c)
        self.b = where(c < 0, 1.0/abs(c), inf)
        return where(c==0, 0, 1)
    def _pdf(self, x, c):
        Px = pow(1+c*x,arr(-1.0-1.0/c))
        return Px
    def _munp(self, n, c):
        k = arange(0,n+1)
        val = (-1.0/c)**n * sum(comb(n,k)*(-1)**k / (1.0-c*k),axis=0)
        return where(c*n < 1, val, inf)
    def _entropy(self, c):
        if (c > 0):
            return 1+c
    def _argcheck(self, c):
        min = np.minimum
        max = np.maximum
        sml = floatinfo.machar.xmin
        #self.b = where(c > 0, 1.0 / c,inf)
        #self.a = where(c < 0, 1.0 / c, -inf)
        self.b = where(c > 0, 1.0 / max(c, sml),inf)
        self.a = where(c < 0, 1.0 / min(c,-sml), -inf)
        return where(abs(c)==inf, 0, 1) #True #(c!=0)
        ##        return p2
        cx = c*x
 
        logex2 = where((c==0)*(x==x),0.0,log1p(-cx))
        logpex2 = where((c==0)*(x==x),-x,logex2/c)
        pex2 = exp(logpex2)
        # % Handle special cases
        logpdf = where((cx==1) | (cx==-inf),-inf,-pex2+logpex2-logex2)
    def _cdf(self, x, c):
        #return exp(-pow(1-c*x,1.0/c))
        loglogcdf = where((c==0)*(x==x),-x,log1p(-c*x)/c)
        return exp(-exp(loglogcdf))
 
    def _ppf(self, q, c):
        #return 1.0/c*(1.-(-log(q))**c)
        x = -log(-log(q))
        return where((c==0)*(x==x),x,-expm1(-c*x)/c)
        g2 = g(2)
        g3 = g(3);
        g4 = g(4)
        g2mg12 = where(abs(c)<1e-7,(c*pi)**2.0/6.0,g2-g1**2.0)
        gam2k = where(abs(c)<1e-7,pi**2.0/6.0, expm1(gamln(2.0*c+1.0)-2*gamln(c+1.0))/c**2.0);
        eps = 1e-14
        gamk = where(abs(c)<eps,-_EULER,expm1(gamln(c+1))/c)
 
        m = where(c<-1.0,nan,-gamk)
        v = where(c<-0.5,nan,g1**2.0*gam2k)
 
        #% skewness
        sk1 = where(c<-1./3,nan,np.sign(c)*(-g3+(g2+2*g2mg12)*g1)/((g2mg12)**(3./2.)));
        sk = where(abs(c)<=eps**0.29,12*sqrt(6)*_ZETA3/pi**3,sk1)
        sk = where(abs(c)<=eps**0.29,12*sqrt(6)*_ZETA3/pi**3,sk1)
 
        #% The kurtosis is:
        ku1 = where(c<-1./4,nan,(g4+(-4*g3+3*(g2+g2mg12)*g1)*g1)/((g2mg12)**2))
        ku = where(abs(c)<=(eps)**0.23,12.0/5.0,ku1-3.0)
        return m,v,sk,ku
 
 
    def _munp(self, n, c):
        k = arange(0,n+1)
        vals = 1.0/c**n * sum(comb(n,k) * (-1)**k * special.gamma(c*k + 1),axis=0)
        return where(c*n > -1, vals, inf)
    def _cdf(self, x, a, c):
        val = special.gammainc(a,x**c)
        cond = c + 0*val
        return where(cond>0,val,1-val)
    def _ppf(self, q, a, c):
        val1 = special.gammaincinv(a,q)
        val2 = special.gammaincinv(a,1.0-q)
        ic = 1.0/c
        cond = c+0*val1
        return where(cond > 0,val1**ic,val2**ic)
    def _cdf(self, x):
        return where(x > 0, 1.0-0.5*exp(-x), 0.5*exp(x))
    def _ppf(self, q):
        return where(q > 0.5, -log(2*(1-q)), log(2*q))
    def _stats(self):
        return 0, 2, 0, 3
    def _entropy(self):
    def _pdf(self, x, c):
        cd2 = c/2.0
        c = where(x < 1, c, -c)
        return cd2*x**(c-1)
    def _cdf(self, x, c):
        return where(x < 1, 0.5*x**c, 1-0.5*x**(-c))
    def _ppf(self, q, c):
        return where(q < 0.5, (2.0*q)**(1.0/c), (2*(1.0-q))**(-1.0/c))
    def _stats(self, dfn, dfd, nc):
        mu = where(dfd <= 2, inf, dfd / (dfd-2.0)*(1+nc*1.0/dfn))
        mu2 = where(dfd <=4, inf, 2*(dfd*1.0/dfn)**2.0 * \
                    ((dfn+nc/2.0)**2.0 + (dfn+nc)*(dfd-2.0)) / \
                    ((dfd-2.0)**2.0 * (dfd-4.0)))
        return mu, mu2, None, None
ncf = ncf_gen(a=0.0, name='ncf', longname="A non-central F distribution",
    def _stats(self, df):
        mu2 = where(df > 2, df / (df-2.0), inf)
        g1 = where(df > 3, 0.0, nan)
        g2 = where(df > 4, 6.0/(df-4.0), nan)
        return 0, mu2, g1, g2
t = t_gen(name='t',longname="Student's T",
          shapes="df", extradoc="""
    def _pdf(self, x, c):
        return where(x < c, 2*x/c, 2*(1-x)/(1-c))
    def _cdf(self, x, c):
        return where(x < c, x*x/c, (x*x-2*x+c)/(c-1))
    def _ppf(self, q, c):
        return where(q < c, sqrt(c*q), 1-sqrt((1-c)*(1-q)))
    def _stats(self, c):
    def _pdf(self, x, lam):
        Fx = arr(special.tklmbda(x,lam))
        Px = Fx**(lam-1.0) + (arr(1-Fx))**(lam-1.0)
        Px = 1.0/arr(Px)
        return where((lam > 0) & (abs(x) < 1.0/lam), Px, 0.0)
    def _cdf(self, x, lam):
        return special.tklmbda(x, lam)
    def _ppf(self, q, lam):
        q = q*1.0
        vals1 = (q**lam - (1-q)**lam)/lam
        vals2 = log(q/(1-q))
        return where((lam == 0)&(q==q), vals2, vals1)
    def _ppf(self, q, c):
        val = (1.0-c)/(1.0+c)
        rcq = 2*arctan(val*tan(pi*q))
        rcmq = 2*pi-2*arctan(val*tan(pi*(1-q)))
        return where(q < 1.0/2, rcq, rcmq)
    def _entropy(self, c):
        return log(2*pi*(1-c*c))
    pk = arr(pk)
    pk = 1.0* pk / sum(pk,axis=0)
    if qk is None:
        vec = where(pk == 0, 0.0, pk*log(pk))
    else:
        qk = arr(qk)
        if len(qk) != len(pk):
            raise ValueError, "qk and pk must have same length."
        qk = 1.0*qk / sum(qk,axis=0)
        # If qk is zero anywhere, then unless pk is zero at those places
        #   too, the relative entropy is infinite.
        if any(take(pk,nonzero(qk==0.0),axis=0)!=0.0, 0):
            return inf
        vec = where (pk == 0, 0.0, -pk*log(pk / qk))
    def _ppf(self, q, n, pr):
        vals = ceil(special.bdtrik(q,n,pr))
        vals1 = vals-1
        temp = special.bdtr(vals1,n,pr)
        return where(temp >= q, vals1, vals)
    def _stats(self, n, pr):
        q = 1.0-pr
    def _entropy(self, n, pr):
        k = r_[0:n+1]
        vals = self._pmf(k,n,pr)
        lvals = where(vals==0,0.0,log(vals))
        return -sum(vals*lvals,axis=0)
binom = binom_gen(name='binom',shapes="n, pr",extradoc="""
 
    def _ppf(self, q, n, pr):
        vals = ceil(special.nbdtrik(q,n,pr))
        vals1 = (vals-1).clip(0.0, np.inf)
        temp = self._cdf(vals1,n,pr)
        return where(temp >= q, vals1, vals)
    def _stats(self, n, pr):
        Q = 1.0 / pr
    def _ppf(self, q, pr):
        vals = ceil(log(1.0-q)/log(1-pr))
        temp = 1.0-(1.0-pr)**(vals-1)
        return where((temp >= q) & (vals > 0), vals-1, vals)
    def _stats(self, pr):
        mu = 1.0/pr
        qr = 1.0-pr
    def _entropy(self, M, n, N):
        k = r_[N-(M-n):min(n,N)+1]
        vals = self.pmf(k,M,n,N)
        lvals = where(vals==0.0,0.0,log(vals))
        return -sum(vals*lvals,axis=0)
hypergeom = hypergeom_gen(name='hypergeom',longname="A hypergeometric",
                          shapes="M, n, N", extradoc="""
    def _ppf(self, q, mu):
        vals = ceil(special.pdtrik(q,mu))
        vals1 = vals-1
        temp = special.pdtr(vals1,mu)
        return where((temp >= q), vals1, vals)
    def _stats(self, mu):
        var = mu
    def _ppf(self, q, lambda_):
        vals = ceil(-1.0/lambda_ * log1p(-q)-1)
        vals1 = (vals-1).clip(self.a, np.inf)
        temp = self._cdf(vals1, lambda_)
        return where(temp >= q, vals1, vals)
    def _stats(self, lambda_):
        mu = 1/(exp(lambda_)-1)
    def _ppf(self, q, lambda_, N):
        qnew = q*(1-exp(-lambda_*N))
        vals = ceil(-1.0/lambda_ * log(1-qnew)-1)
        vals1 = (vals-1).clip(0.0, np.inf)
        temp = self._cdf(vals1, lambda_, N)
        return where(temp >= q, vals1, vals)
    def _stats(self, lambda_, N):
    def _ppf(self, q, min, max):
        vals = ceil(q*(max-min)+min)-1
        vals1 = (vals-1).clip(min, max)
        temp = self._cdf(vals1, min, max)
        return where(temp >= q, vals1, vals)
    def _stats(self, min, max):
        m2, m1 = arr(max), arr(min)
    def _cdf(self, x, a):
        k = floor(x)
        ind = (k >= 0)
        const = exp(a)+1
        return where(ind, 1.0-exp(-a*k)/const, exp(a*(k+1))/const)
    def _ppf(self, q, a):
        const = 1.0/(1+exp(-a))
        cons2 = 1+exp(a)
        ind = q < const
        vals = ceil(where(ind, log(q*cons2)/a-1, -log((1-q)*cons2)/a))
        vals1 = (vals-1)
        temp = self._cdf(vals1, a)
        return where(temp >= q, vals1, vals)
    def _pmf(self, x, mu1, mu2):
        px = np.where(x < 0, ncx2.pdf(2*mu2, 2*(1-x), 2*mu1)*2,
                         ncx2.pdf(2*mu1, 2*(x+1), 2*mu2)*2)
        #ncx2.pdf() returns nan's for extremely low probabilities
        return px
    def _cdf(self, x, mu1, mu2):
        x = np.floor(x)
        px = np.where(x < 0, ncx2.cdf(2*mu2, -2*x, 2*mu1),

src/w/a/wafo-0.11/src/wafo/stats/distributions.py   wafo(Download)
from scipy import optimize
 
import inspect
from numpy import alltrue, where, arange, put, putmask, \
     ravel, take, ones, sum, shape, product, repeat, reshape, \
     zeros, floor, logical_and, log, sqrt, exp, arctanh, tan, sin, arcsin, \
     arctan, tanh, ndarray, cos, cosh, sinh, newaxis, array, log1p, expm1
        def integ(x):
            val = self._pdf(x, *args)
            return where(val == 0.0, 0.0, val * log(val))
        entr = -quad(integ, self.a, self.b)[0]
        if np.isnan(entr):
            # try with different limits if integration problems
            low, upp = self.ppf([0.001, 0.999], *args)
    def _cdf_skip(self, x, a, b):
        # remove for now: special.hyp2f1 is incorrect for large a
        x = where(x == 1.0, 1.0 - 1e-6, x)
        return pow(x, a) * special.hyp2f1(a + b, a, 1 + a, -x) / a / special.beta(a, b)
    def _munp(self, n, a, b):
        if (n == 1.0):
            return where(b > 1, a / (b - 1.0), inf)
        elif (n == 2.0):
            return where(b > 2, a * (a + 1.0) / ((b - 2.0) * (b - 1.0)), inf)
        elif (n == 3.0):
            return where(b > 3, a * (a + 1.0) * (a + 2.0) / ((b - 3.0) * (b - 2.0) * (b - 1.0)),
                         inf)
        elif (n == 4.0):
            return where(b > 4,
    def _rvs(self, a):
        u = random(size=self._size)
        return (gamma.rvs(a, size=self._size) * where(u >= 0.5, 1, -1))
    def _pdf(self, x, a):
        ax = abs(x)
        return 1.0 / (2 * special.gamma(a)) * ax ** (a - 1.0) * exp(-ax)
    def _cdf(self, x, a):
        fac = 0.5 * special.gammainc(a, abs(x))
        return where(x > 0, 0.5 + fac, 0.5 - fac)
    def _sf(self, x, a):
        fac = 0.5 * special.gammainc(a, abs(x))
        #return where(x>0,0.5-0.5*fac,0.5+0.5*fac)
        return where(x > 0, 0.5 - fac, 0.5 + fac)
    def _ppf(self, q, a):
        fac = special.gammainccinv(a, 1 - abs(2 * q - 1))
        return where(q > 0.5, fac, -fac)
    def _stats(self, a):
        mu2 = a * (a + 1.0)
        return 0.0, mu2, 0.0, (a + 2.0) * (a + 3.0) / mu2 - 3.0
dgamma = dgamma_gen(name='dgamma', longname="A double gamma",
    def _rvs(self, c):
        u = random(size=self._size)
        return weibull_min.rvs(c, size=self._size) * (where(u >= 0.5, 1, -1))
    def _pdf(self, x, c):
        ax = abs(x)
        Px = c / 2.0 * ax ** (c - 1.0) * exp(-ax ** c)
        return Px
    def _cdf(self, x, c):
        Cx1 = 0.5 * exp(-abs(x) ** c)
        return where(x > 0, 1 - Cx1, Cx1)
    def _ppf_skip(self, q, c):
        fac = where(q <= 0.5, 2 * q, 2 * q - 1)
        fac = pow(arr(log(1.0 / fac)), 1.0 / c)
        return where(q > 0.5, fac, -fac)
    def _stats(self, dfn, dfd):
        v2 = arr(dfd * 1.0)
        v1 = arr(dfn * 1.0)
        mu = where (v2 > 2, v2 / arr(v2 - 2), inf)
        mu2 = 2 * v2 * v2 * (v2 + v1 - 2) / (v1 * (v2 - 2) ** 2 * (v2 - 4))
        mu2 = where(v2 > 4, mu2, inf)
        g1 = 2 * (v2 + 2 * v1 - 2) / (v2 - 6) * sqrt((2 * v2 - 4) / (v1 * (v2 + v1 - 2)))
        g1 = where(v2 > 6, g1, nan)
        g2 = 3 / (2 * v2 - 16) * (8 + g1 * g1 * (v2 - 6))
        g2 = where(v2 > 8, g2, nan)
def log1pxdx(x):
    '''Computes Log(1+x)/x
    '''
    y = where(x == 0, 1.0, log1p(x) / x)
    return where(x == inf, 0.0, y)
##    y = ones(shape(x))
##    k = (x!=0.0)
    def _argcheck(self, c):
        c = arr(c)
        self.b = where(0 <= c, inf, 1.0 / abs(c))
        return where(abs(c) == inf, 0, 1)
    def _pdf(self, x, c):
        cx = where((c == 0) & (x == inf), 0.0, c * x).clip(min= -1.0)
        #putmask(cx,cx<-1,-1.0)
        logpdf = where((cx == inf) | (cx == -1), -inf, -(x + cx) * log1pxdx(cx))
    def _chf(self, x, c):
        cx = c * x
        return where((0.0 < x) & (-1.0 <= cx) & (c != 0), log1p(cx) / c, x)
    def _cdf(self, x, c):
        log_sf = -self._chf(x, c)
        return - expm1(log_sf)
        #return 1.0 - pow(1+c*x,arr(-1.0/c))
    def _sf(self, x, c):
        log_sf = -self._chf(x, c)
        return exp(log_sf)
    def _isf2(self, log_sf, c):
        return where((c != 0) & (-inf < log_sf), expm1(-c * log_sf) / c, -log_sf)
    def _stats(self, c):
        #return None,None,None,None
        k = -c
        m = where(k < -1.0, inf, 1.0 / (1 + k))
        v = where(k < -0.5, nan, 1.0 / ((1 + k) ** 2.0 * (1 + 2 * k)))
        sk = where(k < -1.0 / 3, nan, 2. * (1 - k) * sqrt(1 + 2.0 * k) / (1.0 + 3. * k))
        #% E(X^r) = s^r*(-k)^-(r+1)*gamma(1+r)*gamma(-1/k-r)/gamma(1-1/k)
        r = 4.0;
        Ex4 = gam(1. + r) / ((1. + k) * (1. + 2. * k) * (1. + 3. * k) * (1 + 4. * k))
        m1 = m
        ku = where(k < -1. / 4, nan, (Ex4 - 4. * sk * v ** (3. / 2) * m1 - 6 * m1 ** 2. * v - m1 ** 4.) / v ** 2. - 3.0)
        return m, v, sk, ku
    def _munp(self, n, c):
        k = arange(0, n + 1)
        val = (-1.0 / c) ** n * sum(comb(n, k) * (-1) ** k / (1.0 - c * k), axis=0)
        return where(c * n < 1, val, inf)
    def _argcheck(self, c):
        min = np.minimum
        max = np.maximum
        sml = floatinfo.machar.xmin
        #self.b = where(c > 0, 1.0 / c,inf)
        #self.a = where(c < 0, 1.0 / c, -inf)
        self.b = where(c > 0, 1.0 / max(c, sml), inf)
        self.a = where(c < 0, 1.0 / min(c, -sml), -inf)
        return where(abs(c) == inf, 0, 1) #True #(c!=0)
        ##        return p2
        cx = c * x
 
        logex2 = where((c == 0) * (x == x), 0.0, log1p(-cx))
        logpex2 = where((c == 0) * (x == x), -x, logex2 / c)
        pex2 = exp(logpex2)
        # % Handle special cases
        logpdf = where((cx == 1) | (cx == -inf), -inf, -pex2 + logpex2 - logex2)
    def _cdf(self, x, c):
        #return exp(-pow(1-c*x,1.0/c))
        loglogcdf = where((c == 0) * (x == x), -x, log1p(-c * x) / c)
        return exp(-exp(loglogcdf))
 
    def _ppf(self, q, c):
        #return 1.0/c*(1.-(-log(q))**c)
        x = -log(-log(q))
        return where((c == 0) * (x == x), x, -expm1(-c * x) / c)
        g2 = g(2)
        g3 = g(3);
        g4 = g(4)
        g2mg12 = where(abs(c) < 1e-7, (c * pi) ** 2.0 / 6.0, g2 - g1 ** 2.0)
        gam2k = where(abs(c) < 1e-7, pi ** 2.0 / 6.0, expm1(gamln(2.0 * c + 1.0) - 2 * gamln(c + 1.0)) / c ** 2.0);
        eps = 1e-14
        gamk = where(abs(c) < eps, -_EULER, expm1(gamln(c + 1)) / c)
 
        m = where(c < -1.0, nan, -gamk)
        v = where(c < -0.5, nan, g1 ** 2.0 * gam2k)
 
        #% skewness
        sk1 = where(c < -1. / 3, nan, np.sign(c) * (-g3 + (g2 + 2 * g2mg12) * g1) / ((g2mg12) ** (3. / 2.)));
        sk = where(abs(c) <= eps ** 0.29, 12 * sqrt(6) * _ZETA3 / pi ** 3, sk1)
        sk = where(abs(c) <= eps ** 0.29, 12 * sqrt(6) * _ZETA3 / pi ** 3, sk1)
 
        #% The kurtosis is:
        ku1 = where(c < -1. / 4, nan, (g4 + (-4 * g3 + 3 * (g2 + g2mg12) * g1) * g1) / ((g2mg12) ** 2))
        ku = where(abs(c) <= (eps) ** 0.23, 12.0 / 5.0, ku1 - 3.0)
        return m, v, sk, ku
 
 
    def _munp(self, n, c):
        k = arange(0, n + 1)
        vals = 1.0 / c ** n * sum(comb(n, k) * (-1) ** k * special.gamma(c * k + 1), axis=0)
        return where(c * n > -1, vals, inf)
    def _cdf(self, x, a, c):
        val = special.gammainc(a, x ** c)
        cond = c + 0 * val
        return where(cond > 0, val, 1 - val)
    def _ppf(self, q, a, c):
        val1 = special.gammaincinv(a, q)
        val2 = special.gammaincinv(a, 1.0 - q)
        ic = 1.0 / c
        cond = c + 0 * val1
        return where(cond > 0, val1 ** ic, val2 ** ic)
    def _cdf(self, x):
        return where(x > 0, 1.0 - 0.5 * exp(-x), 0.5 * exp(x))
    def _ppf(self, q):
        return where(q > 0.5, -log(2 * (1 - q)), log(2 * q))
    def _stats(self):
        return 0, 2, 0, 3
    def _entropy(self):
    def _pdf(self, x, c):
        cd2 = c / 2.0
        c = where(x < 1, c, -c)
        return cd2 * x ** (c - 1)
    def _cdf(self, x, c):
        return where(x < 1, 0.5 * x ** c, 1 - 0.5 * x ** (-c))
    def _ppf(self, q, c):
        return where(q < 0.5, (2.0 * q) ** (1.0 / c), (2 * (1.0 - q)) ** (-1.0 / c))
    def _stats(self, dfn, dfd, nc):
        mu = where(dfd <= 2, inf, dfd / (dfd - 2.0) * (1 + nc * 1.0 / dfn))
        mu2 = where(dfd <= 4, inf, 2 * (dfd * 1.0 / dfn) ** 2.0 * \
                    ((dfn + nc / 2.0) ** 2.0 + (dfn + nc) * (dfd - 2.0)) / \
                    ((dfd - 2.0) ** 2.0 * (dfd - 4.0)))
        return mu, mu2, None, None
ncf = ncf_gen(a=0.0, name='ncf', longname="A non-central F distribution",
    def _stats(self, df):
        mu2 = where(df > 2, df / (df - 2.0), inf)
        g1 = where(df > 3, 0.0, nan)
        g2 = where(df > 4, 6.0 / (df - 4.0), nan)
        return 0, mu2, g1, g2
t = t_gen(name='t', longname="Student's T",
          shapes="df", extradoc="""
    def _pdf(self, x, c):
        return where(x < c, 2 * x / c, 2 * (1 - x) / (1 - c))
    def _cdf(self, x, c):
        return where(x < c, x * x / c, (x * x - 2 * x + c) / (c - 1))
    def _ppf(self, q, c):
        return where(q < c, sqrt(c * q), 1 - sqrt((1 - c) * (1 - q)))
    def _stats(self, c):
    def _pdf(self, x, lam):
        Fx = arr(special.tklmbda(x, lam))
        Px = Fx ** (lam - 1.0) + (arr(1 - Fx)) ** (lam - 1.0)
        Px = 1.0 / arr(Px)
        return where((lam > 0) & (abs(x) < 1.0 / lam), Px, 0.0)
    def _cdf(self, x, lam):
        return special.tklmbda(x, lam)
    def _ppf(self, q, lam):
        q = q * 1.0
        vals1 = (q ** lam - (1 - q) ** lam) / lam
        vals2 = log(q / (1 - q))
        return where((lam == 0) & (q == q), vals2, vals1)
    def _ppf(self, q, c):
        val = (1.0 - c) / (1.0 + c)
        rcq = 2 * arctan(val * tan(pi * q))
        rcmq = 2 * pi - 2 * arctan(val * tan(pi * (1 - q)))
        return where(q < 1.0 / 2, rcq, rcmq)
    def _entropy(self, c):
        return log(2 * pi * (1 - c * c))
    pk = arr(pk)
    pk = 1.0 * pk / sum(pk, axis=0)
    if qk is None:
        vec = where(pk == 0, 0.0, pk * log(pk))
    else:
        qk = arr(qk)
        if len(qk) != len(pk):
            raise ValueError, "qk and pk must have same length."
        qk = 1.0 * qk / sum(qk, axis=0)
        # If qk is zero anywhere, then unless pk is zero at those places
        #   too, the relative entropy is infinite.
        if any(take(pk, nonzero(qk == 0.0), axis=0) != 0.0, 0):
            return inf
        vec = where (pk == 0, 0.0, -pk * log(pk / qk))
        nq = n * (1. - pr)
        lc = stirlerr(n) - stirlerr(x) - stirlerr(nx) - bd0(x, n * pr) - bd0(nx, nq)
        inside = (0. < pr) & (pr < 1.) & (0. < x) & (x < n)
        return where(inside, exp(lc) * sqrt(n / (PI2 * x * nx)), yborder)
 
    def _cdf(self, x, n, pr):
        k = floor(x)
    def _ppf(self, q, n, pr):
        vals = ceil(special.bdtrik(q, n, pr))
        vals1 = vals - 1
        temp = special.bdtr(vals1, n, pr)
        return where(temp >= q, vals1, vals)
    def _stats(self, n, pr):
        q = 1.0 - pr
    def _entropy(self, n, pr):
        k = r_[0:n + 1]
        vals = self._pmf(k, n, pr)
        lvals = where(vals == 0, 0.0, log(vals))
        return - sum(vals * lvals, axis=0)
binom = binom_gen(name='binom', shapes="n,pr", extradoc="""
 
    def _ppf(self, q, n, pr):
        vals = ceil(special.nbdtrik(q, n, pr))
        vals1 = (vals - 1).clip(0.0, np.inf)
        temp = self._cdf(vals1, n, pr)
        return where(temp >= q, vals1, vals)
    def _stats(self, n, pr):
        Q = 1.0 / pr
    def _ppf(self, q, pr):
        vals = ceil(log(1.0 - q) / log(1 - pr))
        temp = 1.0 - (1.0 - pr) ** (vals - 1)
        return where((temp >= q) & (vals > 0), vals - 1, vals)
    def _stats(self, pr):
        mu = 1.0 / pr
        qr = 1.0 - pr
    def _entropy(self, M, n, N):
        k = r_[N - (M - n):min(n, N) + 1]
        vals = self.pmf(k, M, n, N)
        lvals = where(vals == 0.0, 0.0, log(vals))
        return - sum(vals * lvals, axis=0)
hypergeom = hypergeom_gen(name='hypergeom', longname="A hypergeometric",
                          shapes="M,n,N", extradoc="""
    def _ppf(self, q, mu):
        vals = ceil(special.pdtrik(q, mu))
        vals1 = vals - 1
        temp = special.pdtr(vals1, mu)
        return where((temp >= q), vals1, vals)
    def _stats(self, mu):
        var = mu
    def _ppf(self, q, lambda_):
        vals = ceil(-1.0 / lambda_ * log1p(-q) - 1)
        vals1 = (vals - 1).clip(self.a, np.inf)
        temp = self._cdf(vals1, lambda_)
        return where(temp >= q, vals1, vals)        
    def _stats(self, lambda_):
        mu = 1 / (exp(lambda_) - 1)
    def _ppf(self, q, lambda_, N):
        qnew = q * (1 - exp(-lambda_ * N))
        vals = ceil(-1.0 / lambda_ * log(1 - qnew) - 1)
        vals1 = (vals - 1).clip(0.0, np.inf)
        temp = self._cdf(vals1, lambda_, N)
        return where(temp >= q, vals1, vals)
    def _stats(self, lambda_, N):
    def _ppf(self, q, min, max):
        vals = ceil(q * (max - min) + min) - 1
        vals1 = (vals - 1).clip(min, max)
        temp = self._cdf(vals1, min, max)
        return where(temp >= q, vals1, vals)
    def _stats(self, min, max):
        m2, m1 = arr(max), arr(min)
    def _cdf(self, x, a):
        k = floor(x)
        ind = (k >= 0)
        const = exp(a) + 1
        return where(ind, 1.0 - exp(-a * k) / const, exp(a * (k + 1)) / const)
    def _ppf(self, q, a):
        const = 1.0 / (1 + exp(-a))
        cons2 = 1 + exp(a)
        ind = q < const
        vals = ceil(where(ind, log(q * cons2) / a - 1, -log((1 - q) * cons2) / a))
        vals1 = (vals - 1)
        temp = self._cdf(vals1, a)
        return where(temp >= q, vals1, vals)
    def _pmf(self, x, mu1, mu2):
        px = np.where(x < 0, ncx2.pdf(2 * mu2, 2 * (1 - x), 2 * mu1) * 2,
                         ncx2.pdf(2 * mu1, 2 * (x + 1), 2 * mu2) * 2)
        #ncx2.pdf() returns nan's for extremely low probabilities
        return px
    def _cdf(self, x, mu1, mu2):
        x = np.floor(x)
        px = np.where(x < 0, ncx2.cdf(2 * mu2, -2 * x, 2 * mu1),

src/w/a/wafo-0.11/src/wafo/spectrum/models.py   wafo(Download)
from scipy.fftpack import fft
#from scipy.misc import ppimport
#import numpy as np
from numpy import (inf, atleast_1d, newaxis, any, minimum, maximum, array, #@UnresolvedImport
    asarray, exp, log, sqrt, where, pi, arange, linspace, sin, cos, abs, sinh, #@UnresolvedImport
    isfinite, mod, expm1, tanh, cosh, finfo, ones, ones_like, isnan, #@UnresolvedImport
    zeros_like, flatnonzero, sinc, hstack, vstack, real, flipud, clip) #@UnresolvedImport
    def peak_e_factor(self, wn):
        ''' PEAKENHANCEMENTFACTOR
        '''
        w = maximum(atleast_1d(wn), 0.0)
        sab = where(w > 1, self.sigmaB, self.sigmaA)
 
        wnm12 = 0.5 * ((w - 1.0) / sab) ** 2.0
    k2 = w2k(w, 0, h, g=g)[0]
 
    k2h = k2 * h
    dw2 = where(k1 != 0, dw1 / (tanh(k2h) + k2h / cosh(k2h) ** 2.0), 0) # dw/dk|h=h0
 
    return where(k1 != 0, (k1 / k2) ** 3.0 * dw2 / dw1, 0)
 
 
        [B, TH, phi0, unused_Nt] = self.chk_input(theta, w, wc)
        NB = tanh(pi * B) #% Normalization factor.
        NB = where(NB == 0, 1.0, NB) # Avoid division by zero
##        if not self.method[0]=='n':
##            B = B[newaxis,:]
##            NB = NB[newaxis,:]
    def fourier2d(self, r1):
        ''' Returns the solution of R1 = exp(-D**2/2).
        '''
        r = clip(r1, 0., 1.0)
        return where(r <= 0, inf, sqrt(-2.0 * log(r)))
 
 
 
            # Mitsuyasu et. al and Hasselman et. al parametrization   of
            # frequency dependent spreading
            s = where(wn <= wn_c, spa * wn ** ma, spb * wn ** mb)
            s[wn <= wn_lo] = 0.0
 
            k = flatnonzero(wn_up < wn)
        tiny = 1. / realmax
        x = clip(2. * B, tiny, realmax)
        xk = pi / x
        return where(x < 100., xk / sinh(xk), -2. * xk / (exp(xk) * expm1(-2. * xk)))
 
 
 

  1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9  Next