Probabilistic forecasting for univariate time series


source

prob_forecasts


def prob_forecasts(
    model, # Any model with ``.target_col``, ``.fit(df)``, and ``.forecast(H, exog)`` attributes.
    H:int, # Forecast horizon.
    n_calibration:Union[int, None]=None, # Number of calibration windows for cross-validated residual estimation. If None, in sample residuals are used without cross-validation (Horizon-specific uncalibrated intervals may be too narrow in this case. This is recommended when data size is small as the model may not have enough data to fit well in each calibration fold).
    step_size:int=1, # Step size between consecutive calibration windows.
    random_state:int=42, # Seed for all internal random-number generators.
    n_iter:Union[int, None]=None, # Number of EM iterations during each calibration window.  Only relevant for Markov-switching Autoregressive model ([`ms_arr`](https://mustafaslanCoto.github.io/peshbeen/modules/02_models/ms_arr.html#ms_arr)). A smaller value than the model's default speeds up calibration at the cost of convergence quality per fold — typically a value of 3–10 is sufficient for calibration windows where the model is already close to the solution.
    verbose:bool=False, # Print progress during calibration.
):

Probabilistic forecasting wrapper for any point-forecasting model.

Type Default Details
model Any model with .target_col, .fit(df), and .forecast(H, exog) attributes.
H int Forecast horizon.
n_calibration Union[int, None] None Number of calibration windows for cross-validated residual estimation. If None, in sample residuals are used without cross-validation (Horizon-specific uncalibrated intervals may be too narrow in this case. This is recommended when data size is small as the model may not have enough data to fit well in each calibration fold).
step_size int 1 Step size between consecutive calibration windows.
random_state int 42 Seed for all internal random-number generators.
n_iter Union[int, None] None Number of EM iterations during each calibration window. Only relevant for Markov-switching Autoregressive model (ms_arr). A smaller value than the model’s default speeds up calibration at the cost of convergence quality per fold — typically a value of 3–10 is sufficient for calibration windows where the model is already close to the solution.
verbose bool False Print progress during calibration.

source

prob_forecasts.calibrate


def calibrate(
    df:pd.DataFrame, # Calibration dataset.
    delta:Union[float, List[float]]=0.5, # Coverage level(s).  A single float produces one symmetric interval; a list produces one interval per level.  For example,``delta=0.9`` produces a 90 % prediction interval.
)->'prob_forecasts': # The fitted object, with ``self.q_hat`` set to the calibrated

Calibrate the conformal predictor.

Runs rolling-window cross-validation (if not already done) to collect non-conformity scores, then computes the per-horizon conformal quantile q_hat for each requested delta level.

Type Default Details
df pd.DataFrame Calibration dataset.
delta Union[float, List[float]] 0.5 Coverage level(s). A single float produces one symmetric interval; a list produces one interval per level. For example,delta=0.9 produces a 90 % prediction interval.
Returns ‘prob_forecasts’ The fitted object, with self.q_hat set to the calibrated

source

prob_forecasts.sample


def sample(
    df:pd.DataFrame, # Training data.  Residuals are computed via cross-validation if not yet available.
    n_samples:int=1000, # Number of sample paths to draw.
    method:str='empirical', # Sampling strategy (see above).
    future_exog:Union[pd.DataFrame, None]=None, # Future exogenous variables passed to ``forecast``.
)->'prob_forecasts':

Draw sample paths from the predictive distribution.

Three methods are available:

  • "empirical" — residuals are resampled with replacement independently at each horizon.
  • "kde" — a Gaussian KDE is fitted to each horizon’s residuals; samples are drawn from the smoothed distribution.
  • "correlated" — a multivariate normal is fitted to the full H-dimensional residual vectors, preserving cross-horizon correlation. Samples are drawn jointly.

Results are stored on self:

  • self.sample_paths(n_samples, H) array of sampled trajectories centred on the point forecast.
  • self.point_forecast(H,) point forecast array.
  • self.sample_paths_df — the same data as a DataFrame with columns h_1, …, h_H.
