2
0
Fork 0
CVE/CVE_legacy/Test_weigthed_cve.R

114 lines
5.0 KiB
R

#C:\Users\Lukas\Desktop\owncloud\Shared\Lukas\CVE
install.packages("C:/Users/Lukas/Desktop/owncloud/Shared/Lukas/CVE_1.0.tar.gz", repos=NULL, type="source")
install.packages(file.choose(), repos=NULL, type="source")
dim<-12
N<-100
s<-0.5
dat<-creat_sample(rep(1,dim)/sqrt(dim),N,fsquare,0.5)
test<-cve(Y~.,data=as.data.frame(dat),k=1)
##############
#initialize model parameterss
m<-100 #number of replications in simulation
dim<-12 #dimension of random variable X
truedim<-2 #dimension of B=b
qs<-dim-truedim # dimension of orthogonal complement of B
b1=c(1,1,1,1,1,1,0,0,0,0,0,0)/sqrt(6)
b2=c(1,-1,1,-1,1,-1,0,0,0,0,0,0)/sqrt(6)
b<-cbind(b1,b2)
#b<-b1
P<-b%*%t(b)
sigma=0.5 #error standard deviation
N<-70 #sample size
K<-30 #number of arbitrary starting values for curvilinear optimization
MAXIT<-30 #maximal number of iterations in curvilinear search algorithm
var_vec<-mat.or.vec(m,12)
M1_weight<-mat.or.vec(m,13)
#colnames(M1_weight)<-c('CVE1','CVE2','CVE3','CVE1_Rcpp','CVE2_Rcpp','CVE3_Rcpp','meanMAVE','csMAVE','phd','sir','save','CVE4')
#link function for M1
fM1<-function(x){return(x[1]/(0.5+(x[2]+1.5)^2))}
for (i in 1:m){
#generate dat according to M1
dat<-creat_sample_nor_nonstand(b,N,fsquare,diag(rep(1,dim)),sigma)
#est sample covariance matrix
Sig_est<-est_varmat(dat[,-1])
#est trace of sample covariance matrix
tr<-var_tr(Sig_est)
#calculates Vhat_k for CVE1,CVE2, CVE3 for k=qs
CVE1<-stiefl_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(0.8),tr=tr),maxit = MAXIT,sclack_para = 0)
CVE2<-stiefl_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(2/3),tr=tr),maxit = MAXIT,sclack_para = 0)
CVE3<-stiefl_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(0.5),tr=tr),maxit = MAXIT,sclack_para = 0)
CVE4<-stiefl_weight_partial_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(0.8),tr=tr),maxit = MAXIT,sclack_para = 0)
CVE5<-stiefl_weight_partial_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(2/3),tr=tr),maxit = MAXIT,sclack_para = 0)
CVE6<-stiefl_weight_partial_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(0.5),tr=tr),maxit = MAXIT,sclack_para = 0)
#
CVE7<-stiefl_weight_full_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(0.8),tr=tr),maxit = MAXIT,sclack_para = 0)
CVE8<-stiefl_weight_full_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(2/3),tr=tr),maxit = MAXIT,sclack_para = 0)
CVE9<-stiefl_weight_full_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(0.5),tr=tr),maxit = MAXIT,sclack_para = 0)
#
#
var_vec[i,1]<-CVE1$var
var_vec[i,2]<-CVE2$var
var_vec[i,3]<-CVE3$var
var_vec[i,4]<-CVE4$var
var_vec[i,5]<-CVE5$var
var_vec[i,6]<-CVE6$var
#
var_vec[i,7]<-CVE7$var
var_vec[i,8]<-CVE8$var
var_vec[i,9]<-CVE9$var
CVE1$est_base<-fill_base(CVE1$est_base)
CVE2$est_base<-fill_base(CVE2$est_base)
CVE3$est_base<-fill_base(CVE3$est_base)
CVE4$est_base<-fill_base(CVE4$est_base)
CVE5$est_base<-fill_base(CVE5$est_base)
CVE6$est_base<-fill_base(CVE6$est_base)
CVE7$est_base<-fill_base(CVE7$est_base)
CVE8$est_base<-fill_base(CVE8$est_base)
CVE9$est_base<-fill_base(CVE9$est_base)
# calculate distance between true B and estimated B
M1_weight[i,1]<-subspace_dist(CVE1$est_base[,1:truedim],b)
M1_weight[i,2]<-subspace_dist(CVE2$est_base[,1:truedim],b)
M1_weight[i,3]<-subspace_dist(CVE3$est_base[,1:truedim],b)
M1_weight[i,4]<-subspace_dist(CVE4$est_base[,1:truedim],b)
M1_weight[i,5]<-subspace_dist(CVE5$est_base[,1:truedim],b)
M1_weight[i,6]<-subspace_dist(CVE6$est_base[,1:truedim],b)
M1_weight[i,7]<-subspace_dist(CVE7$est_base[,1:truedim],b)
M1_weight[i,8]<-subspace_dist(CVE8$est_base[,1:truedim],b)
M1_weight[i,9]<-subspace_dist(CVE9$est_base[,1:truedim],b)
CVE1_Rcpp<-cve(Y~.,data=as.data.frame(dat),k=truedim,nObs=N^0.8,attempts=K,tol=10^(-3),slack=0)[[2]]
CVE2_Rcpp<-cve(Y~.,data=as.data.frame(dat),k=truedim,nObs=N^(2/3),attempts=K,tol=10^(-3),slack=0)[[2]]
CVE3_Rcpp<-cve(Y~.,data=as.data.frame(dat),k=truedim,nObs=N^0.5,attempts=K,tol=10^(-3),slack=0)[[2]]
# CVE4_Rcpp<-cve(Y~.,data=as.data.frame(dat),k=truedim,h=h_opt,attempts=K,tol=10^(-3))[[2]]
#M1_Rcpp[i,12]<-subspace_dist(CVE4_Rcpp$B,b)
var_vec[i,10]<-CVE1_Rcpp$loss
var_vec[i,11]<-CVE2_Rcpp$loss
var_vec[i,12]<-CVE3_Rcpp$loss
#calculate orthogonal complement of Vhat_k
#i.e. CVE1$est_base[,1:truedim] is estimator for B with dimension (dim times (dim-qs))
M1_weight[i,10]<-subspace_dist(CVE1_Rcpp$B,b)
M1_weight[i,11]<-subspace_dist(CVE2_Rcpp$B,b)
M1_weight[i,12]<-subspace_dist(CVE3_Rcpp$B,b)
#meanMAVE
mod_t2<-mave(Y~.,data=as.data.frame(dat),method = 'meanMAVE')
M1_weight[i,13]<-subspace_dist(mod_t2$dir[[truedim]],b)
print(paste(i,paste('/',m)))
}
boxplot(M1_weight[1:(i-1),]/sqrt(2*truedim),names=colnames(M1_weight),ylab='err',main='M1')
summary(M1_weight[1:(i-1),])