-
Notifications
You must be signed in to change notification settings - Fork 2
/
6_Finite_REDSs_ismomentRMSEcalibration.R
284 lines (191 loc) · 10.8 KB
/
6_Finite_REDSs_ismomentRMSEcalibration.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#Copyright 2023 Tuobang Li
#These codes and manuscripts are under review in PNAS, please do not share them.
#If you are interested, please do not hesitate to contact me. Cooperation is also welcomed!
#require foreach and doparallel for parallel processing of bootstrap (not available for some types of computers)
if (!require("foreach")) install.packages("foreach")
library(foreach)
if (!require("doParallel")) install.packages("doParallel")
library(doParallel)
#require randtoolbox for random number generations
if (!require("randtoolbox")) install.packages("randtoolbox")
library(randtoolbox)
if (!require("Rcpp")) install.packages("Rcpp")
library(Rcpp)
if (!require("Rfast")) install.packages("Rfast")
library(Rfast)
if (!require("REDSReview")) install.packages("REDSReview_1.0.tar.gz", repos = NULL)
library(REDSReview)
if (!require("matrixStats")) install.packages("matrixStats")
library(matrixStats)
numCores <- detectCores()-4 # Detect the number of available cores
cl <- makeCluster(numCores) # Create a cluster with the number of cores
registerDoParallel(cl) # Register the parallel backend
#bootsize for bootstrap approximation of the distributions of the kernal of U-statistics.
n <- 13824*2*3
(n%%10)==0
# maximum order of moments
morder <- 4
#large sample size (approximating asymptotic)
largesize<-13824*2
#generate quasirandom numbers based on the Sobol sequence
quasiunisobol<-sobol(n=n, dim = morder, init = TRUE, scrambling = 0, seed = NULL, normal = FALSE,
mixed = FALSE, method = "C", start = 1)
quasiuni<-quasiunisobol
quasiuni_sorted2 <- na.omit(rowSort(quasiuni[,1:2], descend = FALSE, stable = FALSE, parallel = TRUE))
quasiuni_sorted3 <- na.omit(rowSort(quasiuni[,1:3], descend = FALSE, stable = FALSE, parallel = TRUE))
quasiuni_sorted4 <- na.omit(rowSort(quasiuni, descend = FALSE, stable = FALSE, parallel = TRUE))
# Forever...
#load asymptotic d for two parameter distributions
#set the stop criterion
criterionset=1e-10
kurtlognorm<- read.csv(("kurtlognorm_31180.csv"))
allkurtlognorm<-unlist(kurtlognorm)
samplesize=576*9
batchsizebase=1000
orderlist1_AB20<-createorderlist(quni1=quasiuni_sorted2,size=samplesize,interval=8,dimension=2)
orderlist1_AB20<-orderlist1_AB20[1:largesize,]
orderlist1_AB30<-createorderlist(quni1=quasiuni_sorted3,size=samplesize,interval=8,dimension=3)
orderlist1_AB30<-orderlist1_AB30[1:largesize,]
orderlist1_AB40<-createorderlist(quni1=quasiuni_sorted4,size=samplesize,interval=8,dimension=4)
orderlist1_AB40<-orderlist1_AB40[1:largesize,]
batchsize=batchsizebase
n <- samplesize
setSeed(1)
unibatchran<-matrix(SFMT(samplesize*batchsize),ncol=batchsize)
unibatch<-colSort(unibatchran, descend = FALSE, stable = FALSE, parallel = TRUE)
#input the d value table previously generated
d_values<- read.csv(("d_values.csv"))
#Then, start the Monte Simulation
I_values<-read.csv(("I_values.csv"))
#Then, start the Monte Simulation
simulatedbatch_bias_Monte<-foreach(batchnumber =c((1:length(allkurtlognorm))), .combine = 'rbind') %dopar% {
library(Rfast)
library(matrixStats)
library(REDSReview)
set.seed(1)
a=allkurtlognorm[batchnumber]
targetm<-exp((a^2)/2)
targetvar<-(exp((a/1)^2)*(-1+exp((a/1)^2)))
targettm<-sqrt(exp((a/1)^2)-1)*((2+exp((a/1)^2)))*((sqrt(exp((a/1)^2)*(-1+exp((a/1)^2))))^3)
targetfm<-((-3+exp(4*((a/1)^2))+2*exp(3*((a/1)^2))+3*exp(2*((a/1)^2))))*((sqrt(exp((a/1)^2)*(-1+exp((a/1)^2))))^4)
kurtx<-targetfm/(targetvar^(4/2))
skewx<-targettm/(targetvar^(3/2))
RMSEbataches<- read.csv(paste("finite_lognorm_Icalibration_raw",samplesize,round(kurtx,digits = 1),".csv", sep = ","))
RMSEbataches2<-c()
for (batch1 in c(1:batchsize)){
iall11<-RMSEbataches[batch1,]
x<-c(dslnorm(uni=unibatch[,batch1], meanlog =0, sdlog = a/1))
sortedx<-Sort(x,descending=FALSE,partial=NULL,stable=FALSE,na.last=NULL)
targetall<-c(targetm=targetm,targetvar=targetvar,targettm=targettm,targetfm=targetfm)
x<-c()
rqmomentselect1<-rqmoments2(x=sortedx,iall1=iall11,dtype1=1,Iskewtype1 = 4,Ikurttype1 = 4,releaseall=TRUE,standist_d=d_values,standist_I=I_values,orderlist1_sorted20=orderlist1_AB20,orderlist1_sorted30=orderlist1_AB30,orderlist1_sorted40=orderlist1_AB40,percentage=1/24,batch="auto",stepsize=50,criterion=1e-10,boot=TRUE)
standardizedmomentsx<-standardizedmoments(x=sortedx)
sortedx<-c()
all1<-(c(rqmomentselect1,targetall,standardizedmomentsx))
RMSEbataches2<-rbind(RMSEbataches2,all1)
}
write.csv(RMSEbataches2,paste("finite_lognorm_Ismomentscalibration_raw",samplesize,round(kurtx,digits = 1),".csv", sep = ","), row.names = FALSE)
RMSEbatachesmean <-apply(RMSEbataches2, 2, calculate_column_mean)
ikurt2<-sqrt(colMeans((RMSEbataches2[1:batchsize,c(1,3)]-kurtx)^2))
iskew2<-sqrt(colMeans((RMSEbataches2[1:batchsize,c(2,4)]-skewx)^2))
rankikurt2<-rank(ikurt2)
rankiskew2<-rank(iskew2)
allresultsSE<-c(samplesize=samplesize,type=4,kurtx,skewx,rankikurt2,rankiskew2,RMSEbatachesmean,RMSEikurt2=ikurt2,RMSEiskew2=iskew2)
}
write.csv(simulatedbatch_bias_Monte,paste("finite_lognorm_Ismomentscalibration_raw",samplesize,".csv", sep = ","), row.names = FALSE)
Optimum_RMSE<-simulatedbatch_bias_Monte[,1:8]
write.csv(Optimum_RMSE,paste("finite_Ismoments_lognorm.csv", sep = ","), row.names = FALSE)
simulatedbatch_bias_Monte_SE<-foreach(batchnumber =c((1:length(allkurtlognorm))), .combine = 'rbind') %dopar% {
library(Rfast)
library(matrixStats)
library(REDSReview)
a=allkurtlognorm[batchnumber]
targetm<-exp((a^2)/2)
targetvar<-(exp((a/1)^2)*(-1+exp((a/1)^2)))
targettm<-sqrt(exp((a/1)^2)-1)*((2+exp((a/1)^2)))*((sqrt(exp((a/1)^2)*(-1+exp((a/1)^2))))^3)
targetfm<-((-3+exp(4*((a/1)^2))+2*exp(3*((a/1)^2))+3*exp(2*((a/1)^2))))*((sqrt(exp((a/1)^2)*(-1+exp((a/1)^2))))^4)
kurtx<-targetfm/(targetvar^(4/2))
skewx<-targettm/(targetvar^(3/2))
SEbataches<- read.csv(paste("finite_lognorm_Ismomentscalibration_raw",samplesize,round(kurtx,digits = 1),".csv", sep = ","))
se_mean_all1<-apply((SEbataches[1:batchsize,]), 2, se_mean)
ikurt2_se<-apply(((SEbataches[1:batchsize,c(1,3)])), 2, se_sd)
iskew2_se<-apply((SEbataches[1:batchsize,c(2,4)]), 2, se_sd)
allresultsSE<-c(samplesize=samplesize,type=4,kurtx,skewx,se_mean_all1,ikurt2_se,iskew2_se)
allresultsSE
}
write.csv(simulatedbatch_bias_Monte_SE,paste("finite_lognorm_Ismomentscalibration_raw_error",samplesize,".csv", sep = ","), row.names = FALSE)
kurtgnorm<- read.csv(("kurtgnorm_21180.csv"))
allkurtgnorm<-unlist(kurtgnorm)
simulatedbatch_bias_Monte<-foreach(batchnumber =c((1:length(allkurtgnorm))), .combine = 'rbind') %dopar% {
library(Rfast)
library(matrixStats)
library(REDSReview)
set.seed(1)
a=allkurtgnorm[batchnumber]
targetm<-0
targetvar<-gamma(3/a)/((gamma(1/a)))
targettm<-0
targetfm<-((gamma(3/a)/((gamma(1/a))))^2)*gamma(5/a)*gamma(1/a)/((gamma(3/a))^2)
kurtx<-targetfm/(targetvar^(4/2))
skewx<-targettm/(targetvar^(3/2))
RMSEbataches<- read.csv(paste("finite_gnorm_Icalibration_raw",samplesize,round(kurtx,digits = 1),".csv", sep = ","))
RMSEbataches2<-c()
for (batch1 in c(1:batchsize)){
iall11<-RMSEbataches[batch1,]
x<-c(dsgnorm(uni=unibatch[,batch1], shape=a/1, scale = 1))
sortedx<-Sort(x,descending=FALSE,partial=NULL,stable=FALSE,na.last=NULL)
targetall<-c(targetm=targetm,targetvar=targetvar,targettm=targettm,targetfm=targetfm)
x<-c()
rqmomentselect1<-rqmoments2(x=sortedx,iall1=iall11,dtype1=1,Iskewtype1 = 5,Ikurttype1 = 5,releaseall=TRUE,standist_d=d_values,standist_I=I_values,orderlist1_sorted20=orderlist1_AB20,orderlist1_sorted30=orderlist1_AB30,orderlist1_sorted40=orderlist1_AB40,percentage=1/24,batch="auto",stepsize=50,criterion=1e-10,boot=TRUE)
standardizedmomentsx<-standardizedmoments(x=sortedx)
sortedx<-c()
all1<-(c(rqmomentselect1,targetall,standardizedmomentsx))
RMSEbataches2<-rbind(RMSEbataches2,all1)
}
write.csv(RMSEbataches2,paste("finite_gnorm_Ismomentscalibration_raw",samplesize,round(kurtx,digits = 1),".csv", sep = ","), row.names = FALSE)
RMSEbatachesmean <-apply(RMSEbataches2, 2, calculate_column_mean)
ikurt2<-sqrt(colMeans((RMSEbataches2[1:batchsize,c(1,3)]-kurtx)^2))
iskew2<-sqrt(colMeans((RMSEbataches2[1:batchsize,c(2,4)]-skewx)^2))
rankikurt2<-rank(ikurt2)
rankiskew2<-rank(iskew2)
allresultsSE<-c(samplesize=samplesize,type=5,kurtx,skewx,rankikurt2,rankiskew2,RMSEbatachesmean,RMSEikurt2=ikurt2,RMSEiskew2=iskew2)
}
write.csv(simulatedbatch_bias_Monte,paste("finite_gnorm_Ismomentscalibration_raw",samplesize,".csv", sep = ","), row.names = FALSE)
Optimum_RMSE<-simulatedbatch_bias_Monte[,1:8]
write.csv(Optimum_RMSE,paste("finite_Ismoments_gnorm.csv", sep = ","), row.names = FALSE)
simulatedbatch_bias_Monte_SE<-foreach(batchnumber =c((1:length(allkurtgnorm))), .combine = 'rbind') %dopar% {
library(Rfast)
library(matrixStats)
library(REDSReview)
a=allkurtgnorm[batchnumber]
targetm<-0
targetvar<-gamma(3/a)/((gamma(1/a)))
targettm<-0
targetfm<-((gamma(3/a)/((gamma(1/a))))^2)*gamma(5/a)*gamma(1/a)/((gamma(3/a))^2)
kurtx<-targetfm/(targetvar^(4/2))
skewx<-targettm/(targetvar^(3/2))
SEbataches<- read.csv(paste("finite_gnorm_Ismomentscalibration_raw",samplesize,round(kurtx,digits = 1),".csv", sep = ","))
se_mean_all1<-apply((SEbataches[1:batchsize,]), 2, se_mean)
ikurt2_se<-apply(((SEbataches[1:batchsize,c(1,3)])), 2, se_sd)
iskew2_se<-apply((SEbataches[1:batchsize,c(2,4)]), 2, se_sd)
allresultsSE<-c(samplesize=samplesize,type=5,kurtx,skewx,se_mean_all1,ikurt2_se,iskew2_se)
allresultsSE
}
write.csv(simulatedbatch_bias_Monte_SE,paste("finite_gnorm_Ismomentscalibration_raw_error",samplesize,".csv", sep = ","), row.names = FALSE)
finite_I_gnorm<-( read.csv(("finite_Ismoments_gnorm.csv")))
finite_I_lognorm<-( read.csv(("finite_Ismoments_lognorm.csv")))
names(finite_I_lognorm)<-1:ncol(finite_I_lognorm)
names(finite_I_gnorm)<-1:ncol(finite_I_lognorm)
finite_I_lognorm<-rbind(finite_I_lognorm,finite_I_gnorm)
finite_I_gnorm<- read.csv(("finite_Ismoments_lognorm.csv"))
names(finite_I_lognorm)<-names(finite_I_gnorm)
asymptotic_I_lognorm<- read.csv(paste("asymptotic_Ismoments.csv", sep = ","))
colnames(finite_I_lognorm)<-colnames(asymptotic_I_lognorm)
all1<-rbind(asymptotic_I_lognorm,finite_I_lognorm)
finite_I_lognorm<-data.frame(finite_I_lognorm)
asymptotic_I_lognorm<- read.csv(paste("asymptotic_Ismoments.csv", sep = ","))
colnames(asymptotic_I_lognorm)<-colnames(finite_I_lognorm)
all1<-rbind(asymptotic_I_lognorm,finite_I_lognorm)
write.csv(all1,paste("Ismoments_values.csv", sep = ","), row.names = FALSE)
stopCluster(cl)
registerDoSEQ()