Bootstrap

class tsbootstrap.bootstrap.BlockDistributionBootstrap(n_bootstraps: Integral = 10, block_bootstrap: BlockBootstrap | None = None, distribution: str = 'normal', refit: bool = False, model_type: Literal['ar', 'arima', 'sarima', 'var'] = 'ar', model_params: dict | None = None, order=None, save_models: bool = False, rng: Generator | Integral | None = None)[source]

Block Distribution Bootstrap class for time series data.

This class applies distribution bootstrapping to blocks of the time series. The residuals are fit to a distribution, then resampled using the specified block structure. Then new residuals are generated from the fitted distribution and added to the fitted values to generate new samples.

Parameters:
  • block_bootstrap (BlockBootstrap, default=MovingBlockBootstrap()) – The block bootstrap algorithm.

  • n_bootstraps (Integral, default=10) – The number of bootstrap samples to create.

  • distribution (str, default='normal') – The distribution to use for generating the bootstrapped samples. Must be one of ‘poisson’, ‘exponential’, ‘normal’, ‘gamma’, ‘beta’, ‘lognormal’, ‘weibull’, ‘pareto’, ‘geometric’, or ‘uniform’.

  • refit (bool, default=False) – Whether to refit the distribution to the resampled residuals for each bootstrap. If False, the distribution is fit once to the residuals and the same distribution is used for all bootstraps.

  • model_type (str, default="ar") – The model type to use. Must be one of “ar”, “arima”, “sarima”, “var”, or “arch”.

  • model_params (dict, default=None) – Additional keyword arguments to pass to the TSFit model.

  • order (Integral or list or tuple, default=None) – The order of the model. If None, the best order is chosen via TSFitBestLag. If Integral, it is the lag order for AR, ARIMA, and SARIMA, and the lag order for ARCH. If list or tuple, the order is a tuple of (p, o, q) for ARIMA and (p, d, q, s) for SARIMAX. It is either a single Integral or a list of non-consecutive ints for AR, and an Integral for VAR and ARCH. If None, the best order is chosen via TSFitBestLag. Do note that TSFitBestLag only chooses the best lag, not the best order, so for the tuple values, it only chooses the best p, not the best (p, o, q) or (p, d, q, s). The rest of the values are set to 0.

  • save_models (bool, default=False) – Whether to save the fitted models.

  • rng (Integral or np.random.Generator, default=np.random.default_rng()) – The random number generator or seed used to generate the bootstrap samples.

resids_dist

The distribution object used to generate the bootstrapped samples. If None, the distribution has not been fit yet.

Type:

scipy.stats.rv_continuous or None

resids_dist_params

The parameters of the distribution used to generate the bootstrapped samples. If None, the distribution has not been fit yet.

Type:

tuple or None

__init__ : Initialize self.
_generate_samples_single_bootstrap : Generate a single bootstrap sample.

Notes

We either fit the distribution to the residuals once and generate new samples from the fitted distribution with a new random seed, or resample the residuals once and fit the distribution to the resampled residuals, then generate new samples from the fitted distribution with the same random seed n_bootstrap times.

classmethod get_test_params(parameter_set='default')[source]

Return testing parameter settings for the estimator.

Parameters:

parameter_set (str, default="default") – Name of the set of test parameters to return, for use in tests. If no special parameters are defined for a value, will return “default” set.

Returns:

params – Parameters to create testing instances of the class Each dict are parameters to construct an “interesting” test instance, i.e., MyClass(**params) or MyClass(**params[i]) creates a valid test instance. create_test_instance uses the first (or only) dictionary in params

Return type:

dict or list of dict, default = {}

class tsbootstrap.bootstrap.BlockMarkovBootstrap(n_bootstraps: Integral = 10, block_bootstrap: BlockBootstrap | None = None, method: Literal['first', 'middle', 'last', 'mean', 'mode', 'median', 'kmeans', 'kmedians', 'kmedoids'] = 'middle', apply_pca_flag: bool = False, pca=None, n_iter_hmm: Integral = 10, n_fits_hmm: Integral = 1, blocks_as_hidden_states_flag: bool = False, n_states: Integral = 2, model_type: Literal['ar', 'arima', 'sarima', 'var'] = 'ar', model_params: dict | None = None, order=None, save_models: bool = False, rng: Generator | Integral | None = None)[source]

Block Markov Bootstrap class for time series data.

