[SciPy-user] Pb with scipy.stats

Youngsu Park youngsu999@gmail....
Sun Nov 18 23:10:34 CST 2007


Hi,
In the scipy/stats directory, there is distribution.py file.
Several distribution is defined in it.
The betaprime is a instance of betaprime_gen that inherit rv_continuous.
The ppf method is not defined In betaprime_gen class.

## Beta Prime
class betaprime_gen(rv_continuous):
    def _rvs(self, a, b):
        u1 = gamma.rvs(a,size=self._size)
        u2 = gamma.rvs(b,size=self._size)
        return (u1 / u2)
    def _pdf(self, x, a, b):
        return 1.0/special.beta(a,b)*x**(a-1.0)/(1+x)**(a+b)
    def _cdf(self, x, a, b):
        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,
                         a*(a+1.0)*(a+2.0)*(a+3.0)/((b-4.0)*(b-3.0) \
                                                    *(b-2.0)*(b-1.0)), inf)
        else:
            raise NotImplementedError
betaprime = betaprime_gen(a=0.0, b=500.0, name='betaprime', shapes='a,b',
                          extradoc="""~~~""")



so it uses the rv_continuous class's method.

    def ppf(self,q,*args,**kwds):
        """Percent point function (inverse of cdf) at q of the given RV.

        *args
        =====
          The shape parameter(s) for the distribution (see docstring of the
           instance object for more information)

        **kwds
        ======
        loc   - location parameter (default=0)
        scale - scale parameter (default=1)
        """
        loc,scale=map(kwds.get,['loc','scale'])
        args, loc, scale = self.__fix_loc_scale(args, loc, scale)
        q,loc,scale = map(arr,(q,loc,scale))
        args = tuple(map(arr,args))
        cond0 = self._argcheck(*args) & (scale > 0) & (loc==loc)
        cond1 = (q > 0) & (q < 1)
        cond2 = (q==1) & cond0
        cond = cond0 & cond1
        output = valarray(shape(cond),value=self.a*scale + loc)
        place(output,(1-cond0)+(1-cond1)*(q!=0.0), self.badvalue)
        place(output,cond2,self.b*scale + loc)
        goodargs = argsreduce(cond, *((q,)+args+(scale,loc)))
        scale, loc, goodargs = goodargs[-2], goodargs[-1], goodargs[:-2]
        place(output,cond,self._ppf(*goodargs)*scale + loc)
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        return output
....
    sgf = vectorize
    self.vecfunc = sgf(self._ppf_single_call,otypes='d')
...
    def _ppf(self, q, *args):
        return self.vecfunc(q,*args)
        ~~~~~~~~~~~~~~~~~~~~
...    def _ppf_single_call(self, q, *args):
         return scipy.optimize.brentq(self._ppf_to_solve, self.xa,
self.xb, args=(q,)+args, xtol=self.xtol)
I'm not sure but I think that the problem comes from the vectorization
of defalut _ppf_single_call funstion.
But I don'k know how to fix it.
Is there someone to fix it?


More information about the SciPy-user mailing list