subroutine  backtests

'--------------------------------------------------------------------------------------------------------------------------------------
' *** Coverage rates: frequencies of actual outcomes falling inside 50%, 70% or 90% intervals ***

matrix(99,model_max) zh1_percent_hits_full
matrix(99,model_max) zh2_percent_hits_full
matrix(99,model_max) zh3_percent_hits_full
matrix(99,model_max) zh4_percent_hits_full
matrix(99,model_max) zh8_percent_hits_full
matrix(99,model_max) zh12_percent_hits_full

matrix(h_max,model_max) z_coverage_rate_50  ' between quants 25 and 75
matrix(h_max,model_max) z_coverage_rate_70  ' between quants 15 and 85
matrix(h_max,model_max) z_coverage_rate_90  ' between quants  5  and 95

matrix(h_max,model_max) z_cover_50_pvalue
matrix(h_max,model_max) z_cover_70_pvalue
matrix(h_max,model_max) z_cover_90_pvalue

for !moodel = 1 to model_max
for !h=h_min to h_max
if (!h<5) or (!h=8) or (!h=12) then

for !quantil=1 to 99

matrix(T_VaR , 1) I
matrix(T_VaR , 1) lag_I
matrix(T_VaR , 1) I1

scalar T1 = 0
scalar cont=0

for !i = 1 to T_VaR  
 cont=cont+1
 I(cont,1)=0
 !quantile = !quantil

 smpl 1985q1+!i-1  1985q1+!i-1

 stom(pi_aux_h!h,m1)

 if (!moodel=1) then
   stom(m1_h!h_q!quantile,m2)   
 endif 
 if (!moodel=2) then
   stom(m2_h!h_q!quantile,m2)
 endif
 if (!moodel=3) then
   stom(m3_h!h_q!quantile,m2)
 endif
  if (!moodel=4) then
   stom(m4_h!h_q!quantile,m2)
  endif
  if (!moodel=5) then
   stom(m5_h!h_q!quantile,m2)
  endif
  if (!moodel=6) then
   stom(m6_h!h_q!quantile,m2)
  endif
  if (!moodel=7) then
   stom(m7_h!h_q!quantile,m2)
  endif
  if (!moodel=8) then
   stom(m8_h!h_q!quantile,m2)
  endif
  if (!moodel=9) then
   stom(m9_h!h_q!quantile,m2)
  endif
  if (!moodel=10) then
   stom(m10_h!h_q!quantile,m2)
  endif
  if (!moodel=11) then
   stom(m11_h!h_q!quantile,m2)
  endif
  if (!moodel=12) then
   stom(m12_h!h_q!quantile,m2)
  endif
  if (!moodel=13) then
   stom(m13_h!h_q!quantile,m2)
  endif
  if (!moodel=14) then
   stom(m14_h!h_q!quantile,m2)
  endif
  if (!moodel=15) then
   stom(m15_h!h_q!quantile,m2)
  endif
  if (!moodel=16) then
   stom(m16_h!h_q!quantile,m2)
  endif
 if (!moodel=17) then
   stom(m17_h!h_q!quantile,m2)   
 endif 
 if (!moodel=18) then
   stom(m18_h!h_q!quantile,m2)
 endif
 if (!moodel=19) then
   stom(m19_h!h_q!quantile,m2)
 endif
  if (!moodel=20) then
   stom(m20_h!h_q!quantile,m2)
  endif
  if (!moodel=21) then
   stom(m21_h!h_q!quantile,m2)
  endif
  if (!moodel=22) then
   stom(m22_h!h_q!quantile,m2)
  endif
  if (!moodel=23) then
   stom(m23_h!h_q!quantile,m2)
  endif
  if (!moodel=24) then
   stom(m24_h!h_q!quantile,m2)
  endif
  if (!moodel=25) then
   stom(m25_h!h_q!quantile,m2)
  endif
  if (!moodel=26) then
   stom(m26_h!h_q!quantile,m2)
  endif
  if (!moodel=27) then
   stom(m27_h!h_q!quantile,m2)
  endif
  if (!moodel=28) then
   stom(m28_h!h_q!quantile,m2)
  endif
  if (!moodel=29) then
   stom(m29_h!h_q!quantile,m2)
  endif
  if (!moodel=30) then
   stom(m30_h!h_q!quantile,m2)
  endif
  if (!moodel=31) then
   stom(m31_h!h_q!quantile,m2)
  endif
  if (!moodel=32) then
   stom(m32_h!h_q!quantile,m2)
  endif
  if (!moodel=33) then
   stom(m33_h!h_q!quantile,m2)
  endif
  if (!moodel=34) then
   stom(m34_h!h_q!quantile,m2)
  endif
  if (!moodel=35) then
   stom(m35_h!h_q!quantile,m2)
  endif
  if (!moodel=36) then
   stom(m36_h!h_q!quantile,m2)
  endif


 if (m1<m2)  then
      T1=T1+1
      I(cont,1)=1
 endif

