clear all

*** This is part of the replication files for 
* Cecchetti, Feroli, Kashyap, Mann and Schoenholtz (September 2020)
* 
*
* Julien Weber (julien.weber@chicagobooth.edu) 2020-09-14
***

*clean data
use "results/cleandata/data_with_conditions_and_nmp.dta",clear


matrix avgdfci = J(5,8,.)
matrix avgdfci_pct = J(5,8,.)
matrix avgdfci_pctile = J(3,8,.)

matrix rownames avgdfci = fgd fgs qe me nrd
matrix colnames avgdfci = us fra deu it jpn swe che uk
matrix rownames avgdfci_pct = fgd fgs qe me nrd
matrix colnames avgdfci_pct = us fra deu it jpn swe che uk
matrix rownames avgdfci_pctile = 90to70 70to50 70
matrix colnames avgdfci_pctile = us fra deu it jpn swe che uk

local mm = 1

*calculate average dfci1 and dfci percentiles
foreach country in us fra deu it jpn swe che uk {
    local nn = 1
	
	foreach var in fgd fgs qe me nrd {
	    
		if ("`country'"=="us" & "`var'"=="nrd")|("`country'"=="swe" & "`var'"=="fgs")|("`country'"=="che" & "`var'"=="fgd")|("`country'"=="che" & "`var'"=="fgs")|("`country'"=="uk" & "`var'"=="nrd") {
		    matrix avgdfci[`nn',`mm'] = .
		}
		else{
		    gen `var'_`country'p1 = `var'_`country'[_n-1]
			gen `var'_`country'change = `var'_`country' - `var'_`country'p1

			*average dfci1 when monetary policy switches on
			sum pc1_dfci_`country' if `var'_`country'change==1
			matrix avgdfci[`nn',`mm'] = `r(mean)'
						
			*average dfci1 percentile when monetary policy switches on 
			egen pctile_`country'_`var' = mean(( pc1_dfci_`country' < `r(mean)' ) / ( pc1_dfci_`country' < .)) if ym>=ym(1995,1)
			sum pctile_`country'_`var'  if ym>=ym(1995,1)
			matrix avgdfci_pct[`nn',`mm'] = `r(mean)'
		}
		local nn = `nn' +1
	}
	
	*percentiles of each dfci1
	_pctile pc1_dfci_`country' if ym>=ym(1995,1), p(50, 70, 90)
	matrix avgdfci_pctile[1,`mm'] = `r(r2)' - `r(r3)'
	matrix avgdfci_pctile[2,`mm'] = `r(r1)' - `r(r2)'
	matrix avgdfci_pctile[3,`mm'] = `r(r2)'
	
	local mm = `mm' +1
}


esttab matrix(avgdfci) using "results/step3/3_dfci_avg.csv", cells(us fra deu it jpn swe che uk, layout(@ @ @ @ @ @ @ @)) replace
esttab matrix(avgdfci_pct, fmt(%9.3f)) using "results/step3/3_dfci_avg_pct.csv", cells(us fra deu it jpn swe che uk, layout(@ @ @ @ @ @ @ @)) replace
esttab matrix(avgdfci_pctile) using "results/step3/3_dfci_avg_pctile.csv", cells(us fra deu it jpn swe che uk, layout(@ @ @ @ @ @ @ @)) replace

