forked from agi-lab/reserving-ensemble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Building Three Subsets ADLP ensembles.R
185 lines (158 loc) · 9.7 KB
/
Building Three Subsets ADLP ensembles.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
##Caculate the Density of the Optimized Ensemble
ntri<-100
##Partition Strategy 1
OW_out_dens_3par_PS1<-matrix(NA,nrow=780,ncol=ntri)
model_weights_simul_3par_PS1<-list()
for (D in 1:ntri){
meta_dens_augvalid<-valid_dens_list[[D]][,-c(21,22)]
meta_dens_out<-out_dens_list[[D]][,-c(21,22)]
augvalid_meta_1_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=5,]
augvalid_meta_2_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=15,]
augvalid_meta_3_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=40,]
#Construct the Corresponding Out-of-Sample Test sets
out_meta_1_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=2&as.numeric(as.character(meta_dens_out$origin))<=5,]
out_meta_2_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=6&as.numeric(as.character(meta_dens_out$origin))<=14,]
out_meta_3_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=15&as.numeric(as.character(meta_dens_out$origin))<=40,]
#Train the model weights
meta_valid_list_aug_new1<-list(augvalid_meta_1_new1,augvalid_meta_2_new1,augvalid_meta_3_new1)
meta_test_list_aug_new1<-list(out_meta_1_aug_new1,out_meta_2_aug_new1,out_meta_3_aug_new1)
TrainL_MM_aug_new1<-list()
FinalTrainL_MM_aug_new1<-list()
TestL_MM_aug_new1<-list()
w_MM_aug_new1<-list()
finalw_MM_aug_new1<-list()
for(i in 1:3){
mat_valid<-as.matrix(meta_valid_list_aug_new1[[i]][,3:ncol(meta_valid_list_aug_new1[[i]])])
mat_test<-as.matrix(meta_test_list_aug_new1[[i]][,3:ncol(meta_test_list_aug_new1[[i]])])
optim_MM<-MM_optim(w_init_1_3,dat=mat_valid,testdat=mat_test,nIters=500)
w_MM_aug_new1[[i]]<-optim_MM$params
finalw_MM_aug_new1[[i]]<-optim_MM$finalparams
TrainL_MM_aug_new1[[i]]<-optim_MM$NLL
FinalTrainL_MM_aug_new1[[i]]<-optim_MM$finalNLL
TestL_MM_aug_new1[[i]]<--log(mat_test%*%finalw_MM_aug_new1[[i]])
}
Out_sample_EnDens_aug_new1<-list()
for (i in 1:3){
out_meta<-as.matrix(meta_test_list_aug_new1[[i]][,3:ncol(meta_test_list_aug_new1[[i]])])
Out_sample_EnDens_aug_new1[[i]]<-out_meta%*%as.vector(unlist(finalw_MM_aug_new1[[i]]))
}
OW_out_dens_3par_PS1[,D]<-as.vector(unlist(Out_sample_EnDens_aug_new1))
model_weights_simul_3par_PS1[[D]]<-finalw_MM_aug_new1
}
##Partition Strategy 2
OW_out_dens_3par_PS2<-matrix(NA,nrow=780,ncol=ntri)
model_weights_simul_3par_PS2<-list()
for (D in 1:ntri){
meta_dens_augvalid<-valid_dens_list[[D]][,-c(21,22)]
meta_dens_out<-out_dens_list[[D]][,-c(21,22)]
augvalid_meta_1_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=15,]
augvalid_meta_2_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=29,]
augvalid_meta_3_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=40,]
#Construct the Corresponding Out-of-Sample Test sets
out_meta_1_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=2&as.numeric(as.character(meta_dens_out$origin))<=15,]
out_meta_2_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=16&as.numeric(as.character(meta_dens_out$origin))<=29,]
out_meta_3_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=30&as.numeric(as.character(meta_dens_out$origin))<=40,]
#Train the model weights
meta_valid_list_aug_new1<-list(augvalid_meta_1_new1,augvalid_meta_2_new1,augvalid_meta_3_new1)
meta_test_list_aug_new1<-list(out_meta_1_aug_new1,out_meta_2_aug_new1,out_meta_3_aug_new1)
TrainL_MM_aug_new1<-list()
FinalTrainL_MM_aug_new1<-list()
TestL_MM_aug_new1<-list()
w_MM_aug_new1<-list()
finalw_MM_aug_new1<-list()
for(i in 1:3){
mat_valid<-as.matrix(meta_valid_list_aug_new1[[i]][,3:ncol(meta_valid_list_aug_new1[[i]])])
mat_test<-as.matrix(meta_test_list_aug_new1[[i]][,3:ncol(meta_test_list_aug_new1[[i]])])
optim_MM<-MM_optim(w_init_1_3,dat=mat_valid,testdat=mat_test,nIters=500)
w_MM_aug_new1[[i]]<-optim_MM$params
finalw_MM_aug_new1[[i]]<-optim_MM$finalparams
TrainL_MM_aug_new1[[i]]<-optim_MM$NLL
FinalTrainL_MM_aug_new1[[i]]<-optim_MM$finalNLL
TestL_MM_aug_new1[[i]]<--log(mat_test%*%finalw_MM_aug_new1[[i]])
}
Out_sample_EnDens_aug_new1<-list()
for (i in 1:3){
out_meta<-as.matrix(meta_test_list_aug_new1[[i]][,3:ncol(meta_test_list_aug_new1[[i]])])
Out_sample_EnDens_aug_new1[[i]]<-out_meta%*%as.vector(unlist(finalw_MM_aug_new1[[i]]))
}
OW_out_dens_3par_PS2[,D]<-as.vector(unlist(Out_sample_EnDens_aug_new1))
model_weights_simul_3par_PS2[[D]]<-finalw_MM_aug_new1
}
##Partition Strategy 3
OW_out_dens_3par_PS3<-matrix(NA,nrow=780,ncol=ntri)
model_weights_simul_3par_PS3<-list()
for (D in 1:ntri){
meta_dens_augvalid<-valid_dens_list[[D]][,-c(21,22)]
meta_dens_out<-out_dens_list[[D]][,-c(21,22)]
augvalid_meta_1_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=17,]
augvalid_meta_2_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=31,]
augvalid_meta_3_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=40,]
#Construct the Corresponding Out-of-Sample Test sets
out_meta_1_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=2&as.numeric(as.character(meta_dens_out$origin))<=17,]
out_meta_2_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=18&as.numeric(as.character(meta_dens_out$origin))<=31,]
out_meta_3_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=32&as.numeric(as.character(meta_dens_out$origin))<=40,]
#Train the model weights
meta_valid_list_aug_new1<-list(augvalid_meta_1_new1,augvalid_meta_2_new1,augvalid_meta_3_new1)
meta_test_list_aug_new1<-list(out_meta_1_aug_new1,out_meta_2_aug_new1,out_meta_3_aug_new1)
TrainL_MM_aug_new1<-list()
FinalTrainL_MM_aug_new1<-list()
TestL_MM_aug_new1<-list()
w_MM_aug_new1<-list()
finalw_MM_aug_new1<-list()
for(i in 1:3){
mat_valid<-as.matrix(meta_valid_list_aug_new1[[i]][,3:ncol(meta_valid_list_aug_new1[[i]])])
mat_test<-as.matrix(meta_test_list_aug_new1[[i]][,3:ncol(meta_test_list_aug_new1[[i]])])
optim_MM<-MM_optim(w_init_1_3,dat=mat_valid,testdat=mat_test,nIters=500)
w_MM_aug_new1[[i]]<-optim_MM$params
finalw_MM_aug_new1[[i]]<-optim_MM$finalparams
TrainL_MM_aug_new1[[i]]<-optim_MM$NLL
FinalTrainL_MM_aug_new1[[i]]<-optim_MM$finalNLL
TestL_MM_aug_new1[[i]]<--log(mat_test%*%finalw_MM_aug_new1[[i]])
}
Out_sample_EnDens_aug_new1<-list()
for (i in 1:3){
out_meta<-as.matrix(meta_test_list_aug_new1[[i]][,3:ncol(meta_test_list_aug_new1[[i]])])
Out_sample_EnDens_aug_new1[[i]]<-out_meta%*%as.vector(unlist(finalw_MM_aug_new1[[i]]))
}
OW_out_dens_3par_PS3[,D]<-as.vector(unlist(Out_sample_EnDens_aug_new1))
model_weights_simul_3par_PS3[[D]]<-finalw_MM_aug_new1
}
##Partition Strategy 4
OW_out_dens_3par_PS4<-matrix(NA,nrow=780,ncol=ntri)
model_weights_simul_3par_PS4<-list()
for (D in 1:ntri){
meta_dens_augvalid<-valid_dens_list[[D]][,-c(21,22)]
meta_dens_out<-out_dens_list[[D]][,-c(21,22)]
augvalid_meta_1_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=23,]
augvalid_meta_2_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=33,]
augvalid_meta_3_new1<-meta_dens_augvalid[as.numeric(as.character(meta_dens_augvalid$origin))>=2&as.numeric(as.character(meta_dens_augvalid$origin))<=40,]
#Construct the Corresponding Out-of-Sample Test sets
out_meta_1_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=2&as.numeric(as.character(meta_dens_out$origin))<=23,]
out_meta_2_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=24&as.numeric(as.character(meta_dens_out$origin))<=33,]
out_meta_3_aug_new1<-meta_dens_out[as.numeric(as.character(meta_dens_out$origin))>=34&as.numeric(as.character(meta_dens_out$origin))<=40,]
#Train the model weights
meta_valid_list_aug_new1<-list(augvalid_meta_1_new1,augvalid_meta_2_new1,augvalid_meta_3_new1)
meta_test_list_aug_new1<-list(out_meta_1_aug_new1,out_meta_2_aug_new1,out_meta_3_aug_new1)
TrainL_MM_aug_new1<-list()
FinalTrainL_MM_aug_new1<-list()
TestL_MM_aug_new1<-list()
w_MM_aug_new1<-list()
finalw_MM_aug_new1<-list()
for(i in 1:3){
mat_valid<-as.matrix(meta_valid_list_aug_new1[[i]][,3:ncol(meta_valid_list_aug_new1[[i]])])
mat_test<-as.matrix(meta_test_list_aug_new1[[i]][,3:ncol(meta_test_list_aug_new1[[i]])])
optim_MM<-MM_optim(w_init_1_3,dat=mat_valid,testdat=mat_test,nIters=500)
w_MM_aug_new1[[i]]<-optim_MM$params
finalw_MM_aug_new1[[i]]<-optim_MM$finalparams
TrainL_MM_aug_new1[[i]]<-optim_MM$NLL
FinalTrainL_MM_aug_new1[[i]]<-optim_MM$finalNLL
TestL_MM_aug_new1[[i]]<--log(mat_test%*%finalw_MM_aug_new1[[i]])
}
Out_sample_EnDens_aug_new1<-list()
for (i in 1:3){
out_meta<-as.matrix(meta_test_list_aug_new1[[i]][,3:ncol(meta_test_list_aug_new1[[i]])])
Out_sample_EnDens_aug_new1[[i]]<-out_meta%*%as.vector(unlist(finalw_MM_aug_new1[[i]]))
}
OW_out_dens_3par_PS4[,D]<-as.vector(unlist(Out_sample_EnDens_aug_new1))
model_weights_simul_3par_PS4[[D]]<-finalw_MM_aug_new1
}