library(changepoints)
This is a simple guide for offline change points detection in transition matrix of a VAR1 model.
Function simu.VAR1 simulates a segment of data from a VAR1 model with fixed transition matrix. We prepare data with change points by combining several such segments with different transition matrices.
Dynamic programming and local refinement are implemented for VAR1 change points detection:
## parameters for simulating data
= 0.4 #candidate of rho (rho can not be too large so that the time series is stable)
rho = 20 # dimension
p = 1 # standard deviation of error terms
sigma = 10 # minimum gap for DP
delta1 = 10 # minimum gap for local.refine
delta.local = 30 # length for each segment
n = 2*(seq(1,p,1)%%2) - 1
v1 = -v1
v2 = matrix(0, nrow = p, ncol = p-2)
AA = rho * cbind(v1,v2,AA) # transition matrix for the first segment
A1 = rho * cbind(v2,v1,AA) # transition matrix for the second segment
A2 = A1 # transition matrix for the third segment
A3
set.seed(123)
# generate data
= simu.VAR1(sigma, p, 2*n+1, A1)
data = cbind(data, simu.VAR1(sigma, p, 2*n, A2, vzero=c(data[,ncol(data)])))
data = cbind(data, simu.VAR1(sigma, p, 2*n, A3, vzero=c(data[,ncol(data)])))
data = c(2*n, 4*n) cpt_true
= c(0.1, 0.5, 1, 2, 5) # a set of tuning parameters for DP
gamma_set = c(0.1, 0.5, 1, 2, 5) # a set of tuning parameters for lasso
lambda_set = CV.search.DP.VAR1(data, gamma_set, lambda_set, delta1) # grid search through cross-validation
DP_result = as.vector(arrayInd(which.min(DP_result$test_error), dim(DP_result$test_error)))# select gamma achieves the minimum validation error
min_idx = unlist(DP_result$cpt_hat[min_idx[1], min_idx[2]]) # estimated changepoints by DP
cpt_DP_hat
cpt_DP_hat#> [1] 66 124
Hausdorff.dist(cpt_DP_hat, cpt_true)
#> [1] 6
= c(1, 1.5, 2) # tuning parameter for group lasso
zeta_set = local.refine.CV.VAR1(cpt_DP_hat, data, zeta_set, delta.local) # perform local refinement
cpt_DPlr_hat
cpt_DPlr_hat#> $cpt_hat
#> [1] 60 120
#>
#> $zeta
#> [1] 1.5
Hausdorff.dist(cpt_DPlr_hat$cpt_hat, cpt_true)
#> [1] 0