This class applies Markov bootstrapping to blocks of the time series. The residuals are fit to a Markov model, then resampled using the specified block structure. The resampled residuals are added to the fitted values to generate new samples. This class is a combination of the BlockResidualBootstrap and WholeMarkovBootstrap classes.

Parameters:
  • block_bootstrap (BlockBootstrap, default=MovingBlockBootstrap()) – The block bootstrap algorithm.

  • n_bootstraps (Integral, default=10) – The number of bootstrap samples to create.

  • method (str, default="middle") – The method to use for compressing the blocks. Must be one of “first”, “middle”, “last”, “mean”, “mode”, “median”, “kmeans”, “kmedians”, “kmedoids”.

  • apply_pca_flag (bool, default=False) – Whether to apply PCA to the residuals before fitting the HMM.

  • pca (PCA, default=None) – The PCA object to use for applying PCA to the residuals.

  • n_iter_hmm (Integral, default=10) – Number of iterations for fitting the HMM.

  • n_fits_hmm (Integral, default=1) – Number of times to fit the HMM.

  • blocks_as_hidden_states_flag (bool, default=False) – Whether to use blocks as hidden states.

  • n_states (Integral, default=2) – Number of states for the HMM.

  • model_type (str, default="ar") – The model type to use. Must be one of “ar”, “arima”, “sarima”, “var”, or “arch”.

  • model_params (dict, default=None) – Additional keyword arguments to pass to the TSFit model.

  • order (Integral or list or tuple, default=None) – The order of the model. If None, the best order is chosen via TSFitBestLag. If Integral, it is the lag order for AR, ARIMA, and SARIMA, and the lag order for ARCH. If list or tuple, the order is a tuple of (p, o, q) for ARIMA and (p, d, q, s) for SARIMAX. It is either a single Integral or a list of non-consecutive ints for AR, and an Integral for VAR and ARCH. If None, the best order is chosen via TSFitBestLag. Do note that TSFitBestLag only chooses the best lag, not the best order, so for the tuple values, it only chooses the best p, not the best (p, o, q) or (p, d, q, s). The rest of the values are set to 0.

  • save_models (bool, default=False) – Whether to save the fitted models.

  • rng (Integral or np.random.Generator, default=np.random.default_rng()) – The random number generator or seed used to generate the bootstrap samples.

__init__ : Initialize self.
_generate_samples_single_bootstrap : Generate a single bootstrap sample.

Notes

Fitting Markov models is expensive, hence we do not allow re-fititng. We instead fit once to the residuals, resample using blocks once, and generate new samples by changing the random_seed.

classmethod get_test_params(parameter_set='default')[source]

Return testing parameter settings for the estimator.

Parameters:

parameter_set (str, default="default") – Name of the set of test parameters to return, for use in tests. If no special parameters are defined for a value, will return “default” set.

Returns:

params – Parameters to create testing instances of the class Each dict are parameters to construct an “interesting” test instance, i.e., MyClass(**params) or MyClass(**params[i]) creates a valid test instance. create_test_instance uses the first (or only) dictionary in params

Return type:

dict or list of dict, default = {}

class tsbootstrap.bootstrap.BlockResidualBootstrap(n_bootstraps: Integral = 10, block_bootstrap: BlockBootstrap | None = None, model_type: Literal['ar', 'arima', 'sarima', 'var'] = 'ar', model_params: dict | None = None, order: Integral | List[Integral] | tuple[Integral, Integral, Integral] | tuple[Integral, Integral, Integral, Integral] | None = None, save_models: bool = False, rng: Generator | Integral | None = None)[source]

Block Residual Bootstrap class for time series data.

This class applies residual bootstrapping to blocks of the time series. The residuals are bootstrapped using the specified block structure and added to the fitted values to generate new samples.

Parameters:
  • block_bootstrap (BlockBootstrap, default=MovingBlockBootstrap()) – The block bootstrap algorithm.

  • n_bootstraps (Integral, default=10) – The number of bootstrap samples to create.

  • model_type (str, default="ar") – The model type to use. Must be one of “ar”, “arima”, “sarima”, “var”, or “arch”.

  • model_params (dict, default=None) – Additional keyword arguments to pass to the TSFit model.

  • order (OrderTypes, default=None) – The order of the model. If None, the best order is chosen via TSFitBestLag. If Integral, it is the lag order for AR, ARIMA, and SARIMA, and the lag order for ARCH. If list or tuple, the order is a tuple of (p, o, q) for ARIMA and (p, d, q, s) for SARIMAX. It is either a single Integral or a list of non-consecutive ints for AR, and an Integral for VAR and ARCH. If None, the best order is chosen via TSFitBestLag. Do note that TSFitBestLag only chooses the best lag, not the best order, so for the tuple values, it only chooses the best p, not the best (p, o, q) or (p, d, q, s). The rest of the values are set to 0.

  • save_models (bool, default=False) – Whether to save the fitted models.

  • rng (RngTypes, default=None) – The random number generator or seed used to generate the bootstrap samples.

