'*****************************************************************************
'this program calibrates the state indicator F(s) in bootstrap repetitions
'*****************************************************************************
'special case for DEU because the first year available for output per capita growth is 1992	
IF %IDC="DEU" THEN
'computing moving average of output, normalization and percent of recession in the sample considered (that on which we have obs for output growth)
SMPL 1994 !YEAR
GENR 		ma_y_{%IDC}_0m = (y_pc_s{%IDC}_0m(-2) + y_pc_s{%IDC}_0m(-1))/2
GENR 		mean_{%IDC}_0m = @MEAN(ma_y_{%IDC}_0m)
GENR 		sd_{%IDC}_0m = @STDEV(ma_y_{%IDC}_0m)
SMPL !YEAR+1 @LAST		
mean_{%IDC}_0m = @ELEM(mean_{%IDC}_0m, "2000")		'this is needed to have values for simulation
sd_{%IDC}_0m = @ELEM(sd_{%IDC}_0m, "2000")				'this is needed to have values for simulation
SMPL 1994-2 !YEAR
GENR 		recessions_{%IDC}_0m = (y_pc_s{%IDC}_0m < 0)
SCALAR 	n_rec_{%IDC}_0m = @SUM(recessions_{%IDC}_0m)
SCALAR 	prob_rec_{%IDC}_0m = n_rec_{%IDC}_0m/(!YEAR-1994+3)
SCALAR 	prob_exp_{%IDC}_0m = 1-prob_rec_{%IDC}_0m
!QUANTILE_{%IDC}_0m = prob_exp_{%IDC}_0m
'here starts the calibration for gamma
SMPL 1994 !YEAR
SCALAR gamma_{%IDC}_0m = 0
	FOR !i=1 TO 10000
		gamma_{%IDC}_0m = !i*0.001
		GENR fs_s{%IDC}_0m = (exp(-gamma_{%IDC}_0m*((y_pc_s{%IDC}_0m(-2) + y_pc_s{%IDC}_0m(-1) - 2*mean_{%IDC}_0m)*0.5)/sd_{%IDC}_0m))/(1 + (exp(-gamma_{%IDC}_0m*((y_pc_s{%IDC}_0m(-2) + y_pc_s{%IDC}_0m(-1) - 2*mean_{%IDC}_0m)*0.5)/sd_{%IDC}_0m)))
		IF @QUANTILE(fs_s{%IDC}_0m, !QUANTILE_{%IDC}_0m)<=0.8 THEN
	NEXT
ELSE
EXITLOOP
ENDIF
ELSE
'computing moving average of output, normalization and percent of recession in the sample considered (that on which we have obs for output growth)
SMPL !START !YEAR
GENR 		ma_y_{%IDC}_0m = (y_pc_s{%IDC}_0m(-2) + y_pc_s{%IDC}_0m(-1))/2
GENR 		mean_{%IDC}_0m = @MEAN(ma_y_{%IDC}_0m)
GENR 		sd_{%IDC}_0m = @STDEV(ma_y_{%IDC}_0m)
SMPL !YEAR+1 @LAST
mean_{%IDC}_0m = @ELEM(mean_{%IDC}_0m, "2000")		'this is needed to have values for simulation
sd_{%IDC}_0m = @ELEM(sd_{%IDC}_0m, "2000")				'this is needed to have values for simulation
SMPL !START-2 !YEAR
GENR recessions_{%IDC}_0m = (y_pc_s{%IDC}_0m < 0)
SCALAR n_rec_{%IDC}_0m = @SUM(recessions_{%IDC}_0m)
SCALAR prob_rec_{%IDC}_0m = n_rec_{%IDC}_0m/(!YEAR-!START+3)
SCALAR prob_exp_{%IDC}_0m = 1-prob_rec_{%IDC}_0m
!QUANTILE_{%IDC}_0m = prob_exp_{%IDC}_0m
'here starts the calibration for gamma
SMPL !START !YEAR
SCALAR gamma_{%IDC}_0m = 0
	FOR !i=1 TO 10000
		gamma_{%IDC}_0m = !i*0.001
		GENR fs_s{%IDC}_0m = (exp(-gamma_{%IDC}_0m*((y_pc_s{%IDC}_0m(-2) + y_pc_s{%IDC}_0m(-1) - 2*mean_{%IDC}_0m)*0.5)/sd_{%IDC}_0m))/(1+(exp(-gamma_{%IDC}_0m*((y_pc_s{%IDC}_0m(-2) + y_pc_s{%IDC}_0m(-1) - 2*mean_{%IDC}_0m)*0.5)/sd_{%IDC}_0m)))
		IF @QUANTILE(fs_s{%IDC}_0m, !QUANTILE_{%IDC}_0m)<=0.8 THEN 
	NEXT
ELSE
EXITLOOP
ENDIF
ENDIF