Type Default Details
df pd.DataFrame Training data. Residuals are computed via cross-validation if not yet available.
n_samples int 1000 Number of sample paths to draw.
method str empirical Sampling strategy (see above).
future_exog Union[pd.DataFrame, None] None Future exogenous variables passed to forecast.
Returns ‘prob_forecasts’

source

prob_forecasts.sample_quantiles


def sample_quantiles(
    quantiles:Union[float, List[float]], # Desired quantile levels (e.g. ``[0.1, 0.5, 0.9]``).
)->pd.DataFrame: # Columns: ``point_forecast``, ``q_<level>`` for each level.

Compute quantiles from the sample paths generated by sample.

Works identically regardless of which method was passed to sample.

Type Details
quantiles Union[float, List[float]] Desired quantile levels (e.g. [0.1, 0.5, 0.9]).
Returns pd.DataFrame Columns: point_forecast, q_<level> for each level.

source

prob_forecasts.conformal_quantiles


def conformal_quantiles(
    df:pd.DataFrame, # Training data for the final model fit.
    quantiles:Union[float, List[float]], # Desired quantile levels (e.g. ``[0.1, 0.5, 0.9]``).
    future_exog:Union[pd.DataFrame, None]=None, # Future exogenous variables.
)->pd.DataFrame: # Columns: ``point_forecast``, ``q_<level>`` for each level.

Generate conformal prediction quantiles.

Requires calibrate to have been called first.

Type Default Details
df pd.DataFrame Training data for the final model fit.
quantiles Union[float, List[float]] Desired quantile levels (e.g. [0.1, 0.5, 0.9]).
future_exog Union[pd.DataFrame, None] None Future exogenous variables.
Returns pd.DataFrame Columns: point_forecast, q_<level> for each level.

Probabilistic forecasting for multivariate time series


source

mv_prob_forecasts


def mv_prob_forecasts(
    model, # Any model with ``.target_col``, ``.fit(df)``, and ``.forecast(H, exog)`` attributes.
    target_col:str, # Name of the target variable column in the input DataFrames.
    H:int, # Forecast horizon.
    n_calibration:Union[int, None]=None, # Number of calibration windows for cross-validated residual estimation. If None, in sample residuals are used without cross-validation (Horizon-specific uncalibrated intervals may be too narrow in this case. This is recommended when data size is small as the model may not have enough data to fit well in each calibration fold).
    step_size:int=1, # Step size between consecutive calibration windows.
    random_state:int=42, # Seed for all internal random-number generators.
    n_iter:Union[int, None]=None, # Number of EM iterations during each calibration window.  Only relevant for Markov-switching Autoregressive model ([`ms_arr`](https://mustafaslanCoto.github.io/peshbeen/modules/02_models/ms_arr.html#ms_arr)). A smaller value than the model's default speeds up calibration at the cost of convergence quality per fold — typically a value of 3–10 is sufficient for calibration windows where the model is already close to the solution.
    verbose:bool=False, # Print progress during calibration.
):

Probabilistic forecasting wrapper for any point-forecasting model.

Type Default Details
model Any model with .target_col, .fit(df), and .forecast(H, exog) attributes.
target_col str Name of the target variable column in the input DataFrames.
H int Forecast horizon.
n_calibration Union[int, None] None Number of calibration windows for cross-validated residual estimation. If None, in sample residuals are used without cross-validation (Horizon-specific uncalibrated intervals may be too narrow in this case. This is recommended when data size is small as the model may not have enough data to fit well in each calibration fold).
step_size int 1 Step size between consecutive calibration windows.
random_state int 42 Seed for all internal random-number generators.
n_iter Union[int, None] None Number of EM iterations during each calibration window. Only relevant for Markov-switching Autoregressive model (ms_arr). A smaller value than the model’s default speeds up calibration at the cost of convergence quality per fold — typically a value of 3–10 is sufficient for calibration windows where the model is already close to the solution.
verbose bool False Print progress during calibration.

