-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDRD-HAR models.R
2370 lines (1537 loc) · 99.2 KB
/
DRD-HAR models.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#DRD-HAR model
source("Previous functions/projectfunctions.R")
source("functions.R")
library(xts)
library(highfrequency)
library(matlib)
library(MCS)
library(Rsolnp)
library(matrixcalc)
library(MASS)
library(Matrix)
#They use percentage realized variances for HARQ even though they do not specify it.
getDates <- readRDS("getDates.rds")
calccov <- readRDS("calculatedcovariances.rds")
mergedfrequencies <- readRDS("mergedfrequencies.rds")
#5min correlations
fivemincorr <- array(0L, dim = c(2,2,2516))
for(i in 1:2516){
fivemincorr[,,i] <- realCov(mergedfrequencies[[7]][[i]]*100, T)
}
#no need for vech operator when working in a bivariate framework.
#-----------------------------------------CORRELATION HAR USING VECH OPERATOR------------------------------------------
mlag <- function(x,n=1,init=0){
xlag <- matrix(0L, ncol = ncol(x)*n, nrow = nrow(x))
icnt <- 0
for(i in 1:ncol(x)){
for(j in 1:n){
xlag[(j+1):nrow(x), icnt+j] <- x[1:(nrow(x)-j),i]
}
icnt <- icnt+n
}
xlag[xlag == 0] <- init
return(xlag)
}
fivemincorr <- xts(fivemincorr[2,1,], order.by = as.Date(getDates))
#one-day lag is today since we are in F_t-1.
corrday <- fivemincorr
corrweek <- rowMeans(cbind(fivemincorr, mlag(fivemincorr,4,mean(fivemincorr))))
corrmonth <- rowMeans(cbind(fivemincorr, mlag(fivemincorr,21,mean(fivemincorr))))
ones <- rep(1, length(corrday)-22)
#trying to get the intercept term:
intercept <- mean(fivemincorr) * (1- corrday - corrweek - corrmonth)
#needs 23 data points to initialize. This is in agreement with HARestimate from HARmodel library when removing intercept part.
tester <- lm(fivemincorr[23:2516] ~ 0 +
corrday[22:(2516-1)] + corrweek[22:(2516-1)] + corrmonth[22:(2516-1)])
summary(tester)
sum(coef(tester))
coef(tester)[coef(tester) == 0]
c(-1,2,3,4)[c(-1,2,3,4) < 0]
#-----------------------------rolling forecast to see if params violate restrictions when posing no restrictions:
window <- 1000
params <- matrix(NaN, ncol=3, nrow = 2516)
for(i in (window+1):2516){
fivemincorr1 <- fivemincorr[(i-window):(i-1)]
corrday1 <- fivemincorr1
corrweek1 <- rowMeans(cbind(fivemincorr1, mlag(fivemincorr1,4,mean(fivemincorr1))))
corrmonth1 <- rowMeans(cbind(fivemincorr1, mlag(fivemincorr1,21,mean(fivemincorr1))))
fivemincorr1 <- fivemincorr1[22:length(fivemincorr1)]
corrday1 <- corrday1[22:length(corrday1)]
corrweek1 <- corrweek1[22:length(corrweek1)]
corrmonth1 <- corrmonth1[22:length(corrmonth1)]
#end <- length(corrday1)
est <- EstimatecorrHAR(list(fivemincorr1[2:length(fivemincorr1)], corrday1[1:(length(corrday1)-1)],
corrweek1[1:(length(corrweek1)-1)], corrmonth1[1:(length(corrmonth1)-1)]), 0)
params[i,] <- est$vPar
print(sprintf("%s", i))
}
#-------------------------------------------------------------------------------------------------------------------
params2 <- params[!is.na(params[,1]), ]
#parameter changes for the correlation estimate under five minute sampling frequency.
library(ggplot2)
ggplot() + geom_line(aes(as.Date(getDates[(window+1):2516]), params2[,1], col="daily")) +
geom_line(aes(as.Date(getDates[(window+1):2516]), params2[,2], col="weekly")) +
geom_line(aes(as.Date(getDates[(window+1):2516]), params2[,3], col="monthly"))
#solving the above using quadratic programing, from solnp function:
#problems with y, it had date indexation and thus misaligned by other data.
#################################################################################################################
#
#
# HAR models estimations 5min with standard errors
#
#
#################################################################################################################
#can be estimated using package for time efficiency:
library(highfrequency)
#calculating realized quarticity and tripower quarticity for 5 min interval:
#from highfrequency. Could not find it in the package, so found in source.
RTQ <- function(rData) {
returns <- as.vector(as.numeric(rData))
n <- length(returns)
tq <- n * (n/(n-2)) *((2^(2/3) * gamma(7/6) * gamma(1/2)^(-1))^(-3)) * sum(abs(returns[1:(n - 2)])^(4/3) *
abs(returns[2:(n-1)])^(4/3) * abs(returns[3:n])^(4/3))
return(tq)
}
rq_TLT <- numeric()
rq_SPY <- numeric()
trq_TLT <- numeric()
trq_SPY <- numeric()
for(i in 1:length(mergedfrequencies[[7]])){
#realized quarticity
rq_TLT[i] <- rQuar(mergedfrequencies[[7]][[i]][,1] * 100)
rq_SPY[i] <- rQuar(mergedfrequencies[[7]][[i]][,2] * 100)
#realized tripower quarticity
trq_TLT[i] <- RTQ(mergedfrequencies[[7]][[i]][,1] * 100)
trq_SPY[i] <- RTQ(mergedfrequencies[[7]][[i]][,2] * 100)
}
rq_TLT <- matrix(rq_TLT)
rq_SPY <- matrix(rq_SPY)
#in HARestimation he both squareroots the realized quarticity and demeans it.
#-------------------------------------------------TLT---------------------------------------------------------
#
#
#
#
#
#TRYING WITHOUT SQRT BECAUSE IT FOLLOWS PATTONS DATA. THEREFORE TO GET VARIANCES ON PERCENTAGE LOG-RETURN
#YOU HAVE TO SCALE THEM BY A FACTOR 10000!
#RV
fiveminvol_TLT <- matrix((calccov[[1]][[7]][1,1,])) * 10000 #sqrt
volday_TLT <- fiveminvol_TLT
voldayposvar_TLT <- matrix((calccov[[2]][[7]][1,1,])) * 10000 #sqrt
voldaynegvar_TLT <- matrix((calccov[[3]][[7]][1,1,])) * 10000 #sqrt
volweek_TLT <- rowMeans(cbind(fiveminvol_TLT, mlag(fiveminvol_TLT,4,mean(fiveminvol_TLT))))
volmonth_TLT <- rowMeans(cbind(fiveminvol_TLT, mlag(fiveminvol_TLT,21,mean(fiveminvol_TLT))))
#BPV
fiveminbpvol_TLT <- matrix((calccov[[5]][[7]][1,1,])*10000) #sqrt
bpvolday_TLT <- fiveminbpvol_TLT
bpvolweek_TLT <- rowMeans(cbind(fiveminbpvol_TLT, mlag(fiveminbpvol_TLT,4,mean(fiveminbpvol_TLT))))
bpvolmonth_TLT <- rowMeans(cbind(fiveminbpvol_TLT, mlag(fiveminbpvol_TLT,21,mean(fiveminbpvol_TLT))))
#RQ
sqrtrq_TLT <- sqrt(rq_TLT) - mean(sqrt(rq_TLT))
sqrttrq_TLT <- sqrt(trq_TLT) - mean(sqrt(trq_TLT))
rqTLTweek <- rowMeans(cbind(rq_TLT, mlag(rq_TLT,4,mean(rq_TLT))))
sqrtrq_TLTweek <- sqrt(rqTLTweek) - mean(sqrt(rqTLTweek))
rqTLTmonth <- rowMeans(cbind(rq_TLT, mlag(rq_TLT,21,mean(rq_TLT))))
sqrtrq_TLTmonth <- sqrt(rqTLTmonth) - mean(sqrt(rqTLTmonth))
jumpparam_TLT <- ifelse(fiveminvol_TLT - fiveminbpvol_TLT>0, fiveminvol_TLT - fiveminbpvol_TLT, 0)
ones <- matrix(rep(1, 2516-22))
#HAR
HAR_TLT <- lm(fiveminvol_TLT[23:2516] ~ volday_TLT[22:(2516-1)] + volweek_TLT[22:(2516-1)] + volmonth_TLT[22:(2516-1)])
hhat_HAR_TLT <- cbind(ones, volday_TLT[22:2515], volweek_TLT[22:2515], volmonth_TLT[22:2515]) %*% matrix(coef(HAR_TLT))
QLIKE_HAR_TLT <- mean(QLIKE(hhat_HAR_TLT, fiveminvol_TLT[23:2516], 1))
#R-squared
Rsquared_HAR_TLT <- 1- var(fiveminvol_TLT[23:2516] - hhat_HAR_TLT)/var(fiveminvol_TLT[23:2516])
#HARQ
HARQ_TLT <- lm(fiveminvol_TLT[23:2516] ~ volday_TLT[22:(2516-1)] + volweek_TLT[22:(2516-1)] + volmonth_TLT[22:(2516-1)] + I(volday_TLT[22:(2516-1)] * sqrtrq_TLT[22:(2516-1)]))
hhat_HARQ_TLT <- cbind(ones, volday_TLT[22:2515], volweek_TLT[22:2515], volmonth_TLT[22:2515],volday_TLT[22:(2516-1)] * sqrtrq_TLT[22:(2516-1)]) %*% matrix(coef(HARQ_TLT))
QLIKE_HARQ_TLT <- mean(QLIKE(hhat_HARQ_TLT, fiveminvol_TLT[23:2516], 1))
#HARQF_TLT
HARQF_TLT <- lm(fiveminvol_TLT[23:2516] ~ volday_TLT[22:(2516-1)] + volweek_TLT[22:(2516-1)] +
volmonth_TLT[22:(2516-1)] + I(volday_TLT[22:(2516-1)] * sqrtrq_TLT[22:(2516-1)]) +
I(volweek_TLT[22:(2516-1)] * sqrtrq_TLTweek[22:(2516-1)]) + I(volmonth_TLT[22:(2516-1)] * sqrtrq_TLTmonth[22:(2516-1)]))
hhat_HARQF_TLT <- cbind(ones, volday_TLT[22:2515], volweek_TLT[22:2515], volmonth_TLT[22:2515],
volday_TLT[22:(2516-1)] * sqrtrq_TLT[22:(2516-1)], volweek_TLT[22:(2516-1)] * sqrtrq_TLTweek[22:(2516-1)], volmonth_TLT[22:(2516-1)] * sqrtrq_TLTmonth[22:(2516-1)]) %*% matrix(coef(HARQF_TLT))
QLIKE_HARQF_TLT <- mean(QLIKE(hhat_HARQF_TLT, fiveminvol_TLT[23:2516], 1))
ts.plot(residuals(HAR_TLT, standardized = T))
ts.plot(rstandard(HAR_TLT))
#HARJ TLT
HARJ_TLT <- lm(fiveminvol_TLT[23:2516] ~ volday_TLT[22:(2516-1)] + volweek_TLT[22:(2516-1)] + volmonth_TLT[22:(2516-1)] + jumpparam_TLT[22:(2516-1)])
hhat_HARJ_TLT <- cbind(ones, volday_TLT[22:2515], volweek_TLT[22:2515], volmonth_TLT[22:2515],jumpparam_TLT[22:(2516-1)]) %*% matrix(coef(HARJ_TLT))
QLIKE_HARJ_TLT <- mean(QLIKE(hhat_HARJ_TLT, fiveminvol_TLT[23:2516], 1))
#HARQJ TLT
HARQJ_TLT <- lm(fiveminvol_TLT[23:2516] ~ volday_TLT[22:(2516-1)] + volweek_TLT[22:(2516-1)] + volmonth_TLT[22:(2516-1)] + I(volday_TLT[22:(2516-1)] * sqrtrq_TLT[22:(2516-1)]) + jumpparam_TLT[22:(2516-1)])
hhat_HARQJ_TLT <- cbind(ones, volday_TLT[22:2515], volweek_TLT[22:2515], volmonth_TLT[22:2515],volday_TLT[22:(2516-1)] * sqrtrq_TLT[22:(2516-1)], jumpparam_TLT[22:(2516-1)]) %*% matrix(coef(HARQJ_TLT))
QLIKE_HARQJ_TLT <- mean(QLIKE(hhat_HARQJ_TLT, fiveminvol_TLT[23:2516], 1))
#CHAR TLT
CHAR_TLT <- lm(fiveminvol_TLT[23:2516] ~ bpvolday_TLT[22:(2516-1)] + bpvolweek_TLT[22:(2516-1)] + bpvolmonth_TLT[22:(2516-1)])
hhat_CHAR_TLT <- cbind(ones, bpvolday_TLT[22:2515], bpvolweek_TLT[22:2515], bpvolmonth_TLT[22:2515]) %*% matrix(coef(CHAR_TLT))
QLIKE_CHAR_TLT <- mean(QLIKE(hhat_CHAR_TLT, fiveminvol_TLT[23:2516], 1))
#CHARQ TLT
CHARQ_TLT <- lm(fiveminvol_TLT[23:2516] ~ bpvolday_TLT[22:(2516-1)] + bpvolweek_TLT[22:(2516-1)] + bpvolmonth_TLT[22:(2516-1)] + I(bpvolday_TLT[22:(2516-1)] * sqrttrq_TLT[22:(2516-1)]))
hhat_CHARQ_TLT <- cbind(ones, bpvolday_TLT[22:2515], bpvolweek_TLT[22:2515], bpvolmonth_TLT[22:2515], bpvolday_TLT[22:(2516-1)] * sqrttrq_TLT[22:(2516-1)]) %*% matrix(coef(CHARQ_TLT))
QLIKE_CHARQ_TLT <- mean(QLIKE(hhat_CHARQ_TLT, fiveminvol_TLT[23:2516], 1))
#SHAR TLT
SHAR_TLT <- lm(fiveminvol_TLT[23:2516] ~ voldayposvar_TLT[22:(2516-1)] + voldaynegvar_TLT[22:(2516-1)] + volweek_TLT[22:(2516-1)] + volmonth_TLT[22:(2516-1)])
hhat_SHAR_TLT <- cbind(ones, voldayposvar_TLT[22:(2516-1)], voldaynegvar_TLT[22:(2516-1)], volweek_TLT[22:2515], volmonth_TLT[22:2515]) %*% matrix(coef(SHAR_TLT))
QLIKE_SHAR_TLT <- mean(QLIKE(hhat_SHAR_TLT, fiveminvol_TLT[23:2516], 1))
QLIKES_TLT <- c(QLIKE_HAR_TLT, QLIKE_HARQ_TLT, QLIKE_HARQF_TLT, QLIKE_HARJ_TLT, QLIKE_HARQJ_TLT, QLIKE_CHAR_TLT, QLIKE_CHARQ_TLT, QLIKE_SHAR_TLT)
library(sandwich)
#ROBUST STANDARD ERRORS:
vcv1 <- sqrt(diag(vcovHC(HAR_TLT)))
vcv2 <- sqrt(diag(vcovHC(HARQ_TLT)))
vcv3 <- matrix(sqrt(diag(vcovHC(HARQF_TLT))))
harqf_ttest <- coef(HARQF_TLT)/vcv3
2 * pt(abs(harqf_ttest), df = df.residual(HARQF_TLT), lower.tail = FALSE)
vcv4 <- sqrt(diag(vcovHC(HARJ_TLT)))
vcv5 <- sqrt(diag(vcovHC(HARQJ_TLT)))
vcv6 <- sqrt(diag(vcovHC(CHAR_TLT)))
vcv7 <- sqrt(diag(vcovHC(CHARQ_TLT)))
vcv8 <- sqrt(diag(vcovHC(SHAR_TLT)))
HAR_TLT.show <- rbind(coef(HAR_TLT), vcv1)
HARQ_TLT.show <- rbind(coef(HARQ_TLT), vcv2)
HARQF_TLT.show <- rbind(coef(HARQF_TLT), vcv3)
HARJ_TLT.show <- rbind(coef(HARJ_TLT), vcv4)
HARQJ_TLT.show <- rbind(coef(HARQJ_TLT), vcv5)
CHAR_TLT.show <- rbind(coef(CHAR_TLT), vcv6)
CHARQ_TLT.show <- rbind(coef(CHARQ_TLT), vcv7)
SHAR_TLT.show <- rbind(coef(SHAR_TLT), vcv8)
(cbind(HAR_TLT.show, SHAR_TLT.show, HARQ_TLT.show, HARQF_TLT.show, HARJ_TLT.show, HARQJ_TLT.show, CHAR_TLT.show, CHARQ_TLT.show))
#--------------------------------------------SPY-----------------------------------------------------
#RV
fiveminvol_SPY <- matrix((calccov[[1]][[7]][2,2,]*10000)) #sqrt
volday_SPY <- fiveminvol_SPY
voldayposvar_SPY <- matrix((calccov[[2]][[7]][2,2,]* 10000))
voldaynegvar_SPY <- matrix((calccov[[3]][[7]][2,2,]* 10000))
volweek_SPY <- rowMeans(cbind(fiveminvol_SPY, mlag(fiveminvol_SPY,4,mean(fiveminvol_SPY)))) #sqrt
volmonth_SPY <- rowMeans(cbind(fiveminvol_SPY, mlag(fiveminvol_SPY,21,mean(fiveminvol_SPY)))) #sqrt
#BPV
fiveminbpvol_SPY <- matrix((calccov[[5]][[7]][2,2,]*10000)) #sqrt
bpvolday_SPY <- fiveminbpvol_SPY
bpvolweek_SPY <- rowMeans(cbind(fiveminbpvol_SPY, mlag(fiveminbpvol_SPY,4,mean(fiveminbpvol_SPY))))
bpvolmonth_SPY <- rowMeans(cbind(fiveminbpvol_SPY, mlag(fiveminbpvol_SPY,21,mean(fiveminbpvol_SPY))))
#RQ
sqrtrq_SPY <- sqrt(rq_SPY) - mean(sqrt(rq_SPY))
sqrttrq_SPY <- sqrt(trq_SPY) - mean(sqrt(trq_SPY))
rqSPYweek <- rowMeans(cbind(rq_SPY, mlag(rq_SPY,4,mean(rq_SPY))))
sqrtrq_SPYweek <- sqrt(rqSPYweek) - mean(sqrt(rqSPYweek))
rqSPYmonth <- rowMeans(cbind(rq_SPY, mlag(rq_SPY,21,mean(rq_SPY))))
sqrtrq_SPYmonth <- sqrt(rqSPYmonth) - mean(sqrt(rqSPYmonth))
jumpparam_SPY <- ifelse(fiveminvol_SPY - fiveminbpvol_SPY>0, fiveminvol_SPY - fiveminbpvol_SPY, 0)
#HAR
HAR_SPY <- lm(fiveminvol_SPY[23:2516] ~ volday_SPY[22:(2516-1)] + volweek_SPY[22:(2516-1)] + volmonth_SPY[22:(2516-1)])
hhat_HAR_SPY <- cbind(ones, volday_SPY[22:2515], volweek_SPY[22:2515], volmonth_SPY[22:2515]) %*% matrix(coef(HAR_SPY))
QLIKE_HAR_SPY <- colMeans(QLIKE(hhat_HAR_SPY, fiveminvol_SPY[23:2516], 1))
#HARQ
HARQ_SPY <- lm(fiveminvol_SPY[23:2516] ~ volday_SPY[22:(2516-1)] + volweek_SPY[22:(2516-1)] + volmonth_SPY[22:(2516-1)] + I(volday_SPY[22:(2516-1)] * sqrtrq_SPY[22:(2516-1)]))
hhat_HARQ_SPY <- cbind(ones, volday_SPY[22:2515], volweek_SPY[22:2515], volmonth_SPY[22:2515],volday_SPY[22:(2516-1)] * sqrtrq_SPY[22:(2516-1)]) %*% matrix(coef(HARQ_SPY))
QLIKE_HARQ_SPY <- colMeans(QLIKE(hhat_HARQ_SPY, fiveminvol_SPY[23:2516], 1))
#HARQF_SPY
HARQF_SPY <- lm(fiveminvol_SPY[23:2516] ~ volday_SPY[22:(2516-1)] + volweek_SPY[22:(2516-1)] +
volmonth_SPY[22:(2516-1)] + I(volday_SPY[22:(2516-1)] * sqrtrq_SPY[22:(2516-1)]) +
I(volweek_SPY[22:(2516-1)] * sqrtrq_SPYweek[22:(2516-1)]) + I(volmonth_SPY[22:(2516-1)] * sqrtrq_SPYmonth[22:(2516-1)]))
hhat_HARQF_SPY <- cbind(ones, volday_SPY[22:2515], volweek_SPY[22:2515], volmonth_SPY[22:2515],
volday_SPY[22:(2516-1)] * sqrtrq_SPY[22:(2516-1)], volweek_SPY[22:(2516-1)] * sqrtrq_SPYweek[22:(2516-1)], volmonth_SPY[22:(2516-1)] * sqrtrq_SPYmonth[22:(2516-1)]) %*% matrix(coef(HARQF_SPY))
hhat_HARQF_SPY[hhat_HARQF_SPY<0] <- mean(hhat_HARQF_SPY[80:110])
QLIKE_HARQF_SPY <- mean(QLIKE(hhat_HARQF_SPY, fiveminvol_SPY[23:2516], 1))
#HARJ SPY
HARJ_SPY <- lm(fiveminvol_SPY[23:2516] ~ volday_SPY[22:(2516-1)] + volweek_SPY[22:(2516-1)] + volmonth_SPY[22:(2516-1)] + jumpparam_SPY[22:(2516-1)])
hhat_HARJ_SPY <- cbind(ones, volday_SPY[22:2515], volweek_SPY[22:2515], volmonth_SPY[22:2515],jumpparam_SPY[22:(2516-1)]) %*% matrix(coef(HARJ_SPY))
QLIKE_HARJ_SPY <- mean(QLIKE(hhat_HARJ_SPY, fiveminvol_SPY[23:2516], 1))
#HARQJ SPY
HARQJ_SPY <- lm(fiveminvol_SPY[23:2516] ~ volday_SPY[22:(2516-1)] + volweek_SPY[22:(2516-1)] + volmonth_SPY[22:(2516-1)] + I(volday_SPY[22:(2516-1)] * sqrtrq_SPY[22:(2516-1)]) + jumpparam_SPY[22:(2516-1)])
hhat_HARQJ_SPY <- cbind(ones, volday_SPY[22:2515], volweek_SPY[22:2515], volmonth_SPY[22:2515],volday_SPY[22:(2516-1)] * sqrtrq_SPY[22:(2516-1)], jumpparam_SPY[22:(2516-1)]) %*% matrix(coef(HARQJ_SPY))
QLIKE_HARQJ_SPY <- mean(QLIKE(hhat_HARQJ_SPY, fiveminvol_SPY[23:2516], 1))
#CHAR SPY
CHAR_SPY <- lm(fiveminvol_SPY[23:2516] ~ bpvolday_SPY[22:(2516-1)] + bpvolweek_SPY[22:(2516-1)] + bpvolmonth_SPY[22:(2516-1)])
hhat_CHAR_SPY <- cbind(ones, bpvolday_SPY[22:2515], bpvolweek_SPY[22:2515], bpvolmonth_SPY[22:2515]) %*% matrix(coef(CHAR_SPY))
QLIKE_CHAR_SPY <- mean(QLIKE(hhat_CHAR_SPY, fiveminvol_SPY[23:2516], 1))
#CHARQ SPY
CHARQ_SPY <- lm(fiveminvol_SPY[23:2516] ~ bpvolday_SPY[22:(2516-1)] + bpvolweek_SPY[22:(2516-1)] + bpvolmonth_SPY[22:(2516-1)] + I(bpvolday_SPY[22:(2516-1)] * sqrttrq_SPY[22:(2516-1)]))
hhat_CHARQ_SPY <- cbind(ones, bpvolday_SPY[22:2515], bpvolweek_SPY[22:2515], bpvolmonth_SPY[22:2515], bpvolday_SPY[22:(2516-1)] * sqrttrq_SPY[22:(2516-1)]) %*% matrix(coef(CHARQ_SPY))
QLIKE_CHARQ_SPY <- mean(QLIKE(hhat_CHARQ_SPY, fiveminvol_SPY[23:2516], 1))
#SHAR SPY
SHAR_SPY <- lm(fiveminvol_SPY[23:2516] ~ voldayposvar_SPY[22:(2516-1)] + voldaynegvar_SPY[22:(2516-1)] + volweek_SPY[22:(2516-1)] + volmonth_SPY[22:(2516-1)])
hhat_SHAR_SPY <- cbind(ones, voldayposvar_SPY[22:(2516-1)], voldaynegvar_SPY[22:(2516-1)], volweek_SPY[22:2515], volmonth_SPY[22:2515]) %*% matrix(coef(SHAR_SPY))
QLIKE_SHAR_SPY <- mean(QLIKE(hhat_SHAR_SPY, fiveminvol_SPY[23:2516], 1))
QLIKES_SPY <- c(QLIKE_HAR_SPY, QLIKE_HARQ_SPY, QLIKE_HARQF_SPY, QLIKE_HARJ_SPY, QLIKE_HARQJ_SPY, QLIKE_CHAR_SPY, QLIKE_CHARQ_SPY, QLIKE_SHAR_SPY)
library(sandwich)
#ROBUST STANDARD ERRORS:
vcv1s <- sqrt(diag(vcovHC(HAR_SPY)))
vcv2s <- sqrt(diag(vcovHC(HARQ_SPY)))
vcv3s <- sqrt(diag(vcovHC(HARQF_SPY)))
vcv4s <- sqrt(diag(vcovHC(HARJ_SPY)))
vcv5s <- sqrt(diag(vcovHC(HARQJ_SPY)))
vcv6s <- sqrt(diag(vcovHC(CHAR_SPY)))
vcv7s <- sqrt(diag(vcovHC(CHARQ_SPY)))
vcv8s <- sqrt(diag(vcovHC(SHAR_SPY)))
HAR_SPY.show <- rbind(coef(HAR_SPY), vcv1s)
HARQ_SPY.show <- rbind(coef(HARQ_SPY), vcv2s)
HARQF_SPY.show <- rbind(coef(HARQF_SPY), vcv3s)
HARJ_SPY.show <- rbind(coef(HARJ_SPY), vcv4s)
HARQJ_SPY.show <- rbind(coef(HARQJ_SPY), vcv5s)
CHAR_SPY.show <- rbind(coef(CHAR_SPY), vcv6s)
CHARQ_SPY.show <- rbind(coef(CHARQ_SPY), vcv7s)
SHAR_SPY.show <- rbind(coef(SHAR_SPY), vcv8s)
#########################################################################################################
#
#
# PERSISTENCE IN SEMIVARIANCES FOR TLT AS SEEN BY AUTOCORRELATIONS
#
#
#########################################################################################################
acf_TLT <- matrix(0L, ncol = 7, nrow = 101)
acf_posvol_TLT <- matrix(0L, ncol = 7, nrow = 101)
acf_negvol_TLT <- matrix(0L, ncol = 7, nrow = 101)
for(i in 1:7){
temp <- matrix((calccov[[1]][[i]][1,1,])) * 10000
temp2 <- matrix((calccov[[2]][[i]][1,1,])) * 10000 #sqrt
temp3 <- matrix((calccov[[3]][[i]][1,1,])) * 10000 #sqrt
acf_TLT[,i] <- acf(temp, lag.max = 100, plot = F)$acf
acf_posvol_TLT[,i] <- acf(temp2, lag.max = 100, plot = F)$acf
acf_negvol_TLT[,i] <- acf(temp3, lag.max = 100, plot = F)$acf
}
acfpos_TLT <- rowMeans(acf_posvol_TLT)
acfneg_TLT <- rowMeans(acf_negvol_TLT)
acf_TLT <- rowMeans(acf_TLT)
p1 <- ggplot() + geom_line(aes(1:100, acfpos_TLT[-1], col ="RVPos"), lwd = 1) + geom_line(aes(1:100, acfneg_TLT[-1], col ="RVneg"), lwd = 1) +
geom_point(aes(1:100, acf_TLT[-1], col="RV"), size = 1.4, shape = 16) + ylim(c(0,0.6)) + xlab("Lag") + ylab("Autocorrelation") +
theme(legend.title = element_blank(), legend.position = c(0.8, 0.8), legend.background = element_rect(fill="lightblue",
size=0.5, linetype="solid", colour = 'darkblue'),
legend.text = element_text(colour="black", size=9, face="bold"), legend.text.align = 0) +
scale_fill_manual(name ='', values = c("blue", "red"))
#ggsave("TLT_Autocorrelation.eps", device = "eps")
#See below for combined graph with QLIKE losses under different DRD-type models.
#########################################################################################################
#
#
# SIGNATURE VOLATILITY PLOT OF REALIZED QUARTICITY ESTIMATORS
#
#
#########################################################################################################
library(highfrequency)
dataTLT <- readRDS("dataTLT.rds")
dataSPY <- readRDS("dataSPY.rds")
secs <-c(1,5,15,20,30,60,300,900,1800)
frequenciesTLT <- list()
frequenciesSPY <- list()
tempTLT <- list()
tempSPY <- list()
days <- 2516
for(j in 1:length(secs)){
for(i in 1:days){
tempTLT[[i]] <- aggregatets(dataTLT[[i]], on="seconds", k=secs[j])
tempSPY[[i]] <- aggregatets(dataSPY[[i]], on="seconds", k=secs[j])
}
frequenciesTLT[[j]] <- tempTLT
frequenciesSPY[[j]] <- tempSPY
print(sprintf("%s",j))
}
for(j in 1:length(secs)){
for(i in 1:days){
frequenciesTLT[[j]][[i]] <- diff(log(frequenciesTLT[[j]][[i]]))[-1] * 100
frequenciesSPY[[j]][[i]] <- diff(log(frequenciesSPY[[j]][[i]]))[-1] * 100
}
}
RQ_TLT <- matrix(0L, ncol = length(secs), nrow= days)
RQ_SPY <- matrix(0L, ncol = length(secs), nrow= days)
TRQ_TLT <- matrix(0L, ncol = length(secs), nrow= days)
TRQ_SPY <- matrix(0L, ncol = length(secs), nrow= days)
#contains outliers
for(j in 1:length(secs)){
for(i in 1:days){
RQ_TLT[i,j] <- rQuar(frequenciesTLT[[j]][[i]])
RQ_SPY[i,j] <- rQuar(frequenciesSPY[[j]][[i]])
TRQ_TLT[i,j] <- RTQ(frequenciesTLT[[j]][[i]])
TRQ_SPY[i,j] <- RTQ(frequenciesSPY[[j]][[i]])
}
}
#removing outliers
rem <- which(TRQ_TLT[,1]>11)
rem2 <- which(TRQ_TLT[,2]>11)
rem3 <- which(TRQ_TLT[,3]>11)
rem4 <- which(TRQ_TLT[,4]>11)
rem5 <- which(TRQ_TLT[,5]>11)
rem6 <- which(TRQ_SPY[,1]>9)
rem7 <- which(TRQ_SPY[,2]>9)
rem8 <- which(RQ_SPY[,1]>11)
rem9 <- which(RQ_SPY[,2]>11)
remove <- unique(c(rem, rem2, rem3, rem4, rem5, rem6, rem7,rem8, rem9))
RQ_TLT <- RQ_TLT[-remove, ]
RQ_SPY <- RQ_SPY[-remove, ]
TRQ_SPY <- TRQ_SPY[-remove, ]
TRQ_TLT <- TRQ_TLT[-remove, ]
RQ_TLT <- colMeans(RQ_TLT)
RQ_SPY <- colMeans(RQ_SPY)
RQ_SPY[1] <- RQ_SPY[1] + 2.5 #removing too many values
TRQ_TLT <- colMeans(TRQ_TLT)
TRQ_SPY <- colMeans(TRQ_SPY)
library(ggplot2)
ggplot() + geom_line(aes(secs, RQ_TLT, col ="RQ_TLT"), lwd =1) + geom_line(aes(secs, RQ_SPY, col ="RQ_SPY"), lwd=1) +
geom_line(aes(secs, TRQ_TLT, col ="TRQ_TLT"), lwd=1) + geom_line(aes(secs, TRQ_SPY, col ="TRQ_SPY"),lwd=1) + ylim(0,4) +
ylab("Realized Quarticity") + xlab("Seconds") +
scale_x_continuous(breaks = c(secs[1],secs[6:9]), labels = c("1", "60", "300", "900", "1800"))
p2 <- ggplot() + geom_smooth(aes(x=secs, y= RQ_TLT, col = "RQ_TLT"), span = 0.3, se=FALSE) +
geom_smooth(aes(secs, RQ_SPY, col ="RQ_SPY"), span = 0.3, se=FALSE) +
geom_smooth(aes(secs, TRQ_SPY, col ="TRQ_SPY"), span =0.3, se=FALSE) +
geom_smooth(aes(secs, TRQ_TLT, col ="TRQ_TLT"), span = 0.3, se=FALSE) +
ylim(0,3.45) + ylab("Realized Quarticity") + xlab("Seconds") +
scale_x_continuous(breaks = c(secs[1],secs[6:9]), labels = c("1", "60", "300", "900", "1800")) +
theme(legend.title = element_blank(), legend.position = c(0.8, 0.8), legend.background = element_rect(fill="lightblue",
size=0.5, linetype="solid", colour = 'darkblue'),
legend.text = element_text(colour="black", size=9, face="bold"), legend.text.align = 0) +
scale_fill_manual(name ='', values = c("blue", "red"))
#########################################################################################################
#
#
# THE DRD-HAR MODEL WITHOUT USING STANDARDIZED RESIDUALS
#
#
#########################################################################################################
minimizingfunc <- function(data, params){
dA <- params[1]
dB <- params[2]
dC <- params[3]
y <- as.numeric(data[[1]])
x1 <- as.numeric(data[[2]])
x2 <- as.numeric(data[[3]])
x3 <- as.numeric(data[[4]])
dint <- (1-dA-dB-dC) * mean(y)
mini <- (y - dint - dA * x1 - dB * x2 - dC * x3)^2
minis <- mini
mini <- sum(mini)
lOut <- list()
lOut[["mini"]] <- mini
lOut[["minis"]] <- minis
return(lOut)
}
ineqconstraint <- function(vPar, ...){
dA <- vPar[1]
dB <- vPar[2]
dC <- vPar[3]
return(dA+dB+dC)
}
min.RSS <- function(vPar, data){
rss <- minimizingfunc(data, vPar)$mini
return(rss)
}
EstimatecorrHAR <- function(variances, correlation = NULL, proxy, trace=1, ineqfun = ineqconstraint, ineqLB = 0.00, ineqUB = 0.9999){
#mEta is standardized residuals from univariate models!
#variances should be produced by the univariate models and not come from realized measures!
#correlation should be found via eg. five min samples but adhere to the same frequency as variances from HAR models.
dA = 0.01
dB = 0.001185
dC = 0.2
## vector of starting parameters
vPar = c(dA, dB, dC)
correlation <- as.matrix(correlation)
corrday <- correlation
corrweek <- rowMeans(cbind(correlation, mlag(correlation,4,mean(correlation))))
corrmonth <- rowMeans(cbind(correlation, mlag(correlation,21,mean(correlation))))
data <- list(proxy[23:2516], corrday[22:2515], corrweek[22:2515], corrmonth[22:2515])
optimizer = solnp(vPar, fun = min.RSS, data = data,
ineqfun = ineqfun, #the inequality constraint
ineqLB = ineqLB, ## the inequality lower bound
ineqUB = ineqUB, ## the inequality lower bound, i.e. 0.0 <= a + b + c < 0.9999
## lower and upper bounds for all parameters
LB = c(0, 0, 0), UB = c(0.9999, 0.9999, 0.9999),
control = list(tol = 1e-22, outer.iter = 800, inner.iter = 1200, delta = 1e-8,
trace = trace))
params <- optimizer$pars
min <- tail(optimizer$values, 1)
hessian <- optimizer$hessian
scores <- matrix(0L, nrow=(nrow(variances)), ncol = 3)
step <- 1e-5 * vPar
for(i in 1:length(step)){
h <- step[i]
delta <- rep(0, length(vPar))
delta[i] <- h
loglikeminus <- minimizingfunc(data, vPar-delta)$minis
loglikeplus <- minimizingfunc(data, vPar+delta)$minis
scores[,i] <- (loglikeplus - loglikeminus)/(2*h)
}
J <- (t(scores) %*% scores)/2516
I <- optimizer$hessian/2516
I <- solve(I)[-1 ,-1]
vars <- (I * J * I)/2516
rse <- sqrt(diag(vars))
t.stat <- vPar/rse
#calculating covariances:
hhatcorrHAR <- cbind(corrday[22:2515], corrweek[22:2515], corrmonth[22:2515]) %*% matrix(params)
covs <- hhatcorrHAR * sqrt(variances[,1]) * sqrt(variances[,2])
vSigma2 <- array(0L, dim = c(2,2, (nrow(variances))))
for(i in 1:(nrow(variances))){
vSigma2[,,i] <- matrix(c(variances[i,1], covs[i], covs[i], variances[i,2]), ncol=2, nrow=2)
}
#R-squared
Rsquared <- 1-var(correlation[23:2516] - hhatcorrHAR)/var(correlation[23:2516])
lOut <- list()
lOut[["vPar"]] <- params
lOut[["vSigma2"]] <- vSigma2
lOut[["R2"]] <- Rsquared
lOut[["estcor"]] <- hhatcorrHAR
lOut[["MSE"]] <- min/2516
lOut[["rse"]] <- rse
lOut[["hessian"]] <- hessian
lOut[["Tstats"]] <- t.stat
return(lOut)
}
#######################################################################################################################
#
#
# In-sample estimations for DRD-HAR model
#
#
#######################################################################################################################
ones <- rep(1, 2516-22)
ones <- as.matrix(ones)
fiveminvol_TLT <- matrix((calccov[[1]][[7]][1,1,])) * 10000 #sqrt
volday_TLT <- fiveminvol_TLT
volweek_TLT <- rowMeans(cbind(fiveminvol_TLT, mlag(fiveminvol_TLT,4,mean(fiveminvol_TLT))))
volmonth_TLT <- rowMeans(cbind(fiveminvol_TLT, mlag(fiveminvol_TLT,21,mean(fiveminvol_TLT))))
#RV
fiveminvol_SPY <- matrix((calccov[[1]][[7]][2,2,]*10000)) #sqrt
volday_SPY <- fiveminvol_SPY
volweek_SPY <- rowMeans(cbind(fiveminvol_SPY, mlag(fiveminvol_SPY,4,mean(fiveminvol_SPY)))) #sqrt
volmonth_SPY <- rowMeans(cbind(fiveminvol_SPY, mlag(fiveminvol_SPY,21,mean(fiveminvol_SPY)))) #sqrt
HAR_TLT <- lm(fiveminvol_TLT[23:2516] ~ volday_TLT[22:(2516-1)] + volweek_TLT[22:(2516-1)] + volmonth_TLT[22:(2516-1)])
hhat_HAR_TLT <- cbind(ones, volday_TLT[22:2515], volweek_TLT[22:2515], volmonth_TLT[22:2515]) %*% matrix(coef(HAR_TLT))
HAR_SPY <- lm(fiveminvol_SPY[23:2516] ~ volday_SPY[22:(2516-1)] + volweek_SPY[22:(2516-1)] + volmonth_SPY[22:(2516-1)])
hhat_HAR_SPY <- cbind(ones, volday_SPY[22:2515], volweek_SPY[22:2515], volmonth_SPY[22:2515]) %*% matrix(coef(HAR_SPY))
variances <- cbind(hhat_HAR_TLT , hhat_HAR_SPY)
RC5min <- calccov[[1]][[7]][2,1,]/sqrt(calccov[[1]][[7]][1,1,] * calccov[[1]][[7]][2,2,])
#i've used estimation scheme based on realized correlations and not standardized residuals, which is, the same.
tt2 <- EstimatecorrHAR(variances, correlation = RC5min, proxy = RC5min, 1)
RC1min <- calccov[[1]][[6]][2,1,]/sqrt(calccov[[1]][[6]][1,1,] * calccov[[1]][[6]][2,2,])
RC15min <- calccov[[1]][[8]][2,1,]/sqrt(calccov[[1]][[8]][1,1,] * calccov[[1]][[8]][2,2,])
RC30min <- calccov[[1]][[9]][2,1,]/sqrt(calccov[[1]][[9]][1,1,] * calccov[[1]][[9]][2,2,])
RCdaily <- calccov[[1]][[10]][2,1,]/sqrt(calccov[[1]][[10]][1,1,] * calccov[[1]][[10]][2,2,])
fivemincorr <- list(NULL,NULL,NULL,NULL,NULL,as.matrix(RC1min), as.matrix(RC5min), as.matrix(RC15min), as.matrix(RC30min), as.matrix(RCdaily))
HARCor_freq <- list()
fiveminvol_TLT <- matrix((calccov[[1]][[7]][1,1,])) * 10000
fiveminvol_SPY <- matrix((calccov[[1]][[7]][2,2,]*10000)) #sqrt
for(i in 6:9){
vol_TLT <- matrix((calccov[[1]][[i]][1,1,])) * 10000 #sqrt
volday_TLT <- vol_TLT
volweek_TLT <- rowMeans(cbind(vol_TLT, mlag(vol_TLT,4,mean(vol_TLT))))
volmonth_TLT <- rowMeans(cbind(vol_TLT, mlag(vol_TLT,21,mean(vol_TLT))))
#RV
vol_SPY <- matrix((calccov[[1]][[i]][2,2,]*10000)) #sqrt
volday_SPY <- vol_SPY
volweek_SPY <- rowMeans(cbind(vol_SPY, mlag(vol_SPY,4,mean(vol_SPY)))) #sqrt
volmonth_SPY <- rowMeans(cbind(vol_SPY, mlag(vol_SPY,21,mean(vol_SPY)))) #sqrt
HAR_TLT <- lm(fiveminvol_TLT[23:2516] ~ volday_TLT[22:(2516-1)] + volweek_TLT[22:(2516-1)] + volmonth_TLT[22:(2516-1)])
hhat_HAR_TLT <- cbind(ones, volday_TLT[22:2515], volweek_TLT[22:2515], volmonth_TLT[22:2515]) %*% matrix(coef(HAR_TLT))
HAR_SPY <- lm(fiveminvol_SPY[23:2516] ~ volday_SPY[22:(2516-1)] + volweek_SPY[22:(2516-1)] + volmonth_SPY[22:(2516-1)])
hhat_HAR_SPY <- cbind(ones, volday_SPY[22:2515], volweek_SPY[22:2515], volmonth_SPY[22:2515]) %*% matrix(coef(HAR_SPY))
variances <- cbind(hhat_HAR_TLT, hhat_HAR_SPY)
HARCor_freq[[i]] <- EstimatecorrHAR(variances, correlation = fivemincorr[[i]],proxy = RC5min, 1)
}
params <- matrix(unlist(lapply(HARCor_freq, function(x) x$vPar)), ncol = 3, byrow = T)
apply(params, MARGIN = c(2), FUN =function(x) quantile(x, 0.1))
apply(params, MARGIN = c(2), FUN =function(x) quantile(x, 0.9))
qlikes <- matrix(0L, nrow = 2494, ncol = 4)
qlikes5min <- numeric()
for(i in 1:2494){
#vsigma2 starts from 22 and ends at 2515
qlikes5min[i] <- QLIKE(HARCor_freq[[7]]$vSigma2[,,i], calccov[[1]][[7]][,,22+i]*10000, 2)
}
for(j in 1:4){
for(i in 1:2494){
#vsigma2 starts from 22 and ends at 2515
qlikes[i, j] <- QLIKE(HARCor_freq[[j+5]]$vSigma2[,,i], calccov[[1]][[7]][,,22+i]*10000, 2)
}
}
colMeans(qlikes)
mean(colMeans(qlikes))
mean(c(HARCor_freq[[6]]$R2, HARCor_freq[[7]]$R2, HARCor_freq[[8]]$R2, HARCor_freq[[9]]$R2))
HARCor_freq[[6]]$R2; HARCor_freq[[7]]$R2; HARCor_freq[[8]]$R2; HARCor_freq[[9]]$R2
#######################################################################################################################
#
#
# BOOTSTRAPPING DRD-HAR model (TIME-SERIES REDUCES TO RESIDUALS)
#
#
#######################################################################################################################
#REMEMBER THAT THE INTERDEPENDENCE OF THE UNIVARIATE MODELS IS COMPLETELY CAPTURED IN THE STANDARDIZED RESIDUALS.
#TAKING THE COVARIANCE OF THE STANDARDIZED RESIDUALS SHOULD GET YOU THE CORRELATION (DUE TO NORMALIZING WITH STD.DEV)
library(boot)
library(np)
#uses the row to construct the block length. Therefore you should transpose it.
ll <- tsboot(dailyretotc[,1], mean ,R = 1000, l = 97, sim = "fixed", endcorr = TRUE)
indexation <- boot.array(ll)
HARCor_Boot <- matrix(0L, nrow = 1000, ncol = 3)
for(i in 1:1000){
RC5min_boot <- RC5min[indexation[i, ]]
HARCor_Boot[i, ] <- EstimatecorrHAR2(variances, correlation = RC5min_boot, proxy = RC5min_boot, 1)$vPar
}
standarderror_HARCor <- apply(HARCor_Boot, MARGIN = c(2), FUN = function(x) sd(x))
ttt <- RC5min[indexation[1, ]]
#######################################################################################################################
#
#
# QLIKE COMPARISON USING 5-MIN FREQUENCY AND CONSTRUCTING IT USING BAR-CHART
#
#
#######################################################################################################################
#UNIVARIATE INSANITY FILTER!
volatility.insanity.filter <- function(forecasts, lb, ub, adjujsted_forecast){
ut1 <- forecasts
items <- sum(forecasts < lb) + sum( forecasts > ub)
ut1[forecasts < lb] = adjujsted_forecast
ut1[forecasts > ub] = adjujsted_forecast
lOut <- list()
lOut[["vol"]] <- ut1
lOut[["items"]] <- items
return(lOut)
}
#Get correlations for all measures on 5 minute scheme.
#Theres 8 measures in total, with two being rscov+ and rscor-, which will be discarded, so 6.
#its not fiveminutes!
Correlations_5min_measures <- matrix(0L, ncol=6, nrow = 2516)
#list of kernels based on frequency. 5 minute is list object 7.
kernel.param <- readRDS("bandwidthH.rds")
for(i in 1:2516){
Correlations_5min_measures[i,1] <- realCov(mergedfrequencies[[6]][[i]]*100, T)[2,1]
Correlations_5min_measures[i,2] <- preavthrCOV(mergedfrequencies[[6]][[i]]*100, F)[2,1] / (sqrt(preavthrCOV(mergedfrequencies[[6]][[i]]*100, F)[1,1] * preavthrCOV(mergedfrequencies[[6]][[i]]*100, F)[2,2]))
Correlations_5min_measures[i,3] <- preavBPCOV(mergedfrequencies[[6]][[i]]*100,F,T,F,1)[2,1] #bpcov
Correlations_5min_measures[i,5] <- preavCov(mergedfrequencies[[6]][[i]]*100,T,T,T,1)[2,1] #mrc
Correlations_5min_measures[i,6] <- rKernelCov(list(mergedfrequencies[[6]][[i]][,1]* 100, mergedfrequencies[[6]][[i]][,2]* 100),
cor =TRUE, makeReturns = FALSE, kernel.type = "Parzen", kernel.param = kernel.param[[6]][i])[2,1]
}
#really slow, thats why its in its own loop.
for(i in 1:2516){
Correlations_5min_measures[i,4] <- preavBPCOV(mergedfrequencies[[1]][[i]]*100,T,T,T,1)[2,1] #pbpcov
print(sprintf("%s", i))
}
#saveRDS(Correlations_5min_measures, "Correlations_for_barplot.rds")
#------------------------------------------------univariate models
#RUN ALL
{
Correlations_barplot <- readRDS("Correlations_for_barplot.rds")
rq_TLT_1min <- numeric()
rq_SPY_1min <- numeric()
trq_TLT_1min <- numeric()
trq_SPY_1min <- numeric()
#for pbpcov
trq_TLT_1sec <- numeric()
trq_SPY_1sec <- numeric()
for(i in 1:length(mergedfrequencies[[6]])){
#realized quarticity
rq_TLT_1min[i] <- rQuar(mergedfrequencies[[6]][[i]][,1] * 100)
rq_SPY_1min[i] <- rQuar(mergedfrequencies[[6]][[i]][,2] * 100)
#realized tripower quarticity
trq_TLT_1min[i] <- RTQ(mergedfrequencies[[6]][[i]][,1] * 100)
trq_SPY_1min[i] <- RTQ(mergedfrequencies[[6]][[i]][,2] * 100)
#for pbpcov
trq_TLT_1sec[i] <- RTQ(mergedfrequencies[[1]][[i]][,1] * 100)
trq_SPY_1sec[i] <- RTQ(mergedfrequencies[[1]][[i]][,2] * 100)
}
rq_TLT_1min <- matrix(rq_TLT_1min)
rq_SPY_1min <- matrix(rq_SPY_1min)
rqSPYweek <- rowMeans(cbind(rq_SPY_1min, mlag(rq_SPY_1min,4,mean(rq_SPY_1min))))
sqrtrq_SPYweek <- sqrt(rqSPYweek) - mean(sqrt(rqSPYweek))
rqSPYmonth <- rowMeans(cbind(rq_SPY_1min, mlag(rq_SPY_1min,21,mean(rq_SPY_1min))))
sqrtrq_SPYmonth <- sqrt(rqSPYmonth) - mean(sqrt(rqSPYmonth))
rqTLTweek <- rowMeans(cbind(rq_TLT_1min, mlag(rq_TLT_1min,4,mean(rq_TLT_1min))))
sqrtrq_TLTweek <- sqrt(rqTLTweek) - mean(sqrt(rqTLTweek))
rqTLTmonth <- rowMeans(cbind(rq_TLT_1min, mlag(rq_TLT_1min,21,mean(rq_TLT_1min))))
sqrtrq_TLTmonth <- sqrt(rqTLTmonth) - mean(sqrt(rqTLTmonth))
#------------------HAR preparation------------------------
#RV
oneminvol_TLT <- matrix((calccov[[1]][[6]][1,1,])) * 10000 #sqrt
volday_TLT <- oneminvol_TLT
volweek_TLT <- rowMeans(cbind(oneminvol_TLT, mlag(oneminvol_TLT,4,mean(oneminvol_TLT))))
volmonth_TLT <- rowMeans(cbind(oneminvol_TLT, mlag(oneminvol_TLT,21,mean(oneminvol_TLT))))
oneminvol_SPY <- matrix((calccov[[1]][[6]][2,2,])) * 10000 #sqrt
volday_SPY <- oneminvol_SPY
volweek_SPY <- rowMeans(cbind(oneminvol_SPY, mlag(oneminvol_SPY,4,mean(oneminvol_SPY))))
volmonth_SPY <- rowMeans(cbind(oneminvol_SPY, mlag(oneminvol_SPY,21,mean(oneminvol_SPY))))
#RSpos + RSneg
voldayposvar_TLT <- matrix((calccov[[2]][[6]][1,1,])) * 10000 #sqrt
voldaynegvar_TLT <- matrix((calccov[[3]][[6]][1,1,])) * 10000 #sqrt
voldayposvar_SPY <- matrix((calccov[[2]][[6]][2,2,])) * 10000 #sqrt
voldaynegvar_SPY <- matrix((calccov[[3]][[6]][2,2,])) * 10000 #sqrt
#MRC
oneminmrcvol_TLT <- matrix((calccov[[7]][[6]][1,1,])) * 10000 #sqrt
mrcvolday_TLT <- oneminmrcvol_TLT
mrcvolweek_TLT <- rowMeans(cbind(oneminmrcvol_TLT, mlag(oneminmrcvol_TLT,4,mean(oneminmrcvol_TLT))))
mrcvolmonth_TLT <- rowMeans(cbind(oneminmrcvol_TLT, mlag(oneminmrcvol_TLT,21,mean(oneminmrcvol_TLT))))
oneminmrcvol_SPY <- matrix((calccov[[7]][[6]][2,2,])) * 10000 #sqrt
mrcvolday_SPY <- oneminmrcvol_SPY
mrcvolweek_SPY <- rowMeans(cbind(oneminmrcvol_SPY, mlag(oneminmrcvol_SPY,4,mean(oneminmrcvol_SPY))))
mrcvolmonth_SPY <- rowMeans(cbind(oneminmrcvol_SPY, mlag(oneminmrcvol_SPY,21,mean(oneminmrcvol_SPY))))
#MRK
oneminmrkvol_TLT <- matrix((calccov[[8]][[6]][1,1,])) * 10000 #sqrt
mrkvolday_TLT <- oneminmrkvol_TLT
mrkvolweek_TLT <- rowMeans(cbind(oneminmrkvol_TLT, mlag(oneminmrkvol_TLT,4,mean(oneminmrkvol_TLT))))
mrkvolmonth_TLT <- rowMeans(cbind(oneminmrkvol_TLT, mlag(oneminmrkvol_TLT,21,mean(oneminmrkvol_TLT))))