next   '  !i

scalar T0 = T_VaR-T1
scalar P1 = T1/T_VaR
scalar P0 = 1-P1

zh!h_percent_hits_full(!quantil,!moodel) = P1

next  'quantil

 z_coverage_rate_50(!h,!moodel) =  zh!h_percent_hits_full(75,!moodel)   -   zh!h_percent_hits_full(25,!moodel) 
 z_coverage_rate_70(!h,!moodel) =  zh!h_percent_hits_full(85,!moodel)   -   zh!h_percent_hits_full(15,!moodel) 
 z_coverage_rate_90(!h,!moodel) =  zh!h_percent_hits_full(95,!moodel)   -   zh!h_percent_hits_full(5,!moodel) 

endif  ' h<5, =8, =12
next  ' h
next  ' moodel

'   end of loopings



' *** pvalue unconditional

for !moodel = 1 to model_max
for !h=h_min to h_max
if (!h<5) or (!h=8) or (!h=12) then
for !quantile=1 to 99

        smpl 1985q1  1985q1+T_VaR-1      

        if (!moodel=1) then
          series x_vqr  =     m1_h!h_q!quantile   
        endif
        if (!moodel=2) then
          series x_vqr   =    m2_h!h_q!quantile   
        endif
        if (!moodel=3) then
           series   x_vqr   =    m3_h!h_q!quantile   
        endif
        if (!moodel=4) then
          series   x_vqr  =     m4_h!h_q!quantile   
        endif
        if (!moodel=5) then
          series   x_vqr  =     m5_h!h_q!quantile   
        endif
        if (!moodel=6) then
         series   x_vqr   =    m6_h!h_q!quantile   
        endif
        if (!moodel=7) then
            series   x_vqr  =     m7_h!h_q!quantile   
        endif
        if (!moodel=8) then
          series   x_vqr    =    m8_h!h_q!quantile   
        endif
        if (!moodel=9) then
          series   x_vqr    =    m9_h!h_q!quantile   
        endif
        if (!moodel=10) then
          series   x_vqr    =    m10_h!h_q!quantile   
        endif
        if (!moodel=11) then
          series  x_vqr    =    m11_h!h_q!quantile   
        endif
        if (!moodel=12) then
          series  x_vqr    =    m12_h!h_q!quantile   
        endif
        if (!moodel=13) then
          series  x_vqr    =    m13_h!h_q!quantile   
        endif
        if (!moodel=14) then
          series  x_vqr    =    m14_h!h_q!quantile   
        endif
        if (!moodel=15) then
          series  x_vqr    =    m15_h!h_q!quantile   
        endif
        if (!moodel=16) then
          series  x_vqr    =    m16_h!h_q!quantile   
        endif
        if (!moodel=17) then
          series x_vqr  =     m17_h!h_q!quantile   
        endif
        if (!moodel=18) then
          series x_vqr   =    m18_h!h_q!quantile   
        endif
        if (!moodel=19) then
           series   x_vqr   =    m19_h!h_q!quantile   
        endif
        if (!moodel=20) then
          series   x_vqr  =     m20_h!h_q!quantile   
        endif
        if (!moodel=21) then
          series   x_vqr  =     m21_h!h_q!quantile   
        endif
        if (!moodel=22) then
         series   x_vqr   =    m22_h!h_q!quantile   
        endif
        if (!moodel=23) then
            series   x_vqr  =     m23_h!h_q!quantile   
        endif
        if (!moodel=24) then
          series   x_vqr    =    m24_h!h_q!quantile   
        endif
        if (!moodel=25) then
          series   x_vqr    =    m25_h!h_q!quantile   
        endif
        if (!moodel=26) then
          series   x_vqr    =    m26_h!h_q!quantile   
        endif
        if (!moodel=27) then
          series  x_vqr    =    m27_h!h_q!quantile   
        endif
        if (!moodel=28) then
          series  x_vqr    =    m28_h!h_q!quantile   
        endif
        if (!moodel=29) then
          series  x_vqr    =    m29_h!h_q!quantile   
        endif
        if (!moodel=30) then
          series  x_vqr    =    m30_h!h_q!quantile   
        endif
        if (!moodel=31) then
          series  x_vqr    =    m31_h!h_q!quantile   
        endif
        if (!moodel=32) then
          series  x_vqr    =    m32_h!h_q!quantile   
        endif
        if (!moodel=33) then
          series  x_vqr    =    m33_h!h_q!quantile   
        endif
        if (!moodel=34) then
          series  x_vqr    =    m34_h!h_q!quantile   
        endif
        if (!moodel=35) then
          series  x_vqr    =    m35_h!h_q!quantile   
        endif
        if (!moodel=36) then
          series  x_vqr    =    m36_h!h_q!quantile   
        endif

           if !quantile=75 then 
                 series quant_sup_50 = x_vqr 
           endif
           if !quantile=25 then 
                 series quant_inf_50 = x_vqr 
           endif

           if !quantile=85 then 
                 series quant_sup_70 = x_vqr 
           endif
           if !quantile=15 then 
                 series quant_inf_70 = x_vqr 
           endif

           if !quantile=95 then 
                 series quant_sup_90 = x_vqr 
           endif
           if !quantile=5 then 
                 series quant_inf_90 = x_vqr 
           endif

