Issue 51: Handle flat targets for GP fit#74
Merged
Conversation
Add robustness for near-constant targets so AutoGP fittings don't produce singular covariances. Introduces _stabilize_for_fit(y; flat_threshold=1e-3) which adds small Gaussian jitter when the transformed series' relative range is below the threshold, and uses it in make_and_fit_model to construct the GP with stabilized y. In transformations, detect pathological Box-Cox fits that collapse the transformed range (compare Box-Cox range to log range) and fall back to the positive (log) transformation with a warning. Add tests covering Box-Cox fallback, make_and_fit_model behavior on near-constant and exactly constant data, and unit tests for _stabilize_for_fit behavior. This prevents PosDefException during fitting/forecasting (issue #51) while preserving existing behavior for healthy data.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #51 where AutoGP fits raised PosDefException on near-constant target series (e.g., flat hospital admission counts where the Box-Cox MLE selects a pathological λ that collapses the transformed range to a constant, yielding a singular GP covariance). Two complementary safeguards are introduced, plus tests.
Changes:
- Add
_stabilize_for_fitinmake_and_fit_model.jlthat injects small Gaussian jitter when the (transformed) target's relative range falls belowflat_threshold(default1e-3), and wires it intomake_and_fit_modelvia a newflat_thresholdkwarg. - In
get_transformations("boxcox", ...), evaluate the fitted Box-Cox transform on the data and fall back to the"positive"(log) transformation with a@warnif the transformed range is non-finite or collapses to ≤ 1% of the log range. - Add regression tests for Box-Cox fallback, flat/exactly-constant data end-to-end fits + forecasts, and unit tests for
_stabilize_for_fit.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/make_and_fit_model.jl | New _stabilize_for_fit helper and flat_threshold kwarg; applies jitter to data.y before constructing AutoGP.GPModel. |
| src/transformations.jl | Detect degenerate Box-Cox fits and fall back to log transformation with warning; updated docstring. |
| test/test_helper_functions.jl | Test for Box-Cox fallback on near-constant data and that healthy data still uses Box-Cox. |
| test/test_model_fitting.jl | Tests for end-to-end fit+forecast on near-constant and exactly-constant data, and unit tests for _stabilize_for_fit. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #74 +/- ##
==========================================
+ Coverage 95.08% 95.68% +0.60%
==========================================
Files 5 5
Lines 122 139 +17
==========================================
+ Hits 116 133 +17
Misses 6 6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
This PR introduces two fixes for adding robustness for near-constant targets so
AutoGPfits don't produce singular covariances._stabilize_for_fit(y; flat_threshold=1e-3)which adds small Gaussian jitter when the transformed series' relative range is below the threshold, and uses it in make_and_fit_model to construct the GP with stabilized input data.Add tests covering Box-Cox fallback,
make_and_fit_modelbehavior on near-constant and exactly constant data, and unit tests for_stabilize_for_fitbehavior. This prevents PosDefException during fitting/forecasting (issue #51) while preserving existing behavior for healthy data.Pinging @damonbayer for awareness.