if (FALSE) {
set.seed(123)
## Simulating a factor process (Heston model)
drift <- c("mu*S", "-theta*(V-v)")
diffusion <- matrix(c("sqrt(max(V,0))*S", "gamma*sqrt(max(V,0))*rho",
0, "gamma*sqrt(max(V,0))*sqrt(1-rho^2)"),
2,2)
mod <- setModel(drift = drift, diffusion = diffusion,
state.variable = c("S", "V"))
n <- 2340
samp <- setSampling(n = n)
heston <- setYuima(model = mod, sampling = samp)
param <- list(mu = 0.03, theta = 3, v = 0.09,
gamma = 0.3, rho = -0.6)
result <- simulate(heston, xinit = c(1, 0.1),
true.parameter = param)
zdata <- get.zoo.data(result) # extract the zoo data
X <- log(zdata[[1]]) # log-price process
V <- zdata[[2]] # squared volatility process
## Simulating a residual process (correlated BM)
d <- 100 # dimension
Q <- 0.1 * toeplitz(0.7^(1:d-1)) # residual covariance matrix
dZ <- matrix(rnorm(n*d),n,d) %*% chol(Q)/sqrt(n)
Z <- zoo(apply(dZ, 2, "diffinv"), samp@grid[[1]])
## Constructing observation data
b <- runif(d, 0.25, 2.25) # factor loadings
Y <- X %o% b + Z
yuima <- setData(cbind(X, Y))
# We subsample yuima to construct observation data
yuima <- subsampling(yuima, setSampling(n = 78))
## Estimating the covariance matrix (factor is known)
cmat <- tcrossprod(b) * mean(V[-1]) + Q # true covariance matrix
pmat <- solve(cmat) # true precision matrix
# (1) Regularization method is glasso (the default)
est <- cce.factor(yuima, factor = 1)
norm(est$covmat.y - cmat, type = "2")
norm(est$premat.y - pmat, type = "2")
# (2) Regularization method is tapering
est <- cce.factor(yuima, factor = 1, regularize = "tapering")
norm(est$covmat.y - cmat, type = "2")
norm(est$premat.y - pmat, type = "2")
# (3) Regularization method is thresholding
est <- cce.factor(yuima, factor = 1, regularize = "thresholding")
norm(est$covmat.y - cmat, type = "2")
norm(est$premat.y - pmat, type = "2")
# (4) Regularization method is eigen.cleaning
est <- cce.factor(yuima, factor = 1, regularize = "eigen.cleaning")
norm(est$covmat.y - cmat, type = "2")
norm(est$premat.y - pmat, type = "2")
## Estimating the covariance matrix (factor is unknown)
yuima2 <- setData(Y)
# We subsample yuima to construct observation data
yuima2 <- subsampling(yuima2, setSampling(n = 78))
# (A) Ignoring the factor structure (regularize = "glasso")
est <- cce.factor(yuima2)
norm(est$covmat.y - cmat, type = "2")
norm(est$premat.y - pmat, type = "2")
# (B) Estimating the factor by PCA (regularize = "glasso")
est <- cce.factor(yuima2, PCA = TRUE, nfactor = 1) # use 1 factor
norm(est$covmat.y - cmat, type = "2")
norm(est$premat.y - pmat, type = "2")
# One can interactively select the number of factors
# after implementing PCA (the scree plot is depicted)
# Try: est <- cce.factor(yuima2, PCA = TRUE)
}
Run the code above in your browser using DataLab