Skip to content

fix NaN sums after SimpleRegression.removeData empties the model - #360

Open
insaf021 wants to merge 1 commit into
apache:masterfrom
insaf021:simpleregression-remove-last-point
Open

insaf021 wants to merge 1 commit into
apache:masterfrom
insaf021:simpleregression-remove-last-point

Conversation

@insaf021

Copy link
Copy Markdown
Contributor

Reproduced on master, JDK 21:

SimpleRegression r = new SimpleRegression();
r.addData(1, 1);
r.removeData(1, 1);                              // n == 0
r.addData(0, 0); r.addData(1, 2); r.addData(2, 4);
r.getSlope();                                    // NaN, expected 2.0

removeData divides by n - 1, so removing the last observation sets sumXX, sumYY and sumXY to NaN (0 * Infinity) when the model has an intercept, which is the default. addData only resets xbar and ybar at n == 0, so the NaN sums survive and every later fit on the instance is NaN until clear(). The documented sliding-window use hits this whenever the window drains, removeData(double[][]) included, and append of an emptied instance turns a healthy fit's slope into NaN too.

Removing the last observation now clears the model, which is the state an empty model is in anyway. Removals that leave at least one observation are unchanged. The UnnecessaryParentheses suppression for regress() in checkstyle-suppressions-legacy.xml is pinned by line number, so it moves from 823 to 827.

testRemoveToEmptyThenReuse in SimpleRegressionTest fails on master (expected:<0.0> but was:<NaN>) and passes with the fix. mvn clean verify javadoc:javadoc -pl commons-math-legacy is green on JDK 21 (3266 tests, 0 Checkstyle violations; the only reruns are the randomised optimizer tests).

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.

Move the line-number pin of the existing UnnecessaryParentheses
suppression for regress() from 823 to 827.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.24%. Comparing base (f554608) to head (040c51e).
⚠️ Report is 423 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #360      +/-   ##
============================================
+ Coverage     86.54%   87.24%   +0.70%     
+ Complexity     9787       91    -9696     
============================================
  Files           532      499      -33     
  Lines         35516    33544    -1972     
  Branches       6194     5857     -337     
============================================
- Hits          30738    29267    -1471     
+ Misses         3518     3160     -358     
+ Partials       1260     1117     -143     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants