subroutine  backtests

'------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'*** Combination of models ***   
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

' Models 25 a 27 (1/N weights)
      smpl @all
      series m25_h!h_q!quantile  = (m1_h!h_q!quantile + m2_h!h_q!quantile + m3_h!h_q!quantile + m4_h!h_q!quantile + m5_h!h_q!quantile + m6_h!h_q!quantile + m7_h!h_q!quantile + m8_h!h_q!quantile) / 8  
      series m26_h!h_q!quantile  = (m9_h!h_q!quantile + m10_h!h_q!quantile + m11_h!h_q!quantile + m12_h!h_q!quantile + m13_h!h_q!quantile + m14_h!h_q!quantile + m15_h!h_q!quantile + m16_h!h_q!quantile) / 8  
      series m27_h!h_q!quantile  = (m1_h!h_q!quantile + m2_h!h_q!quantile + m3_h!h_q!quantile + m4_h!h_q!quantile + m5_h!h_q!quantile + m6_h!h_q!quantile + m7_h!h_q!quantile + m8_h!h_q!quantile + m9_h!h_q!quantile + m10_h!h_q!quantile + m11_h!h_q!quantile + m12_h!h_q!quantile + m13_h!h_q!quantile + m14_h!h_q!quantile + m15_h!h_q!quantile + m16_h!h_q!quantile) / 16  

' Model 28 a 30 (MSE weights)
      smpl @all
      series m28_h!h_q!quantile  = msewg1_m1_h!h * m1_h!h_q!quantile + msewg1_m2_h!h * m2_h!h_q!quantile + msewg1_m3_h!h * m3_h!h_q!quantile + msewg1_m4_h!h * m4_h!h_q!quantile + msewg1_m5_h!h * m5_h!h_q!quantile + msewg1_m6_h!h * m6_h!h_q!quantile + msewg1_m7_h!h * m7_h!h_q!quantile + msewg1_m8_h!h * m8_h!h_q!quantile
      series m29_h!h_q!quantile  = msewg2_m9_h!h * m9_h!h_q!quantile + msewg2_m10_h!h * m10_h!h_q!quantile + msewg2_m11_h!h * m11_h!h_q!quantile + msewg2_m12_h!h * m12_h!h_q!quantile + msewg2_m13_h!h * m13_h!h_q!quantile + msewg2_m14_h!h * m14_h!h_q!quantile + msewg2_m15_h!h * m15_h!h_q!quantile + msewg2_m16_h!h * m16_h!h_q!quantile
      series m30_h!h_q!quantile  =  msewg3_m1_h!h * m1_h!h_q!quantile + msewg3_m2_h!h * m2_h!h_q!quantile + msewg3_m3_h!h * m3_h!h_q!quantile + msewg3_m4_h!h * m4_h!h_q!quantile + msewg3_m5_h!h * m5_h!h_q!quantile + msewg3_m6_h!h * m6_h!h_q!quantile + msewg3_m7_h!h * m7_h!h_q!quantile + msewg3_m8_h!h * m8_h!h_q!quantile + msewg3_m9_h!h * m9_h!h_q!quantile + msewg3_m10_h!h * m10_h!h_q!quantile + msewg3_m11_h!h * m11_h!h_q!quantile + msewg3_m12_h!h * m12_h!h_q!quantile + msewg3_m13_h!h * m13_h!h_q!quantile + msewg3_m14_h!h * m14_h!h_q!quantile + msewg3_m15_h!h * m15_h!h_q!quantile + msewg3_m16_h!h * m16_h!h_q!quantile

' Model 31 a 33 (Recursive Log Score weights - RLSW)
      smpl @all
      series m31_h!h_q!quantile  = RLSWG1_m1_h!h * m1_h!h_q!quantile + RLSWG1_m2_h!h * m2_h!h_q!quantile + RLSWG1_m3_h!h * m3_h!h_q!quantile + RLSWG1_m4_h!h * m4_h!h_q!quantile + RLSWG1_m5_h!h * m5_h!h_q!quantile + RLSWG1_m6_h!h * m6_h!h_q!quantile + RLSWG1_m7_h!h * m7_h!h_q!quantile + RLSWG1_m8_h!h * m8_h!h_q!quantile

      series m32_h!h_q!quantile  = RLSWG2_m9_h!h * m9_h!h_q!quantile + RLSWG2_m10_h!h * m10_h!h_q!quantile + RLSWG2_m11_h!h * m11_h!h_q!quantile + RLSWG2_m12_h!h * m12_h!h_q!quantile + RLSWG2_m13_h!h * m13_h!h_q!quantile + RLSWG2_m14_h!h * m14_h!h_q!quantile + RLSWG2_m15_h!h * m15_h!h_q!quantile + RLSWG2_m16_h!h * m16_h!h_q!quantile

      series m33_h!h_q!quantile  = RLSWG3_m1_h!h * m1_h!h_q!quantile + RLSWG3_m2_h!h * m2_h!h_q!quantile + RLSWG3_m3_h!h * m3_h!h_q!quantile + RLSWG3_m4_h!h * m4_h!h_q!quantile + RLSWG3_m5_h!h * m5_h!h_q!quantile + RLSWG3_m6_h!h * m6_h!h_q!quantile + RLSWG3_m7_h!h * m7_h!h_q!quantile + RLSWG3_m8_h!h * m8_h!h_q!quantile + RLSWG3_m9_h!h * m9_h!h_q!quantile + RLSWG3_m10_h!h * m10_h!h_q!quantile + RLSWG3_m11_h!h * m11_h!h_q!quantile + RLSWG3_m12_h!h * m12_h!h_q!quantile + RLSWG3_m13_h!h * m13_h!h_q!quantile + RLSWG3_m14_h!h * m14_h!h_q!quantile + RLSWG3_m15_h!h * m15_h!h_q!quantile + RLSWG3_m16_h!h * m16_h!h_q!quantile



for !i = 1 to 109  ' 1985q1 to 2012q1


' Model 34 (Combination of Elliott and Timmermann (2004, p.64))
      smpl   1985q1-TS    1985q1+!i-1
      equation model34a.ls        pi_aux_h!h    one   m9_h!h_cond_mean  m10_h!h_cond_mean  m11_h!h_cond_mean  m12_h!h_cond_mean  m13_h!h_cond_mean  m14_h!h_cond_mean  m15_h!h_cond_mean   m16_h!h_cond_mean    
      series resid_ols = resid
      series fitted_ols = pi_aux_h!h  -  resid
      equation model34b.qreg(quant =  !quantile/100 ,  cov=sandwich)   resid_ols   one
      smpl   1985q1+!i-1   1985q1+!i-1
      series m34_h!h_q!quantile =  fitted_ols + (resid_ols  -  resid)

      if !quantile=25 then 
           smpl 1985q1+!i-1  1985q1+!i-1
           series ETW25_intercept_h!h = model34a.@coef(1) + model34b.@coef(1) 
           series ETW25_m9_h!h = model34a.@coef(2)
           series ETW25_m10_h!h = model34a.@coef(3)
           series ETW25_m11_h!h = model34a.@coef(4)
           series ETW25_m12_h!h = model34a.@coef(5)
           series ETW25_m13_h!h = model34a.@coef(6)
           series ETW25_m14_h!h = model34a.@coef(7)
           series ETW25_m15_h!h = model34a.@coef(8)
           series ETW25_m16_h!h = model34a.@coef(9)
      endif

      if !quantile=50 then 
           smpl 1985q1+!i-1  1985q1+!i-1
           series ETW50_intercept_h!h = model34a.@coef(1) + model34b.@coef(1) 
           series ETW50_m9_h!h = model34a.@coef(2)
           series ETW50_m10_h!h = model34a.@coef(3)
           series ETW50_m11_h!h = model34a.@coef(4)
           series ETW50_m12_h!h = model34a.@coef(5)
           series ETW50_m13_h!h = model34a.@coef(6)
           series ETW50_m14_h!h = model34a.@coef(7)
           series ETW50_m15_h!h = model34a.@coef(8)
           series ETW50_m16_h!h = model34a.@coef(9)
      endif
    
      if !quantile=75 then 
           smpl 1985q1+!i-1  1985q1+!i-1
           series ETW75_intercept_h!h = model34a.@coef(1) + model34b.@coef(1) 
           series ETW75_m9_h!h = model34a.@coef(2)
           series ETW75_m10_h!h = model34a.@coef(3)
           series ETW75_m11_h!h = model34a.@coef(4)
           series ETW75_m12_h!h = model34a.@coef(5)
           series ETW75_m13_h!h = model34a.@coef(6)
           series ETW75_m14_h!h = model34a.@coef(7)
           series ETW75_m15_h!h = model34a.@coef(8)
           series ETW75_m16_h!h = model34a.@coef(9)
      endif



