MODULE REFERENCE
midas
anomx.data.processing.transformers.midasMixed-data sampling (MIDAS) Transformer
MIDAS
MIDAS(low_freq: str, strip: bool = True, drop_static_covariates: bool = False, name: str = 'MIDAS', n_jobs: int = 1, verbose: bool = False)Bases: FittableDataTransformer, InvertibleDataTransformer
| Parameter | Type | Default |
|---|---|---|
low_freq | str | required |
strip | bool | True |
drop_static_covariates | bool | False |
name | str | 'MIDAS' |
n_jobs | int | 1 |
verbose | bool | False |
Mixed-data sampling transformer.
A transformer that converts higher frequency time series to lower frequency using mixed-data sampling; see [1]_ for further details. This allows higher frequency covariates to be used whilst forecasting a lower frequency target series. For example, using monthly inputs to forecast a quarterly target.
Notes
The high input frequency should always relate in the same rate to the low target frequency. For example, there's always three months in quarter. However, the number of days in a month varies per month. In the latter case a MIDAS transformation does not work and the transformer will raise an error.
Parameters
- low_freq
The pd.DateOffset string alias corresponding to the target low frequency [2]_. Passed on to the
ruleparameter of pandas.DataFrame.resample().- strip
Whether to remove the NaNs from the start and the end of the transformed series.
- drop_static_covariates
If set to
True, the statics covariates of the input series won't be transferred to the output. This might be useful for multivariate series with component-specific static covariates.- name
A specific name for the scaler
- n_jobs
The number of jobs to run in parallel. Parallel jobs are created only when a
Sequence[TimeSeries]is passed as input to a method, parallelizing operations regarding differentTimeSeries. Defaults to1(sequential). Setting the parameter to-1means using all the available processors. Note: for a small amount of data, the parallelization overhead could end up increasing the total required amount of time.- verbose
Optionally, whether to print operations progress
Examples
>>> import pandas as pd
>>> from anomx.data.timeseries import TimeSeries
>>> from anomx.data.processing.transformers import MIDAS
>>> monthly_series = TimeSeries.from_times_and_values(
... pd.date_range("1949-01-01", periods=9, freq="MS", name="Month"),
... [112., 118., 132., 129., 121., 135., 148., 148., 136.],
... )
>>> print(monthly_series.time_index[:4])
DatetimeIndex(['1949-01-01', '1949-02-01', '1949-03-01', '1949-04-01'], dtype='datetime64[ns]', name='Month', freq='MS')
>>> print(monthly_series.values()[:4])
[[112.]
[118.]
[132.]
[129.]]
>>> midas = MIDAS(low_freq="QS")
>>> quarterly_series = midas.fit_transform(monthly_series)
>>> print(quarterly_series.time_index[:3])
DatetimeIndex(['1949-01-01', '1949-04-01', '1949-07-01'], dtype='datetime64[ns]', name='Month', freq='QS-JAN')
>>> print(quarterly_series.values()[:3])
[[112. 118. 132.]
[129. 121. 135.]
[148. 148. 136.]]
>>> inversed_quaterly = midas.inverse_transform(quarterly_series)
>>> print(inversed_quaterly.time_index[:4])
DatetimeIndex(['1949-01-01', '1949-02-01', '1949-03-01', '1949-04-01'], dtype='datetime64[ns]', name='Month', freq='MS')
>>> print(inversed_quaterly.values()[:4])
[[112.]
[118.]
[132.]
[129.]]References
.. [1] https://en.wikipedia.org/wiki/Mixed-data_sampling .. [2] https://pandas.pydata.org/docs/user_guide/timeseries.html#dateoffset-objects
Members
ts_fitstaticmethod
ts_fit(series: TimeSeriesLike, params: Mapping[str, Any], *args, **kwargs) -> dict[str, Any] | list[dict[str, Any]]MIDAS needs the high frequency period name in order to easily reverse_transform TimeSeries, the parallelization is handled by transform and/or inverse_transform (see InvertibleDataTransformer.__init__() docstring).
| Parameter | Type | Default |
|---|---|---|
series | TimeSeriesLike | required |
params | Mapping[str, Any] | required |
*args | — | — |
**kwargs | — | — |
Returns dict[str, Any] | list[dict[str, Any]]
ts_transformstaticmethod
ts_transform(series: TimeSeries, params: Mapping[str, Any]) -> TimeSeriesTransforms series from high to low frequency using a mixed-data sampling approach. Uses and relies on pandas.DataFrame.resample.
When converting to/from anchorable offset [1]_, the index is rolled backward if the series does not start on the anchor date to preserve all the values.
Steps: (1) Transform series to pd.DataFrame and get frequency string for PeriodIndex (2) Downsample series and then upsample it again (3) Replace input series by unsampled series if it's not 'full' (4) Transform every column of the high frequency series into multiple columns for the low frequency series (5) Transform the low frequency series back into a TimeSeries
References
.. [1] https://en.wikipedia.org/wiki/Mixed-data_sampling
| Parameter | Type | Default |
|---|---|---|
series | TimeSeries | required |
params | Mapping[str, Any] | required |
Returns TimeSeries
ts_inverse_transformstaticmethod
ts_inverse_transform(series: TimeSeries, params: Mapping[str, Any], insample: TimeSeries | None = None) -> TimeSeriesTransforms series back to high frequency by retrieving the original high frequency and reshaping the values.
When converting to/from anchorable offset [1]_, the index is rolled backward if the series does not start on the anchor date to preserve all the values.
Steps: (1) Reshape the values to flatten the components introduced by the transform (2) Eliminate the rows filled with NaNs, to facilitate time index adjustment (3) Retrieve the original components name (4) When applicable, shift the time index start back in time (5) Generate a new time index with the high frequency
References
.. [1] https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#anchored-offsets
| Parameter | Type | Default |
|---|---|---|
series | TimeSeries | required |
params | Mapping[str, Any] | required |
insample | TimeSeries | None | None |
Returns TimeSeries
Inherited members
fitFittableDataTransformertransformFittableDataTransformerfit_transformFittableDataTransformerset_verboseBaseDataTransformerset_n_jobsBaseDataTransformerapply_component_maskBaseDataTransformerunapply_component_maskBaseDataTransformerstack_samplesBaseDataTransformerunstack_samplesBaseDataTransformernameBaseDataTransformerinverse_transformInvertibleDataTransformer