You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2023/09/27 18:50:43 UTC
[commons-statistics] 02/04: Use FirstMoment instance in constructor
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-statistics.git
commit 8af5405114ae8e329448cfc785bd01dff6c22475
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Sep 27 18:24:33 2023 +0100
Use FirstMoment instance in constructor
---
.../commons/statistics/descriptive/FirstMoment.java | 11 +++++++++++
.../descriptive/SumOfSquaredDeviations.java | 20 +++++++++-----------
2 files changed, 20 insertions(+), 11 deletions(-)
diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/FirstMoment.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/FirstMoment.java
index eb9e3d8..6eb2eeb 100644
--- a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/FirstMoment.java
+++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/FirstMoment.java
@@ -92,6 +92,17 @@ class FirstMoment implements DoubleConsumer {
// No-op
}
+ /**
+ * Copy constructor.
+ *
+ * @param source Source to copy.
+ */
+ FirstMoment(FirstMoment source) {
+ m1 = source.m1;
+ n = source.n;
+ nonFiniteValue = source.nonFiniteValue;
+ }
+
/**
* Returns a {@code FirstMoment} instance that has the arithmetic mean of all input
* values, or {@code NaN} if the input array is empty.
diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/SumOfSquaredDeviations.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/SumOfSquaredDeviations.java
index 0d41db4..9525d49 100644
--- a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/SumOfSquaredDeviations.java
+++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/SumOfSquaredDeviations.java
@@ -62,16 +62,13 @@ class SumOfSquaredDeviations extends FirstMoment {
}
/**
- * Create an instance with the given sum of
- * squared deviations and first moment.
+ * Create an instance with the given sum of squared deviations and first moment.
*
* @param sumSquaredDev Sum of squared deviations.
* @param m1 First moment.
- * @param n Number of values.
- * @param nonFiniteValue Running sum of values seen so far.
*/
- SumOfSquaredDeviations(double sumSquaredDev, double m1, long n, double nonFiniteValue) {
- super(m1, n, nonFiniteValue);
+ private SumOfSquaredDeviations(double sumSquaredDev, FirstMoment m1) {
+ super(m1);
this.sumSquaredDev = sumSquaredDev;
}
@@ -97,14 +94,15 @@ class SumOfSquaredDeviations extends FirstMoment {
// "Corrected two-pass algorithm"
// See: Chan et al (1983) Equation 1.7
- final double m1 = FirstMoment.of(values).getFirstMoment();
- if (!Double.isFinite(m1)) {
- return new SumOfSquaredDeviations(Math.abs(m1), m1, values.length, m1);
+ final FirstMoment m1 = FirstMoment.of(values);
+ final double xbar = m1.getFirstMoment();
+ if (!Double.isFinite(xbar)) {
+ return new SumOfSquaredDeviations(Math.abs(xbar), m1);
}
double s = 0;
double ss = 0;
for (final double x : values) {
- final double dx = x - m1;
+ final double dx = x - xbar;
s += dx;
ss += dx * dx;
}
@@ -117,7 +115,7 @@ class SumOfSquaredDeviations extends FirstMoment {
final double sumSquaredDev = ss == Double.POSITIVE_INFINITY ?
Double.POSITIVE_INFINITY :
ss - (s * s / n);
- return new SumOfSquaredDeviations(sumSquaredDev, m1, n, s + (m1 * n));
+ return new SumOfSquaredDeviations(sumSquaredDev, m1);
}
/**