# NOT RUN {
# The following is an example to detect covariance structure change
# in a real-time manner, in the sense that we pretend that
# the observations in the dataset continually arrive in time.
# At each time, we determine whether the process should be
# terminated through the proposed stopping rule.
# there is an immediate change point at n0=200
p<-200;n<-10000;n0<-200 #n0 is traing sample size
rho<-0.6;M<-2
H<-100;ARL<-5000
Gam1<-diag(1,p,p)
times<-1:p
d<-abs(outer(times, times, "-"))
sigma<-rho^d
Gam2<-eigen(sigma,symmetric=TRUE)$vectors%*%diag(sqrt(eigen(sigma,symmetric=TRUE)$values),p)
Gam<-cbind(Gam1,Gam2)
data_Mat<-matrix(0,n0,p)
L<-M+1
Z<-matrix(rnorm(p*(n+L-1)),p*(n+L-1),1)
vec.coef<-1/rep(c(L:1),each=p)
for(j in 1:n0){
Gam.m<-Gam[,1:p]
Gam.mat<-t(apply(Gam.m,1,rep,L))*matrix(vec.coef,ncol=L*p,nrow=p,byrow=TRUE)
data_Mat[j,]<-matrix((Gam.mat%*%Z[((j-1)*p+1):((j+L-1)*p),]),1,p,byrow=FALSE)
}
old.data<-data_Mat
nuisance.results<-nuisance.est(old.data)
mu<-nuisance.results$mu.hat
M<-nuisance.results$M.hat
cor<-nuisance.results$cor.hat
j<-n0+1;decision = 0
while(decision==0){
Gam.m<-Gam[,(p+1):(2*p)]
Gam.mat<-t(apply(Gam.m,1,rep,L))*matrix(vec.coef,ncol=L*p,nrow=p,byrow=TRUE)
new.data<-matrix((Gam.mat%*%Z[((j-1)*p+1):((j+L-1)*p),]),1,p,byrow=FALSE)
result<-stopping.rule(ARL,H,mu,M,cor,old.data,new.data)
decision<-result$decision
old.data<-result$old.updated
cpt.est<-j-n0
j<-j+1
}
print(cpt.est) #The point where the detection procedure terminates.
# }
Run the code above in your browser using DataLab