You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2018/01/27 13:56:47 UTC

[1/2] commons-statistics git commit: Unnecessary method calls.

Repository: commons-statistics
Updated Branches:
  refs/heads/master 3eb53ba17 -> 9cadb96cb


Unnecessary method calls.


Project: http://git-wip-us.apache.org/repos/asf/commons-statistics/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-statistics/commit/33d1552f
Tree: http://git-wip-us.apache.org/repos/asf/commons-statistics/tree/33d1552f
Diff: http://git-wip-us.apache.org/repos/asf/commons-statistics/diff/33d1552f

Branch: refs/heads/master
Commit: 33d1552f0140d207ba5ce5eab3eb7ec9b743ef28
Parents: 3eb53ba
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Sat Jan 27 14:40:25 2018 +0100
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Sat Jan 27 14:40:25 2018 +0100

----------------------------------------------------------------------
 .../commons/statistics/distribution/TDistribution.java | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/33d1552f/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java
----------------------------------------------------------------------
diff --git a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java
index 07b1fc6..7177308 100644
--- a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java
+++ b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java
@@ -103,9 +103,7 @@ public class TDistribution extends AbstractContinuousDistribution {
      */
     @Override
     public double getMean() {
-        final double df = getDegreesOfFreedom();
-
-        if (df > 1) {
+        if (degreesOfFreedom > 1) {
             return 0;
         }
 
@@ -125,13 +123,12 @@ public class TDistribution extends AbstractContinuousDistribution {
      */
     @Override
     public double getVariance() {
-        final double df = getDegreesOfFreedom();
-
-        if (df > 2) {
-            return df / (df - 2);
+        if (degreesOfFreedom > 2) {
+            return degreesOfFreedom / (degreesOfFreedom - 2);
         }
 
-        if (df > 1 && df <= 2) {
+        if (degreesOfFreedom > 1 &&
+            degreesOfFreedom <= 2) {
             return Double.POSITIVE_INFINITY;
         }
 


[2/2] commons-statistics git commit: Cache value of mean and variance.

Posted by er...@apache.org.
Cache value of mean and variance.


Project: http://git-wip-us.apache.org/repos/asf/commons-statistics/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-statistics/commit/9cadb96c
Tree: http://git-wip-us.apache.org/repos/asf/commons-statistics/tree/9cadb96c
Diff: http://git-wip-us.apache.org/repos/asf/commons-statistics/diff/9cadb96c

Branch: refs/heads/master
Commit: 9cadb96cbb82c37187b6b4833c73cc0340c08454
Parents: 33d1552
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Sat Jan 27 14:55:07 2018 +0100
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Sat Jan 27 14:55:07 2018 +0100

----------------------------------------------------------------------
 .../statistics/distribution/TDistribution.java  | 43 ++++++++------------
 1 file changed, 18 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9cadb96c/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java
----------------------------------------------------------------------
diff --git a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java
index 7177308..a579e15 100644
--- a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java
+++ b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java
@@ -29,6 +29,10 @@ public class TDistribution extends AbstractContinuousDistribution {
     private final double dofOver2;
     /** Cached value. */
     private final double factor;
+    /** Cached value. */
+    private final double mean;
+    /** Cached value. */
+    private final double variance;
 
     /**
      * Creates a distribution.
@@ -47,6 +51,11 @@ public class TDistribution extends AbstractContinuousDistribution {
         factor = LogGamma.value(dofOver2 + 0.5) -
                  0.5 * (Math.log(Math.PI) + Math.log(degreesOfFreedom)) -
                  LogGamma.value(dofOver2);
+        mean = degreesOfFreedom > 1 ? 0 :
+            Double.NaN;
+        variance = degreesOfFreedom > 2 ? degreesOfFreedom / (degreesOfFreedom - 2) :
+            degreesOfFreedom > 1 && degreesOfFreedom <= 2 ? Double.POSITIVE_INFINITY :
+            Double.NaN;
     }
 
     /**
@@ -97,17 +106,13 @@ public class TDistribution extends AbstractContinuousDistribution {
      *
      * For degrees of freedom parameter {@code df}, the mean is
      * <ul>
-     *  <li>if {@code df > 1} then {@code 0},</li>
-     * <li>else undefined ({@code Double.NaN}).</li>
+     *  <li>zero if {@code df > 1}, and</li>
+     *  <li>undefined ({@code Double.NaN}) otherwise.</li>
      * </ul>
      */
     @Override
     public double getMean() {
-        if (degreesOfFreedom > 1) {
-            return 0;
-        }
-
-        return Double.NaN;
+        return mean;
     }
 
     /**
@@ -115,31 +120,20 @@ public class TDistribution extends AbstractContinuousDistribution {
      *
      * For degrees of freedom parameter {@code df}, the variance is
      * <ul>
-     *  <li>if {@code df > 2} then {@code df / (df - 2)},</li>
-     *  <li>if {@code 1 < df <= 2} then positive infinity
-     *  ({@code Double.POSITIVE_INFINITY}),</li>
-     *  <li>else undefined ({@code Double.NaN}).</li>
+     *  <li>{@code df / (df - 2)} if {@code df > 2},</li>
+     *  <li>infinite ({@code Double.POSITIVE_INFINITY}) if {@code 1 < df <= 2}, and</li>
+     *  <li>undefined ({@code Double.NaN}) otherwise.</li>
      * </ul>
      */
     @Override
     public double getVariance() {
-        if (degreesOfFreedom > 2) {
-            return degreesOfFreedom / (degreesOfFreedom - 2);
-        }
-
-        if (degreesOfFreedom > 1 &&
-            degreesOfFreedom <= 2) {
-            return Double.POSITIVE_INFINITY;
-        }
-
-        return Double.NaN;
+        return variance;
     }
 
     /**
      * {@inheritDoc}
      *
-     * The lower bound of the support is always negative infinity no matter the
-     * parameters.
+     * The lower bound of the support is always negative infinity..
      *
      * @return lower bound of the support (always
      * {@code Double.NEGATIVE_INFINITY})
@@ -152,8 +146,7 @@ public class TDistribution extends AbstractContinuousDistribution {
     /**
      * {@inheritDoc}
      *
-     * The upper bound of the support is always positive infinity no matter the
-     * parameters.
+     * The upper bound of the support is always positive infinity.
      *
      * @return upper bound of the support (always
      * {@code Double.POSITIVE_INFINITY})