Skip to content

Commit

Permalink
reduce compilation time of article2.Rnw; update reference to stochvol…
Browse files Browse the repository at this point in the history
…; update example; remove some dependencies
  • Loading branch information
hdarjus committed Nov 24, 2020
1 parent ee8f6a4 commit d9070a8
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 24 deletions.
6 changes: 2 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ License: GPL (>= 2)
Depends: R (>= 3.5)
Imports: Rcpp (>= 1.0), coda (>= 0.19), graphics, stats, utils, grDevices
Suggests:
mvtnorm (>= 1.1-1),
mvtnorm,
testthat (>= 2.3.2),
magrittr (>= 1.5),
knitr, LSD, RColorBrewer,
zoo
knitr
LinkingTo: Rcpp, RcppArmadillo (>= 0.9.900)
RoxygenNote: 7.1.1
BuildResaveData: best
Expand Down
4 changes: 2 additions & 2 deletions inst/examples/svsample.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ res <- svsample(y, draws = 30000, burnin = 10000,
parallel = "multicore", n_chains = 3, n_cpus = 2)

# Plot using a color palette
palette(rainbow(coda::nchain(para(res))))
palette(rainbow(coda::nchain(para(res, "all"))))
plot(res)

# Use functionality from package 'coda'
## E.g. Geweke's convergence diagnostics
coda::geweke.diag(para(res)[, c("mu", "phi", "sigma")])
coda::geweke.diag(para(res, "all")[, c("mu", "phi", "sigma")])

# Use functionality from package 'bayesplot'
bayesplot::mcmc_pairs(res, pars = c("sigma", "mu", "phi", "h_0", "h_15"))
Expand Down
4 changes: 2 additions & 2 deletions man/svsample.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions tests/testthat/test-samplers.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ context("Samplers are correct")

