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


*EXTRACT AND CLEAN MONETARY POLICY VARIABLES
*monetary policies
local n=1
foreach country in "United States" France Germany Italy Japan Sweden Switzerland "United Kingdom"{
	display "`country'"
	
	if "`country'"=="United States" | "`country'"=="United Kingdom" local rangers="H"
	if "`country'"=="France" | "`country'"=="Germany" | "`country'"=="Italy" | "`country'"=="Japan" | "`country'"=="Sweden" | "`country'"=="Switzerland" local rangers="I"
	
	import excel "rawdata/USMPF 2020 NMP Data only.xlsx", clear sheet("`country'") firstrow cellrange("A1:`rangers'373")
	gen ym= mofd(A + td(01jan1960))
	format ym %tm
	drop A
	tempfile USMP`n'
	save "`USMP`n''"
	local n= `n' + 1
}

import excel "rawdata/USMPF 2020 NMP Data only.xlsx", clear sheet("United States") firstrow cellrange("A1:H373")
gen ym= mofd(A + td(01jan1960))
format ym %tm
drop A
tempfile USMP
save "`USMP'"


*EXTRACT AND CLEAN THE FINANCIAL AND ACTIVITY VARIABLES
* build the activity and inflation data from the raw excel file
foreach var in US France Germany Italy Japan Sweden Switzerland "United Kingdom"{
	display "`var'"
	
	if "`var'"=="France" | "`var'"=="Italy" | "`var'"=="Sweden" | "`var'"=="Switzerland" {
		local range="R"
	}
	else{
		local range="Q"
	}
			
	import excel "rawdata/USMPF 2020 Financial Activity and Inflation Data.xlsx", clear sheet("`var' NORMALIZED") cellrange(A6:`range'389)
	rename A date
	drop if date==""

	if "`var'"=="United Kingdom" local var = "uk"
	if "`var'"=="Switzerland" local var = "che"
	if "`var'"=="Germany" local var = "de"
	if "`var'"=="France" local var = "fra"
	if "`var'"=="Italy" local var = "it"
	if "`var'"=="Japan" local var = "jpn"
	if "`var'"=="Sweden" local var = "swe"
	if "`var'"=="US" local var = "us"

	gen ym=mofd(date(date+"01", "YMD"))
	format ym %tm
	drop date

	if "`var'"=="us"|"`var'"=="de"|"`var'"=="jpn"|"`var'"=="uk" {
		rename B `var'_equity_index
		rename C `var'_interbank_spread
		rename D `var'_yield_curve
		rename E `var'_credit_spread
		rename F `var'_loans_credit
		rename G `var'_fx_rate
		rename H `var'_equity_vol
		rename J `var'_inflation
		rename L `var'_activity_coin
		rename M `var'_activity_retail
		rename N `var'_activity_ip
		rename P `var'_policy_rate
		rename Q `var'_mbase_gdp
	}
	if "`var'"=="fra" | "`var'"=="it" | "`var'"=="swe" | "`var'"=="che" {
		rename B `var'_equity_index
		rename C `var'_interbank_spread
		rename D `var'_yield_curve
		rename E `var'_sovereign_spread
		rename F `var'_credit_spread
		rename G `var'_loans_credit
		rename H `var'_fx_rate
		rename I `var'_equity_vol
		rename K `var'_inflation
		rename M `var'_activity_coin
		rename N `var'_activity_retail
		rename O `var'_activity_ip
		rename Q `var'_policy_rate
		rename R `var'_mbase_gdp
	}

	keep ym `var'_*

	tempfile import`var'
	save "`import`var''"
}

use `importus', clear
foreach var in fra de it jpn swe che uk{
	merge 1:1 ym using `import`var'', nogen 
}

tsset ym


drop in 1/3

*for these countries, we use ip activity data
replace che_activity_coin = che_activity_ip
replace it_activity_coin = it_activity_ip
replace swe_activity_coin = swe_activity_ip



********************************
*FINANCIAL CONDITIONS INDICES
********************************

*principal components of inflation and gdp
pca *_inflation if _n>6 & _n<=373, components(1) cov
predict pc1_inflation

pca us_activity_coin uk_activity_coin jpn_activity_coin de_activity_coin fra_activity_coin if _n>6 & _n<=373, components(1) cov
predict pc1_gdp

*equation 1: regress each variable on the pc1 of these global measures
foreach country in us fra de it jpn swe che uk {
	display "`country'"
	
	local varlist = "`country'_equity_index `country'_interbank_spread `country'_yield_curve `country'_credit_spread `country'_loans_credit `country'_fx_rate `country'_equity_vol"
	
	if "`country'"=="it" | "`country'"=="swe" | "`country'"=="che" | "`country'"=="fra" {
	    local varlist = "`country'_equity_index `country'_interbank_spread `country'_yield_curve `country'_sovereign_spread `country'_loans_credit `country'_fx_rate `country'_equity_vol"
	}

	foreach var in `varlist' {
		display "`var'"
		regress `var' L(1/6).pc1_inflation L(1/6).pc1_gdp if _n>6 & _n<=373, robust
		predict `var'_resi_g if _n>6 & _n<=373, residuals
	}
}

