TSFit

class tsbootstrap.tsfit.TSFit(order: Integral | List[Integral] | tuple[Integral, Integral, Integral] | tuple[Integral, Integral, Integral, Integral], model_type: Literal['ar', 'arima', 'sarima', 'var', 'arch'], **kwargs)[source]

Performs fitting for various time series models including ‘ar’, ‘arima’, ‘sarima’, ‘var’, and ‘arch’.

rescale_factors

Rescaling factors for the input data and exogenous variables.

Type:

dict

model

The fitted model.

Type:

Union[AutoRegResultsWrapper, ARIMAResultsWrapper, SARIMAXResultsWrapper, VARResultsWrapper, ARCHModelResult]

fit(X, y=None)[source]

Fit the chosen model to the data.

get_coefs()[source]

Return the coefficients of the fitted model.

get_intercepts()[source]

Return the intercepts of the fitted model.

get_residuals()[source]

Return the residuals of the fitted model.

get_fitted_X()[source]

Return the fitted values of the model.

get_order()[source]

Return the order of the fitted model.

predict(X, n_steps=1)[source]

Predict future values using the fitted model.

score(X, y_true)[source]

Compute the R-squared score for the fitted model.

Raises:

ValueError – If the model type or the model order is invalid.

Notes

The following table shows the valid model types and their corresponding orders.

Model

Valid orders

Invalid orders

‘ar’

int

list, tuple

‘arima’

tuple of length 3

int, list, tuple

‘sarima’| tuple of length 4

int, list, tuple

‘var’

int

list, tuple

‘arch’

int

list, tuple

Examples

