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/26 16:58:05 UTC
[math] MATH-1441: Cache instance of "TDistribution".
Repository: commons-math
Updated Branches:
refs/heads/master 54fa0a855 -> 1f07a0bf7
MATH-1441: Cache instance of "TDistribution".
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/1f07a0bf
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/1f07a0bf
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/1f07a0bf
Branch: refs/heads/master
Commit: 1f07a0bf70049cb0a2f6b52ea7c261da5adb1abb
Parents: 54fa0a8
Author: Gilles <er...@apache.org>
Authored: Fri Jan 26 17:56:32 2018 +0100
Committer: Gilles <er...@apache.org>
Committed: Fri Jan 26 17:56:32 2018 +0100
----------------------------------------------------------------------
.../math4/stat/regression/SimpleRegression.java | 26 ++++++++++++++++----
1 file changed, 21 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/1f07a0bf/src/main/java/org/apache/commons/math4/stat/regression/SimpleRegression.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/stat/regression/SimpleRegression.java b/src/main/java/org/apache/commons/math4/stat/regression/SimpleRegression.java
index 7fc823d..f7340a3 100644
--- a/src/main/java/org/apache/commons/math4/stat/regression/SimpleRegression.java
+++ b/src/main/java/org/apache/commons/math4/stat/regression/SimpleRegression.java
@@ -91,6 +91,9 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
/** include an intercept or not */
private final boolean hasIntercept;
+
+ /** Cache (performance optimization). */
+ private transient TDistribution distribution;
// ---------------------Public methods--------------------------------------
/**
@@ -697,9 +700,8 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
alpha, 0, 1);
}
// No advertised NotStrictlyPositiveException here - will return NaN above
- TDistribution distribution = new TDistribution(n - 2);
return getSlopeStdErr() *
- distribution.inverseCumulativeProbability(1d - alpha / 2d);
+ getDistribution().inverseCumulativeProbability(1d - alpha / 2d);
}
/**
@@ -729,14 +731,28 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
return Double.NaN;
}
// No advertised NotStrictlyPositiveException here - will return NaN above
- TDistribution distribution = new TDistribution(n - 2);
- return 2d * (1.0 - distribution.cumulativeProbability(
- FastMath.abs(getSlope()) / getSlopeStdErr()));
+ return 2d * (1.0 - getDistribution()
+ .cumulativeProbability(FastMath.abs(getSlope()) / getSlopeStdErr()));
}
// ---------------------Private methods-----------------------------------
/**
+ * Computes or return a cached distribution.
+ *
+ * @return the distribution.
+ */
+ private TDistribution getDistribution() {
+ final double numDeg = n - 2;
+ if (distribution == null ||
+ distribution.getDegreesOfFreedom() != numDeg) {
+ distribution = new TDistribution(numDeg);
+ }
+
+ return distribution;
+ }
+
+ /**
* Returns the intercept of the estimated regression line, given the slope.
* <p>
* Will return <code>NaN</code> if slope is <code>NaN</code>.</p>