source

mv_prob_forecasts.calibrate


def calibrate(
    df:pd.DataFrame, # Calibration dataset.
    delta:Union[float, List[float]]=0.5, # Coverage level(s).  A single float produces one symmetric interval; a list produces one interval per level.  For example,``delta=0.9`` produces a 90 % prediction interval.
)->'prob_forecasts': # The fitted object, with ``self.q_hat`` set to the calibrated

Calibrate the conformal predictor.

Runs rolling-window cross-validation (if not already done) to collect non-conformity scores, then computes the per-horizon conformal quantile q_hat for each requested delta level.

Type Default Details
df pd.DataFrame Calibration dataset.
delta Union[float, List[float]] 0.5 Coverage level(s). A single float produces one symmetric interval; a list produces one interval per level. For example,delta=0.9 produces a 90 % prediction interval.
Returns ‘prob_forecasts’ The fitted object, with self.q_hat set to the calibrated

source

mv_prob_forecasts.sample


def sample(
    df:pd.DataFrame, # Training data.  Residuals are computed via cross-validation if not yet available.
    n_samples:int=1000, # Number of sample paths to draw.
    method:str='empirical', # Sampling strategy (see above).
    future_exog:Union[pd.DataFrame, None]=None, # Future exogenous variables passed to ``forecast``.
)->'prob_forecasts':

Draw sample paths from the predictive distribution.

Three methods are available:

  • "empirical" — residuals are resampled with replacement independently at each horizon.
  • "kde" — a Gaussian KDE is fitted to each horizon’s residuals; samples are drawn from the smoothed distribution.
  • "correlated" — a multivariate normal is fitted to the full H-dimensional residual vectors, preserving cross-horizon correlation. Samples are drawn jointly.

Results are stored on self:

  • self.sample_paths(n_samples, H) array of sampled trajectories centred on the point forecast.
  • self.point_forecast(H,) point forecast array.
  • self.sample_paths_df — the same data as a DataFrame with columns h_1, …, h_H.
Type Default Details
df pd.DataFrame Training data. Residuals are computed via cross-validation if not yet available.
n_samples int 1000 Number of sample paths to draw.
method str empirical Sampling strategy (see above).
future_exog Union[pd.DataFrame, None] None Future exogenous variables passed to forecast.
Returns ‘prob_forecasts’

source

mv_prob_forecasts.sample_quantiles


def sample_quantiles(
    quantiles:Union[float, List[float]], # Desired quantile levels (e.g. ``[0.1, 0.5, 0.9]``).
)->pd.DataFrame: # Columns: ``point_forecast``, ``q_<level>`` for each level.

Compute quantiles from the sample paths generated by sample.

Works identically regardless of which method was passed to sample.

Type Details
quantiles Union[float, List[float]] Desired quantile levels (e.g. [0.1, 0.5, 0.9]).
Returns pd.DataFrame Columns: point_forecast, q_<level> for each level.

source

mv_prob_forecasts.conformal_quantiles


def conformal_quantiles(
    df:pd.DataFrame, # Training data for the final model fit.
    quantiles:Union[float, List[float]], # Desired quantile levels (e.g. ``[0.1, 0.5, 0.9]``).
    future_exog:Union[pd.DataFrame, None]=None, # Future exogenous variables.
)->pd.DataFrame: # Columns: ``point_forecast``, ``q_<level>`` for each level.

Generate conformal prediction quantiles.

Requires calibrate to have been called first.

Type Default Details
df pd.DataFrame Training data for the final model fit.
quantiles Union[float, List[float]] Desired quantile levels (e.g. [0.1, 0.5, 0.9]).
future_exog Union[pd.DataFrame, None] None Future exogenous variables.
Returns pd.DataFrame Columns: point_forecast, q_<level> for each level.