>>> from tsbootstrap import TSFit
>>> import numpy as np
>>> X = np.random.normal(size=(100, 1))
>>> fit_obj = TSFit(order=2, model_type='ar')  
>>> fit_obj.fit(X)  
TSFit(order=2, model_type='ar')
>>> fit_obj.get_coefs()  
array([[ 0.003, -0.002]])
>>> fit_obj.get_intercepts()  
array([0.001])
>>> fit_obj.get_residuals()  
array([[ 0.001],
          [-0.002],
            [-0.002],
                [-0.002],
                    [-0.002], ...
>>> fit_obj.get_fitted_X()  
array([[ 0.001],
            [-0.002],
                [-0.002],
                    [-0.002],
                        [-0.002], ...
>>> fit_obj.get_order()  
2
>>> fit_obj.predict(X, n_steps=5)  
array([[ 0.001],
            [-0.002],
                [-0.002],
                    [-0.002],
                        [-0.002], ...
>>> fit_obj.score(X, X)  
0.999
fit(X: ndarray, y=None) TSFit[source]

Fit the chosen model to the data.

Parameters:
  • X (np.ndarray) – Input data of shape (n_timepoints, n_features).

  • y (np.ndarray, optional) – Exogenous variables, by default None.

Returns:

The fitted TSFit object.

Return type:

TSFit

Raises:
  • ValueError – If the model type or the model order is invalid.

  • RuntimeError – If the maximum number of iterations is reached before the variance is within the desired range.

get_coefs() ndarray[source]

Return the coefficients of the fitted model.

Returns:

The coefficients of the fitted model.

Return type:

np.ndarray

Raises:

NotFittedError – If the model is not fitted.

Notes

The shape of the coefficients depends on the model type.

Model

Coefficient shape

‘ar’

(1, order)

‘arima’

(1, order)

‘sarima’| (1, order)

‘var’

(n_features, n_features, order)

‘arch’

(1, order)

get_fitted_X() ndarray[source]

Return the fitted values of the model.

Returns:

The fitted values of the model.

Return type:

np.ndarray

Raises:

NotFittedError – If the model is not fitted.

Notes

The shape of the fitted values depends on the model type.

Model

Fitted values shape

‘ar’

(n, 1)

‘arima’

(n, 1)

‘sarima’| (n, 1)

‘var’

(n, k)

‘arch’

(n, 1)

get_intercepts() ndarray[source]

Return the intercepts of the fitted model.

Returns:

The intercepts of the fitted model.

Return type:

np.ndarray

Raises:

NotFittedError – If the model is not fitted.

Notes

The shape of the intercepts depends on the model type.

Model

Intercept shape

‘ar’

(1, trend_terms)

‘arima’

(1, trend_terms)

‘sarima’| (1, trend_terms)

‘var’

(n_features, trend_terms)

‘arch’

(0,)

get_order() Integral | List[Integral] | tuple[Integral, Integral, Integral] | tuple[Integral, Integral, Integral, Integral][source]

Return the order of the fitted model.

Returns:

The order of the fitted model.

Return type:

OrderTypesWithoutNone

Raises:

NotFittedError – If the model is not fitted.

Notes

The shape of the order depends on the model type.

Model

Order shape

‘ar’

int

‘arima’

tuple of length 3

‘sarima’| tuple of length 4

‘var’

int

‘arch’

int

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:

deep (bool, optional) – When set to True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

Parameter names mapped to their values.

Return type:

dict

get_residuals() ndarray[source]

Return the residuals of the fitted model.

Returns:

The residuals of the fitted model.

Return type:

np.ndarray

Raises:

NotFittedError – If the model is not fitted.

Notes

The shape of the residuals depends on the model type.

Model

Residual shape

‘ar’

(n, 1)

‘arima’

(n, 1)

‘sarima’| (n, 1)

‘var’

(n, k)

‘arch’

(n, 1)

property model_type: str

The type of the model.

property order: Integral | List[Integral] | tuple[Integral, Integral, Integral] | tuple[Integral, Integral, Integral, Integral]

The order of the model.

predict(X: ndarray, y=None, n_steps: int = 1) ndarray[source]

Predict time series values using the fitted model.

Parameters:
  • X (np.ndarray) – Input data of shape (n_timepoints, n_features).

  • y (np.ndarray, optional) – Exogenous variables, by default None.

  • n_steps (int, optional) – Number of steps to forecast, by default 1.

Returns:

Predicted values.

Return type:

np.ndarray

Raises:

RuntimeError – If the model is not fitted.

score(X: ndarray, y_true: ndarray) float[source]

Compute the R-squared score for the fitted model.

Parameters:
  • X (np.ndarray) – The input data.

  • y_true (np.ndarray) – The true values.

Returns:

The R-squared score.

Return type:

float

Raises:
  • NotFittedError – If the model is not fitted.

  • ValueError – If the number of lags is greater than the length of the input data.

set_params(**params)[source]

Set the parameters of this estimator.

Parameters:

**params – Estimator parameters.

set_predict_request(*, n_steps: bool | None | str = '$UNCHANGED$') TSFit

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

n_steps (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for n_steps parameter in predict.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, y_true: bool | None | str = '$UNCHANGED$') TSFit

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

y_true (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for y_true parameter in score.

Returns:

self – The updated object.

Return type:

object

class tsbootstrap.tsfit.TSFitBestLag(model_type: str, max_lag: int = 10, order: Integral | List[Integral] | tuple[Integral, Integral, Integral] | tuple[Integral, Integral, Integral, Integral] | None = None, save_models=False, **kwargs)[source]

A class used to fit time series data and find the best lag for forecasting.

rank_lagger

An instance of the RankLags class.

Type:

RankLags

ts_fit

An instance of the TSFit class.

Type:

TSFit

model

The fitted time series model.

Type:

Union[AutoRegResultsWrapper, ARIMAResultsWrapper, SARIMAXResultsWrapper, VARResultsWrapper, ARCHModelResult]

rescale_factors

The rescaling factors used for the input data and exogenous variables.

Type:

Dict[str, Union[float, List[float] | None]]

fit(X, y=None)[source]

Fit the time series model to the data.

get_coefs()[source]

Return the coefficients of the fitted model.

get_intercepts()

Return the intercepts of the fitted model.

get_residuals()[source]

Return the residuals of the fitted model.

get_fitted_X()[source]

Return the fitted values of the model.

get_order()[source]

Return the order of the fitted model.

get_model()[source]

Return the fitted time series model.

predict(X, n_steps=1)[source]

Predict future values using the fitted model.

score(X, y_true)[source]

Compute the R-squared score for the fitted model.

fit(X: ndarray, y=None)[source]

Fit the time series model to the data.

Parameters:
  • X (np.ndarray) – The input data.

  • y (np.ndarray, optional, default=None) – Exogenous variables to include in the model.

Returns:

The fitted model.

Return type:

self

get_coefs() ndarray[source]

Return the coefficients of the fitted model.

Returns:

The coefficients of the fitted model.

Return type:

np.ndarray

get_fitted_X() ndarray[source]

Return the fitted values of the model.

Returns:

The fitted values of the model.

Return type:

np.ndarray

get_model()[source]

Return the fitted time series model.

Returns:

The fitted time series model.

Return type:

Union[AutoRegResultsWrapper, ARIMAResultsWrapper, SARIMAXResultsWrapper, VARResultsWrapper, ARCHModelResult]

Raises:

ValueError – If models were not saved during initialization.

get_order() Integral | List[Integral] | tuple[Integral, Integral, Integral] | tuple[Integral, Integral, Integral, Integral][source]

Return the order of the fitted model.

Returns:

The order of the fitted model.

Return type:

int, List[int], Tuple[int, int, int], Tuple[int, int, int, int]

get_residuals() ndarray[source]

Return the residuals of the fitted model.

Returns:

The residuals of the fitted model.

Return type:

np.ndarray

predict(X: ndarray, n_steps: int = 1)[source]

Predict future values using the fitted model.

Parameters:
  • X (np.ndarray) – The input data.

  • n_steps (int, optional, default=1) – The number of steps to predict.

Returns:

The predicted values.

Return type:

np.ndarray

score(X: ndarray, y_true: ndarray)[source]

Compute the R-squared score for the fitted model.

Parameters:
  • X (np.ndarray) – The input data.

  • y_true (np.ndarray) – The true values of the target variable.

Returns:

The R-squared score.

Return type:

float

set_predict_request(*, n_steps: bool | None | str = '$UNCHANGED$') TSFitBestLag

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

n_steps (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for n_steps parameter in predict.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, y_true: bool | None | str = '$UNCHANGED$') TSFitBestLag

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

y_true (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for y_true parameter in score.

Returns:

self – The updated object.

Return type:

object