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-math.git


The following commit(s) were added to refs/heads/master by this push:
     new 273cc5d02 Fix NaN sums after SimpleRegression.removeData empties the 
model (#360)
273cc5d02 is described below

commit 273cc5d029d4ee9b58288eaa8044792001a22829
Author: insaf021 <[email protected]>
AuthorDate: Sun Nov 29 22:31:28 2026 +0530

    Fix NaN sums after SimpleRegression.removeData empties the model (#360)
    
    removeData divides by n - 1, so removing the last observation set
    sumXX, sumYY and sumXY to NaN when the model has an intercept. addData
    only resets xbar and ybar at n == 0, so every later fit on the instance
    returned NaN until clear(). Removing the last observation now clears the
    model.
---
 .../legacy/stat/regression/SimpleRegression.java   |  6 +++++-
 .../stat/regression/SimpleRegressionTest.java      | 25 ++++++++++++++++++++++
 .../checkstyle/checkstyle-suppressions-legacy.xml  |  2 +-
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git 
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/regression/SimpleRegression.java
 
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/regression/SimpleRegression.java
index 83aef56be..ac9c8a725 100644
--- 
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/regression/SimpleRegression.java
+++ 
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/regression/SimpleRegression.java
@@ -207,7 +207,11 @@ public class SimpleRegression implements 
UpdatingMultipleLinearRegression {
      * @param y dependent variable value
      */
     public void removeData(final double x,final double y) {
-        if (n > 0) {
+        if (n == 1) {
+            // Removing the last observation empties the model; the updating
+            // formulas below would divide by n - 1 = 0.
+            clear();
+        } else if (n > 0) {
             if (hasIntercept) {
                 final double fact1 = n - 1.0;
                 final double fact2 = n / (n - 1.0);
diff --git 
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/SimpleRegressionTest.java
 
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/SimpleRegressionTest.java
index 52752eb99..e941b7962 100644
--- 
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/SimpleRegressionTest.java
+++ 
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/SimpleRegressionTest.java
@@ -687,4 +687,29 @@ public final class SimpleRegressionTest {
         regression.removeData(removeMultiple);
         Assert.assertEquals(regression.getN(), 0);
     }
+
+    // Remove multiple observations to empty, then reuse the instance
+    @Test
+    public void testRemoveToEmptyThenReuse() {
+        final SimpleRegression expected = new SimpleRegression();
+        expected.addData(infData);
+
+        final SimpleRegression regression = new SimpleRegression();
+        regression.addData(removeMultiple);
+        regression.removeData(removeMultiple);
+        Assert.assertEquals(0, regression.getN());
+        Assert.assertEquals(0, regression.getSumOfCrossProducts(), 0);
+
+        // Appending the emptied instance must not change the target
+        final SimpleRegression appended = new SimpleRegression();
+        appended.addData(infData);
+        appended.append(regression);
+        Assert.assertEquals(expected.getSlope(), appended.getSlope(), 0);
+        Assert.assertEquals(expected.getIntercept(), appended.getIntercept(), 
0);
+
+        regression.addData(infData);
+        Assert.assertEquals(expected.getSlope(), regression.getSlope(), 0);
+        Assert.assertEquals(expected.getIntercept(), 
regression.getIntercept(), 0);
+        Assert.assertEquals(expected.getRSquare(), regression.getRSquare(), 0);
+    }
 }
diff --git a/src/main/resources/checkstyle/checkstyle-suppressions-legacy.xml 
b/src/main/resources/checkstyle/checkstyle-suppressions-legacy.xml
index 3af66fb0c..2916e424d 100644
--- a/src/main/resources/checkstyle/checkstyle-suppressions-legacy.xml
+++ b/src/main/resources/checkstyle/checkstyle-suppressions-legacy.xml
@@ -46,7 +46,7 @@
   <suppress checks="UnnecessaryParentheses" 
files=".*[/\\]SimpleCurveFitter\.java$" lines="321-322" />
   <suppress checks="UnnecessaryParentheses" 
files=".*[/\\]LevenbergMarquardtOptimizer\.java$" lines="525,739" />
   <suppress checks="UnnecessaryParentheses" 
files=".*[/\\]EnumeratedDistribution\.java$" lines="128-129" />
-  <suppress checks="UnnecessaryParentheses" 
files=".*[/\\]SimpleRegression\.java$" lines="823" />
+  <suppress checks="UnnecessaryParentheses" 
files=".*[/\\]SimpleRegression\.java$" lines="827" />
   <suppress checks="UnnecessaryParentheses" 
files=".*[/\\]MillerUpdatingRegression\.java$" lines="172-173" />
   <suppress checks="UnnecessaryParentheses" 
files=".*[/\\]GraggBulirschStoerIntegrator\.java$" lines="638-639,679,767" />
   <suppress checks="UnnecessaryParentheses" 
files=".*[/\\]MultiDirectionalTransform\.java$" lines="94" />

Reply via email to