fix(metrics): correct explained_variance residual-variance term#447
Open
teddytennant wants to merge 1 commit into
Open
fix(metrics): correct explained_variance residual-variance term#447teddytennant wants to merge 1 commit into
teddytennant wants to merge 1 commit into
Conversation
The residual-variance numerator in explained_variance subtracted the raw residual mean from a sum of squared residuals, mixing units of y with units of y^2. Explained variance is 1 - Var(y_true - y_pred) / Var(y_true), so the numerator must be the sum of squared deviations of the residuals from their mean. For y_true=[0,0.1,0.2,0.3,0.4], y_pred=[0.1,0.3,0.2,0.5,0.7] the function returned 0.8; the correct explained variance is 0.48, matching sklearn.metrics.explained_variance_score. Update the test accordingly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
SingleTargetRegression::explained_variancecomputed the residual-variance numerator asdiff.mapv_into(|x| x * x).sum() - mean_error, i.e.Σ(diffᵢ²) − mean(diff). That subtracts the raw residual mean (units of y) from a sum of squared residuals (units of y²), which is dimensionally inconsistent.Explained variance is defined as:
so the numerator must be the sum of squared deviations of the residuals from their mean,
Σ(diffᵢ − mean_error)².mean_error = diff.mean()was already computed but misused. This PR uses it correctly. Numerator and denominator are both sums over the same n samples, so the biased-variance1/nfactors cancel and the existing+ 1e-10denominator guard is preserved.Why
For
y_true = [0, 0.1, 0.2, 0.3, 0.4],y_pred = [0.1, 0.3, 0.2, 0.5, 0.7]:explained_variancer2explained_variancesklearn.metrics.explained_variance_scoreThe old result of 0.8 corresponded to no standard metric. The corrected value matches scikit-learn's reference implementation.
Testing
test_explained_variance_for_single_targetsto assert the corrected value0.48(was0.8), with a comment citing the scikit-learn reference.test_same(explained_variance(ones, ones) == 1.0) still passes:diff == 0⇒ numerator0⇒1 − 0/1e-10 = 1.0.cargo test -p linfa explained_varianceand the fullcargo test -p linfa --lib metrics_regressionsuite (16 tests) pass.cargo fmt --checkandcargo clippy -p linfa --lib --tests -- -D warningsare clean.