//Reproduces Table 7. Set year in line 16 (from 2009 to 2015)
clear all
mata mata clear
capture log close
set more off
cls

set seed 31415
set sortseed 31415



cap log close
log using "vc_random_sic2.log", text replace

local Y = 2015

//set of suppliers active in year Y and present in cogs_deflated
use "..\cogs_deflated.dta",clear
keep if year == `Y'
keep cusip
rename cusip s_cusip
duplicates drop
save suppliers`Y'.dta, replace

//set of actual customers listed in Revere for which data is also available in Compustat
use "..\..\Revere_quarterly.dta", clear 
replace s_cusip = substr(s_cusip,1,6)
replace c_cusip = substr(c_cusip,1,6)
keep if datacqtr == `Y'4
gen data_quarter = yq(`Y', 4)


rename c_cusip cusip
keep cusip data_quarter
duplicates drop
merge 1:1 cusip data_quarter using cogs_prod.dta, keep(match)
keep cusip sic2
gen i = _n
sum i
local NCUST = r(N)
disp `NCUST'
save customers`Y'actual.dta, replace

use "..\..\Revere_quarterly.dta",clear 
replace s_cusip = substr(s_cusip,1,6)
replace c_cusip = substr(c_cusip,1,6)
keep if datacqtr == `Y'4
keep datacqtr s_cusip c_cusip


merge m:1 s_cusip using suppliers`Y'.dta, keep(match) nogen
rename c_cusip cusip
merge m:1 cusip using customers`Y'actual.dta, keep(match) keepusing(sic2) nogen
rename (cusip sic2) (c_cusip sic2)

sort datacqtr s_cusip c_cusip

//joining by sic2 then drawing a random set of customers
joinby sic2 using customers`Y'actual
by s_cusip c_cusip datacqtr, sort : sample 1, count
rename cusip rc_cusip
drop i

//same for the second random set
joinby sic2 using customers`Y'actual
by s_cusip c_cusip datacqtr, sort : sample 1, count
rename cusip rc2_cusip
drop i


sort datacqtr s_cusip c_cusip
rename datacqtr refqtr


//merging production data for past 10 years
expand 40
sort s_cusip c_cusip rc_cusip
gen data_quarter = yq(`Y', 4)
format data_quarter %tq
bys s_cusip c_cusip rc_cusip: replace data_quarter = data_quarter+1-_n

rename c_cusip cusip
merge m:1 cusip data_quarter using cogs_prod, keepusing(prodqd)
drop if _merge ==2
drop _merge
rename (cusip prodqd) (c_cusip c_prod)

rename rc_cusip cusip
merge m:1 cusip data_quarter using cogs_prod, keepusing(prodqd)
drop if _merge ==2
drop _merge
rename (cusip prodqd) (rc_cusip rc_prod)

rename rc2_cusip cusip
merge m:1 cusip data_quarter using cogs_prod, keepusing(prodqd)
drop if _merge ==2
drop _merge
rename (cusip prodqd) (rc2_cusip rc2_prod)


//rescaling rc_prod
bys s_cusip c_cusip: egen m_c_prod = mean(c_prod)
by s_cusip c_cusip: egen m_rc_prod = mean(rc_prod)
by s_cusip c_cusip: egen m_rc2_prod = mean(rc2_prod)
replace rc_prod = rc_prod*m_c_prod/m_rc_prod
replace rc2_prod = rc2_prod*m_c_prod/m_rc2_prod
drop m_*

bys s_cusip c_cusip : egen nonmissing = count(c_prod)
drop if nonmissing < 22
drop nonmissing

bys s_cusip c_cusip : egen nonmissing = count(rc_prod)
drop if nonmissing < 22
drop nonmissing

bys s_cusip c_cusip : egen nonmissing = count(rc2_prod)
drop if nonmissing < 22
drop nonmissing

sort s_cusip c_cusip rc_cusip data_quarter

save psd_input.dta, replace

//comparing sd and coef of variation
bys s_cusip data_quarter: egen t_c_prod = total(c_prod)
bys s_cusip data_quarter: egen t_rc_prod = total(rc_prod)

bys s_cusip data_quarter: replace t_c_prod = . if _n>1
bys s_cusip data_quarter: replace t_rc_prod = . if _n>1



//t-test of DVs
drop if mi(t_c_prod)
egen fid = group(s_cusip)
xtset fid data_quarter
gen ld_t_c_prod = ln(t_c_prod)-ln(l.t_c_prod)
gen ld_t_rc_prod = ln(t_rc_prod)-ln(l.t_rc_prod)

bys s_cusip: egen dv1 = sd(ld_t_c_prod)
bys s_cusip: egen dv2 = sd(ld_t_rc_prod)
bys s_cusip: replace dv1 = . if _n>1
bys s_cusip: replace dv2 = . if _n>1

ttest dv1 == dv2, unpaired unequal




log close


do "vc_random_vs_selected_psd.do"