' Model 35 (Optimal combination via Quantile Regression)
      smpl 1985q1-TS  1985q1+!i-1
      equation model35.qreg(quant =  !quantile/100 ,  cov=sandwich)   pi_aux_h!h    one   m9_h!h_cond_mean  m10_h!h_cond_mean  m11_h!h_cond_mean  m12_h!h_cond_mean  m13_h!h_cond_mean  m14_h!h_cond_mean  m15_h!h_cond_mean   m16_h!h_cond_mean     
      smpl   1985q1+!i-1   1985q1+!i-1
      series m35_h!h_q!quantile =  pi_aux_h!h  -  resid
      if !quantile=25 then 
           smpl 1985q1+!i-1  1985q1+!i-1
           series QRW25_intercept_h!h = model35.@coef(1)
           series QRW25_m9_h!h = model35.@coef(2)
           series QRW25_m10_h!h = model35.@coef(3)
           series QRW25_m11_h!h = model35.@coef(4)
           series QRW25_m12_h!h = model35.@coef(5)
           series QRW25_m13_h!h = model35.@coef(6)
           series QRW25_m14_h!h = model35.@coef(7)
           series QRW25_m15_h!h = model35.@coef(8)
           series QRW25_m16_h!h = model35.@coef(9)
      endif
      if !quantile=50 then 
           smpl 1985q1+!i-1  1985q1+!i-1
           series QRW50_intercept_h!h = model35.@coef(1)
           series QRW50_m9_h!h = model35.@coef(2)
           series QRW50_m10_h!h = model35.@coef(3)
           series QRW50_m11_h!h = model35.@coef(4)
           series QRW50_m12_h!h = model35.@coef(5)
           series QRW50_m13_h!h = model35.@coef(6)
           series QRW50_m14_h!h = model35.@coef(7)
           series QRW50_m15_h!h = model35.@coef(8)
           series QRW50_m16_h!h = model35.@coef(9)
      endif
      if !quantile=75 then 
           smpl 1985q1+!i-1  1985q1+!i-1
           series QRW75_intercept_h!h = model35.@coef(1)
           series QRW75_m9_h!h = model35.@coef(2)
           series QRW75_m10_h!h = model35.@coef(3)
           series QRW75_m11_h!h = model35.@coef(4)
           series QRW75_m12_h!h = model35.@coef(5)
           series QRW75_m13_h!h = model35.@coef(6)
           series QRW75_m14_h!h = model35.@coef(7)
           series QRW75_m15_h!h = model35.@coef(8)
           series QRW75_m16_h!h = model35.@coef(9)
      endif

' Model 36 (Granger combination, based on auxiliary models 17-24) 
      smpl 1985q1-TS  1985q1+!i-1
      equation model36.qreg(quant =  !quantile/100 ,  cov=sandwich)   pi_aux_h!h    one    m17_h!h_q!quantile  m18_h!h_q!quantile  m19_h!h_q!quantile  m20_h!h_q!quantile  m21_h!h_q!quantile   m22_h!h_q!quantile   m23_h!h_q!quantile    m24_h!h_q!quantile  
      smpl   1985q1+!i-1   1985q1+!i-1
      series m36_h!h_q!quantile =  pi_aux_h!h  -  resid

next  'i  = obsm
next  'quantil
endif  ' h<5, =8, =12
next  'h


endsub
call backtests
