-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibraries_and_functions.R
1359 lines (1128 loc) · 37.9 KB
/
libraries_and_functions.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
#######################################################
######################## Packages #####################
#######################################################
# Please install and load the following packages:
library(skewt)
library(sn)
library(fBasics)
library(poibin)
library(rugarch)
library(Quandl)
library(quantmod)
library(fGarch)
library(foreach) # install.packages("doParallel")
library(iterators) # install.packages("doParallel")
library(parallel) # install.packages("doParallel")
library(doParallel) # install.packages("doParallel")
set.seed(2018)
options(scipen = 30)
rm(list=ls()) # Remove all objects
graphics.off()
#######################################################
######################## Backtests ####################
#######################################################
# Acerbi/Szekely - Test 1 (2014)
as1 = function(x,var,es){
# Inputs:
# x | time series
# var | VaR prediction
# es | ES prediction
# Output:
# z | test statistic
z = 0
n = length(x)
nt= 0
for (i in 1:n)
{
if (x[i] < - var[i])
{
nt = nt + 1
z = z + x[i]/es[i]
}
}
z = z/nt + 1
return(z)
}
# Acerbi/Szekely - Test 2 (2014)
as2 = function(x,var,es,alpha){
# Inputs:
# x | time series
# var | VaR prediction
# es | ES prediction
# alpha | confidence level
# Output:
# z | test statistic
n = length(x)
z = 0
for (i in 1:n){
if (x[i] < - var[i])
{
z = z + (x[i])/(n*alpha*es[i])
}
}
z = z+1
return(z)
}
# Acerbi/Szekely - Test3 (2014) (IID Setup)
as3 = function(x,H0,alpha){
# Inputs:
# x | iid time series
# H0 | assumption
# alpha | confidence level
# Output:
# z | test statistic
T = length(x)
x = sort(x)
h = floor(T*alpha)
H0_dist = H0[1]
H0_par1 = as.numeric(H0[2]) # mu
H0_par2 = as.numeric(H0[3]) # sigma/sd
H0_par3 = as.numeric(H0[4]) # df / skewness
H0_par4 = as.numeric(H0[5]) # skewness for skewt
numerator = 0
denominator = 0
# auxiliary function for integration in the denominator
fb = function(p){
return(pbinom(floor(T*alpha)-1,T-1,p))
}
if (H0_dist=="normal"){
summe = function(x){
return(fb(x)*qnorm(x,H0_par1,H0_par2)) # 18.12. Hier geht es so
}
}
if (H0_dist=="stdt"){
summe = function(x){
# 18.12.: return(fb(x)*H0_par2*qt((x-H0_par1),H0_par3)) # 12.12. vorher: return(fb(x)*qt((x-H0_par1)/H0_par2,H0_par3))
return(fb(x)*((H0_par2*qt((x),H0_par3))+H0_par1)) # neu am 18.12.
}
}
if (H0_dist=="skewn"){
summe = function(x){
return(fb(x)*(H0_par2*qsn((x),alpha=H0_par3)+H0_par1)) # 18.12.
}
}
if (H0_dist=="skewt"){
summe = function(x){
return(fb(x)*(H0_par2*qskt((x),H0_par3,H0_par4)+H0_par1)) # 18.12.
}
}
# Calculate the numerator
for (i in 1:h){
numerator = numerator + x[i]
}
numerator = -1/h * numerator
# Calculate the denominator
denominator = integrate(summe,0,1)
denominator = as.numeric(denominator[1])
denominator = -T/(floor(T*alpha)) * denominator
z = - (numerator/denominator) + 1
return(z)
}
# Acerbi/Szekely - Test 3 (2014) adjustet with individual denominator
as3_adjusted = function(x,alpha,denominator){
# Inputs:
# x | iid time series
# alpha | confidence level
# denominator | the denominator as a vector
# Output:
# z | test statistic
T = length(x)
x = sort(x)
h = floor(T*alpha)
numerator = 0
# Calculate numerator:
for (i in 1:h){
numerator = numerator + x[i]
}
numerator = T*numerator
numerator = -1/h * numerator
denominator_sum = sum(denominator)
z = - (numerator/denominator_sum) + 1
return(z)
}
# Acerbi/Szekely - Test 4 (2017) (IID Setup)
as4 = function(x,var,es,alpha){
# Inputs:
# x | time series
# var | VaR prediction
# es | ES prediction
# alpha | confidence level
# Output:
# z | test statistic
T = length(x)
z = 0
for (i in 1:T){
add = alpha*(es[i]-var[i])
if (x[i] < - var[i]){
add = add + x[i] + var[i]
}
add = add/(es[i]*alpha)
z = z + add
}
z = z/T
return(z)
}
# The simple BCBS VaR binomial test (99% VaR prediction)
var_test = function(x,var,p){
# Inputs:
# x | time series
# var | VaR prediction, should be the 99 % prediction
# p | legal probability of error
# Output:
# reject | BOOLEAN
T = length(x)
reject = FALSE
# Count Exceedances
n = 0
for (i in 1:T){
if (x[i] <= -var[i]){
n = n+1
}
}
# If the p_value (1 - pbinom(n,t,0.01)) is lower than the probability of error, the tests rejects
# Note that 0.01 is selected analogous to Basel requirements
if (1 - pbinom(n,T,0.01) < p){
reject = TRUE
}
return (reject)
}
# Corbetta/Peri - (2016) (IID Setting)
cp = function(x,rho,H0,p){
# Inputs:
# x | time series
# rho | risk measure prediction
# H0 | assumption
# p | significance level
# Output:
# z1 | test statistic of test 1
# z2 | test statistic of test 2
# reject1 | rejection of test 1 (as BOOLEAN)
# reject2 | rejection of test 2 (as BOOLEAN)
T = length(x)
pb = numeric(T)
lambda = 0
z1 = 0
z2 = 0
h1 = 0 # numerator
h2 = 0 # denominator
H0_dist = H0[1]
H0_par1 = as.numeric(H0[2]) # mu
H0_par2 = as.numeric(H0[3]) # sigma/sd
H0_par3 = as.numeric(H0[4]) # df / skewness
H0_par4 = as.numeric(H0[5]) # skewness for skewt
reject1 = FALSE
reject2 = FALSE
for (i in 1:T){
y = -rho[i]
if (H0_dist=="normal"){
lambda = pnorm(y,H0_par1,H0_par2)
}
if (H0_dist=="stdt"){
lambda = pt((y-H0_par1)/H0_par2,H0_par3)
}
if (H0_dist=="skewn"){
lambda = psn(x=(y-H0_par1)/H0_par2,alpha=H0_par3)
}
if (H0_dist=="skewt"){
lambda = pskt((y-H0_par1)/H0_par2,H0_par3,H0_par4)
}
pb[i] = lambda
if (x[i]<y){
z1 = z1 + 1
h1 = h1 + (1 - lambda)
}
if (x[i]>y){
h1 = h1 - lambda
}
h2 = h2 + lambda*(1-lambda)
}
h2 = sqrt(h2)
z2 = h1/h2
# If the p value of Z1 ~ Poiss.Bin(lambda) is lower than the significance level, the test rejects
if((1-ppoibin(z1,pp=pb))<p){
reject1 = TRUE
}
# If the p value of Z2 ~ N(0,1) is lower than the significance level, the test reject
#
# Since one does not know exactly in which direction the test statistic
# develops under H1, one must test in both directions.
#
if((1-pnorm(z2))<p/2){
reject2 = TRUE
}
if(pnorm(z2)<p/2){
reject2 = TRUE
}
return(list(z1,z2,reject1,reject2))
}
# Corbetta/Peri functions, such that lambda_t = P_t(-rho_t) is delivered seperately. Non-IID and non-standard distributions possible.
cp_adjusted = function(x,rho,cp_pt,p){
# Inputs:
# x | time series
# rho | risk measure prediction
# cp_pt | lambda_t as a vector
# p | significance level
# Output:
# z1 | test statistic of test 1
# z2 | test statistic of test 2
# reject1 | rejection of test 1 (as BOOLEAN)
# reject2 | rejection of test 2 (as BOOLEAN)
T = length(x)
pb = numeric(T)
lambda = 0
z1 = 0
z2 = 0
h1 = 0
h2 = 0
reject1 = FALSE
reject2 = FALSE
for (i in 1:T){
y = -rho[i]
lambda = cp_pt[i]
pb[i] = lambda
if (x[i]<y){
z1 = z1 + 1
h1 = h1 + (1 - lambda)
}
if (x[i]>y){
h1 = h1 - lambda
}
h2 = h2 + lambda*(1-lambda)
}
h2 = sqrt(h2)
z2 = h1/h2
# If the p value of Z1 ~ Poiss.Bin(lambda) is lower than the significance level, the test rejects:
if((1-ppoibin(z1,pp=pb))<p){
reject1 = TRUE
}
# If the p value of Z2 ~ N(0,1) is lower than the significance level, the test rejects:
if((1-pnorm(z2))<p/2){
reject2 = TRUE
}
if(pnorm(z2)<p/2){
reject2 = TRUE
}
return(list(z1,z2,reject1,reject2))
}
###########################################################
######################## Functions ########################
###########################################################
# Simulation of an iid-time-series
pl_iid_sim = function(T,H0){
# Inputs:
# T | length of time series
# H0 | assumption
# H0 = c(dist,parameter1,parameter2,parameter3,parameter4)
# If dist = normal: parameter1 = mu, parameter2 = sd
# If dist = st: parameter1 = mu, parameter2 = sigma, parameter3 = df
# If dist = skewn: parameter1 = mu, parameter2 = sigma, parameter3 = alpha
# If dist = skewt: parameter1 = mu, parameter2 = sigma, parameter3 = df, parameter4 = gamma
# Note that sigma is the SD-Shift for all distributions exept the N(mu,sd) one
# Output:
# x | simulated time series
x = numeric(T)
dist = H0[1]
parameter1 = as.numeric(H0[2])
parameter2 = as.numeric(H0[3])
parameter3 = as.numeric(H0[4])
parameter4 = as.numeric(H0[5])
if (dist=="normal"){
mu = parameter1
sd= parameter2
x = sd*rnorm(T) + mu
}
if (dist=="stdt"){
mu = parameter1
sigma = parameter2
df = parameter3
x = mu + sigma*rt(T,df)
}
if (dist=="skewn"){
mu = parameter1
sigma = parameter2
shape = parameter3
x = as.numeric(rsn(n=T, xi=mu -sigma*shape/sqrt(1+(shape)^2)*sqrt(2/pi), omega=sigma, alpha=shape))
# Since with the shape, die expected value is moved by (shape/sqrt(1+(shape)^2)*sqrt(2/pi))
}
if (dist=="skewt"){
mu = parameter1
sigma = parameter2
df = parameter3
gamma = parameter4
x = sigma*rskt(T,df,gamma)
x = x - mean(x) # Mean adjustment due to the skewness
x = x + mu
}
return(x)
}
# Simulation of an iid-time-series with n VaR exceedances
pl_iid_sim_fixn = function(T,H0,n,alpha){
# Inputs:
# T | length of time series
# H0 | assumption
# n | number of VaR exceedances
# alpha | confidence level
# H0 = c(dist,parameter1,parameter2,parameter3,parameter4)
# If dist = normal: parameter1 = mu, parameter2 = sd
# If dist = st: parameter1 = mu, parameter2 = sigma, parameter3 = df
# If dist = skewn: parameter1 = mu, parameter2 = sigma, parameter3 = alpha
# If dist = skewt: parameter1 = mu, parameter2 = sigma, parameter3 = df, parameter4 = gamma
# Note that sigma is the SD-Shift for all distributions exept the N(mu,sd) one
# Output:
# x | simulated time series
x = numeric(T)
outlier = numeric(n)
rest = numeric(T-n)
dist = H0[1]
par1 = as.numeric(H0[2])
mu = par1
par2 = as.numeric(H0[3])
par3 = as.numeric(H0[4])
par4 = as.numeric(H0[5])
q1 = runif(n,min=0,max=alpha)
q2 = runif(T-n,min=alpha,max=1)
if (dist=="normal"){
outlier = qnorm(q1,par1,par2)
rest = qnorm(q2,par1,par2)
}
if (dist=="stdt"){
aux = pt(-var_es_analytic(H0,alpha)[1]/par2,par3)
q1 = runif(n,min=0,max=aux)
q2 = runif(T-n,min=aux,max=1)
outlier = par2*qt(q1,par3)
rest = par2*qt(q2,par3)
}
if (dist=="skewn"){
shape = par3
mu= par1 -shape/sqrt(1+(shape)^2)*sqrt(2/pi)
outlier = qsn(q1,xi=mu,omega=par2,alpha=shape)
rest = qsn(q2,xi=mu,omega=par2,alpha=shape)
}
if (dist=="skewt"){
outlier = par2*qskt(q1,par3,par4)
rest = par2*qskt(q2,par3,par4)
# Mean Adjustment due to the skewness
sample = par2*rskt(T,df=par3,gamma=par4)
mean_adjust = mean(sample)
outlier = outlier + mu - mean_adjust
rest = rest + mu - mean_adjust
}
x = c(outlier,rest)
return(x)
}
# Calculation of VaR und ES for standard distributions
var_es_analytic = function(H0, alpha){
# Inputs:
# H0 | assumption
# alpha | confidence level
# H0 = c(dist,parameter1,parameter2,parameter3,parameter4)
# If dist = normal: parameter1 = mu, parameter2 = sigma
# If dist = st: parameter1 = mu, parameter2 = sigma, parameter3 = df
# If dist = skewn: parameter1 = mu, parameter2 = sigma, parameter3 = alpha
# If dist = skewt: parameter1 = mu, parameter2 = sigma, parameter3 = df, parameter4 = gamma
# Note that sigma is the SD-Shift for all distributions exept the N(mu,sd) one
# Output:
# var | Value-at-Risk
# es | Expected Shortfall
dist = H0[1]
parameter1 = as.numeric(H0[2])
parameter2 = as.numeric(H0[3])
parameter3 = as.numeric(H0[4])
parameter4 = as.numeric(H0[5])
if (dist=="normal"){
mu = parameter1
sd = parameter2
var = -sd*qnorm(alpha) - mu
es = sd*dnorm(qnorm(1-alpha))/alpha - mu
}
if(dist == "stdt"){
mu = parameter1
sd = parameter2
df = parameter3
var = -sd*qt(alpha,df) - mu
b = beta(df/2,0.5)
es = 2*sd/(alpha*2*sqrt(df)*b)*1/((1+((qt(alpha,df))^2/df))^((df+1)/2))*(df + (qt(alpha,df))^2)/(df-1) - mu
}
if(dist == "skewn"){
sd = parameter2
al = parameter3
shape = parameter3
mu = parameter1 - sd*shape/sqrt(1+(shape)^2)*sqrt(2/pi)
var = - sd*qsn(alpha,xi=0,omega=1, alpha=al, tau=0, dp=NULL, tol=1e-10, solver="NR") - mu
yp= (-var-mu)/sd
zp=sqrt(1+(al)^2)*yp
# Bernadi (2012)
# es = - ((sd*sqrt(2))/(alpha*sqrt(pi))*(al*pnorm(zp) - sqrt(2*pi)*dnorm(yp)*pnorm(al*yp))) - mu
es = - sd*as.numeric((integrate((function(x) qsn(x,omega=1,alpha=al)),0,alpha))[1])/alpha - mu
}
if(dist == "skewt"){
mu = parameter1
sd = parameter2
df = parameter3
gamma = parameter4
help = sd*rskt(100000,df,gamma) # Mean Adjustment
mean_adjust = mean(help) # Mean Adjustment
var = -sd*qskt(alpha,df,gamma) - mu + mean_adjust
es = - sd*as.numeric((integrate((function(x) qskt(x,df,gamma)),0,alpha))[1])/alpha - mu + mean_adjust
}
return(c(var,es))
}
# Critical values for the distributions of the test statistics of A/S 1-4 for standard distributions (IID Setup)
crit_H0 = function(T,H0,M,alpha,p){
# Inputs:
# T | length of time series
# H0 | assumption
# M | number of Monte Carlo simulations
# alpha | significance level
# p | significance level
# Outputs:
# crit_mc | vector with 4 critical values for A/S tests 1-4
# var_es_H0 | vectors of VaR and ES for each day (iid assumption)
x = numeric(T)
z_H0_mc = matrix(0,M,4)
crit_mc = numeric(4)
var_es_H0_help = c(0,0)
var_es_H0 = matrix(0,T,2)
# Calculate VaR and ES under H0 and save it as a vector for every day t (iid assumption)
var_es_H0_help = var_es_analytic(H0,alpha)
var_es_H0[,1] = rep(var_es_H0_help[1],T)
var_es_H0[,2] = rep(var_es_H0_help[2],T)
# Adjustment for the skewed distributions
# The 3rd Test of A/S and the Tests of C/P calculate with values using the quantile function.
# The re-adjustment of the mean as described in the master thesis is already transfered here.
if(H0[[1]]=="skewn")
{
mu = as.numeric(H0[2])
sigma = as.numeric(H0[3])
shape = as.numeric(H0[4])
mean_adjust = sigma*shape/sqrt(1+(shape)^2)*sqrt(2/pi)
H0_adj = c("skewn",mu - mean_adjust,sigma,shape,0)
}
if(H0[[1]]=="skewt")
{
mu = as.numeric(H0[2])
sigma = as.numeric(H0[3])
df = as.numeric(H0[4])
gamma = as.numeric(H0[5])
mean_adjust = mean(sigma*rskt(100000,df,gamma))
H0_adj = c("skewt",mu-mean_adjust,sigma,df,gamma)
}
################
# Calculate a Monte-Carlo sample of Z_i under H0:
for (i in 1:M){
x = pl_iid_sim(T,H0)
z_H0_mc[i,1]=as1(x,var_es_H0[,1],var_es_H0[,2])
z_H0_mc[i,2]=as2(x,var_es_H0[,1],var_es_H0[,2],alpha)
if (H0[[1]]=="skewn" || H0[[1]]=="skewt"){
z_H0_mc[i,3]=as3(x,H0_adj,alpha) # H0 Adjusted
}
if (H0[[1]]=="normal" || H0[[1]]=="stdt"){
z_H0_mc[i,3]=as3(x,H0,alpha)
}
#z_H0_mc[i,3]=as3(x,H0,alpha)
z_H0_mc[i,4]=as4(x,var_es_H0[,1],var_es_H0[,2],alpha)
}
# Calculate the critical values:
z_H0_mc[,1]=sort(z_H0_mc[,1],na.last=TRUE)
z_H0_mc[,2]=sort(z_H0_mc[,2])
z_H0_mc[,3]=sort(z_H0_mc[,3])
z_H0_mc[,4]=sort(z_H0_mc[,4])
crit_mc[1]=z_H0_mc[,1][floor(M*p)]
crit_mc[2]=z_H0_mc[,2][floor(M*p)]
crit_mc[3]=z_H0_mc[,3][floor(M*p)]
crit_mc[4]=z_H0_mc[,4][floor(M*p)]
return(list(crit_mc,var_es_H0))
}
# Calculation of the power of the tests for different standard H0 and H1 distributions (IID Setup)
power_calc = function(T,crit_mc,var_es_H0,H0,H1,N,alpha,p){
# Inputs:
# T | length of time series
# crit_mc | critical values of the tests 1-4 under H0
# var_es_H0 | vector with VaR and ES for each day (iid assumption)
# H0 | H0 assumption
# H1 | actual assumption
# N | number of Monte Carlo simulations which are done to calculate the power
# alpha | confidence level
# p | significance level
# Outputs:
# power | vector with 6 values, power of tests A/S 1-4, C/P 1,2
x = numeric(T)
z_H1_mc = matrix(0,N,4)
cp_tests = matrix(0,N,4)
var_tests = numeric(T)
power = numeric(6)
# Adjustment for the skewed distributions
# The 3rd Test of A/S and the Tests of C/P calculate with values using the quantile function.
# The re-adjustment of the mean as described in the master thesis is already transfered here.
if(H0[[1]]=="skewn")
{
mu = as.numeric(H0[2])
sigma = as.numeric(H0[3])
shape = as.numeric(H0[4])
mean_adjust = sigma*shape/sqrt(1+(shape)^2)*sqrt(2/pi)
H0_adj = c("skewn",mu - mean_adjust,sigma,shape,0)
}
if(H0[[1]]=="skewt")
{
mu = as.numeric(H0[2])
sigma = as.numeric(H0[3])
df = as.numeric(H0[4])
gamma = as.numeric(H0[5])
mean_adjust = mean(sigma*rskt(100000,df,gamma))
H0_adj = c("skewt",mu-mean_adjust,sigma,df,gamma)
}
################
for (i in 1:N){
# Calculate a Monte-Carlo sample of Z under H1 (with assumed VaR and ES from H0):
x = pl_iid_sim(T,H1)
z_H1_mc[i,1]=as1(x,var_es_H0[,1],var_es_H0[,2])
z_H1_mc[i,2]=as2(x,var_es_H0[,1],var_es_H0[,2],alpha)
if (H0[[1]]=="skewn" || H0[[1]]=="skewt"){
z_H1_mc[i,3] = as3(x,H0_adj,alpha) # H0 Adjusted!
}
if (H0[[1]]=="normal" || H0[[1]]=="stdt"){
z_H1_mc[i,3] = as3(x,H0,alpha)
}
z_H1_mc[i,4]=as4(x,var_es_H0[,1],var_es_H0[,2],alpha)
# Test C/P directly:
if (H0[[1]]=="skewn" || H0[[1]]=="skewt"){
cp_test = cp(x,var_es_H0[,2],H0_adj,p) # H0 Adjusted!
}
if (H0[[1]]=="normal" || H0[[1]]=="stdt"){
cp_test = cp(x,var_es_H0[,2],H0,p)
}
cp_tests[i,1]=cp_test[[3]]
cp_tests[i,2]=cp_test[[4]]
}
# Calculate the power of the tests:
power[1]=length(z_H1_mc[,1][z_H1_mc[,1]<crit_mc[1]])/N
power[2]=length(z_H1_mc[,2][z_H1_mc[,2]<crit_mc[2]])/N
power[3]=length(z_H1_mc[,3][z_H1_mc[,3]<crit_mc[3]])/N
power[4]=length(z_H1_mc[,4][z_H1_mc[,4]<crit_mc[4]])/N
power[5]=mean(cp_tests[,1])
power[6]=mean(cp_tests[,2])
return(power)
}
# Calculation of the significance of the tests (IID Setup)
significance = function(H0,T,setting,M,alpha,p,crit_mc){ # crit_mc=numeric(4) wieder Pflicht!
# Inputs:
# H0 | H0 distribution H0
# T | length of time series T
# setting | "random" or numeric, if one wants to simulate a fixed number of VaR exceedances
# M | number of simulations to compute the significance
# p | the prescribed significance level
# crit_mc | optional. Needed if one wants to simulate a fixed number of VaR exceedances
# Output:
# significance | a vector with calculated significances for all tests
simulations = matrix(0,6,M)
significance = numeric(6)
# Calculation of VaR and ES
var1 = var_es_analytic(H0,alpha)[[1]]
es1 = var_es_analytic(H0,alpha)[[2]]
var_es = matrix(0,T,2)
var_es[,1] = rep(var1,T)
var_es[,2] = rep(es1,T)
cp_test = 0
# Adjustment for the skewed distributions
# The 3rd Test of A/S and the Tests of C/P calculate with values using the quantile function.
# The re-adjustment of the mean as described in the master thesis is already transfered here.
if(H0[[1]]=="skewn")
{
mu = as.numeric(H0[2])
sigma = as.numeric(H0[3])
shape = as.numeric(H0[4])
mean_adjust = sigma*shape/sqrt(1+(shape)^2)*sqrt(2/pi)
H0_adj = c("skewn",mu - mean_adjust,sigma,shape,0)
}
if(H0[[1]]=="skewt")
{
mu = as.numeric(H0[2])
sigma = as.numeric(H0[3])
df = as.numeric(H0[4])
gamma = as.numeric(H0[5])
mean_adjust = mean(sigma*rskt(100000,df,gamma))
H0_adj = c("skewt",mu-mean_adjust,sigma,df,gamma)
}
# End Mean Adjustment
# Calculate the significance for all tests
# Simulation of M iid p&l time series and performing of the tests
for (i in 1:M){
if (is.numeric(setting) == FALSE){
x = pl_iid_sim(T,H0)
}
if (is.numeric(setting) == TRUE){
n = setting
x = pl_iid_sim_fixn(T,H0,n,alpha)
}
simulations[1,i] = as1(x,var_es[,1],var_es[,2])
simulations[2,i] = as2(x,var_es[,1],var_es[,2],alpha)
if (H0[[1]]=="skewn" || H0[[1]]=="skewt"){
simulations[3,i] = as3(x,H0_adj,alpha) # H0 Adjusted
}
if (H0[[1]]=="normal" || H0[[1]]=="stdt"){
simulations[3,i] = as3(x,H0,alpha)
}
simulations[4,i] = as4(x,var_es[,1],var_es[,2],alpha)
if (H0[[1]]=="skewn" || H0[[1]]=="skewt"){
cp_test = cp(x,var_es[,2],H0_adj,p) # H0 Adjusted
}
if (H0[[1]]=="normal" || H0[[1]]=="stdt"){
cp_test = cp(x,var_es[,2],H0,p)
}
simulations[5,i] = cp_test[[3]]
simulations[6,i] = cp_test[[4]]
}
for (k in 1:4)
{
significance[k] = 1-(length(simulations[k,][simulations[k,]<crit_mc[k]])/M)
}
significance[5] = 1-mean(simulations[5,])
significance[6] = 1-mean(simulations[6,])
return(significance)
}
# Power plot for the six backtests, see section "Power"
plot_power = function(seq,power,xlab,main){
n = length(power[1,])
plot(seq,power[,1], type="l", col="blue", lwd=2, xlab=xlab, ylab="power", ylim = range(0:1))
lines(seq,power[,2], col="red", lwd=2)
lines(seq,power[,3], col="green", lwd=2)
lines(seq,power[,4], col="orange", lwd=2)
lines(seq,power[,5], col="black", lwd=2)
lines(seq,power[,6], col="grey", lwd=2)
title(main=main)
legend("bottomright", box.lty=0, cex=0.7, c("A/S 1", "A/S 2", "A/S 3", "A/S 4", "C/P 1", "C/P 2"), col=c("blue","red","green","orange","black","grey"), lwd=2)
}
# Power: A fixed number of exceedances. Adjustments of the functions "pl_iid_sim" and "power_calc"
# Simulation of a fixed number of VaR exceedances under H0 by a given distribution H1
pl_iid_sim_fixn_H0H1 = function(T,var_H0,H1,n,alpha_H0){
# Simulate a p&l time series with n exceedances under the VaR of alpha
# Inputs:
# T = length of time series
# var_H0 = the VaR under H0
# H1 = alternative hypothesis
# n = number of returns under VaR
# alpha_H0 = confidence level
x = numeric(T)
outlier = numeric(n)
rest = numeric(T-n)
dist = H1[1]
par1 = as.numeric(H1[2])
par2 = as.numeric(H1[3])
par3 = as.numeric(H1[4])
par4 = as.numeric(H1[5])
if (dist=="normal"){
alpha_new = pnorm(-var_H0,par1,par2) # Adjusted
q1 = runif(n,0,alpha_new) # Adjusted
q2 = runif(T-n,alpha_new,1) # Adjusted
outlier = qnorm(q1,par1,par2)
rest = qnorm(q2,par1,par2)
}
if (dist=="stdt"){
alpha_new = pt(-var_H0/par2,par3) # Adjusted
q1 = runif(n,0,alpha_new) # Adjusted
q2 = runif(T-n,alpha_new,1) # Adjusted
outlier = qt(q1,par3)*par2
rest = qt(q2,par3)*par2
}
if (dist=="skewn"){
shape = par3
mu= par1 -shape/sqrt(1+(shape)^2)*sqrt(2/pi) # Mean adjustment
alpha_new = psn(-var_H0,xi=mu,omega=par2,alpha=par3) # Adjusted
q1 = runif(n,0,alpha_new) # Adjusted
q2 = runif(T-n,alpha_new,1) # Adjusted
outlier = qsn(q1,xi=mu,omega=par2,alpha=shape)
rest = qsn(q2,xi=mu,omega=par2,alpha=shape)
}
if (dist=="skewt"){
# Mean Adjustment
sample = par2*rskt(T,df=par3,gamma=par4)
mean_adjust = mean(sample)
outlier = qskt(q1,par3,par4)*par2 - mean_adjust
rest = qskt(q2,par3,par4)*par2 - mean_adjust
}
x = c(outlier,rest)
return(x)
}
# Calculation of the power in that case
power_calc_fixn_H0H1 = function(T,n,crit_mc,var_es_H0,H0,H1,N,alpha,p){
# Everything works analogue to the power function above.
# The only difference is that here, a simulation of a fixed number of VaR_H0 exceedances is made.
x = numeric(T)
z_H1_mc = matrix(0,N,4) # N simulations, 4 tests of A/S
cp_tests = matrix(0,N,2) # N simulations, 2 CP Tests
power = numeric(6)
# Adjustment for the skewed distributions
# The 3rd Test of A/S and the Tests of C/P calculate with values using the quantile function.
# The re-adjustment of the mean as described in the master thesis is already transfered here.
if(H0[[1]]=="skewn")
{
mu = as.numeric(H0[2])
sigma = as.numeric(H0[3])
shape = as.numeric(H0[4])
mean_adjust = sigma*shape/sqrt(1+(shape)^2)*sqrt(2/pi)
H0_adj = c("skewn",mu - mean_adjust,sigma,shape,0)
}
if(H0[[1]]=="skewt")
{
mu = as.numeric(H0[2])
sigma = as.numeric(H0[3])
df = as.numeric(H0[4])
gamma = as.numeric(H0[5])
mean_adjust = mean(sigma*rskt(100000,df,gamma))
H0_adj = c("skewt",mu-mean_adjust,sigma,df,gamma)
}
################
for (i in 1:N){
# Calculate a Monte-Carlo sample of Z under H1 (with Var & ES from H0):
x = pl_iid_sim_fixn_H0H1(T,var_es_H0[1],H1,n,alpha)
z_H1_mc[i,1]=as1(x,var_es_H0[,1],var_es_H0[,2])
z_H1_mc[i,2]=as2(x,var_es_H0[,1],var_es_H0[,2],alpha)
if (H0[[1]]=="skewn" || H0[[1]]=="skewt"){
z_H1_mc[i,3] = as3(x,H0_adj,alpha) # H0 Adjusted
}
if (H0[[1]]=="normal" || H0[[1]]=="stdt"){
z_H1_mc[i,3] = as3(x,H0,alpha)
}
z_H1_mc[i,4]=as4(x,var_es_H0[,1],var_es_H0[,2],alpha)
# Test C/P directly:
if (H0[[1]]=="skewn" || H0[[1]]=="skewt"){
cp_test = cp(x,var_es_H0[,2],H0_adj,p) # H0 Adjusted
}
if (H0[[1]]=="normal" || H0[[1]]=="stdt"){
cp_test = cp(x,var_es_H0[,2],H0,p)
}
cp_tests[i,1]=cp_test[[3]]
cp_tests[i,2]=cp_test[[4]]
}
# Calculate the power of the tests:
power[1]=length(z_H1_mc[,1][z_H1_mc[,1]<crit_mc[1]])/N
power[2]=length(z_H1_mc[,2][z_H1_mc[,2]<crit_mc[2]])/N
power[3]=length(z_H1_mc[,3][z_H1_mc[,3]<crit_mc[3]])/N
power[4]=length(z_H1_mc[,4][z_H1_mc[,4]<crit_mc[4]])/N
power[5]=mean(cp_tests[,1])
power[6]=mean(cp_tests[,2])
return(power)
}
# Which role does VaR play? Adjustments of the functions "crit_H0" and "significance"
# Adjustement of crit_H0 for example 1
crit_H0_example1 = function(T,var_es_H0,M,alpha,p,denom){
# Similar structure as the function "crit_H0", adjusted for the example1 (VaR-sensitivity)
x = numeric(T)
z_H0_mc = matrix(0,M,4) # M simulations, 4 tests
crit_mc = numeric(4)
# Calculate a Monte-Carlo sample of Z under H0:
for (i in 1:M){
x = example1(T,alpha)
z_H0_mc[i,1]=as1(x,var_es_H0[,1],var_es_H0[,2])
z_H0_mc[i,2]=as2(x,var_es_H0[,1],var_es_H0[,2],alpha)
z_H0_mc[i,3]=as3_adjusted(x,alpha,denom)
z_H0_mc[i,4]=as4(x,var_es_H0[,1],var_es_H0[,2],alpha)
}
# Calculate the critical values:
z_H0_mc[,1]=sort(z_H0_mc[,1],na.last=TRUE)
z_H0_mc[,2]=sort(z_H0_mc[,2])
z_H0_mc[,3]=sort(z_H0_mc[,3])
z_H0_mc[,4]=sort(z_H0_mc[,4])
crit_mc[1]=z_H0_mc[,1][floor(M*p)]
crit_mc[2]=z_H0_mc[,2][floor(M*p)]
crit_mc[3]=z_H0_mc[,3][floor(M*p)]
crit_mc[4]=z_H0_mc[,4][floor(M*p)]
return(crit_mc)