test_that("vanilla SV passes Geweke test", {
skip_on_cran()
skip_if_not_installed(pkg = "magrittr", minimum_version = "1.5")
library("magrittr")

# general helper functions
increment_fun <- function (mu, phi, sigma) {
function (h_t, eps) { mu + phi * (h_t - mu) + sigma * eps }
}
generate_h <- function (len, increment_fun, h0) {
increment_fun %>%
Reduce(rnorm(len), h0,
accumulate = TRUE) %>%
tail(-1) # remove h0 from h
res <- Reduce(increment_fun, rnorm(len), h0,
accumulate = TRUE)
res <- tail(res, -1) # remove h0 from h
}
omori_constants <- get_omori_constants()
p <- omori_constants$prob
Expand Down
18 changes: 9 additions & 9 deletions vignettes/article2.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ ind <- which(exrates$date >= as.Date("2008-03-01") &
exrates$date <= as.Date("2012-03-01"))
CHF_price <- exrates$CHF[ind]
@
<<svrunmodel, results='hide', echo=TRUE>>=
<<svrunmodel, eval=FALSE, echo=TRUE>>=
<<presvrunmodel>>
res_sv <- svsample(CHF_price, designmatrix = "ar1")
@
Expand All @@ -584,7 +584,7 @@ Then, to estimate the constant mean model with heavy tailed SV residuals, we pas
set.seed(2)
CHF_logret <- 100 * logret(CHF_price)
@
<<svtrunmodel, echo=TRUE, results='hide'>>=
<<svtrunmodel, echo=TRUE, eval=FALSE>>=
<<presvtrunmodel>>
res_svt <- svtsample(CHF_logret, designmatrix = "ar0")
@
Expand All @@ -608,7 +608,7 @@ set.seed(3)
X <- cbind(constant = 1,
100 * logret(exrates$USD[ind]),
100 * logret(exrates$JPY[ind]))
res_svl <- svlsample(CHF_logret, designmatrix = X)
res_svl <- svlsample(CHF_logret, designmatrix = X, thin = 10)
@

\subsection{Visualizing the results} \label{sec:svuse2}
Expand Down Expand Up @@ -747,7 +747,7 @@ They expect as their first argument the input data $\yvec_{[1:L]}$, which is of
For estimating the SV model in each time window $j=1,\dots,J$ in the moving or expanding window scheme, the sub-vector $\yvec_{[j:(t+j-1)]}$, or, respectively, $\yvec_{[1:(t+j-1)]}$, is taken as data and is used to predict $n_\text{ahead}\ge1$ time steps ahead.
The width $t$ of the first time window can be determined from $L$, $J$, and $n_\text{ahead}$.
The following example demonstrates how the rolling window sampling routines can be called in \stochvol{}.
<<svroll, echo=TRUE, results='hide'>>=
<<svroll, echo=TRUE, eval=FALSE>>=
set.seed(5)
res <- svsample_roll(CHF_logret, n_ahead = 1,
forecast_length = 30,
Expand All @@ -769,7 +769,7 @@ The prior for $\nupar$ can be influenced in \fct{svtsample} and \fct{svtlsample}
Finally, \fct{svlsample} and \fct{svtlsample} take the \code{numeric} sequence $(\rhopriorf,\rhopriors)$ through the input argument \code{priorrho}.

The code snippet below shows all the default values of the prior hyperparameters.
<<printpriordefault, echo=TRUE, results='hide'>>=
<<printpriordefault, echo=TRUE, eval=FALSE>>=
svsample(CHF_logret, priormu = c(0, 100), priorphi = c(5, 1.5),
priorsigma = 1, priorbeta = c(0, 10000))
svtsample(CHF_logret, priormu = c(0, 100), priorphi = c(5, 1.5),
Expand Down Expand Up @@ -804,7 +804,7 @@ Additionally, \code{latent0\_variance} accepts the character value \code{"statio
All four sampling methods accept the prior specification object through the input argument \code{priorspec}.

All input arguments for \code{specify\_priors} are optional, their default values and how they are used is seen below.
<<printpriorspecdefault, echo=TRUE, results='hide'>>=
<<printpriorspecdefault, echo=TRUE, eval=FALSE>>=
ps <- specify_priors(
mu = sv_normal(mean = 0, sd = 100),
phi = sv_beta(shape1 = 5, shape2 = 1.5),
Expand Down Expand Up @@ -850,7 +850,7 @@ Finally, in case ``SNOW'' is applied, the sampling routines optionally accept an
As mentioned earlier, the sampling algorithms for the latent states $\hvec$ in \stochvol{} rely on a Gaussian mixture approximation as in~\citet{omori2007stochastic} and~\citet{kastner2014ancillarity}.
The approximation tends to be very good, therefore the default setting is not to correct for model misspecification.
However, this correction can be enabled in all of the sampling routines through the \code{expert} argument as shown for \fct{svsample} in the following.
<<results='hide'>>=
<<eval=FALSE>>=
y <- svsim(50)$y
svsample(y, expert = list(correct_model_misspecification = TRUE))
@
Expand All @@ -862,7 +862,7 @@ The default behavior is to store every draw after the burn-in phase.

\section[The factorstochvol package]{The \factorstochvol{} package} \label{sec:factorstochvol}

For more information on the \factorstochvol{} package, please see its vignette on CRAN.
For more information on \factorstochvol{}, please see the vignette attached to the package~\citep{factorstochvol}.
%
%The most common workflow of using \factorstochvol{} for fitting multivariate factor SV models consists of the following steps:
%(1) Prepare the data,
Expand Down Expand Up @@ -1238,7 +1238,7 @@ We extended the work of~\citet{kastner2016dealing} to other SV models, including
We showcased the features that are the most important to end users in \proglang{R}: estimation through the sampler functions, visualization, summary, and prediction methods.
Due to its more involved nature, however, we did not include the description of the \proglang{C++} interface.
Two functions called \fct{update\_fast\_sv} and \fct{update\_general\_sv} are exported and programmers have the possibility to access the samplers in \stochvol{} directly from \proglang{C++} after linking to the compiled package.
For usage examples, see the implementations of \factorstochvol{} or \pkg{shrinkTVP} \citep{shrinktvp}.
For usage examples, see the implementations of \factorstochvol{} or \pkg{shrinkTVP}~\citep{shrinktvp}.
%
\bibliography{ref}

Expand Down
2 changes: 1 addition & 1 deletion vignettes/ref.bib
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ @manual{kastner2020stochvol
title = {\pkg{stochvol}: Efficient Bayesian Inference for Stochastic Volatility ({SV}) Models},
author = {Hosszejni, Darjus and Kastner, Gregor},
year = {2020},
note = {\proglang{R} package version 3.0.1},
note = {\proglang{R} package version 3.0.3},
url = {http://CRAN.R-project.org/package=stochvol}
}

Expand Down

0 comments on commit d9070a8

Please sign in to comment.