Title: | Bayesian Multivariate GARCH Models |
---|---|
Description: | Fit Bayesian multivariate GARCH models using 'Stan' for full Bayesian inference. Generate (weighted) forecasts for means, variances (volatility) and correlations. Currently DCC(P,Q), CCC(P,Q), pdBEKK(P,Q), and BEKK(P,Q) parameterizations are implemented, based either on a multivariate gaussian normal or student-t distribution. DCC and CCC models are based on Engle (2002) <doi:10.1198/073500102288618487> and Bollerslev (1990). The BEKK parameterization follows Engle and Kroner (1995) <doi:10.1017/S0266466600009063> while the pdBEKK as well as the estimation approach for this package is described in Rast et al. (2020) <doi:10.31234/osf.io/j57pk>. The fitted models contain 'rstan' objects and can be examined with 'rstan' functions. |
Authors: | Philippe Rast [aut, cre] , Stephen Martin [aut] |
Maintainer: | Philippe Rast <[email protected]> |
License: | GPL (>=3) |
Version: | 2.0.0.9000 |
Built: | 2024-10-31 22:38:55 UTC |
Source: | https://github.com/ph-rast/bmgarch |
as.data.frame method for fitted.bmgarch objects.
## S3 method for class 'fitted.bmgarch' as.data.frame(x, ...)
## S3 method for class 'fitted.bmgarch' as.data.frame(x, ...)
x |
fitted.bmgarch object. |
... |
Not used. |
Data frame.
Stephen R. Martin
as.data.frame method for forecast.bmgarch objects.
## S3 method for class 'forecast.bmgarch' as.data.frame(x, ..., backcast = TRUE)
## S3 method for class 'forecast.bmgarch' as.data.frame(x, ..., backcast = TRUE)
x |
forecast.bmgarch object. |
... |
Not used. |
backcast |
Logical (Default: True). Whether to include "backcasted" values from |
Data frame.
Stephen R. Martin
Draw samples from a specified multivariate GARCH model using 'Stan', given multivariate time-series. Currently supports CCC, DCC, BEKK, and pdBEKK model parameterizations.
bmgarch( data, xC = NULL, parameterization = "CCC", P = 1, Q = 1, iterations = 2000, chains = 4, standardize_data = FALSE, distribution = "Student_t", meanstructure = "constant", sampling_algorithm = "MCMC", backend = "rstan", ... )
bmgarch( data, xC = NULL, parameterization = "CCC", P = 1, Q = 1, iterations = 2000, chains = 4, standardize_data = FALSE, distribution = "Student_t", meanstructure = "constant", sampling_algorithm = "MCMC", backend = "rstan", ... )
data |
Time-series or matrix object. A time-series or matrix object containing observations at the same interval. |
xC |
Numeric vector or matrix. Covariates(s) for the constant variance terms in C, or c, used in a log-linear model on the constant variance terms (Rast et al. 2020). If vector, then it acts as a covariate for all constant variance terms. If matrix, must have columns equal to number of time series, and each column acts as a covariate for the respective time series (e.g., column 1 predicts constant variance for time series 1). |
parameterization |
Character (Default: "CCC"). The type of of parameterization. Must be one of "CCC", "DCC", "BEKK", or "pdBEKK". |
P |
Integer. Dimension of GARCH component in MGARCH(P,Q). |
Q |
Integer. Dimension of ARCH component in MGARCH(P,Q). |
iterations |
Integer (Default: 2000). Number of iterations for each chain (including warmup). |
chains |
Integer (Default: 4). The number of Markov chains. |
standardize_data |
Logical (Default: FALSE). Whether data should be standardized to easy computations. |
distribution |
Character (Default: "Student_t"). Distribution of innovation: "Student_t" or "Gaussian" |
meanstructure |
Character (Default: "constant"). Defines model for means. Either 'constant' or 'ARMA'. Currently ARMA(1,1) only. OR 'VAR' (VAR1). |
sampling_algorithm |
Character (Default" "MCMC"). Define sampling algorithm. Either 'MCMC'for Hamiltonian Monte Carlo or 'VB' for variational Bayes. 'VB' is inherited from stan and is currently in heavy development – do not trust estimates. |
backend |
Select backend. Defaults to 'rstan' or select 'cmdstanr' if installed. |
... |
Additional arguments can be ‘chain_id’, ‘init_r’, ‘test_grad’, ‘append_samples’, ‘refresh’, ‘enable_random_init’ etc. See the documentation in |
Four types of paramerizations are implemented. The constant conditional correlation (CCC) and the dynamic conditional correlation (DCC; Engle2002,Engle2001a), as well as BEKK (Engle and Kroner 1995) and a BEKK model with positivity constraints on the diagonals of the ARCH and GARCH parameters "pdBEKK" (Rast et al. 2020).
The fitted models are 'rstan' objects and all posterior parameter estimates can be obtained and can be examined with either the 'rstan' toolbox, plotted and printed using generic functions or passed to 'bmgarch' functions to 'forecast' or compute 'model_weights' or compute fit statistics based on leave-future-out cross-validation.
bmgarch
object.
Philippe Rast, Stephen R. Martin
Engle RF, Kroner KF (1995).
“Multivariate simultaneous generalized arch.”
Econometric Theory, 11(1), 122–150.
doi:10.1017/S0266466600009063.
Rast P, Martin SR, Liu S, Williams DR (2020).
“A New Frontier for Studying Within-Person Variability: Bayesian Multivariate Generalized Autoregressive Conditional Heteroskedasticity Models.”
Psychological Methods.
https://psyarxiv.com/j57pk/.()
## Not run: data(panas) # Fit BEKK(1,1) mgarch model with a ARMA(1,1) meanstructure, # and student-t residual distribution fit <- bmgarch(panas, parameterization = "BEKK", P = 1, Q = 1, meanstructure = "arma", distribution = "Student_t") # Summarize the parameters summary(fit) # Forecast 5 ahead fit.fc <- forecast(fit, ahead = 5) print(fit.fc) # Plot mean forecasts plot(fit.fc, type = "mean") # Plot variance forecasts plot(fit.fc, type = "var") # Plot correlation forecasts plot(fit.fc, type = "cor") # Plot modeled data ("backcasted values"). plot(fit, type = "mean") # Save "backcasted" values fit.bc <- fitted(fit) # Save estimated and forecasted data as a data.frame df.fc <- as.data.frame(fit.fc) # Access rstan's model fit object mf <- fit$model_fit # Return diagnostics and a plot of the first 10 parameters rstan::check_hmc_diagnostics(mf) rstan::plot(mf) ## End(Not run)
## Not run: data(panas) # Fit BEKK(1,1) mgarch model with a ARMA(1,1) meanstructure, # and student-t residual distribution fit <- bmgarch(panas, parameterization = "BEKK", P = 1, Q = 1, meanstructure = "arma", distribution = "Student_t") # Summarize the parameters summary(fit) # Forecast 5 ahead fit.fc <- forecast(fit, ahead = 5) print(fit.fc) # Plot mean forecasts plot(fit.fc, type = "mean") # Plot variance forecasts plot(fit.fc, type = "var") # Plot correlation forecasts plot(fit.fc, type = "cor") # Plot modeled data ("backcasted values"). plot(fit, type = "mean") # Save "backcasted" values fit.bc <- fitted(fit) # Save estimated and forecasted data as a data.frame df.fc <- as.data.frame(fit.fc) # Access rstan's model fit object mf <- fit$model_fit # Return diagnostics and a plot of the first 10 parameters rstan::check_hmc_diagnostics(mf) rstan::plot(mf) ## End(Not run)
Collect bmgarch objects into list.
bmgarch_list(...)
bmgarch_list(...)
... |
bmgarch objects. |
List of bmgarch objects. Class: bmgarch_list and bmgarch.
Extracts the model-predicted means, variances, and correlations for the fitted data.
## S3 method for class 'bmgarch' fitted( object, CrI = c(0.025, 0.975), digits = 2, weights = NULL, inc_samples = FALSE, ... )
## S3 method for class 'bmgarch' fitted( object, CrI = c(0.025, 0.975), digits = 2, weights = NULL, inc_samples = FALSE, ... )
object |
bmgarch object. |
CrI |
Numeric vector (Default: |
digits |
Integer (Default: 2, optional). Number of digits to round to when printing. |
weights |
Takes weights from model_weight function. Defaults to 1 – this parameter is not typically set by user. |
inc_samples |
Logical (Default: FALSE). Whether to return the MCMC samples for the fitted values. |
... |
Not used. |
Whereas forecast.bmgarch
computes the forecasted values for future time periods, fitted.bmgarch
computes the backcasted (model-predicted) values for the observed time periods.
fitted.bmgarch object. List containing meta
data and the backcast
. Backcast is a list containing three elements:
[N, 7, TS]
array of mean backcasts, where N is the timeseries length, and TS is the number of time series. E.g., bc$backcast$mean[3,,"tsA"]
is the mean backcast for the third observation in time series "tsA".
[N, 7, TS]
array of variance backcasts, where N is the timeseries length, and TS is the number of time series. E.g., bc$backcast$var[3,,"tsA"]
is the variance backcast for the third observation in time series "tsA".
[N, 7, TS(TS - 1)/2]
array of correlation backcasts, where N is the timeseries length, and TS(TS - 1)/2
is the number of correlations. E.g., bc$backcast$cor[3,, "tsB_tsA"]
is the backcast for the correlation between "tsB" and "tsA" on the third observation. Lower triangular correlations are saved.
List
. If inc_samples is TRUE
, then a list of arrays of MCMC samples for means, vars, and cors. Each array is [Iteration, Period, ..., ...].
## Not run: data(panas) # Fit CCC(1,1) and constant meanstructure. fit <- bmgarch(panas, parameterization = "CCC", meanstructure = "constant") # Obtain fitted values fit.bc <- fitted(fit) # Print fitted values print(fit.bc) # Plot fitted values (plot.bmgarch calls fitted internally) plot(fit, type = "var") # Save fitted values as data frame fit.bc.df <- as.data.frame(fit.bc) ## End(Not run)
## Not run: data(panas) # Fit CCC(1,1) and constant meanstructure. fit <- bmgarch(panas, parameterization = "CCC", meanstructure = "constant") # Obtain fitted values fit.bc <- fitted(fit) # Print fitted values print(fit.bc) # Plot fitted values (plot.bmgarch calls fitted internally) plot(fit, type = "var") # Save fitted values as data frame fit.bc.df <- as.data.frame(fit.bc) ## End(Not run)
Estimates (weighted) forecasted means, variances, and correlations from a fitted bmgarch model.
## S3 method for class 'bmgarch' forecast( object, ahead = 1, xC = NULL, newdata = NULL, CrI = c(0.025, 0.975), seed = NA, digits = 2, weights = NULL, L = NA, method = "stacking", inc_samples = FALSE, ... )
## S3 method for class 'bmgarch' forecast( object, ahead = 1, xC = NULL, newdata = NULL, CrI = c(0.025, 0.975), seed = NA, digits = 2, weights = NULL, L = NA, method = "stacking", inc_samples = FALSE, ... )
object |
bmgarch object. |
ahead |
Integer (Default: 1). Periods to be forecasted ahead. |
xC |
Numeric vector or matrix. Covariates(s) for the constant variance terms in C, or c. Used in a log-linear model on the constant variance terms. If vector, then it acts as a covariate for all constant variance terms. If matrix, must have columns equal to number of time series, and each column acts as a covariate for the respective time series (e.g., column 1 predicts constant variance for time series 1). |
newdata |
Future datapoints for LFO-CV computation |
CrI |
Numeric vector (Default: |
seed |
Integer (Optional). Specify seed for |
digits |
Integer (Default: 2, optional). Number of digits to round to when printing. |
weights |
Takes weights from model_weight function. Defaults to 1 – this parameter is not typically set by user. |
L |
Minimal length of time series before engaging in lfocv |
method |
Ensemble methods, 'stacking' (default) or 'pseudobma' |
inc_samples |
Logical (Default: FALSE). Whether to return the MCMC samples for the fitted values. |
... |
Not used |
forecast.bmgarch object. List containing forecast
, backcast
, and meta
data.
See fitted.bmgarch
for information on backcast
.
forecast
is a list of four components:
[N, 7, TS]
array of mean forecasts, where N is the timeseries length, and TS is the number of time series. E.g., fc$forecast$mean[3,,"tsA"]
is the 3-ahead mean forecast for time series "tsA".
[N, 7, TS]
array of variance forecasts, where N is the timeseries length, and TS is the number of time series. E.g., fc$forecast$var[3,,"tsA"]
is the 3-ahead variance forecast for time series "tsA".
[N, 7, TS(TS - 1)/2]
array of correlation forecasts, where N is the timeseries length, and TS(TS - 1)/2
is the number of correlations. E.g., fc$forecast$cor[3,, "tsB_tsA"]
is the 3-ahead forecast for the correlation between "tsB" and "tsA". Lower triangular correlations are saved.
Meta-data specific to the forecast. I.e., TS_length (number ahead) and xC.
List
. If inc_samples is TRUE
, then a list of arrays of MCMC samples for means, vars, and cors. Each array is [Iteration, Period, ..., ...].
## Not run: data(panas) # Fit DCC(2,2) with constant mean structure. fit <- bmgarch(panas, parameterization = "DCC", P = 2, Q = 2, meanstructure = "constant") # Forecast 8 ahead fit.fc <- forecast(fit, ahead = 8) # Print forecasts fit.fc print(fit.fc) # Plot variance forecasts plot(fit.fc, type = "var") # Plot correlation forecasts plot(fit.fc, type = "cor") # Save backcasted and forecasted values as data frame. fit.fc.df <- as.data.frame(fit.fc) # Save only forecasted values as data frame. fit.fc.df <- as.data.frame(fit.fc, backcast = FALSE) # Add another model, compute model weights and perform a model weighted forecast # Fit a DCC(1,1) model fit1 <- bmgarch(panas, parameterization = "DCC", P = 1, Q = 1, meanstructure = "constant") # Compute model stacking weights based on the last 19 time points (with L = 80) blist <- bmgarch_list( fit1, fit ) mw <- model_weights(blist, L = 80) # Weighted forecasts: w.fc <- forecast(object = blist, ahead = 8, weights = mw) ## End(Not run)
## Not run: data(panas) # Fit DCC(2,2) with constant mean structure. fit <- bmgarch(panas, parameterization = "DCC", P = 2, Q = 2, meanstructure = "constant") # Forecast 8 ahead fit.fc <- forecast(fit, ahead = 8) # Print forecasts fit.fc print(fit.fc) # Plot variance forecasts plot(fit.fc, type = "var") # Plot correlation forecasts plot(fit.fc, type = "cor") # Save backcasted and forecasted values as data frame. fit.fc.df <- as.data.frame(fit.fc) # Save only forecasted values as data frame. fit.fc.df <- as.data.frame(fit.fc, backcast = FALSE) # Add another model, compute model weights and perform a model weighted forecast # Fit a DCC(1,1) model fit1 <- bmgarch(panas, parameterization = "DCC", P = 1, Q = 1, meanstructure = "constant") # Compute model stacking weights based on the last 19 time points (with L = 80) blist <- bmgarch_list( fit1, fit ) mw <- model_weights(blist, L = 80) # Weighted forecasts: w.fc <- forecast(object = blist, ahead = 8, weights = mw) ## End(Not run)
lfocv
returns the LFO-CV ELPD by either computing the exact ELDP or
by approximating it via
forward or backward approximation strategies based on Pareto smoothed
importance sampling
described in (Bürkner et al. 2020).
## S3 method for class 'bmgarch' loo(x, ..., type = "lfo", L = NULL, M = 1, mode = "backward")
## S3 method for class 'bmgarch' loo(x, ..., type = "lfo", L = NULL, M = 1, mode = "backward")
x |
Fitted bmgarch model. |
... |
Not used |
type |
Takes |
L |
Minimal length of times series before computing LFO |
M |
M step head predictions. Defines to what period the LFO-CV should be tuned to. Defaults to M=1. |
mode |
backward elpd_lfo approximation, or exact elpd-lfo;
Takes 'backward', and 'exact'. 'exact' fits N-L models and may
take a very long time to complete. |
Approximate LFO-CV value and log-likelihood values across (L+1):N timepoints
Bürkner P, Gabry J, Vehtari A (2020). “Approximate leave-future-out cross-validation for Bayesian time series models.” Journal of Statistical Computation and Simulation, 1–25. doi:10.1080/00949655.2020.1783262.
## Not run: data(stocks) # Fit a DCC model fit <- bmgarch(data = stocks[1:100, c("toyota", "nissan" )], parameterization = "DCC", standardize_data = TRUE, iterations = 500) # Compute expected log-predictive density (elpd) using the backward mode # L is the upper boundary of the time-series before we engage in LFO-CV lfob <- loo(fit, mode = 'backward', L = 50 ) print(lfob) ## End(Not run)
## Not run: data(stocks) # Fit a DCC model fit <- bmgarch(data = stocks[1:100, c("toyota", "nissan" )], parameterization = "DCC", standardize_data = TRUE, iterations = 500) # Compute expected log-predictive density (elpd) using the backward mode # L is the upper boundary of the time-series before we engage in LFO-CV lfob <- loo(fit, mode = 'backward', L = 50 ) print(lfob) ## End(Not run)
Compute model weights for a list of candidate models based on leave-future-out
cross validation (lfocv) expected log-predictive density (elpd).
elpd can be approximated via the 'backward' mode described in Bürkner et al. (2020) or via exact cross-validation.
The obtained weights can be passed to the forecast function to obtain weighted forecasts.
bmgarch_objects
takes a bmgarch_object
lists.
model_weights( bmgarch_objects = NULL, L = NULL, M = 1, method = "stacking", mode = "backward" )
model_weights( bmgarch_objects = NULL, L = NULL, M = 1, method = "stacking", mode = "backward" )
bmgarch_objects |
list of bmgarch model objects in |
L |
Minimal length of time series before engaging in lfocv |
M |
M step head predictions. Defines to what period the LFO-CV should be tuned to. Defaults to M=1. |
method |
Ensemble methods, 'stacking' (default) or 'pseudobma' |
mode |
Either 'backward' (default) or 'exact' |
‘model_weights()' is a wrapper around the leave-future-out ’lfo' type in 'loo.bmgarch()'. The weights can be either obtained from an approximate or exact leave-future-out cross-validation to compute expected log predictive density (ELPD).
We can either obtain stacking weights or pseudo-BMA+ weigths as described in (Yao et al. 2018).
Model weights
Bürkner P, Gabry J, Vehtari A (2020).
“Approximate leave-future-out cross-validation for Bayesian time series models.”
Journal of Statistical Computation and Simulation, 1–25.
doi:10.1080/00949655.2020.1783262.
Yao Y, Vehtari A, Simpson D, Gelman A (2018).
“Using Stacking to Average Bayesian Predictive Distributions.”
Bayesian Analysis, 13(3), 917–1007.
doi:10.1214/17-BA1091.
## Not run: data(stocks) # Fit at least two models on a subset of the stocks data # to compute model weights fit <- bmgarch(data = stocks[1:100, c("toyota", "nissan" )], parameterization = "DCC", standardize_data = TRUE, iterations = 500) fit2 <- bmgarch(data = stocks[1:100, c("toyota", "nissan" )], P = 2, Q = 2, parameterization = "DCC", standardize_data = TRUE, iterations = 500) # create a bmgarch_list object blist <- bmgarch_list(fit, fit2 ) # Compute model weights with the default stacking metod # L is the upper boundary of the time-series before we engage in LFO-CV mw <- model_weights( blist, L = 50, method = 'stacking', order = 'backwards' ) # Print model weights in the ordert of the bmgarch_list() print(mw) ## End(Not run)
## Not run: data(stocks) # Fit at least two models on a subset of the stocks data # to compute model weights fit <- bmgarch(data = stocks[1:100, c("toyota", "nissan" )], parameterization = "DCC", standardize_data = TRUE, iterations = 500) fit2 <- bmgarch(data = stocks[1:100, c("toyota", "nissan" )], P = 2, Q = 2, parameterization = "DCC", standardize_data = TRUE, iterations = 500) # create a bmgarch_list object blist <- bmgarch_list(fit, fit2 ) # Compute model weights with the default stacking metod # L is the upper boundary of the time-series before we engage in LFO-CV mw <- model_weights( blist, L = 50, method = 'stacking', order = 'backwards' ) # Print model weights in the ordert of the bmgarch_list() print(mw) ## End(Not run)
A dataset containing simulated values for Positive and Negative Affect scores across 200 measurement occasions for a single individual.
panas
panas
Data frame with 200 rows and 2 variables:
Positive Affect score
Negative Affect score
Plot method for bmgarch objects.
## S3 method for class 'bmgarch' plot(x, type = "mean", askNewPage = TRUE, CrI = c(0.025, 0.975), ...)
## S3 method for class 'bmgarch' plot(x, type = "mean", askNewPage = TRUE, CrI = c(0.025, 0.975), ...)
x |
bmgarch object. |
type |
String (Default: "mean"). Whether to plot conditional means ("mean"), variance ("var"), or correlations ("cor"). |
askNewPage |
askNewPage Logical (Default: True). Whether to ask for new plotting page. |
CrI |
CrI Numeric vector (Default: |
... |
Not used |
List of ggplot objects (one per time series).
Stephen R. Martin
Plot method for forecast.bmgarch objects.
## S3 method for class 'forecast.bmgarch' plot(x, type = "mean", askNewPage = TRUE, last_t = 100, ...)
## S3 method for class 'forecast.bmgarch' plot(x, type = "mean", askNewPage = TRUE, last_t = 100, ...)
x |
forecast.bmgarch object. See |
type |
String (Default: "mean"). Whether to plot conditional means ("mean"), variance ("var"), or correlations ("cor"). |
askNewPage |
Logical (Default: True). Whether to ask for new plotting page. |
last_t |
Integer (Default: 100). Only show |
... |
Not used |
List of ggplot objects (one per time series).
Stephen R. Martin
Print method for fitted.bmgarch objects.
## S3 method for class 'fitted.bmgarch' print(x, ...)
## S3 method for class 'fitted.bmgarch' print(x, ...)
x |
fitted.bmgarch object. |
... |
Not used. |
object (invisible).
Stephen R. Martin
Print method for forecast.bmgarch objects.
## S3 method for class 'forecast.bmgarch' print(x, ...)
## S3 method for class 'forecast.bmgarch' print(x, ...)
x |
forecast.bmgarch object. See |
... |
Not used. |
x (invisible).
Stephen R. Martin
print method for lfocv
## S3 method for class 'loo.bmgarch' print(x, ...)
## S3 method for class 'loo.bmgarch' print(x, ...)
x |
lfo object |
... |
Not used. |
Invisible lfocv object
philippe
Print method for model_weights
## S3 method for class 'model_weights' print(x, ...)
## S3 method for class 'model_weights' print(x, ...)
x |
Model weights object |
... |
Not used. |
model_weights objects with weights, list of log-likelihoods, and r_eff_list
philippe
Print method for bmgarch.summary objects.
## S3 method for class 'summary.bmgarch' print(x, ...)
## S3 method for class 'summary.bmgarch' print(x, ...)
x |
summary.bmgarch object. |
... |
Not used. |
x (invisible).
Philippe Rast, Stephen R. Martin
A dataset used by Stata to illustrate MGARCH models containing daily data on returns of Toyota, Nissan, and Honda stocks.
stocks
stocks
Data frame with 2015 rows and 5 variables:
Date
Sequential time index
Daily returns for Toyota stock
Daily returns for Nissan stock
Daily returns for Honda stock
Computes posterior summaries for all parameters of interest for bmgarch objects.
## S3 method for class 'bmgarch' summary(object, CrI = c(0.025, 0.975), digits = 2, ...)
## S3 method for class 'bmgarch' summary(object, CrI = c(0.025, 0.975), digits = 2, ...)
object |
bmgarch object. |
CrI |
Numeric vector (Default: |
digits |
Integer (Default: 2, optional). Number of digits to round to when printing. |
... |
Not used. |
summary.bmgarch object. A named list containing "meta" and "model_summary". model_summary
contains summary table for all model parameters.
Stephen R. Martin, Philippe Rast