Пример #1
0
    def rcauchy(self, location, scale, xmin=float('-inf'), xmax=float('inf'), \
                                       pmin=0.0, pmax=1.0):
        """
        Generator of random variates from the Cauchy distribution: 
        f = 1 / [s*pi*(1 + [(x-l)/s]**2)]
        F = (1/pi)*arctan((x-l)/s) + 1/2
        (also known as the Lorentzian or Lorentz distribution)
        
        scale must be >= 0 
        """

        self._checkminmax(xmin, xmax, pmin, pmax, 'rcauchy')

        pmn = pmin
        pmx = pmax
        if xmin > float('-inf'):
            pmn = max(pmin, ccauchy(location, scale, xmin))
        if xmax < float('inf'):
            pmx = min(pmax, ccauchy(location, scale, xmax))
        
        p  =  pmn + (pmx-pmn)*self.runif01()
        x  =  icauchy(p, location, scale)

        return x
Пример #2
0
    def rcauchy(self, location, scale, xmin=float('-inf'), xmax=float('inf'), \
                                       pmin=0.0, pmax=1.0):
        """
        Generator of random variates from the Cauchy distribution: 
        f = 1 / [s*pi*(1 + [(x-l)/s]**2)]
        F = (1/pi)*arctan((x-l)/s) + 1/2
        (also known as the Lorentzian or Lorentz distribution)
        
        scale must be >= 0 
        """

        self._checkminmax(xmin, xmax, pmin, pmax, 'rcauchy')

        pmn = pmin
        pmx = pmax
        if xmin > float('-inf'):
            pmn = max(pmin, ccauchy(location, scale, xmin))
        if xmax < float('inf'):
            pmx = min(pmax, ccauchy(location, scale, xmax))

        p = pmn + (pmx - pmn) * self.runif01()
        x = icauchy(p, location, scale)

        return x
Пример #3
0
def _studQ(ndf, confdeg=0.90):
    """
    Quantiles for Student's "t" distribution (one number is returned)
    ---------
    ndf       the number of degrees of freedom
    confdeg  confidence level
    --------
    Accuracy in decimal digits of about:
      5 if ndf >= 8., machine precision if ndf = 1. or 2., 3 otherwise
    ----------
    Reference: G.W. Hill, Communications of the ACM, Vol. 13, no. 10, Oct. 1970
    """

    # Error handling
    assert 0.0 <= confdeg and confdeg <= 1.0, \
          "Confidence degree must be in [0.0, 1.0] in _studQ!"
    assert ndf > 0.0, "Number of degrees of freedom must be positive in _studQ!"


    phi = 1.0 - confdeg

    # For ndf = 1 we can use the Cauchy distribution
    if ndf == 1.0:
        prob = 1.0 - 0.5*phi
        stud = icauchy(prob)

    # Finns exakt metod aven for ndf = 2
    elif ndf == 2.0:
        if phi <= TINY: phi = TINY
        stud = sqrt(2.0 / (phi*(2.0-phi)) - 2.0)

    # Check to see if we're not too far out in the tails
    elif phi < TINY:
        t = 1.0 / TINY
        stud = t

    # General case
    else:
        a = 1.0 / (ndf-0.5)
        b = 48.0 / a**2
        c = ((20700.0*a/b - 98.0) * a - 16.0) * a + 96.36
        d = ((94.5/(b + c) - 3.0) / b + 1.0) * sqrt(a*PIHALF) * ndf
        x = d * phi
        y = x ** (2.0 / ndf)

        if y > 0.05 + a:  #     Asymptotic inverse expansion about normal
            x = inormal(0.5*phi)
            y = x**2
            if ndf < 5.0: c = c + 0.3 * (ndf-4.5) * (x+0.6)
            c = (((0.05 * d * x - 5.0) * x - 7.0) * x - 2.0) * x + b + c
            y = (((((0.4*y + 6.3) * y + 36.0) * y + 94.5) / c - y  - 3.0) \
                                                              / b + 1.0) * x
            y = a * y**2
            if y > 0.002: y = exp(y) - 1.0
            else:         y = 0.5 * y**2 + y
        else:

            y = ((1.0 / (((ndf + 6.0) / (ndf * y) - 0.089 * d -  \
                0.822) * (ndf + 2.0) * 30) + 0.5 / (ndf + 4.0))  \
                * y - 1.0) * (ndf + 1.0) / (ndf + 2.0) + 1.0 / y

        stud = sqrt(ndf*y)

    return stud
Пример #4
0
def _studQ(ndf, confdeg=0.90):
    """
    Quantiles for Student's "t" distribution (one number is returned)
    ---------
    ndf       the number of degrees of freedom
    confdeg  confidence level
    --------
    Accuracy in decimal digits of about:
      5 if ndf >= 8., machine precision if ndf = 1. or 2., 3 otherwise
    ----------
    Reference: G.W. Hill, Communications of the ACM, Vol. 13, no. 10, Oct. 1970
    """

    # Error handling
    assert 0.0 <= confdeg and confdeg <= 1.0, \
          "Confidence degree must be in [0.0, 1.0] in _studQ!"
    assert ndf > 0.0, "Number of degrees of freedom must be positive in _studQ!"

    phi = 1.0 - confdeg

    # For ndf = 1 we can use the Cauchy distribution
    if ndf == 1.0:
        prob = 1.0 - 0.5 * phi
        stud = icauchy(prob)

    # Finns exakt metod aven for ndf = 2
    elif ndf == 2.0:
        if phi <= TINY: phi = TINY
        stud = sqrt(2.0 / (phi * (2.0 - phi)) - 2.0)

    # Check to see if we're not too far out in the tails
    elif phi < TINY:
        t = 1.0 / TINY
        stud = t

    # General case
    else:
        a = 1.0 / (ndf - 0.5)
        b = 48.0 / a**2
        c = ((20700.0 * a / b - 98.0) * a - 16.0) * a + 96.36
        d = ((94.5 / (b + c) - 3.0) / b + 1.0) * sqrt(a * PIHALF) * ndf
        x = d * phi
        y = x**(2.0 / ndf)

        if y > 0.05 + a:  #     Asymptotic inverse expansion about normal
            x = inormal(0.5 * phi)
            y = x**2
            if ndf < 5.0: c = c + 0.3 * (ndf - 4.5) * (x + 0.6)
            c = (((0.05 * d * x - 5.0) * x - 7.0) * x - 2.0) * x + b + c
            y = (((((0.4*y + 6.3) * y + 36.0) * y + 94.5) / c - y  - 3.0) \
                                                              / b + 1.0) * x
            y = a * y**2
            if y > 0.002: y = exp(y) - 1.0
            else: y = 0.5 * y**2 + y
        else:

            y = ((1.0 / (((ndf + 6.0) / (ndf * y) - 0.089 * d -  \
                0.822) * (ndf + 2.0) * 30) + 0.5 / (ndf + 4.0))  \
                * y - 1.0) * (ndf + 1.0) / (ndf + 2.0) + 1.0 / y

        stud = sqrt(ndf * y)

    return stud