Skip to content

Issue 51: Handle flat targets for GP fit#74

Merged
SamuelBrand1 merged 1 commit into
mainfrom
51-Postive-def-fail
Jun 2, 2026
Merged

Issue 51: Handle flat targets for GP fit#74
SamuelBrand1 merged 1 commit into
mainfrom
51-Postive-def-fail

Conversation

@SamuelBrand1

Copy link
Copy Markdown
Collaborator

This PR introduces two fixes for adding robustness for near-constant targets so AutoGP fits don't produce singular covariances.

  1. _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.
  2. In transformations, detect pathological Box-Cox fits that collapse the transformed 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.

Pinging @damonbayer for awareness.

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.
@SamuelBrand1 SamuelBrand1 linked an issue Jun 2, 2026 that may be closed by this pull request
@SamuelBrand1 SamuelBrand1 requested a review from Copilot June 2, 2026 13:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_fit in make_and_fit_model.jl that injects small Gaussian jitter when the (transformed) target's relative range falls below flat_threshold (default 1e-3), and wires it into make_and_fit_model via a new flat_threshold kwarg.
  • In get_transformations("boxcox", ...), evaluate the fitted Box-Cox transform on the data and fall back to the "positive" (log) transformation with a @warn if 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-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.68%. Comparing base (c5549c4) to head (bd265f4).

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.
📢 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.

@SamuelBrand1 SamuelBrand1 merged commit 8e8a56a into main Jun 2, 2026
10 of 11 checks passed
@SamuelBrand1 SamuelBrand1 deleted the 51-Postive-def-fail branch June 2, 2026 13:51
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.

Local test_epiautogp_end_to_end.sh failure

3 participants