Skip to contents

Compute prediction intervals and other information by applying the adaptive conformal prediction (ACP) method.

Usage

acp(
  object,
  alpha = 1 - 0.01 * object$level,
  gamma = 0.005,
  symmetric = FALSE,
  ncal = 10,
  rolling = FALSE,
  quantiletype = 1,
  update = FALSE,
  na.rm = TRUE,
  ...
)

Arguments

object

An object of class "cvforecast". It must have an argument x for original univariate time series, an argument MEAN for point forecasts and ERROR for forecast errors on validation set. See the results of a call to cvforecast.

alpha

A numeric vector of significance levels to achieve a desired coverage level \(1-\alpha\).

gamma

The step size parameter \(\gamma>0\) for \(\alpha\) updating.

symmetric

If TRUE, symmetric nonconformity scores (i.e. \(|e_{t+h|t}|\)) are used. If FALSE, asymmetric nonconformity scores (i.e. \(e_{t+h|t}\)) are used, and then upper bounds and lower bounds are produced separately.

ncal

Length of the calibration set. If rolling = FALSE, it denotes the initial period of calibration sets. Otherwise, it indicates the period of every rolling calibration set.

rolling

If TRUE, a rolling window strategy will be adopted to form the calibration set. Otherwise, expanding window strategy will be used.

quantiletype

An integer between 1 and 9 determining the type of quantile estimator to be used. Types 1 to 3 are for discontinuous quantiles, types 4 to 9 are for continuous quantiles. See the weighted_quantile function in the ggdist package.

update

If TRUE, the function will be compatible with the update(update.cpforecast) function, allowing for easy updates of conformal prediction.

na.rm

If TRUE, corresponding entries in sample values are removed if it is NA when calculating sample quantile.

...

Other arguments are passed to the weighted_quantile function for quantile computation.

Value

A list of class c("acp", "cpforecast", "forecast") with the following components:

x

The original time series.

series

The name of the series x.

method

A character string "acp".

cp_times

The number of times the conformal prediction is performed in cross-validation.

MEAN

Point forecasts as a multivariate time series, where the \(h\)th column holds the point forecasts for forecast horizon \(h\). The time index corresponds to the period for which the forecast is produced.

ERROR

Forecast errors given by \(e_{t+h|t} = y_{t+h}-\hat{y}_{t+h|t}\).

LOWER

A list containing lower bounds for prediction intervals for each level. Each element within the list will be a multivariate time series with the same dimensional characteristics as MEAN.

UPPER

A list containing upper bounds for prediction intervals for each level. Each element within the list will be a multivariate time series with the same dimensional characteristics as MEAN.

level

The confidence values associated with the prediction intervals.

call

The matched call.

model

A list containing information abouth the conformal prediction model.

If mean is included in the object, the components mean, lower, and upper will also be returned, showing the information about the forecasts generated using all available observations.

Details

The ACP method considers the online update: $$\alpha_{t+h|t}:=\alpha_{t+h-1|t-1}+\gamma(\alpha-\mathrm{err}_{t|t-h}),$$ for each individual forecast horizon h, respectively, where \(\mathrm{err}_{t|t-h}=1\) if \(s_{t|t-h}>q_{t|t-h}\), and \(\mathrm{err}_{t|t-h}=0\) if \(s_{t|t-h} \leq q_{t|t-h}\).

References

Gibbs, I., and Candes, E. (2021). "Adaptive conformal inference under distribution shift", Advances in Neural Information Processing Systems, 34, 1660–1672.

Examples

# Simulate time series from an AR(2) model
library(forecast)
series <- arima.sim(n = 1000, list(ar = c(0.8, -0.5)), sd = sqrt(1))

# Cross-validation forecasting
far2 <- function(x, h, level) {
  Arima(x, order = c(2, 0, 0)) |>
    forecast(h = h, level)
}
fc <- cvforecast(series, forecastfun = far2, h = 3, level = c(80, 95),
                 forward = TRUE, initial = 1, window = 100)

# ACP with asymmetric nonconformity scores and rolling calibration sets
acpfc <- acp(fc, symmetric = FALSE, gamma = 0.005, ncal = 100, rolling = TRUE)
print(acpfc)
#> ACP 
#> 
#> Call:
#>  acp(object = fc, gamma = 0.005, symmetric = FALSE, ncal = 100,  
#>      rolling = TRUE) 
#> 
#>  cp_times = 799 (the forward step included) 
#> 
#> Forecasts of the forward step:
#> Cross-validation
#> 
#> Call:
#>  acp(object = fc, gamma = 0.005, symmetric = FALSE, ncal = 100,  
#>      rolling = TRUE) 
#> 
#>  fit_times =  (the forward step included) 
#> 
#> Forecasts of the forward step:
#>      Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
#> 1001     -1.7751855    NA    NA    NA    NA
#> 1002     -0.2712581    NA    NA    NA    NA
#> 1003      0.5907807    NA    NA    NA    NA
summary(acpfc)
#> ACP 
#> 
#> Call:
#>  acp(object = fc, gamma = 0.005, symmetric = FALSE, ncal = 100,  
#>      rolling = TRUE) 
#> 
#>  cp_times = 799 (the forward step included) 
#> 
#> Forecasts of the forward step:
#> Cross-validation
#> 
#> Call:
#>  acp(object = fc, gamma = 0.005, symmetric = FALSE, ncal = 100,  
#>      rolling = TRUE) 
#> 
#>  fit_times =  (the forward step included) 
#> 
#> Forecasts of the forward step:
#>      Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
#> 1001     -1.7751855    NA    NA    NA    NA
#> 1002     -0.2712581    NA    NA    NA    NA
#> 1003      0.5907807    NA    NA    NA    NA
#> 
#> Cross-validation error measures:
#>      ME   MAE   MSE  RMSE     MPE    MAPE  MASE RMSSE Winkler_95 MSIS_95
#> CV 0.01 0.924 1.345 1.038 187.647 359.433 0.902 0.818        NaN     NaN