-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.Rhistory
512 lines (512 loc) · 23.9 KB
/
.Rhistory
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- rgdp_msar_mdl_k3_msmu_1$St[,c(3,1,2)]
pdf(paste0(fig_out,"US_RealGDP_RegimeProbs_K3_M1_msmu_1951Q3_2024Q2.pdf"), width = 12, height = 7)
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("blue", "red","green3"), lty = c(2,1,3))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
dev.off()
reg_prob <- rgdp_msar_mdl_k4_msmu_1$St[,c(2,4,3,1)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("blue", "red","green3","magenta"),lty = c(2,1,3,4))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
load(paste0(res_out,"MCLRT_MSM_Empirical_USGNP.RData"))
usgnp_ar_mdl24
usgnp_msar_mdl24_k2_msmu_msvar
usgnp_msar_mdl24_k2_msmu
usgnp_msar_mdl10_k3_msmu_msvar
usgnp_msar_mdl10_k3_msmu
usgnp_msar_mdl24_k3_msmu_msvar
usgnp_msar_mdl24_k3_msmu
usgnp_msar_mdl10_k2_msmu_msvar
reg_prob <- usgnp_msar_mdl10_k2_msmu_msvar$St[,c(2,1)]
reg_prob <- usgnp_msar_mdl10_k2_msmu_msvar$St[,c(2,1)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
p
reg_prob
dates[(p+1):length(dates)]
reg_prob <- usgnp_msar_mdl24_k2_msmu_msvar$St[,c(2,1)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
usgnp_msar_mdl24_k2_msmu
reg_prob <- usgnp_msar_mdl24_k2_msmu$St[,c(1,2)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
dev.off()
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- usgnp_msar_mdl24_k2_msmu$St[,c(1,2)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- usgnp_msar_mdl24_k2_msmu$St[,c(1,2)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
dev.off()
reg_prob <- usgnp_msar_mdl24_k2_msmu$St[,c(1,2)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
dev.new()
dev.off()
dev.new()
reg_prob <- usgnp_msar_mdl24_k2_msmu$St[,c(1,2)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- usgnp_msar_mdl24_k2_msmu$St[,c(2,1)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- usgnp_msar_mdl24_k2_msmu$St[,c(2,1)]
pdf(paste0(fig_out,"US_RealGNP_RegimeProbs_K2_M1_msmu_1951Q3_2024Q2.pdf"), width = 12, height = 7)
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
dev.off()
usgnp_msar_mdl24_k3_msmu
reg_prob <- usgnp_msar_mdl24_k3_msmu$St[,c(3,2,1)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("blue", "red","green3"), lty = c(2,1,3))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- usgnp_msar_mdl24_k3_msmu$St[,c(3,2,1)]
pdf(paste0(fig_out,"US_RealGNP_RegimeProbs_K3_M1_msmu_1951Q3_2024Q2.pdf"), width = 12, height = 7)
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("blue", "red","green3"), lty = c(2,1,3))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
dev.off()
usgnp_msar_mdl24_k3_msmu_msvar
reg_prob <- usgnp_msar_mdl24_k3_msmu_msvar$St[,c(2,3,1)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("blue", "red","green3"), lty = c(2,1,3))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- usgnp_msar_mdl24_k3_msmu_msvar$St[,c(1,2,3)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("blue", "red","green3"), lty = c(2,1,3))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- usgnp_msar_mdl24_k3_msmu_msvar$St[,c(3,2,1)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("blue", "red","green3"), lty = c(2,1,3))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- usgnp_msar_mdl24_k3_msmu_msvar$St[,c(3,1,2)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("blue", "red","green3"), lty = c(2,1,3))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- usgnp_msar_mdl24_k3_msmu_msvar$St[,c(3,1,2)]
pdf(paste0(fig_out,"US_RealGNP_RegimeProbs_K3_M1_msmu_msvar_1951Q3_2024Q2.pdf"), width = 12, height = 7)
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("blue", "red","green3"), lty = c(2,1,3))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
dev.off()
usgnp_msar_mdl24_k4_msmu
usgnp_msar_mdl24_k4_msmu_msvar
library(MSTest)
fig_out <- "/Users/gabrielrodriguez/Dropbox/Res/papers/MC_LRT_MSM/mc_lrt_msm/empirical/figures/"
res_out <- "/Users/gabrielrodriguez/Dropbox/Res/papers/MC_LRT_MSM/mc_lrt_msm/empirical/results/"
data_loc <- "/Users/gabrielrodriguez/Dropbox/Res/papers/MC_LRT_MSM/mc_lrt_msm/data/"
seed <- 1234
p <- 1
k1 <- 1
k2 <- 2
k3 <- 3
k4 <- 4
N <- 99
workers <- 9
burnin <- 100
est_init <- 30
test_init <- 10
mmc_eps <- 0
mmc_CI <- TRUE
mmc_maxit <- 50
ar_control <- list(const = TRUE,
getSE = TRUE)
msar_control_msmu_msvar <- list(msmu = TRUE,
msvar = TRUE,
method = "EM",
use_diff_init = est_init,
getSE = TRUE)
msar_control_msmu <- msar_control_msvar <- msar_control_msmu_msvar
msar_control_msmu$msvar <- FALSE
msar_control_msvar$msmu <- FALSE
load(paste0(res_out,"MCLRT_MSM_Empirical_RealGDP.RData"))
plot(dates, Y, type = "l", xlab = "Date", ylab = "Percent (%)")
plot(dates, Y, type = "l", xlab = "Date", ylab = "Percent (%)")
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("red", alpha = 0.3))
pdf(paste0(fig_out,"US_RealGDP_1951Q2_2024Q2.pdf"), width = 12, height = 7)
plot(dates, Y, type = "l", xlab = "Date", ylab = "Percent (%)")
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = dates[rec_st], xright = dates[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("red", alpha = 0.3))
dev.off()
library(MSTest)
fig_out <- "/Users/gabrielrodriguez/Dropbox/Res/papers/MC_LRT_MSM/mc_lrt_msm/empirical/figures/"
res_out <- "/Users/gabrielrodriguez/Dropbox/Res/papers/MC_LRT_MSM/mc_lrt_msm/empirical/results/"
data_loc <- "/Users/gabrielrodriguez/Dropbox/Res/papers/MC_LRT_MSM/mc_lrt_msm/data/"
seed <- 1234
p <- 4
k1 <- 1
k2 <- 2
k3 <- 3
k4 <- 4
N <- 99
workers <- 9
burnin <- 100
est_init <- 30
test_init <- 10
mmc_eps <- 0
mmc_CI <- TRUE
mmc_maxit <- 50
ar_control <- list(const = TRUE,
getSE = TRUE)
msar_control_msmu_msvar <- list(msmu = TRUE,
msvar = TRUE,
method = "EM",
use_diff_init = est_init,
getSE = TRUE)
msar_control_msmu <- msar_control_msvar <- msar_control_msmu_msvar
msar_control_msmu$msvar <- FALSE
msar_control_msvar$msmu <- FALSE
load(paste0(res_out,"MCLRT_MSM_Empirical_USGNP.RData"))
reg_prob <- usgnp_msar_mdl24_k2_msmu$St[,c(2,1)]
matplot(dates[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
us_gnp24_date
matplot(us_gnp24_date[(p+1):length(dates)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
matplot(us_gnp24_date[(p+1):length(us_gnp24_date)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = us_gnp24_date[rec_st], xright = us_gnp24_date[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- usgnp_msar_mdl24_k2_msmu_msvar$St[,c(2,1)]
matplot(us_gnp24_date[(p+1):length(us_gnp24_date)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = us_gnp24_date[rec_st], xright = us_gnp24_date[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
reg_prob <- usgnp_msar_mdl24_k2_msmu_msvar$St[,c(2,1)]
pdf(paste0(fig_out,"US_RealGNP_RegimeProbs_K2_M1_msmu_msvar_1951Q3_2024Q2.pdf"), width = 12, height = 7)
matplot(us_gnp24_date[(p+1):length(us_gnp24_date)], reg_prob, type = "l", xlab = "Date", ylab = "Percent (%)", col=c("green3","red"), lty = c(2,1))
rec_st <- which(c(0,diff(BCind24))==1)
rec_ed <- which(c(0,diff(BCind24))==-1)-1
rect(xleft = us_gnp24_date[rec_st], xright = us_gnp24_date[rec_ed], ybottom = par("usr")[3], ytop = par("usr")[4],
border = NA, col = adjustcolor("grey", alpha = 0.3))
dev.off()
usgnp_ar_mdl24
usgnp_msar_mdl24_k2_msmu_msvar$phi
usgnp_msar_mdl24_k3_msmu_msvar
usgnp_msar_mdl24_k2_msmu_msvar$stdev
usgnp_msar_mdl24_k3_msmu_msvar$sigma
kk <-3
mdl_2_reg <- c(2,1)
mdl_3_reg <- c(3,2,1)
mdlk1 <- c(usgnp_ar_mdl24$mu,NaN,NaN,usgnp_ar_mdl24$phi,usgnp_ar_mdl24$stdev,NaN,NaN,rep(NaN,kk*kk),usgnp_ar_mdl24$logLike)
mdlk2 <- c(usgnp_msar_mdl24_k2_msmu_msvar$mu[mdl_2_reg],NaN,usgnp_msar_mdl24_k2_msmu_msvar$phi,usgnp_msar_mdl24_k2_msmu_msvar$stdev[mdl_2_reg],NaN,rbind(cbind(usgnp_msar_mdl24_k2_msmu_msvar$P[mdl_2_reg,mdl_2_reg],c(NaN,NaN)),c(NaN,NaN,NaN)),usgnp_msar_mdl24_k2_msmu_msvar$logLike)
mdlk3 <- c(usgnp_msar_mdl24_k3_msmu_msvar$mu[mdl_3_reg],usgnp_msar_mdl24_k3_msmu_msvar$phi,usgnp_msar_mdl24_k3_msmu_msvar$sigma[mdl_3_reg],usgnp_msar_mdl24_k3_msmu_msvar$P[mdl_3_reg,mdl_3_reg],usgnp_msar_mdl24_k3_msmu_msvar$logLike)
names(mdlk1) <- names(mdlk2) <- names(mdlk3) <- c(paste0("\\mu_", (1:kk)),paste0("\\phi_",(1:p)),paste0("\\sigma_", (1:kk)),
paste0("p_{",c(sapply((1:kk),function(x) paste0(x, (1:kk)) )),"}"),c("logLike"))
mdlk1 <- t(data.frame(mdlk1))
mdlk2 <- t(data.frame(mdlk2))
mdlk3 <- t(data.frame(mdlk3))
out <- merge(merge(mdlk3,mdlk2,all=T),mdlk1,all=T)
out <- format(round(out[c(1,3,2),],2),nsmall=2)
out
out <- merge(merge(mdlk3,mdlk2,all=T),mdlk1,all=T)
out <- format(round(out[c(1,2,3),],2),nsmall=2)
out
mdlk1 <- c(usgnp_ar_mdl24$mu,NaN,NaN,usgnp_ar_mdl24$phi,usgnp_ar_mdl24$stdev,NaN,NaN,rep(NaN,kk*kk),usgnp_ar_mdl24$logLike)
mdlk2 <- c(usgnp_msar_mdl24_k2_msmu_msvar$mu[mdl_2_reg],NaN,usgnp_msar_mdl24_k2_msmu_msvar$phi,usgnp_msar_mdl24_k2_msmu_msvar$stdev[mdl_2_reg],NaN,rbind(cbind(usgnp_msar_mdl24_k2_msmu_msvar$P[mdl_2_reg,mdl_2_reg],c(NaN,NaN)),c(NaN,NaN,NaN)),usgnp_msar_mdl24_k2_msmu_msvar$logLike)
mdlk3 <- c(usgnp_msar_mdl24_k3_msmu_msvar$mu[mdl_3_reg],usgnp_msar_mdl24_k3_msmu_msvar$phi,usgnp_msar_mdl24_k3_msmu_msvar$stdev[mdl_3_reg],usgnp_msar_mdl24_k3_msmu_msvar$P[mdl_3_reg,mdl_3_reg],usgnp_msar_mdl24_k3_msmu_msvar$logLike)
names(mdlk1) <- names(mdlk2) <- names(mdlk3) <- c(paste0("\\mu_", (1:kk)),paste0("\\phi_",(1:p)),paste0("\\sigma_", (1:kk)),
paste0("p_{",c(sapply((1:kk),function(x) paste0(x, (1:kk)) )),"}"),c("logLike"))
mdlk1 <- t(data.frame(mdlk1))
mdlk2 <- t(data.frame(mdlk2))
mdlk3 <- t(data.frame(mdlk3))
out <- merge(merge(mdlk3,mdlk2,all=T),mdlk1,all=T)
out <- format(round(out[c(1,2,3),],2),nsmall=2)
out
# ============================================================================ #
# Author: Gabriel Rodriguez Rondon
# email: [email protected]
# This version: 12-Apr-2023
#
# Notes:
# This script can be used to replicate simulation results for "Monte Carlo
# Likelihood Ratio Tests for Markov Switching Models" by Gabriel Rodriguez
# Rondon & Jean-Marie Dufour. It was prepared using v0.1.2
# of MSTest available through CRAN.
# ============================================================================ #
library(MSTest)
library(foreach)
library(doParallel)
setwd("/Users/gabrielrodriguezrondon/Dropbox/Res/papers/MC_LRT_MSM/mc_lrt_msm/")
seed <- 12345
# Optimization controls
N <- 500
Nsim <- 1000
workers <- 12
rho <- 0.7
chp_control <- list(N = N,
rho_b = rho)
# Parameters of DGP (under null hypothesis)
k0 <- 1
k1 <- 2
ar_mdl_k2 <- list(k = k1)
mu_ls <- c(0,2)
sigma_ls <- c(1,4)
n_ls <- c(100,200,500)
phi_ls <- c(0.1, 0.9, 1)
P22_ls <- c(0.90, 0.50, 0.9999)
param_ls <- list()
param_ls$n <- n_ls
param_ls$phi <- phi_ls
param_ls$mu_ls <- mu_ls
param_ls$sigma_ls <- sigma_ls
param_ls$P22_ls <-P22_ls
combined_list <- expand.grid(param_ls)
combined_list["mu_1"] = 0
combined_list["sigma_1"] = 1
combined_list <- combined_list[((combined_list["mu_ls"]==0) + (combined_list["mu_1"]==0) + (combined_list["sigma_ls"]==1) + (combined_list["sigma_1"]==1))!=4,]
msmu_T <- (combined_list[,3] != combined_list[,6])
msvar_T <- (combined_list[,4] != combined_list[,7])
combined_list <- combined_list[((msmu_T==T) & (msvar_T==F)),]
row.names(combined_list) <- NULL
combined_list
i=1
n <- combined_list[i,1]
phi <- combined_list[i,2]
mu_1 <- combined_list[i,6]
mu_2 <- combined_list[i,3]
stdev_1 <- combined_list[i,7]
stdev_2 <- combined_list[i,4]
p22 <- combined_list[i,5]
# ------------- Model
ar_mdl_k2$n <- n
ar_mdl_k2$phi <- phi
ar_mdl_k2$mu <- c(mu_1,mu_2)
ar_mdl_k2$sigma <- c(stdev_1,stdev_2)
ar_mdl_k2$P <- cbind(c(0.9,0.10),c(1-p22,p22))
msmu <- mu_1!=mu_2
msvar <- stdev_1!=stdev_2
p <- length(phi)
chp_control$msvar <- msvar
mdl_h0_tmp <- ARmdl(y_out$y,p)
y_out <- MSTest::simuMSAR(ar_mdl_k2)
mdl_h0_tmp <- ARmdl(y_out$y,p)
mdl_h0_tmp
bootim <- MSTest::bootCV(mdl_h0_tmp,chp_control$rho_b,N,chp_control$msvar)
bootim
sort(bootim[,1])
alpha
alpha <- 0.05
sort(bootim[,1])[N*(1-alpha)]
y_out <- MSTest::simuMSAR(ar_mdl_k2)
y_out <- MSTest::simuMSAR(ar_mdl_k2)
y_out
attempt <- attempt + 1
attempt <- 0
null_control <- list(const = TRUE, getSE = con$getSE)
null_control <- list(const = TRUE, getSE = chp_control$getSE)
null_control
mdl_h0 <- ARmdl(Y, p, null_control)
mdl_h0 <- ARmdl(y_out$y, p, null_control)
null_control <- list(const = TRUE, getSE = FALSE)
null_control
mdl_h0 <- ARmdl(y_out$y, p, null_control)
ltmt <- chpDmat(mdl_h0, con$msvar)
cv3 <- chpStat(mdl_h0, chp_control$rho_b, ltmt, chp_control$msvar)
ltmt <- chpDmat(mdl_h0, chp_control$msvar)
cv3 <- chpStat(mdl_h0, chp_control$rho_b, ltmt, chp_control$msvar)
supts <- cv3[1]
expts <- cv3[2]
supts
expts
spTS_levelcv <- sort(bootim[,1])[N*(1-alpha)]
spTS_levelcv <- sort(bootim[,2])[N*(1-alpha)]
spTS_levelcv <- sort(bootim[,1])[N*(1-alpha)]
expTS_levelcv <- sort(bootim[,2])[N*(1-alpha)]
expTS_levelcv
spTS_levelcv
expts
file_name_size <-paste0('simulations/results/CHP_Boots_',N,'_rho_',sub('\\.','',rho),'_results_size_of_test_h0_',k0,'_h1_',k1,
'_n_',n,'_phi_',sub('\\.','',phi)'_msmu.txt')
file_name_size <-paste0('simulations/results/CHP_Boots_',N,'_rho_',sub('\\.','',rho),'_results_size_of_test_h0_',k0,'_h1_',k1,
'_n_',n,'_phi_',sub('\\.','',phi),'_msmu.txt')
file_name_size
file_name_size
size_res <- read.table(file_name_size, header = TRUE, sep = ",")
dir <- "/Users/gabrielrodriguezrondon/Dropbox/Res/papers/MC_LRT_MSM/mc_lrt_msm/"
paste0(dir,'simulations/results/CHP_Boots_',N,'_rho_',sub('\\.','',rho),'_results_size_of_test_h0_',k0,'_h1_',k1,
'_n_',n,'_phi_',sub('\\.','',phi),'_msmu.txt')
file_name_size <-paste0(dir,'simulations/results/CHP_Boots_',N,'_rho_',sub('\\.','',rho),'_results_size_of_test_h0_',k0,'_h1_',k1,
'_n_',n,'_phi_',sub('\\.','',phi),'_msmu.txt')
size_res <- read.table(file_name_size, header = TRUE, sep = ",")
getwd()
file_name_size <-paste0(dir,'simulations/results/CHP_Boots_',N,'_rho_',sub('\\.','',rho),'_results_size_of_test_h0_',k0,'_h1_',k1,
'_n_',n,'_phi_',sub('\\.','',phi),'_msmu.txt')
size_res <- read.table(file_name_size, header = TRUE, sep = ",")
size_res <- read.table(file_name_size, header = TRUE, sep = ",")
library(MSTest)
# ============================================================================ #
# Author: Gabriel Rodriguez Rondon
# email: [email protected]
# This version: 12-Apr-2023
#
# Notes:
# This script can be used to replicate simulation results for "Monte Carlo
# Likelihood Ratio Tests for Markov Switching Models" by Gabriel Rodriguez
# Rondon & Jean-Marie Dufour. It was prepared using v0.1.2
# of MSTest available through CRAN.
# ============================================================================ #
library(MSTest)
library(foreach)
library(doParallel)
setwd("/Users/gabrielrodriguezrondon/Dropbox/Res/papers/MC_LRT_MSM/mc_lrt_msm/")
# ============================================================================ #
# ----- USE INPUT -----
# ============================================================================ #
seed <- 12345
# Optimization controls
N <- 500
Nsim <- 1000
workers <- 12
rho <- 0.7
chp_control <- list(N = N,
rho_b = rho)
# Parameters of DGP (under null hypothesis)
k0 <- 1
k1 <- 2
ar_mdl_k2 <- list(k = k1)
mu_ls <- c(0,2)
sigma_ls <- c(1,4)
n_ls <- c(100,200,500)
phi_ls <- c(0.1, 0.9)#, 1)
P22_ls <- c(0.90, 0.50)#, 0.9999)
# ============================================================================ #
# ----- Parallel Loop -----
# ============================================================================ #
param_ls <- list()
param_ls$n <- n_ls
param_ls$phi <- phi_ls
param_ls$mu_ls <- mu_ls
param_ls$sigma_ls <- sigma_ls
param_ls$P22_ls <-P22_ls
combined_list <- expand.grid(param_ls)
combined_list["mu_1"] = 0
combined_list["sigma_1"] = 1
combined_list <- combined_list[((combined_list["mu_ls"]==0) + (combined_list["mu_1"]==0) + (combined_list["sigma_ls"]==1) + (combined_list["sigma_1"]==1))!=4,]
msmu_T <- (combined_list[,3] != combined_list[,6])
msvar_T <- (combined_list[,4] != combined_list[,7])
combined_list <- combined_list[((msmu_T==T) & (msvar_T==F)),]
row.names(combined_list) <- NULL
# ------------- Set up clusters
cl <- makeCluster(workers)
registerDoParallel(cl)
# ------------- Set Seed
finalMatrix <- foreach(i=1:nrow(combined_list), .inorder = FALSE, .packages = "MSTest") %dopar% {
# ------------- parameter values for this loop
n <- combined_list[i,1]
phi <- combined_list[i,2]
mu_1 <- combined_list[i,6]
mu_2 <- combined_list[i,3]
stdev_1 <- combined_list[i,7]
stdev_2 <- combined_list[i,4]
p22 <- combined_list[i,5]
# ------------- Model
ar_mdl_k2$n <- n
ar_mdl_k2$phi <- phi
ar_mdl_k2$mu <- c(mu_1,mu_2)
ar_mdl_k2$sigma <- c(stdev_1,stdev_2)
ar_mdl_k2$P <- cbind(c(0.9,0.10),c(1-p22,p22))
msmu <- mu_1!=mu_2
msvar <- stdev_1!=stdev_2
p <- length(phi)
chp_control$msvar <- msvar
# ------------- File name and columns titles
file_name <-paste0('simulations/results/CHP_Boots_',N,'_rho_',sub('\\.','',rho),'_results_power_of_test_h0_',k0,'_h1_',k1,
'_n_',n,'_phi_',sub('\\.','',phi),'_changeMean_',msmu,'_changeVar_',msvar,
'_p22_',sub('\\.','',p22),'.txt')
res_line <- paste0("Iteration,","Attempts,","TimeSpent,","supTS,","expTS,","supTS_Pvalue,","expTS_Pvalue")
if (file.exists(file_name)){
sim_res <- read.table(file_name, header = TRUE, sep = ",")
sim_start <- length(sim_res[,1]) + 1
}else{
write(res_line,file = file_name, append=TRUE)
sim_start <- 1
}
# ------------- Begin looping
if (sim_start<Nsim){
for (xi in sim_start:Nsim){
set.seed(seed + i + xi)
CHP_out <- NULL
attempt <- 0
startT <- proc.time()
while(is.null(CHP_out)) {
y_out <- MSTest::simuMSAR(ar_mdl_k2)
if ((length(table(y_out$St))==k1) & all(as.matrix(table(y_out$St))>=(n*0.1))){ # only use simulation if each regime is at least 10% of sample size
attempt <- attempt + 1
try(
CHP_out <- MSTest::CHPTest(y_out$y, p,control = chp_control)
)
}
}
endT <- proc.time()-startT
# ------------- Save results
res_line <- paste0(xi,',',attempt,',',round(endT[3],3),',',CHP_out$supTS,',',CHP_out$expTS,',',CHP_out$pval_supTS,',',CHP_out$pval_expTS)
write(res_line, file = file_name, append = TRUE)
}
}else{
print(paste0('Specified file already contains ', Nsim,' simulations. Process is complete.'))
}
}
#stop cluster
stopCluster(cl)
arma::
## Submission
This is a resubmission. In this version I have:
devtools::doc
devtools::document()
devtools::check_win_release()