clear
set more off

use "$temp/predictions_final", clear

local end_time = 26

distinct id_dep if prediction_26_log3 != .

* Define sample period and dependent variable
********************************************************************************

gen diff_`end_time'_log3_pc = (nbre_hospit_corona_cum - prediction_`end_time'_log3)/population *100000

local date_start = mdy(03,01,2020)
local date_end = mdy(03,`end_time',2020) + 7
	
keep if inrange(date,`date_start',`date_end')

* Prediction errors across time
********************************************************************************
	
sort id_dep date

local calib_pos = mdy(03,`end_time',2020) + 0.5
	
local x1 = mdy(03,01,2020)
local x2 = mdy(03,08,2020)
local elec = mdy(03,15,2020)
local x3 = mdy(03,22,2020)
local x4 = mdy(03,29,2020)

local y_calib = 75
local y_elec = 75

twoway scatter diff_`end_time'_log3_pc date, connect(L) msymbol(none) lcolor(gs12) || /// 
	   , ///
	   		yline(0, lcolor(black)) xline(`calib_pos', lcolor(black) lpattern(dash)) xline(`elec', lcolor(black) lpattern(dash)) /// 
		xlabel(`x1' `x2' `elec' `x3' `x4', format(%td_Month_dd) labsize(medsmall)) ///
		text(`y_calib' `calib_pos' "End of calibration" "period (March `end_time')", placement(w) justification(right) margin(vsmall) size(small)) ///
		text(`y_elec' `elec' "First round of" "municipal elections", placement(w) justification(right) margin(vsmall) size(small)) ///
		ylabel(-100(50)100, angle(0) nogrid) ///
		xtitle("") ///
		bgcolor(white) graphregion(color(white)) ///
		legend(off) ///		
		ytitle("Prediction error in" "hospitalizations per 100,000 inhabitants" " ")
		
graph export "$graphs/prediction_errors_time.eps", replace
graph export "$graphs/prediction_errors_time.png", replace

		
* Distribution of predictions errors
********************************************************************************

* For graphical representation
keep if inrange(diff_`end_time',-20,20)
keep if date >= mdy(03,15,2020)

twoway kdensity diff_`end_time'_log3_pc, lwidth(thick) lpattern(solid) lcolor(gs8) || ///
		kdensity diff_`end_time'_log3_pc if date <= mdy(03,`end_time',2020), lwidth(thick) lpattern(dash) lcolor(gs12) || ///
		kdensity diff_`end_time'_log3_pc if date > mdy(03,`end_time',2020), lwidth(thick) lpattern(longdash) lcolor(black) || ///
		, ///
		ytitle("Density" " ") ///
		xtitle(" " "Prediction error in hospitalizations per 100,000 inhabitants") ///
		 legend(position(2) ring(0) bmargin(30 0 0 0) col(1) order(1 "All dates" 2 "Calibration period" 3 "Post-calibration period")  region(lstyle(none) lcolor(white))) ///
		 xlabel(-20(10)20, labsize(medsmall)) ///
		 ylabel(0 0.5 "0.5" 1, angle(0) nogrid) ///
		 bgcolor(white) graphregion(color(white)) plotregion(margin(vsmall)) ///

graph export "$graphs/prediction_errors_distribution.eps", replace
graph export "$graphs/prediction_errors_distribution.png", replace
		