Docs /Templates/ sinapsis-darts-forecasting

KalmanForecasterWrapper

sinapsis-darts-forecastingDartsdartsdynamicforecastingmodels

Python template · sinapsis_darts_forecasting.templates.time_series_model

Overview

Base Template for Darts Time Series forecasting models.

KalmanForecaster — inherited documentation

The base class for transferable future covariates "local" forecasting models, handling single uni- or multivariate target and optional future covariates time series. Additionally, at prediction time, it can be applied to new data unrelated to the original series used for fitting the model.

Transferable Future Covariates Local Forecasting Models (TFC-LFM) are models that can be trained on a single uni- or multivariate target and optional future covariates series. Additionally, at prediction time, it can be applied to new data unrelated to the original series used for fitting the model. Currently in Darts, all models in this category wrap to statsmodel models such as VARIMA. TFC-LFMs usually train on the entire target and future covariates series supplied when calling :func:`fit()` at once. They can also predict in one go with :func:`predict()` for any number of predictions `n` after the end of the training series. When using future covariates, the values for the future `n` prediction steps must be given in the covariate series.

All implementations must implement the :func:`_fit()` and :func:`_predict()` methods.Kalman filter Forecaster

This model uses a Kalman filter to produce forecasts. It uses a :class:`darts.models.filtering.kalman_filter.KalmanFilter` object and treats future values as missing values.

The model can optionally receive a :class:`nfoursid.kalman.Kalman` object specifying the Kalman filter, or, if not specified, the filter will be trained using the N4SID system identification algorithm.

Parameters ---------- dim_x : int Size of the Kalman filter state vector. kf : nfoursid.kalman.Kalman Optionally, an instance of `nfoursid.kalman.Kalman`. If this is provided, the parameter dim_x is ignored. This instance will be copied for every call to `predict()`, so the state is not carried over from one time series to another across several calls to `predict()`. The various dimensionalities of the filter must match those of the `TimeSeries` used when calling `predict()`. If this is specified, it is still necessary to call `fit()` before calling `predict()`, although this will have no effect on the Kalman filter. add_encoders A large number of future covariates can be automatically generated with `add_encoders`. This can be done by adding multiple pre-defined index encoders and/or custom user-made functions that will be used as index encoders. Additionally, a transformer such as Darts' :class:`Scaler` can be added to transform the generated covariates. This happens all under one hood and only needs to be specified at model creation. Read :meth:`SequentialEncoder <darts.dataprocessing.encoders.SequentialEncoder>` to find out more about ``add_encoders``. Default: ``None``. An example showing some of ``add_encoders`` features:

.. highlight:: python .. code-block:: python

def encode_year(idx): return (idx.year - 1950) / 50

add_encoders={ 'cyclic': {'future': ['month']}, 'datetime_attribute': {'future': ['hour', 'dayofweek']}, 'position': {'future': ['relative']}, 'custom': {'future': [encode_year]}, 'transformer': Scaler(), 'tz': 'CET' } .. random_state Controls the randomness for reproducible forecasting.

Examples -------- >>> from darts.datasets import AirPassengersDataset >>> from darts.models import KalmanForecaster >>> from darts.utils.timeseries_generation import datetime_attribute_timeseries >>> series = AirPassengersDataset().load() >>> # optionally, use some future covariates; e.g. the value of the month encoded as a sine and cosine series >>> future_cov = datetime_attribute_timeseries(series, "month", cyclic=True, add_length=6) >>> # increasing the size of the state vector >>> model = KalmanForecaster(dim_x=12) >>> model.fit(series, future_covariates=future_cov) >>> pred = model.predict(6, future_covariates=future_cov) >>> print(pred.values()) [[474.40680728] [440.51801726] [461.94512461] [494.42090089] [528.6436328 ] [590.30647185]]

.. note:: `Kalman example notebook <https://unit8co.github.io/darts/examples/10-Kalman-filter-examples.html>`__ presents techniques that can be used to improve the forecasts quality compared to this simple usage example.

Capabilities

Capabilities: not set

Installation

Install the published package using pip or uv.

Terminalbash
pip install sinapsis-darts-forecasting==1.0.0
uv pip install sinapsis-darts-forecasting==1.0.0
uv add sinapsis-darts-forecasting==1.0.0

Configuration

Effective fields from the inspector schema, including nested Pydantic model fields.

metadata
sinapsis_core.template_base.base_models.TemplateMetadata | None
Optional
Default: null
validation_mode
bool
Optional
Default: false
forecast_horizon
int
Optional
Default: 10
kalmanforecaster_initRequired
__init__Attributes
Required
Default: null
kf
typing.Optional[nfoursid.kalman.Kalman]
Optional
Default: null
dim_x
int
Optional
Default: 1
add_encoders
typing.Optional[dict]
Optional
Default: null
random_state
typing.Optional[int]
Optional
Default: null

Configuration example

Generated from documented defaults. Replace any placeholders and complete required nested settings before running.

yaml
templates:
  - template_name: "KalmanForecasterWrapper"
    class_name: "KalmanForecasterWrapper"
    attributes:
      validation_mode: false
      forecast_horizon: 10
      kalmanforecaster_init:
        kf: null
        dim_x: 1
        add_encoders: null
        random_state: null
View original docstring example

The source example references XGBModelWrapper, not KalmanForecasterWrapper.

yaml
agent:
    name: my_test_agent
templates:
- template_name: InputTemplate
  class_name: InputTemplate
  attributes: {}
- template_name: XGBModelWrapper
  class_name: XGBModelWrapper
  template_input: InputTemplate
  attributes:
    forecast_horizon: 100
    xgbmodel_init:
        lags: 30
        lags_past_covariates: 30
        output_chunk_length: 100
        random_state: 42
        n_estimators: 200
        learning_rate: 0.1
        max_depth: 6
For a full list of options use the sinapsis cli: sinapsis info --all-template-names.
If you want to see all available models, please visit:
https://unit8co.github.io/darts/generated_api/darts.models.forecasting.html

Inheritance

Immediate parent: sinapsis_darts_forecasting.templates.time_series_model.TimeSeriesModel

Environment variables

Environment definitions belong to their canonical Python modules.

SINAPSIS_BUILD_DOCS · defined elsewhere