estimate_forecast_variance: Estimate Forecast Variance via Rolling Window

View source: R/analysis_helpers.R

estimate_forecast_varianceR Documentation

Estimate Forecast Variance via Rolling Window

Description

Estimates forecast variance from historical forecast errors relative to the benchmark using a rolling window. Used to approximate the time-varying predictive standard deviation for each competing model forecast, required by compute_klic, compute_zp, and compute_kupiec.

Usage

estimate_forecast_variance(
  forecast_matrix,
  benchmark_col = ncol(forecast_matrix),
  window_size = 20
)

Arguments

forecast_matrix

matrix of dimension P x K_total, where P is the number of forecast periods and K_total is the total number of columns (competing forecasts plus the benchmark).

benchmark_col

Index or name of the benchmark column. Defaults to the last column.

window_size

integer rolling window size. For the first window_size periods, the full available history is used instead (expanding window). From period window_size + 1 onwards, a rolling window of exactly window_size observations is used.

Details

For each competing forecast k and period t, the forecast error is defined as e_{t,k} = \text{benchmark}_t - \hat{y}_{t,k}. The variance of these errors is estimated over a rolling window:

  • If t < window_size: variance is computed over observations 1:t (expanding window).

  • If t >= window_size: variance is computed over observations max(1, t - window_size):t (rolling window of size window_size).

For t = 1 the variance of a single observation is undefined (NA). Estimated variances that are NA, zero, or negative are replaced by 1e-6 to ensure numerical stability in downstream computations. The benchmark column in the returned matrix is set to zero throughout.

Value

matrix of dimension P x K_total containing estimated variances. Columns correspond to the same forecasts as forecast_matrix; the benchmark column contains zeros.

See Also

compute_klic, compute_zp, compute_kupiec

Examples

data(metals)
forecast_variance <- estimate_forecast_variance(metals, benchmark_col = 15,
                                                window_size = 20)
head(forecast_variance)

RCtest documentation built on June 2, 2026, 9:07 a.m.