subroutine  backtests

'------------------------------------------------------------------------------------------------------------------------------------------------
' *** Backtests Kupiec (1995) Unconditional Coverage and Christoffersen (1998) Conditional Coverage ***

scalar T_VaR = 109   ' 1985q1 to 2012q1

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=2 to 8

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*10

 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

' *** Kupiec (1995) ***
scalar T0 = T_VaR-T1
scalar P1 = T1/T_VaR
scalar P0 = 1-P1
if (T1=0) then pvalue_uc = 0   'zero hits
endif
if (T1>0) then
   LR_uc = -2*log(     ((1-!quantile/100)^T0)  *   ((!quantile/100)^T1)     /      (((1-T1/T_VaR)^T0)*((T1/T_VaR)^T1))      )
   pvalue_uc = 1-@cchisq(LR_uc,1) 
endif
zh!h_p_value_Kupiec(!quantil,!moodel) = pvalue_uc
zh!h_percent_hits(!quantil,!moodel) = P1

' ***  Christoffersen (1998) Independence
scalar T11 = 0 
scalar T00 = 0 
for !i = 1 to T_VaR-1  
   if (I(!i,1)=1) then
       if (I(!i+1,1)=1) then
          scalar T11 = T11+1
       endif
   endif
   if (I(!i,1)=0) then
       if (I(!i+1,1)=0) then
          scalar T00 = T00+1
       endif
   endif
next   ' i
scalar T10 = T1-T11
scalar T01 = T0-T00
if (T0<>0) then 
   scalar P01 = T01/T0
   scalar P11 = T11/T1
   scalar P00 = 1-P01
   scalar P10 = 1-P11
else
   scalar pvaluei = 0     ' zero hits
   scalar pvaluecc = 0   ' zero hits
endif
if (T1=0) then 
   scalar pvaluei = 0     ' zero hits
   scalar pvaluecc = 0   ' zero hits
endif
if (T1>0) and (T0<>0) then
  if (T11>0)  then 
       scalar L_pi_hat   = ((1-T1/T_VaR)^T0)  *  ((T1/T_VaR)^T1) 
       scalar L_pi_hat1 = ((1-P01)^T00)  *  (P01^T01)  *  ((1-P11)^T10)  *  (P11^T11) 
  endif
  if (T11=0) then
       scalar L_pi_hat1 = ((1-P01)^T00)  *  (P01^T01)  
  endif
  scalar LRi = -2*log(L_pi_hat / L_pi_hat1)
  scalar LRi_orig = LRi
  if (LRi>0) then
    scalar pvaluei =  1  -  @cchisq(LRi,1) 
  endif
  if (LRi=0) then
    scalar pvaluei =  1
  endif
  if (LRi<0) then
    scalar pvaluei =  0
    LRi = -LRi
  endif
  scalar LRcc =   LR_uc   +   LRi     'Christoffersen (1998) Conditional Coverage
  scalar pvaluecc = 1  -  @cchisq(LRcc,2) 
endif
zh!h_stat_indep(!quantil,!moodel) = LRi_orig
zh!h_p_value_Christoff(!quantil,!moodel) = pvaluecc

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


' Looping "moodel" still active.

'---------------------------------------------------
' *** Backtest VQR (JBES, 2011) ***

for !h=h_min to h_max
if (!h<5) or (!h=8) or (!h=12) then
  smpl 1985q1    1985q1+T_VaR-1
  for !quantil=2 to 8
        !quantile = !quantil*10
        !q = !quantile/100

        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

        series aux2_ =  log(1+x_vqr/100)  -  log(1+pi_aux_h!h(-!h)/100)
        group   x_vqr2    c    aux2_
         equation vqr_h.qreg(quant = !q ,  cov=sandwich)    pi_aux_vqr_h!h     x_vqr2
        delete x_vqr2
         freeze(Tab_VQR)   vqr_h.wald c(1)=0, c(2)=1 
'         zh!h_stat_vqr(!quantil,!moodel) = Tab_VQR(7,2)
         zh!h_p_value_vqr(!quantil,!moodel) = Tab_VQR(7,4)
         delete Tab_VQR
next   '!quantil
endif  ' h<5, =8, =12
next   ' !h
next   ' !moodel

endsub
call backtests
