Java源码示例:org.apache.commons.math.special.Gamma
示例1
static double getStirlingError(double z) {
double ret;
double z2;
if (z < 15.0D) {
z2 = 2.0D * z;
if (FastMath.floor(z2) == z2) {
ret = EXACT_STIRLING_ERRORS[(int)z2];
} else {
ret = Gamma.logGamma(z + 1.0D) - (z + 0.5D) * FastMath.log(z) + z - HALF_LOG_2_PI;
}
} else {
z2 = z * z;
ret = (0.08333333333333333D - (0.002777777777777778D - (7.936507936507937E-4D - (5.952380952380953E-4D - 8.417508417508417E-4D / z2) / z2) / z2) / z2) / z;
}
return ret;
}
示例2
public void testMomonts() {
final double tol = 1e-9;
WeibullDistribution dist;
dist = new WeibullDistributionImpl(2.5, 3.5);
// In R: 3.5*gamma(1+(1/2.5)) (or emperically: mean(rweibull(10000, 2.5, 3.5)))
assertEquals(dist.getNumericalMean(), 3.5 * FastMath.exp(Gamma.logGamma(1 + (1 / 2.5))), tol);
assertEquals(dist.getNumericalVariance(), (3.5 * 3.5) *
FastMath.exp(Gamma.logGamma(1 + (2 / 2.5))) -
(dist.getNumericalMean() * dist.getNumericalMean()), tol);
dist = new WeibullDistributionImpl(10.4, 2.222);
assertEquals(dist.getNumericalMean(), 2.222 * FastMath.exp(Gamma.logGamma(1 + (1 / 10.4))), tol);
assertEquals(dist.getNumericalVariance(), (2.222 * 2.222) *
FastMath.exp(Gamma.logGamma(1 + (2 / 10.4))) -
(dist.getNumericalMean() * dist.getNumericalMean()), tol);
}
示例3
@Test
public void testMomonts() {
final double tol = 1e-9;
WeibullDistribution dist;
dist = new WeibullDistributionImpl(2.5, 3.5);
// In R: 3.5*gamma(1+(1/2.5)) (or emperically: mean(rweibull(10000, 2.5, 3.5)))
Assert.assertEquals(dist.getNumericalMean(), 3.5 * FastMath.exp(Gamma.logGamma(1 + (1 / 2.5))), tol);
Assert.assertEquals(dist.getNumericalVariance(), (3.5 * 3.5) *
FastMath.exp(Gamma.logGamma(1 + (2 / 2.5))) -
(dist.getNumericalMean() * dist.getNumericalMean()), tol);
dist = new WeibullDistributionImpl(10.4, 2.222);
Assert.assertEquals(dist.getNumericalMean(), 2.222 * FastMath.exp(Gamma.logGamma(1 + (1 / 10.4))), tol);
Assert.assertEquals(dist.getNumericalVariance(), (2.222 * 2.222) *
FastMath.exp(Gamma.logGamma(1 + (2 / 10.4))) -
(dist.getNumericalMean() * dist.getNumericalMean()), tol);
}
示例4
@Test
public void testMomonts() {
final double tol = 1e-9;
WeibullDistribution dist;
dist = new WeibullDistributionImpl(2.5, 3.5);
// In R: 3.5*gamma(1+(1/2.5)) (or emperically: mean(rweibull(10000, 2.5, 3.5)))
Assert.assertEquals(dist.getNumericalMean(), 3.5 * FastMath.exp(Gamma.logGamma(1 + (1 / 2.5))), tol);
Assert.assertEquals(dist.getNumericalVariance(), (3.5 * 3.5) *
FastMath.exp(Gamma.logGamma(1 + (2 / 2.5))) -
(dist.getNumericalMean() * dist.getNumericalMean()), tol);
dist = new WeibullDistributionImpl(10.4, 2.222);
Assert.assertEquals(dist.getNumericalMean(), 2.222 * FastMath.exp(Gamma.logGamma(1 + (1 / 10.4))), tol);
Assert.assertEquals(dist.getNumericalVariance(), (2.222 * 2.222) *
FastMath.exp(Gamma.logGamma(1 + (2 / 10.4))) -
(dist.getNumericalMean() * dist.getNumericalMean()), tol);
}
示例5
/**
* The probability distribution function P(X <= x) for a Poisson distribution.
*
* @param x the value at which the PDF is evaluated.
* @return Poisson distribution function evaluated at x
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
*/
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double)x + 1, mean,
1E-12, Integer.MAX_VALUE);
}
示例6
/**
* Returns the probability density for a particular point.
*
* @param x The point at which the density should be computed.
* @return The pdf at point x.
* @since 2.1
*/
@Override
public double density(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return Math.exp(Gamma.logGamma(nPlus1Over2) - 0.5 * (Math.log(Math.PI) + Math.log(n)) -
Gamma.logGamma(n/2) - nPlus1Over2 * Math.log(1 + x * x /n));
}
示例7
/**
* Returns the probability density for a particular point.
*
* @param x The point at which the density should be computed.
* @return The pdf at point x.
* @since 2.1
*/
@Override
public double density(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return Math.exp(Gamma.logGamma(nPlus1Over2) - 0.5 * (Math.log(Math.PI) + Math.log(n)) -
Gamma.logGamma(n/2) - nPlus1Over2 * Math.log(1 + x * x /n));
}
示例8
/**
* Returns the probability density for a particular point.
*
* @param x The point at which the density should be computed.
* @return The pdf at point x.
* @since 2.1
*/
@Override
public double density(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return Math.exp(Gamma.logGamma(nPlus1Over2) - 0.5 * (Math.log(Math.PI) + Math.log(n)) -
Gamma.logGamma(n/2) - nPlus1Over2 * Math.log(1 + x * x /n));
}
示例9
/**
* Returns the probability density for a particular point.
*
* @param x The point at which the density should be computed.
* @return The pdf at point x.
* @since 2.1
*/
@Override
public double density(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return Math.exp(Gamma.logGamma(nPlus1Over2) - 0.5 * (Math.log(Math.PI) + Math.log(n)) -
Gamma.logGamma(n/2) - nPlus1Over2 * Math.log(1 + x * x /n));
}
示例10
/**
* The probability distribution function P(X <= x) for a Poisson
* distribution.
*
* @param x the value at which the PDF is evaluated.
* @return Poisson distribution function evaluated at x
* @throws MathException if the cumulative probability can not be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double) x + 1, mean, epsilon, maxIterations);
}
示例11
/**
* The probability distribution function P(X <= x) for a Poisson
* distribution.
*
* @param x the value at which the PDF is evaluated.
* @return Poisson distribution function evaluated at x
* @throws MathException if the cumulative probability can not be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double) x + 1, mean, 1E-12,
Integer.MAX_VALUE);
}
示例12
/**
* The probability distribution function P(X <= x) for a Poisson
* distribution.
*
* @param x the value at which the PDF is evaluated.
* @return Poisson distribution function evaluated at x
* @throws MathException if the cumulative probability can not be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double) x + 1, mean, 1E-12,
Integer.MAX_VALUE);
}
示例13
/**
* {@inheritDoc}
*
* The variance is
* <code>scale^2 * Gamma(1 + (2 / shape)) - mean^2</code>
* where <code>Gamma(...)</code> is the Gamma-function
*
* @return {@inheritDoc}
*/
@Override
protected double calculateNumericalVariance() {
final double sh = getShape();
final double sc = getScale();
final double mn = getNumericalMean();
return (sc * sc) *
FastMath.exp(Gamma.logGamma(1 + (2 / sh))) -
(mn * mn);
}
示例14
/**
* {@inheritDoc}
*/
@Override
public double density(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return FastMath.exp(Gamma.logGamma(nPlus1Over2) -
0.5 * (FastMath.log(FastMath.PI) + FastMath.log(n)) -
Gamma.logGamma(n/2) - nPlus1Over2 * FastMath.log(1 + x * x /n));
}
示例15
/**
* {@inheritDoc}
*/
@Override
public double density(double x) {
if (x < 0) return 0;
return FastMath.pow(x / beta, alpha - 1) / beta *
FastMath.exp(-x / beta) / FastMath.exp(Gamma.logGamma(alpha));
}
示例16
/**
* The probability distribution function {@code P(X <= x)} for a Poisson
* distribution.
*
* @param x Value at which the PDF is evaluated.
* @return the Poisson distribution function evaluated at {@code x}.
* @throws MathException if the cumulative probability cannot be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double) x + 1, mean, epsilon, maxIterations);
}
示例17
/**
* Returns the probability density for a particular point.
*
* @param x The point at which the density should be computed.
* @return The pdf at point x.
* @since 2.1
*/
@Override
public double density(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return FastMath.exp(Gamma.logGamma(nPlus1Over2) - 0.5 * (FastMath.log(FastMath.PI) + FastMath.log(n)) -
Gamma.logGamma(n/2) - nPlus1Over2 * FastMath.log(1 + x * x /n));
}
示例18
/**
* The probability distribution function P(X <= x) for a Poisson
* distribution.
*
* @param x the value at which the PDF is evaluated.
* @return Poisson distribution function evaluated at x
* @throws MathException if the cumulative probability can not be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double) x + 1, mean, epsilon, maxIterations);
}
示例19
/**
* {@inheritDoc}
*
* The variance is
* <code>scale^2 * Gamma(1 + (2 / shape)) - mean^2</code>
* where <code>Gamma(...)</code> is the Gamma-function
*
* @return {@inheritDoc}
*/
@Override
protected double calculateNumericalVariance() {
final double sh = getShape();
final double sc = getScale();
final double mn = getNumericalMean();
return (sc * sc) *
FastMath.exp(Gamma.logGamma(1 + (2 / sh))) -
(mn * mn);
}
示例20
/**
* {@inheritDoc}
*/
@Override
public double density(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return FastMath.exp(Gamma.logGamma(nPlus1Over2) -
0.5 * (FastMath.log(FastMath.PI) + FastMath.log(n)) -
Gamma.logGamma(n/2) - nPlus1Over2 * FastMath.log(1 + x * x /n));
}
示例21
/**
* {@inheritDoc}
*/
@Override
public double density(double x) {
if (x < 0) {
return 0;
}
return FastMath.pow(x / beta, alpha - 1) / beta *
FastMath.exp(-x / beta) / FastMath.exp(Gamma.logGamma(alpha));
}
示例22
/**
* The probability distribution function {@code P(X <= x)} for a Poisson
* distribution.
*
* @param x Value at which the PDF is evaluated.
* @return the Poisson distribution function evaluated at {@code x}.
* @throws MathException if the cumulative probability cannot be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double) x + 1, mean, epsilon, maxIterations);
}
示例23
/**
* Returns the probability density for a particular point.
*
* @param x The point at which the density should be computed.
* @return The pdf at point x.
* @since 2.1
*/
@Override
public double density(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return Math.exp(Gamma.logGamma(nPlus1Over2) - 0.5 * (Math.log(Math.PI) + Math.log(n)) -
Gamma.logGamma(n/2) - nPlus1Over2 * Math.log(1 + x * x /n));
}
示例24
/**
* The probability distribution function P(X <= x) for a Poisson
* distribution.
*
* @param x the value at which the PDF is evaluated.
* @return Poisson distribution function evaluated at x
* @throws MathException if the cumulative probability can not be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double) x + 1, mean, epsilon, maxIterations);
}
示例25
/**
* Returns the probability density for a particular point.
*
* @param x The point at which the density should be computed.
* @return The pdf at point x.
* @since 2.1
*/
@Override
public double density(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return Math.exp(Gamma.logGamma(nPlus1Over2) - 0.5 * (Math.log(Math.PI) + Math.log(n)) -
Gamma.logGamma(n/2) - nPlus1Over2 * Math.log(1 + x * x /n));
}
示例26
/**
* The probability distribution function P(X <= x) for a Poisson
* distribution.
*
* @param x the value at which the PDF is evaluated.
* @return Poisson distribution function evaluated at x
* @throws MathException if the cumulative probability can not be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double) x + 1, mean, epsilon, maxIterations);
}
示例27
/**
* The probability distribution function P(X <= x) for a Poisson
* distribution.
*
* @param x the value at which the PDF is evaluated.
* @return Poisson distribution function evaluated at x
* @throws MathException if the cumulative probability can not be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double) x + 1, mean, 1E-12,
Integer.MAX_VALUE);
}
示例28
/**
* The probability distribution function P(X <= x) for a Poisson
* distribution.
*
* @param x the value at which the PDF is evaluated.
* @return Poisson distribution function evaluated at x
* @throws MathException if the cumulative probability can not be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double) x + 1, mean, epsilon, maxIterations);
}
示例29
/**
* Returns the probability density for a particular point.
*
* @param x The point at which the density should be computed.
* @return The pdf at point x.
* @since 2.1
*/
@Override
public double density(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return Math.exp(Gamma.logGamma(nPlus1Over2) - 0.5 * (Math.log(Math.PI) + Math.log(n)) -
Gamma.logGamma(n/2) - nPlus1Over2 * Math.log(1 + x * x /n));
}
示例30
/**
* The probability distribution function P(X <= x) for a Poisson
* distribution.
*
* @param x the value at which the PDF is evaluated.
* @return Poisson distribution function evaluated at x
* @throws MathException if the cumulative probability can not be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
if (x < 0) {
return 0;
}
if (x == Integer.MAX_VALUE) {
return 1;
}
return Gamma.regularizedGammaQ((double) x + 1, mean, epsilon, maxIterations);
}