Repository navigation
fix(timesfm_2p5): align timesfm + xreg residuals with the backcast - #550
Open
rudycelekli wants to merge 1 commit into
Open
rudycelekli wants to merge 1 commit into
rudycelekli wants to merge 1 commit into
Conversation
forecast_with_covariates(xreg_mode="timesfm + xreg") fits the linear model on input minus backcast. The backcast only covers the last max_context - input_patch_len points, but train_len was computed from the full input length, so any context longer than max_context failed with "operands could not be broadcast together with shapes (168,) (96,)". An input no longer than one patch got train_len 0, and x[-0:] selects the whole array, which failed the same way. Cap train_len at what the model sees, slice with explicit start indices, keep normalize() from producing NaN stats for an empty target, and raise a clear error when no input in the batch has anything to fit on.
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's wrong
TimesFM_2p5.forecast_with_covariates(..., xreg_mode="timesfm + xreg")fits the covariate regression oninput - backcast. The backcast only covers the lastmax_context - input_patch_lenpoints of the context.train_len, though, is computed from the full input length. Two cases crash:Context longer than
max_context. Withmax_context=128,return_backcast=True, a 200-point input and a covariate of length 216:Before:
ValueError: operands could not be broadcast together with shapes (168,) (96,). Here 168 = 200 - 32 is the train length, and 96 = 128 - 32 is the backcast length.After: returns point and quantile forecasts of shape
(1, 16), all finite.Input no longer than one patch (32 points). This input gets
train_len = 0, andx[-0:]selects the whole array, so a 20-point input fails withshapes (20,) (96,). The failure happens even when other inputs in the batch are long. If the slice is fixed but nothing else changes,normalize()computes the mean and std of an empty target. That makes the stats NaN, so the forecast for that series comes out all NaN.xreg_mode="xreg + timesfm"is not affected.Fix
max(0, min(input_len, max_context) - p).train_len = 0gives an empty target instead of the whole array.xreg_lib.normalize()returns identity stats(0, 1)for an empty series. That series then contributes no rows to the batched fit and still gets the covariate term from the shared coefficients.ValueErrorthat names the input patch length instead of a broadcast error.Testing
tests/test_forecast_with_covariates.py(new) uses a randomly initialized 2.5 torch model withmax_context=128. It skips viapytest.importorskipwhen jax or sklearn is not installed. It checks output shapes and finiteness for these contexts:[200], with and without target normalization[128](control)[100, 20], with and without target normalizationIt also checks that
[20]alone raises the new error.[128]control passes before and after.normalize()change reverted:[100, 20]with normalization fails on NaN output.tests/(excludingtest_model_loading.py, which needseinshape, not installed here) passes: 65 tests.