[MATH-1125] Performance improvements for students t-distribution. Thanks to Ajo Fod.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1598342 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2014-05-29 17:47:12 +00:00
parent 3e0f532e18
commit 8d3d915dbd
2 changed files with 14 additions and 7 deletions

View File

@ -73,6 +73,9 @@ Users are encouraged to upgrade to this version as this release not
2. A few methods in the FastMath class are in fact slower that their
counterpart in either Math or StrictMath (cf. MATH-740 and MATH-901).
">
<action dev="tn" type="fix" issue="MATH-1125" due-to="Ajo Fod">
Performance improvements for Student's t-distribution.
</action>
<action dev="luc" type="fix" issue="MATH-1123" due-to="Aurélien Labrosse">
Fixed NullPointerException when chopping-off a sub-hyperplane
that is exactly at a region boundary.

View File

@ -18,11 +18,11 @@ package org.apache.commons.math3.distribution;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.random.RandomGenerator;
import org.apache.commons.math3.random.Well19937c;
import org.apache.commons.math3.special.Beta;
import org.apache.commons.math3.special.Gamma;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.random.RandomGenerator;
import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of Student's t-distribution.
@ -43,6 +43,8 @@ public class TDistribution extends AbstractRealDistribution {
private final double degreesOfFreedom;
/** Inverse cumulative probability accuracy. */
private final double solverAbsoluteAccuracy;
/** Static computation factor based on degreesOfFreedom. */
private final double factor;
/**
* Create a t distribution using the given degrees of freedom.
@ -107,6 +109,12 @@ public class TDistribution extends AbstractRealDistribution {
}
this.degreesOfFreedom = degreesOfFreedom;
solverAbsoluteAccuracy = inverseCumAccuracy;
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
factor = Gamma.logGamma(nPlus1Over2) -
0.5 * (FastMath.log(FastMath.PI) + FastMath.log(n)) -
Gamma.logGamma(n / 2);
}
/**
@ -128,11 +136,7 @@ public class TDistribution extends AbstractRealDistribution {
public double logDensity(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return Gamma.logGamma(nPlus1Over2) -
0.5 * (FastMath.log(FastMath.PI) +
FastMath.log(n)) -
Gamma.logGamma(n / 2) -
nPlus1Over2 * FastMath.log(1 + x * x / n);
return factor - nPlus1Over2 * FastMath.log(1 + x * x / n);
}
/** {@inheritDoc} */