3VVM commited on
Commit
a9150e7
·
1 Parent(s): aa4554f

Trim TimesFM backcast from forecasts

Browse files
Files changed (1) hide show
  1. models/timesfm_model.py +6 -3
models/timesfm_model.py CHANGED
@@ -81,7 +81,10 @@ class TimesFMModel(BaseForecastModel):
81
  model = self._ensure_compiled_for(len(context))
82
  if features is None:
83
  point_forecast, _quantiles = model.forecast(horizon=horizon, inputs=[context])
84
- point_values = np.asarray(point_forecast[0], dtype=float)
 
 
 
85
  else:
86
  feat_context = features.iloc[-len(context):].reset_index(drop=True)
87
  validate_features(feat_context, expected_len=len(context), model_name='TimesFM')
@@ -95,7 +98,7 @@ class TimesFMModel(BaseForecastModel):
95
  dynamic_numerical_covariates[col] = [full_array]
96
  try:
97
  point_forecast, _quantiles = model.forecast_with_covariates(inputs=[context[1:]], dynamic_numerical_covariates=dynamic_numerical_covariates, xreg_mode='xreg + timesfm')
98
- point_values = np.asarray(point_forecast[0], dtype=float)
99
  except (ImportError, AttributeError) as e:
100
  raise ImportError(f"TimesFM's covariate forecasting needs the 'xreg' extra (scikit-learn + JAX). Install with:\n pip install timesfm[torch,xreg]\n(original error: {e})")
101
  if point_values.shape != (horizon,):
@@ -103,5 +106,5 @@ class TimesFMModel(BaseForecastModel):
103
  out = point_values.tolist()
104
  if not all(np.isfinite(x) and x > 0 for x in out):
105
  raise ValueError('TimesFM returned a non-finite forecast (NaN/inf) for this window. This is usually a sign the input window is degenerate (e.g. a flat or near-constant price run) — try a different window size or symbol.')
106
- self.last_diagnostics = {'checkpoint': self.checkpoint, 'context_candles': len(context), 'feature_policy': 'lagged xreg; unknown future covariates persist', 'fallback': False}
107
  return out
 
81
  model = self._ensure_compiled_for(len(context))
82
  if features is None:
83
  point_forecast, _quantiles = model.forecast(horizon=horizon, inputs=[context])
84
+ # TimesFM 2.5 returns the requested forecast after an optional
85
+ # backcast when ``return_backcast`` is enabled. The backcast is
86
+ # useful to XReg but is not part of this model's forecast API.
87
+ point_values = np.asarray(point_forecast[0], dtype=float)[-horizon:]
88
  else:
89
  feat_context = features.iloc[-len(context):].reset_index(drop=True)
90
  validate_features(feat_context, expected_len=len(context), model_name='TimesFM')
 
98
  dynamic_numerical_covariates[col] = [full_array]
99
  try:
100
  point_forecast, _quantiles = model.forecast_with_covariates(inputs=[context[1:]], dynamic_numerical_covariates=dynamic_numerical_covariates, xreg_mode='xreg + timesfm')
101
+ point_values = np.asarray(point_forecast[0], dtype=float)[-horizon:]
102
  except (ImportError, AttributeError) as e:
103
  raise ImportError(f"TimesFM's covariate forecasting needs the 'xreg' extra (scikit-learn + JAX). Install with:\n pip install timesfm[torch,xreg]\n(original error: {e})")
104
  if point_values.shape != (horizon,):
 
106
  out = point_values.tolist()
107
  if not all(np.isfinite(x) and x > 0 for x in out):
108
  raise ValueError('TimesFM returned a non-finite forecast (NaN/inf) for this window. This is usually a sign the input window is degenerate (e.g. a flat or near-constant price run) — try a different window size or symbol.')
109
+ self.last_diagnostics = {'checkpoint': self.checkpoint, 'context_candles': len(context), 'feature_policy': 'lagged xreg; unknown future covariates persist', 'backcast_trimmed': True, 'fallback': False}
110
  return out