next ' quantil

for !i = 1 to T_VaR  

   smpl 1985q1+!i-1  1985q1+!i-1

   stom(pi_aux_h!h,m1)
   stom(quant_sup_50,m2)
   stom(quant_inf_50,m3)
   I(!i,1) = 0
   if (m1>m3 and m1<m2) then 
       I(!i,1) = 1
   endif
next  'i

smpl 1985q1  1985q1+T_VaR-1    ' +!h-1

series Indicator_coverage50
mtos(I,Indicator_coverage50)

for !i = 1 to T_VaR  
   smpl 1985q1+!i-1  1985q1+!i-1
   stom(pi_aux_h!h,m1)
   stom(quant_sup_70,m2)
   stom(quant_inf_70,m3)
   I(!i,1) = 0
   if (m1>m3 and m1<m2) then 
       I(!i,1) = 1
   endif
next  'i

smpl 1985q1  1985q1+T_VaR-1     ' +!h-1

series Indicator_coverage70
mtos(I,Indicator_coverage70)

for !i = 1 to T_VaR  
   smpl 1985q1+!i-1  1985q1+!i-1
   stom(pi_aux_h!h,m1)
   stom(quant_sup_90,m2)
   stom(quant_inf_90,m3)
   I(!i,1) = 0
   if (m1>m3 and m1<m2) then 
       I(!i,1) = 1
   endif
next  'i

smpl 1985q1  1985q1+T_VaR-1     ' +!h-1

series Indicator_coverage90
mtos(I,Indicator_coverage90)

equation Coverage_rate50.ls  (Indicator_coverage50 - 0.5)    c
equation Coverage_rate70.ls  (Indicator_coverage70 - 0.7)    c
equation Coverage_rate90.ls  (Indicator_coverage90 - 0.9)    c

if (!h=1) and (!moodel=1) then
    freeze(Tab_CR)  
endif

if (!h=1) then
  delete Tab_CR
  freeze(Tab_CR)   Coverage_rate50.ls  (Indicator_coverage50 - 0.5)    c
  z_cover_50_pvalue(!h,!moodel) = Tab_CR(9,5)

  delete Tab_CR
  freeze(Tab_CR)   Coverage_rate70.ls  (Indicator_coverage70 - 0.7)    c
  z_cover_70_pvalue(!h,!moodel) = Tab_CR(9,5)

  delete Tab_CR
  freeze(Tab_CR)   Coverage_rate90.ls  (Indicator_coverage90 - 0.9)    c
  z_cover_90_pvalue(!h,!moodel) = Tab_CR(9,5)
endif