__init__ : Initialize self.
_generate_samples_single_bootstrap : Generate a single bootstrap sample.
classmethod get_test_params(parameter_set='default')[source]

Return testing parameter settings for the estimator.

Parameters:

parameter_set (str, default="default") – Name of the set of test parameters to return, for use in tests. If no special parameters are defined for a value, will return “default” set.

Returns:

params – Parameters to create testing instances of the class Each dict are parameters to construct an “interesting” test instance, i.e., MyClass(**params) or MyClass(**params[i]) creates a valid test instance. create_test_instance uses the first (or only) dictionary in params

Return type:

dict or list of dict, default = {}

class tsbootstrap.bootstrap.BlockSieveBootstrap(n_bootstraps: Integral = 10, block_bootstrap: BlockBootstrap | None = None, resids_model_type: Literal['ar', 'arima', 'sarima', 'var', 'arch'] = 'ar', resids_order=None, save_resids_models: bool = False, kwargs_base_sieve=None, model_type: Literal['ar', 'arima', 'sarima', 'var'] = 'ar', model_params: dict | None = None, order=None, save_models: bool = False, rng: Generator | Integral | None = None)[source]

Implementation of the Sieve bootstrap method for time series data.

This class applies Sieve bootstrapping to blocks of the time series. The residuals are fit to a second model, then resampled using the specified block structure. The new residuals are then added to the fitted values to generate new samples.

Parameters:
  • block_bootstrap (BlockBootstrap, default=MovingBlockBootstrap()) – The block bootstrap algorithm.

  • resids_model_type (str, default="ar") – The model type to use for fitting the residuals. Must be one of “ar”, “arima”, “sarima”, “var”, or “arch”.

  • resids_order (Integral or list or tuple, default=None) – The order of the model to use for fitting the residuals. If None, the order is automatically determined.

  • save_resids_models (bool, default=False) – Whether to save the fitted models for the residuals.

  • kwargs_base_sieve (dict, default=None) – Keyword arguments to pass to the SieveBootstrap class.

  • model_type (str, default="ar") – The model type to use. Must be one of “ar”, “arima”, “sarima”, “var”, or “arch”.

  • model_params (dict, default=None) – Additional keyword arguments to pass to the TSFit model.

  • order (Integral or list or tuple, default=None) – The order of the model. If None, the best order is chosen via TSFitBestLag. If Integral, it is the lag order for AR, ARIMA, and SARIMA, and the lag order for ARCH. If list or tuple, the order is a tuple of (p, o, q) for ARIMA and (p, d, q, s) for SARIMAX. It is either a single Integral or a list of non-consecutive ints for AR, and an Integral for VAR and ARCH. If None, the best order is chosen via TSFitBestLag. Do note that TSFitBestLag only chooses the best lag, not the best order, so for the tuple values, it only chooses the best p, not the best (p, o, q) or (p, d, q, s). The rest of the values are set to 0.

  • save_models (bool, default=False) – Whether to save the fitted models.

_init_ : Initialize self.
_generate_samples_single_bootstrap : Generate a single bootstrapped sample.
classmethod get_test_params(parameter_set='default')[source]

Return testing parameter settings for the estimator.

Parameters:

parameter_set (str, default="default") – Name of the set of test parameters to return, for use in tests. If no special parameters are defined for a value, will return “default” set.

Returns:

params – Parameters to create testing instances of the class Each dict are parameters to construct an “interesting” test instance, i.e., MyClass(**params) or MyClass(**params[i]) creates a valid test instance. create_test_instance uses the first (or only) dictionary in params

Return type:

dict or list of dict, default = {}

class tsbootstrap.bootstrap.BlockStatisticPreservingBootstrap(n_bootstraps: Integral = 10, block_bootstrap: BlockBootstrap | None = None, statistic=None, statistic_axis: Integral = 0, statistic_keepdims: bool = False, rng: Generator | Integral | None = None)[source]

Block Statistic Preserving Bootstrap class for time series data.