pca *_resi_g, components(1) cov
predict pc1_gfci //this is the gloabal pc1

drop *_resi_g
label var pc1_gfci "global pc1"

matrix globalloadings = e(L) //these are the global loadings


*equation 2: regress each variable on domestic inflation and activity
foreach country in us fra de it jpn swe che uk {
	local varlist = "`country'_inflation `country'_activity_coin"
	foreach var in `varlist'{
	    winsor `var', gen(`var'_w) p(0.025)
	}
}

matrix var_explained = J(8,2,0)
matrix rownames var_explained = us fra de it jpn swe che uk
matrix colnames var_explained = pc1 pc2
local n = 1

foreach country in us fra de it jpn swe che uk{
	display "`country'"
	
	if "`country'"=="us" | "`country'"=="jpn" | "`country'"=="uk" | "`country'"=="swe" {
	    replace `country'_inflation_w=. if _n<4
	}
	if "`country'"=="che"{
	    replace `country'_inflation_w=. if _n<4
		replace `country'_inflation_w=. if _n>_N-15
	}
	
	local varlist = "`country'_equity_index `country'_interbank_spread `country'_yield_curve `country'_credit_spread `country'_loans_credit `country'_fx_rate `country'_equity_vol"
	
	if "`country'"=="fra" | "`country'"=="it" | "`country'"=="swe" | "`country'"=="che" {
	    local varlist = "`country'_equity_index `country'_interbank_spread `country'_yield_curve `country'_sovereign_spread `country'_credit_spread `country'_loans_credit `country'_fx_rate `country'_equity_vol"
	}
	
	foreach var in `varlist' {
		display "`var'"
		regress `var' L(1/6).`country'_inflation_w L(1/6).`country'_activity_coin_w, robust
		predict `var'_resi_d, residuals
	} 

	pca *_resi_d, components(1) cov
	matrix var_explained[`n',1] = e(rho) 
	pca *_resi_d, components(2) cov
	matrix var_explained[`n',2] = e(rho) 
	local n = `n'+1
	
	predict pc1_dfci_`country' pc2_dfci_`country'
	
	label var pc1_dfci_`country' "`country' domestic pc1"
	label var pc2_dfci_`country' "`country' domestic pc2"
	
	matrix domloadings`country' = e(L) 
	
	drop *_resi_d
}


*save results
preserve
keep ym pc1_gfci
export delimited "results/step1/1_gfci.csv", replace
restore

preserve
keep ym pc1_dfci_us  pc1_dfci_fra  pc1_dfci_de  pc1_dfci_it  pc1_dfci_jpn  pc1_dfci_swe  pc1_dfci_che  pc1_dfci_uk  pc2_dfci_us  pc2_dfci_fra  pc2_dfci_de  pc2_dfci_it  pc2_dfci_jpn  pc2_dfci_swe  pc2_dfci_che  pc2_dfci_uk
export delimited "results/step1/1_dfci.csv", replace
restore

*loadings on dfci
matrix domloadings = domloadingsus\domloadingsfra\domloadingsde\domloadingsit\domloadingsjpn\domloadingsswe\domloadingsche\domloadingsuk
matrix colnames domloadings = pc1 pc2
preserve
drop *
svmat2 domloadings, rnames(loadings) names(col)
export delimited "results/step1/1_dfci_loadings.csv",replace
restore

*loadings on gfci
matrix colnames globalloadings = pc1
preserve
drop *
svmat2 globalloadings, rnames(loadings) names(col)
export delimited "results/step1/1_gfci_loadings.csv",replace
restore

*explained variance dfci
preserve
drop *
svmat2 var_explained, rnames(country) names(col)
export delimited "results/step1/1_dfci_explained_var.csv",replace
restore



keep pc1_gfci pc1_dfci_us  pc1_dfci_fra  pc1_dfci_de  pc1_dfci_it  pc1_dfci_jpn  pc1_dfci_swe  pc1_dfci_che  pc1_dfci_uk  pc2_dfci_us  pc2_dfci_fra  pc2_dfci_de  pc2_dfci_it  pc2_dfci_jpn  pc2_dfci_swe  pc2_dfci_che  pc2_dfci_uk  ym us_activity_coin us_inflation fra_activity_coin fra_inflation it_activity_coin it_inflation de_activity_coin de_inflation jpn_activity_coin jpn_inflation swe_activity_coin swe_inflation che_activity_coin che_inflation uk_activity_coin uk_inflation us_policy_rate fra_policy_rate de_policy_rate it_policy_rate swe_policy_rate jpn_policy_rate che_policy_rate uk_policy_rate




*add the monetary policy data here to have a reference data-file
forvalue n=1/8{
	merge 1:1 ym using `USMP`n'', keep(1 3) nogen
}

rename *, lower

rename *_fr *_fra
rename *_de *_deu
rename de_* deu_*

*flip signs on domestic pc2 for france sweden switzerland
foreach country in swe che fra {
	replace pc2_dfci_`country'=-pc2_dfci_`country'
}

save "results/cleandata/data_with_conditions_and_nmp.dta",replace