if (!h>1) then
  scalar bw = 1.5 * !h
  delete Tab_CR
  freeze(Tab_CR)   Coverage_rate50.ls(cov=hac , covbw=bw)  (Indicator_coverage50 - 0.5)    c
  z_cover_50_pvalue(!h,!moodel) = Tab_CR(11,5)

  delete Tab_CR
  freeze(Tab_CR)   Coverage_rate70.ls(cov=hac , covbw=bw)  (Indicator_coverage70 - 0.7)    c
  z_cover_70_pvalue(!h,!moodel) = Tab_CR(11,5)

  delete Tab_CR
  freeze(Tab_CR)   Coverage_rate90.ls(cov=hac , covbw=bw)  (Indicator_coverage90 - 0.9)    c
  z_cover_90_pvalue(!h,!moodel) = Tab_CR(11,5)
endif

endif  ' h<5, =8, =12
next   ' !h
next   ' !moodel

'--------------------------------------------------------------------------------------------------------------------------------------------

matrix(800,100) zzz_all_results

matplace( zzz_all_results, zh1_percent_hits,              1, 1)    
matplace( zzz_all_results, zh1_p_value_kupiec,      11, 1) 
matplace( zzz_all_results, zh1_p_value_christoff,    21, 1) 
matplace( zzz_all_results, zh1_p_value_vqr,             31, 1) 

matplace( zzz_all_results, zh2_percent_hits,            41, 1)    
matplace( zzz_all_results, zh2_p_value_kupiec,      51, 1) 
matplace( zzz_all_results, zh2_p_value_christoff,    61, 1) 
matplace( zzz_all_results, zh2_p_value_vqr,             71, 1) 

matplace( zzz_all_results, zh3_percent_hits,            81, 1)    
matplace( zzz_all_results, zh3_p_value_kupiec,      91, 1) 
matplace( zzz_all_results, zh3_p_value_christoff,  101, 1) 
matplace( zzz_all_results, zh3_p_value_vqr,           111, 1) 

matplace( zzz_all_results, zh4_percent_hits,          121, 1)    
matplace( zzz_all_results, zh4_p_value_kupiec,    131, 1) 
matplace( zzz_all_results, zh4_p_value_christoff,  141, 1) 
matplace( zzz_all_results, zh4_p_value_vqr,           151, 1) 

matplace( zzz_all_results, zh8_percent_hits,          161, 1)    
matplace( zzz_all_results, zh8_p_value_kupiec,    171, 1) 
matplace( zzz_all_results, zh8_p_value_christoff,  181, 1) 
matplace( zzz_all_results, zh8_p_value_vqr,           191, 1) 

matplace( zzz_all_results, zh12_percent_hits,          201, 1)    
matplace( zzz_all_results, zh12_p_value_kupiec,    211, 1) 
matplace( zzz_all_results, zh12_p_value_christoff,  221, 1) 
matplace( zzz_all_results, zh12_p_value_vqr,           231, 1) 

matplace( zzz_all_results, fe_mean,                                                      241       , 1) 
matplace( zzz_all_results, fe_rmse_relative_spf,        model_max+241+3  , 1) 
matplace( zzz_all_results, z_p_value_berkowitz,     2*model_max+241+3  , 1) 

matplace(zzz_all_results  ,  z_coverage_rate_50  ,  3*model_max+241+4                         ,  1) 
matplace(zzz_all_results  ,  z_coverage_rate_70  ,  3*model_max+241+4+1*h_max+2  ,  1) 
matplace(zzz_all_results  ,  z_coverage_rate_90  ,  3*model_max+241+4+2*h_max+2  ,  1) 
matplace(zzz_all_results  ,  z_cover_50_pvalue    ,  3*model_max+241+4+3*h_max+2  ,  1) 
matplace(zzz_all_results  ,  z_cover_70_pvalue    ,  3*model_max+241+4+4*h_max+2  ,  1) 
matplace(zzz_all_results  ,  z_cover_90_pvalue    ,  3*model_max+241+4+5*h_max+2  ,  1) 

matplace(zzz_all_results  ,  z_score                         ,  3*model_max+241+4+6*h_max+4  ,  1) 
matplace(zzz_all_results  ,  z_ag_stat                      ,  3*model_max+241+4+6*h_max+4+1*model_max+2  ,  1) 
matplace(zzz_all_results  ,  z_ag_pvalue                 ,  3*model_max+241+4+6*h_max+4+2*model_max+2  ,  1) 

'****************************************************************************************************************

endsub
call backtests