This class applies statistic-preserving bootstrapping to blocks of the time series. The residuals are resampled using the specified block structure and added to the fitted values to generate new samples.

Parameters:
  • block_bootstrap (BlockBootstrap, default=MovingBlockBootstrap()) – The block bootstrap algorithm.

  • n_bootstraps (Integral, default=10) – The number of bootstrap samples to create.

  • statistic (Callable, default=np.mean) – A callable function to compute the statistic that should be preserved.

  • statistic_axis (Integral, default=0) – The axis along which the statistic should be computed.

  • statistic_keepdims (bool, default=False) – Whether to keep the dimensions of the statistic or not.

  • rng (Integral or np.random.Generator, default=np.random.default_rng()) – The random number generator or seed used to generate the bootstrap samples.

statistic_X

The statistic calculated from the original data. This is used as a parameter for generating the bootstrapped samples.

Type:

np.ndarray, default=None

__init__ : Initialize self.
_generate_samples_single_bootstrap : Generate a single bootstrap sample.
classmethod get_test_params(parameter_set='default')[source]

Return testing parameter settings for the estimator.

Parameters:

parameter_set (str, default="default") – Name of the set of test parameters to return, for use in tests. If no special parameters are defined for a value, will return “default” set.

Returns:

params – Parameters to create testing instances of the class Each dict are parameters to construct an “interesting” test instance, i.e., MyClass(**params) or MyClass(**params[i]) creates a valid test instance. create_test_instance uses the first (or only) dictionary in params

Return type:

dict or list of dict, default = {}

class tsbootstrap.bootstrap.WholeDistributionBootstrap(n_bootstraps: Integral = 10, distribution: str = 'normal', refit: bool = False, model_type: Literal['ar', 'arima', 'sarima', 'var'] = 'ar', model_params=None, order: Integral | List[Integral] | tuple[Integral, Integral, Integral] | tuple[Integral, Integral, Integral, Integral] | None = None, save_models: bool = False, rng=None, **kwargs)[source]

Whole Distribution Bootstrap class for time series data.

This class applies distribution bootstrapping to the entire time series, without any block structure. This is the most basic form of distribution bootstrapping. The residuals are fit to a distribution, and then resampled using the distribution. The resampled residuals are added to the fitted values to generate new samples.

resids_dist

The distribution object used to generate the bootstrapped samples. If None, the distribution has not been fit yet.

Type:

scipy.stats.rv_continuous or None

resids_dist_params

The parameters of the distribution used to generate the bootstrapped samples. If None, the distribution has not been fit yet.

Type:

tuple or None

__init__ : Initialize self.
_generate_samples_single_bootstrap : Generate a single bootstrap sample.

Notes

We either fit the distribution to the residuals once and generate new samples from the fitted distribution with a new random seed, or resample the residuals once and fit the distribution to the resampled residuals, then generate new samples from the fitted distribution with the same random seed n_bootstrap times.

class tsbootstrap.bootstrap.WholeMarkovBootstrap(n_bootstraps: Integral = 10, method: Literal['first', 'middle', 'last', 'mean', 'mode', 'median', 'kmeans', 'kmedians', 'kmedoids'] = 'middle', apply_pca_flag: bool = False, pca=None, n_iter_hmm: Integral = 10, n_fits_hmm: Integral = 1, blocks_as_hidden_states_flag: bool = False, n_states: Integral = 2, model_type: Literal['ar', 'arima', 'sarima', 'var'] = 'ar', model_params=None, order: Integral | List[Integral] | tuple[Integral, Integral, Integral] | tuple[Integral, Integral, Integral, Integral] | None = None, save_models: bool = False, rng=None, **kwargs)[source]

Whole Markov Bootstrap class for time series data.

This class applies Markov bootstrapping to the entire time series, without any block structure. This is the most basic form of Markov bootstrapping. The residuals are fit to a Markov model, and then resampled using the Markov model. The resampled residuals are added to the fitted values to generate new samples.

_generate_samples_single_bootstrap : Generate a single bootstrap sample.

Notes

Fitting Markov models is expensive, hence we do not allow re-fititng. We instead fit once to the residuals and generate new samples by changing the random_seed.

class tsbootstrap.bootstrap.WholeResidualBootstrap(n_bootstraps: Integral = 10, rng: Generator | Integral | None = None, model_type: Literal['ar', 'arima', 'sarima', 'var'] = 'ar', model_params: dict | None = None, order: Integral | List[Integral] | tuple[Integral, Integral, Integral] | tuple[Integral, Integral, Integral, Integral] | None = None, save_models: bool = False)[source]

