Example usage for org.apache.commons.math3.special Gamma regularizedGammaP

List of usage examples for org.apache.commons.math3.special Gamma regularizedGammaP

Introduction

In this page you can find the example usage for org.apache.commons.math3.special Gamma regularizedGammaP.

Prototype

public static double regularizedGammaP(double a, double x) 

Source Link

Document

Returns the regularized gamma function P(a, x).

Usage

From source file:com.opengamma.strata.math.impl.statistics.distribution.NonCentralChiSquaredDistribution.java

/**
 * {@inheritDoc}//from  www.  jav  a  2  s .  c  o  m
 */
@Override
public double getCDF(Double x) {
    ArgChecker.notNull(x, "x");
    if (x < 0) {
        return 0.0;
    }

    if ((_dofOverTwo + _lambdaOverTwo) > 1000) {
        return getFraserApproxCDF(x);
    }

    double regGammaStart = 0;
    double halfX = x / 2.0;
    double logX = Math.log(halfX);
    try {
        regGammaStart = Gamma.regularizedGammaP(_dofOverTwo + _k, halfX);
    } catch (MaxCountExceededException ex) {
        throw new MathException(ex);
    }

    double sum = _pStart * regGammaStart;
    double oldSum = Double.NEGATIVE_INFINITY;
    double p = _pStart;
    double regGamma = regGammaStart;
    double temp;
    int i = _k;

    // first add terms below _k
    while (i > 0 && Math.abs(sum - oldSum) / sum > _eps) {
        i--;
        p *= (i + 1) / _lambdaOverTwo;
        temp = (_dofOverTwo + i) * logX - halfX - Gamma.logGamma(_dofOverTwo + i + 1);
        regGamma += Math.exp(temp);
        oldSum = sum;
        sum += p * regGamma;
    }

    p = _pStart;
    regGamma = regGammaStart;
    oldSum = Double.NEGATIVE_INFINITY;
    i = _k;
    while (Math.abs(sum - oldSum) / sum > _eps) {
        i++;
        p *= _lambdaOverTwo / i;
        temp = (_dofOverTwo + i - 1) * logX - halfX - Gamma.logGamma(_dofOverTwo + i);
        regGamma -= Math.exp(temp);
        oldSum = sum;
        sum += p * regGamma;
    }

    return sum;
}

From source file:gamlss.distributions.PE.java

/** Computes the cumulative distribution 
 * function P(X <= q) for a random variable X .
 * whose values are distributed according to this distribution
 * @param q - value of quantile// w ww  . ja  va  2s  .com
 * @param mu - value of mu distribution parameter
 * @param sigma - value of sigma distribution parameter
 * @param nu - value of nu distribution parameter 
 * @param lowerTail - logical, if TRUE (default), probabilities
 *  are P[X <= x] otherwise, P[X > x].
 * @param isLog - logical, if TRUE, probabilities p are given as log(p)
 * @return value of cumulative probability function values P(X <= q)
 */
public final double pPE(final double q, final double mu, final double sigma, final double nu,
        final boolean lowerTail, final boolean isLog) {

    // {  if (any(sigma < 0))stop(paste("sigma must be positive",))
    if (sigma < 0) {
        System.err.println("sigma must be positive");
        return -1.0;
    }

    //if (any(nu < 0))  stop(paste("nu must be positive", "\n", ""))
    if (nu < 0) {
        System.err.println("nu must be positive");
        return -1.0;
    }

    double out = 0;

    //ifelse(nu>10000, (q-(mu-sqrt(3)*sigma))/(sqrt(12)*sigma),cdf)
    if (nu > 10000) {
        out = (q - (mu - FastMath.sqrt(3) * sigma)) / (FastMath.sqrt(12) * sigma);
    } else {

        //log.c <- 0.5*(-(2/nu)*log(2)+lgamma(1/nu)-lgamma(3/nu))
        final double logC = 0.5
                * (-(2 / nu) * FastMath.log(2) + Gamma.logGamma(1 / nu) - Gamma.logGamma(3 / nu));

        //c <- exp(log.c)
        final double c = FastMath.exp(logC);

        //z <- (y-mu)/sigma
        final double z = (q - mu) / sigma;

        //s <- 0.5*((abs(z/c))^nu)
        final double s = 0.5 * (FastMath.pow(FastMath.abs(z / c), nu));

        //cdf <- 0.5*(1+pgamma(s,shape=1/nu,scale=1)*sign(z))
        out = 0.5 * (1 + Gamma.regularizedGammaP(1 / nu, s) * FastMath.signum(z));
    }

    //if(lower.tail==TRUE) cdf  <- cdf else  cdf <- 1-cdf 
    //if(log.p==FALSE) cdf  <- cdf else  cdf <- log(cdf) 
    if (!lowerTail) {
        if (isLog) {
            out = FastMath.log(1 - out);
        } else {
            out = 1 - out;
        }
    } else if (isLog) {
        //if(log.p==FALSE) cdf  <- cdf else  cdf <- log(cdf)
        out = FastMath.log(out);
    }
    return out;
}

From source file:statalign.utils.GammaDistribution.java

/**
 * {@inheritDoc}/*from  w  w  w  .  j  av a  2  s  . c  om*/
 *
 * The implementation of this method is based on:
 * <ul>
 *  <li>
 *   <a href="http://mathworld.wolfram.com/Chi-SquaredDistribution.html">
 *    Chi-Squared Distribution</a>, equation (9).
 *  </li>
 *  <li>Casella, G., & Berger, R. (1990). <i>Statistical Inference</i>.
 *    Belmont, CA: Duxbury Press.
 *  </li>
 * </ul>
 */
@Override
public double cumulativeProbability(double x) {
    double ret;

    if (x <= 0) {
        ret = 0;
    } else {
        ret = Gamma.regularizedGammaP(shape, x / scale);
    }

    return ret;
}