You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2014/05/29 19:47:12 UTC

svn commit: r1598342 - in /commons/proper/math/trunk/src: changes/changes.xml main/java/org/apache/commons/math3/distribution/TDistribution.java

Author: tn
Date: Thu May 29 17:47:12 2014
New Revision: 1598342

URL: http://svn.apache.org/r1598342
Log:
[MATH-1125] Performance improvements for students t-distribution. Thanks to Ajo Fod.

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1598342&r1=1598341&r2=1598342&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Thu May 29 17:47:12 2014
@@ -73,6 +73,9 @@ Users are encouraged to upgrade to this 
   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.

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java?rev=1598342&r1=1598341&r2=1598342&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java Thu May 29 17:47:12 2014
@@ -18,11 +18,11 @@ package org.apache.commons.math3.distrib
 
 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 Abstr
     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 Abstr
         }
         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 Abstr
     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} */