Whole Residual Bootstrap class for time series data.

This class applies residual bootstrapping to the entire time series, without any block structure. This is the most basic form of residual bootstrapping. The residuals are resampled with replacement and added to the fitted values to generate new samples.

Parameters:
  • n_bootstraps (Integral, default=10) – The number of bootstrap samples to create.

  • model_type (str, default="ar") – The model type to use. Must be one of “ar”, “arima”, “sarima”, “var”, or “arch”.

  • model_params (dict, default=None) – Additional keyword arguments to pass to the TSFit model.

  • order (OrderTypes, default=None) – The order of the model. If None, the best order is chosen via TSFitBestLag. If Integral, it is the lag order for AR, ARIMA, and SARIMA, and the lag order for ARCH. If list or tuple, the order is a tuple of (p, o, q) for ARIMA and (p, d, q, s) for SARIMAX. It is either a single Integral or a list of non-consecutive ints for AR, and an Integral for VAR and ARCH. If None, the best order is chosen via TSFitBestLag. Do note that TSFitBestLag only chooses the best lag, not the best order, so for the tuple values, it only chooses the best p, not the best (p, o, q) or (p, d, q, s). The rest of the values are set to 0.

  • save_models (bool, default=False) – Whether to save the fitted models.

  • rng (RngTypes, default=None) – The random number generator or seed used to generate the bootstrap samples.

__init__ : Initialize self.
_generate_samples_single_bootstrap : Generate a single bootstrap sample.
class tsbootstrap.bootstrap.WholeSieveBootstrap(n_bootstraps: Integral = 10, rng=None, resids_model_type: Literal['ar', 'arima', 'sarima', 'var', 'arch'] = 'ar', resids_order=None, save_resids_models: bool = False, kwargs_base_sieve=None, model_type: Literal['ar', 'arima', 'sarima', 'var'] = 'ar', model_params=None, order: Integral | List[Integral] | tuple[Integral, Integral, Integral] | tuple[Integral, Integral, Integral, Integral] | None = None, **kwargs_base_residual)[source]

Implementation of the Sieve bootstrap method for time series data.

This class applies Sieve bootstrapping to the entire time series, without any block structure. This is the most basic form of Sieve bootstrapping. The residuals are fit to a second model, and then new samples are generated by adding the new residuals to the fitted values.

Parameters:
  • resids_model_type (str, default="ar") – The model type to use for fitting the residuals. Must be one of “ar”, “arima”, “sarima”, “var”, or “arch”.

  • resids_order (Integral or list or tuple, default=None) – The order of the model to use for fitting the residuals. If None, the order is automatically determined.

  • save_resids_models (bool, default=False) – Whether to save the fitted models for the residuals.

  • kwargs_base_sieve (dict, default=None) – Keyword arguments to pass to the SieveBootstrap class.

  • model_type (str, default="ar") – The model type to use. Must be one of “ar”, “arima”, “sarima”, “var”, or “arch”.

  • model_params (dict, default=None) – Additional keyword arguments to pass to the TSFit model.

  • order (Integral or list or tuple, default=None) – The order of the model. If None, the best order is chosen via TSFitBestLag. If Integral, it is the lag order for AR, ARIMA, and SARIMA, and the lag order for ARCH. If list or tuple, the order is a tuple of (p, o, q) for ARIMA and (p, d, q, s) for SARIMAX. It is either a single Integral or a list of non-consecutive ints for AR, and an Integral for VAR and ARCH. If None, the best order is chosen via TSFitBestLag. Do note that TSFitBestLag only chooses the best lag, not the best order, so for the tuple values, it only chooses the best p, not the best (p, o, q) or (p, d, q, s). The rest of the values are set to 0.

_generate_samples_single_bootstrap : Generate a single bootstrapped sample.
class tsbootstrap.bootstrap.WholeStatisticPreservingBootstrap(n_bootstraps: Integral = 10, statistic: Callable | None = None, statistic_axis: Integral = 0, statistic_keepdims: bool = False, rng=None)[source]

Whole Statistic Preserving Bootstrap class for time series data.

This class applies statistic-preserving bootstrapping to the entire time series, without any block structure. This is the most basic form of statistic-preserving bootstrapping. The residuals are resampled with replacement and added to the fitted values to generate new samples.

_generate_samples_single_bootstrap : Generate a single bootstrap sample.