The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

example_VAR

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:

  1. DP.VAR1: performs dynamic programming for VAR1 change points detection.
  2. local.refine.VAR1: performs local refinement for an initial change points estimation.

Simulate data

## parameters for simulating data
rho = 0.4 #candidate of rho (rho can not be too large so that the time series is stable)
p = 20 # dimension
sigma = 1 # standard deviation of error terms
delta1 = 10 # minimum gap for DP
delta.local = 10 # minimum gap for local.refine
n = 30 # length for each segment
v1 = 2*(seq(1,p,1)%%2) - 1
v2 = -v1
AA = matrix(0, nrow = p, ncol = p-2)
A1 = rho * cbind(v1,v2,AA) # transition matrix for the first segment
A2 = rho * cbind(v2,v1,AA) # transition matrix for the second segment
A3 = A1 # transition matrix for the third segment

set.seed(123)
# generate data
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)])))
cpt_true = c(2*n, 4*n)

Perform dynamic programming

gamma_set = c(0.1, 0.5, 1, 2, 5) # a set of tuning parameters for DP
lambda_set = c(0.1, 0.5, 1, 2, 5) # a set of tuning parameters for lasso
DP_result = CV.search.DP.VAR1(data, gamma_set, lambda_set, delta1) # grid search through cross-validation
min_idx = as.vector(arrayInd(which.min(DP_result$test_error), dim(DP_result$test_error)))# select gamma achieves the minimum validation error
cpt_DP_hat = unlist(DP_result$cpt_hat[min_idx[1], min_idx[2]]) # estimated changepoints by DP
cpt_DP_hat
#> [1]  66 124
Hausdorff.dist(cpt_DP_hat, cpt_true)
#> [1] 6
zeta_set = c(1, 1.5, 2) # tuning parameter for group lasso
cpt_DPlr_hat = local.refine.CV.VAR1(cpt_DP_hat, data, zeta_set, delta.local) # perform local refinement
cpt_DPlr_hat
#> $cpt_hat
#> [1]  60 120
#> 
#> $zeta
#> [1] 1.5
Hausdorff.dist(cpt_DPlr_hat$cpt_hat, cpt_true)
#> [1] 0

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.