MedianForecaster#

class openstef_models.models.forecasting.median_forecaster.MedianForecaster(**data: Any) None[source]

Bases: Forecaster, ExplainableForecaster, ContributionsMixin

Median forecaster using lag features for predictions.

This forecaster predicts the median value based on specified lag features. It is particularly useful for signals with slow dynamics or state-switching behavior.

Hyperparameters:
lags: List of time deltas representing the lag features to use for prediction.

These should be aligned with the data sampling frequency.

context_window: Time delta representing the context window size for input data.

This defines how much historical data is considered for making predictions.

Parameters:

data (Any)

HyperParams

alias of MedianForecasterHyperParams

quantiles: list[Quantile]
horizons: list[LeadTime]
hyperparams: MedianForecasterHyperParams
property hparams: MedianForecasterHyperParams

Model hyperparameters for training and prediction.

Concrete forecasters implement this by returning their narrowed hyperparams field, giving callers a polymorphic read-only view while each subclass keeps full type safety on its own field.

property is_fitted: bool

Check if the predictor has been fitted.

property feature_importances: DataFrame

Get feature importance scores for this model.

Returns DataFrame with feature names as index and quantiles as columns. Each quantile represents the importance distribution across multiple model training runs or folds.

Returns:

DataFrame with feature names as index and quantile columns. Values represent normalized importance scores summing to 1.0.

Note

The returned DataFrame must have feature names as index and quantile columns in format ‘quantile_PXX’ (e.g., ‘quantile_P50’, ‘quantile_P95’). All quantile values must be between 0 and 1.

property frequency: timedelta

Retrieve the model input frequency.

Returns:

The frequency of the model input.

predict(data: ForecastInputDataset) ForecastDataset[source]

Predict the median of the lag features for each time step in the context window.

Parameters:
  • data (ForecastInputDataset) – The input data for prediction, this should be a pandas dataframe with lag features.

  • data

Returns:

ForecastDataset with predicted median for each time step. If any lag feature is NaN, this will be ignored. If all lag features are NaN, the regressor will return NaN.

Raises:
  • AttributeError – If the model is not fitted yet.

  • ValueError – If the input data is missing any of the required lag features.

Return type:

ForecastDataset

fit(data: ForecastInputDataset, data_val: ForecastInputDataset | None = None) None[source]

Fit the median forecaster.

This regressor does not need any fitting, but it does need to know the feature names of the lag features and the order of these.

Lag features are expected to be evenly spaced and match the frequency of the input data. The lag features are expected to be named in the format T-<lag_in_minutes> or T-<lag_in_days>d. For example, T-1min, T-2min, T-3min or T-1d, T-2d.

Which lag features are used is determined by the feature engineering step.

Parameters:
Raises:
  • ValueError – If the input data frequency does not match the model frequency.

  • ValueError – If no lag features are found in the input data.

Return type:

None

predict_contributions(data: ForecastInputDataset) TimeSeriesDataset[source]

Return uniform contributions since MedianForecaster weights all lag features equally.

Parameters:

data (ForecastInputDataset)

Return type:

TimeSeriesDataset

model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'ignore', 'protected_namespaces': ()}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(context: Any, /) None

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) – The BaseModel instance.

  • context (Any) – The context.

  • self

  • context

Return type:

None