Beispiel #1
0
    def rgamma(self, alpha, lam, lngamalpha=False, \
                                 xmax=float('inf'), pmax=1.0, \
                                 tolf=FOURMACHEPS, itmax=128):
        """
        The gamma distribution
        f = lam * exp(-lam*x) * (lam*x)**(alpha-1) / gamma(alpha)
        The cdf is the integral = the incomplete gamma ratio.
        x, lam, alpha >= 0 

        NB It is possible to provide the value of the natural logarithm of the 
        complete gamma function lngamma(alpha) as a pre-computed input (may be 
        computed using numlib.specfunc.lngamma) instead of the default "False", 
        a feature that will make rgamma 50 % faster! 
        
        tolf and itmax are the numerical control parameters of igamma 
        and cgamma.
        """

        assert xmax >= 0.0, "xmax must be a non-negative float in rgamma!"
        self._checkpmax(pmax, 'rgamma')

        pmx = pmax
        if xmax < float('inf'): \
             pmx = min(pmax, cgamma(alpha, lam, xmax, lngamalpha, tolf, itmax))

        p  =  pmx * self.runif01()
        x  =  igamma(p, alpha, lam, lngamalpha, tolf, itmax)

        return x
Beispiel #2
0
 def _fifi2fid(x):
     x      = kept_within(0.0, x)
     cdf    = cgamma(alpha, lam, x, lngalpha, tolf, itmax)
     pdf    = dgamma(alpha, lam, x, lngalpha)
     fi     = cdf - prob
     if pdf <= 0.0:
         if fi == 0.0: fi2fid = 1.0
         else:         fi2fid = MAXFLOAT
     else:
         fi2fid = fi/pdf
     return fi, fi2fid