**** 5_PlaceboTest.do - Re-run the results for the change in demand volatility based on a placebo test that randomly selects another date for the
****                    customer change. 

capture log close
log using "5_PlaceboTest_log.txt", text replace
set more off
clear all

**** Start with REVERE data
    use "Revere_quarterly.dta", clear
	keep s_cusip c_cusip datacqtr
	nmissing s_cusip c_cusip
	
    unique s_cusip c_cusip datacqtr
    assert `r(N)' == `r(unique)'

	* Change datacqtr to datetime variable in Stata
    	gen year = floor(datacqtr/10)
        gen qtr = datacqtr-year*10
        replace datacqtr = yq(year, qtr)
        format datacqtr %tq
        drop year qtr

    * Identify changes in customers
        order c_cusip s_cusip datacqtr
        bysort c_cusip s_cusip (datacqtr): gen count = _n
        
        bysort c_cusip s_cusip: egen min = min(count)
        bysort c_cusip s_cusip: egen max = max(count)
        gen firstdate = cond(count == min, 1, 0)
        gen lastdate = cond(count == max, 1, 0)
        
        bysort s_cusip datacqtr: egen Added = sum(firstdate)
        bysort s_cusip datacqtr: egen Lost = sum(lastdate)
        
        drop count c_cusip firstdate lastdate min max
        duplicates drop
        duplicates tag s_cusip datacqtr, gen(dup)
        sum dup
        assert `r(max)' ==  0
        drop dup
        
        bysort s_cusip (datacqtr): gen count = _n
        bysort s_cusip (datacqtr): egen max = max(count)
        bysort s_cusip (datacqtr): egen min = min(count)
    	
	* jump1 is defined as having customer(s) add in current quarter
        gen jump1=Added>0 & Added!=.
    * jump2 is defined as having customer(s) drop in the next quarter
        gen jump2=Lost>0 & Lost!=.     

	drop Added Lost count max min
	
	* Set the "Add" as main event
    	rename jump1 main_jump
    	rename jump2 other_jump
	
	save "alljumps_placebo.dta", replace
	
	keep if main_jump == 1
	drop other_jump

    * Identify the earliest date of COMPUSTAT data coverage (the file used below goes back as early as 1999q4)
        preserve
            use "Data\COMPUSTAT 20180608.dta", clear
            gen year_qtr = datacqtr
            drop datacqtr
            gen datacqtr = quarterly(year_qtr, "YQ")
            * Clean up Cusip
                gen cusip9 = cusip
                replace cusip = substr(cusip9, 1, 6)
                drop cusip9

            bysort cusip (datacqtr): egen minqtr = min(datacqtr)
            format minqtr %tq
            keep cusip datacqtr minqtr cogsq
            rename cusip s_cusip
            rename cogsq sup_cogsq
            drop if datacqtr == .
            duplicates drop s_cusip datacqtr, force
            compress
            save "compustat_placebo.dta", replace
            
            * Create data with just earliest quarter of coverage, dimensioned by CUSIP
                keep s_cusip minqtr 
                duplicates drop
                save "compustat_earliest_qtr.dta", replace

        restore
    
**** Run the placebo test based on the customer change date occurring at some random period prior at least "k" periods prior to the actual date 
**** (but not so early that it conflicts with the exclusion period of any prior customer change event for this supplier)
    forvalues k = 4(4)12 {
	preserve
	
    * Delete overlap, [-k,k] window with Add duplications
    	bysort s_cusip (datacqtr): gen To_last = datacqtr - datacqtr[_n-1]
    	bysort s_cusip (datacqtr): gen To_next = datacqtr[_n+1] - datacqtr
    	
        gen if_overlap = To_last <= `k' | To_next <= `k'
        drop if if_overlap
        drop if_overlap To_next
    	
    * The beginning quarter is 200302, so no jumps should be defined before 200402
        drop if datacqtr < tq(2004q2)
    
    * Merge minimum date for COMPUSTAT coverage
    	merge m:1 s_cusip using "compustat_earliest_qtr.dta", keepusing(minqtr) keep(master matched)
    	drop _merge
    
    * Create a date range that has as a minimum the number of quarter until the prior event for the supplier + k
    * and a maximum of the current event quarter - (k+1)
        gen earliestqtr = datacqtr - To_last + `k'
        gen latestqtr = datacqtr- `k'
        format earliestqtr latestqtr %tq
        gen count = latestqtr- earliestqtr
        replace count = . if count <0
        set seed 20140704
        generate randominteger = floor((count+1)*runiform())
        replace datacqtr = datacqtr - `k' - randominteger
        drop To_last minqtr earliestqtr latestqtr count randominteger

    * Create an index tracking all of the events and a variable to identify original event quarter before expanding the data set for the volatility calculaitons
        egen n_event=group(s_cusip datacqtr)
        gen jumpqtr=datacqtr
    	format jumpqtr %tq

    * expand to [-k-1,k] window
        local d = 2 * `k' + 2
        expand `d'
        sort s_cusip datacqtr
        bys s_cusip datacqtr: gen n = _n
    	replace datacqtr = datacqtr + n -`k'- 2
    
    	drop n
    	replace main_jump = 0 if datacqtr!= jumpqtr
    
    * Calculate log-differenced supplier COGS (pre/post) and t-test
        merge m:1 s_cusip datacqtr using "compustat_placebo.dta"
    	keep if _merge==3
    	drop _merge

        * Since companies with multiple change events have those events shifted back in time randomly, there is a chance that they could overlap by a quarter
        * in the COMPUSTAT data. To addess this, force a duplicates drop.
            sort s_cusip datacqtr
            duplicates drop s_cusip datacqtr, force
            
        * Set time series structure. The following treats every customer event within a company as separate                
        	egen ns=group(s_cusip n_event)
    	   	xtset ns datacqtr
    	
        gen ld_cogsq = ln(sup_cogsq)-ln(l.sup_cogsq)
    	gen pre_t0= datacqtr< jumpqtr
    	gen post_t0= datacqtr> jumpqtr
    	egen sd_cogsq = sd(ld_cogsq) if pre_t0, by(ns)
    	egen sd1_cogs=max(sd_cogsq), by(ns)
    	egen sd_cogsq2 = sd(ld_cogsq) if post_t0, by(ns)
    	egen sd2_cogs=max(sd_cogsq2), by(ns)
    
        sort ns datacqtr
    	duplicates drop ns, force
    	save "mf_add_placebo`k'", replace
     	
        display "modelfree_add"
    	display "Non-overlap Window = [-`k',`k']"
    	sum sd1_cogs sd2_cogs if sd1_cogs != . & sd2_cogs != ., det
    	signtest sd1_cogs = sd2_cogs
    	signrank sd1_cogs = sd2_cogs
    	swilk sd1_cogs sd2_cogs if sd1_cogs != . & sd2_cogs != .
    	
    	ttest sd1_cogs == sd2_cogs
    	restore
}


**** Conduct placebo analysis on drop events
	use "alljumps_placebo.dta", clear
	
	keep if other_jump == 1
	drop main_jump
    rename other_jump main_jump

**** Run the placebo test based on the customer change date occurring at some random period prior at least "k" periods prior to the actual date 
**** (but not so early that it conflicts with the exclusion period of any prior customer change event for this supplier)
    forvalues k = 4(4)12 {
	preserve
	
    * Delete overlap, [-k,k] window with Add duplications
    	bysort s_cusip (datacqtr): gen To_last = datacqtr - datacqtr[_n-1]
    	bysort s_cusip (datacqtr): gen To_next = datacqtr[_n+1] - datacqtr
    	
        gen if_overlap = To_last <= `k' | To_next <= `k'
        drop if if_overlap
        drop if_overlap To_next
    	
    * The beginning quarter is 200302, so no jumps should be defined before 200402
        drop if datacqtr < tq(2004q2)
    
    * Merge minimum date for COMPUSTAT coverage
    	merge m:1 s_cusip using "compustat_earliest_qtr.dta", keepusing(minqtr) keep(master matched)
    	drop _merge
    
    * Create a date range that has as a minimum the number of quarter until the prior event for the supplier + k
    * and a maximum of the current event quarter - (k+1)
        gen earliestqtr = datacqtr - To_last + `k'
        gen latestqtr = datacqtr- `k'
        format earliestqtr latestqtr %tq
        gen count = latestqtr- earliestqtr
        replace count = . if count <0
        set seed 20140704
        generate randominteger = floor((count+1)*runiform())
        replace datacqtr = datacqtr - `k' - randominteger
        drop To_last minqtr earliestqtr latestqtr count randominteger

    * Create an index tracking all of the events and a variable to identify original event quarter before expanding the data set for the volatility calculaitons
        egen n_event=group(s_cusip datacqtr)
        gen jumpqtr=datacqtr
    	format jumpqtr %tq

    * expand to [-k-1,k] window
        local d = 2 * `k' + 2
        expand `d'
        sort s_cusip datacqtr
        bys s_cusip datacqtr: gen n = _n
    	replace datacqtr = datacqtr + n -`k'- 2
    
    	drop n
    	replace main_jump = 0 if datacqtr!= jumpqtr
    
    * Calculate log-differenced supplier COGS (pre/post) and t-test
    	merge m:1 s_cusip datacqtr using "compustat_placebo.dta"
    	keep if _merge==3
    	drop _merge

        * Since companies with multiple change events have those events shifted back in time randomly, there is a chance that they could overlap by a quarter
        * in the COMPUSTAT data. To addess this, force a duplicates drop.
            sort s_cusip datacqtr
            duplicates drop s_cusip datacqtr, force
            
        * Set time series structure. The following treats every customer event within a company as separate                
        	egen ns=group(s_cusip n_event)
    	   	xtset ns datacqtr
    	
        gen ld_cogsq = ln(sup_cogsq)-ln(l.sup_cogsq)
    	gen pre_t0= datacqtr< jumpqtr
    	gen post_t0= datacqtr> jumpqtr
    	egen sd_cogsq = sd(ld_cogsq) if pre_t0, by(ns)
    	egen sd1_cogs=max(sd_cogsq), by(ns)
    	egen sd_cogsq2 = sd(ld_cogsq) if post_t0, by(ns)
    	egen sd2_cogs=max(sd_cogsq2), by(ns)
    
        sort ns datacqtr
    	duplicates drop ns, force
    	save "mf_drop_placebo`k'", replace
     	
        display "modelfree_drop"
    	display "Non-overlap Window = [-`k',`k']"
    	sum sd1_cogs sd2_cogs if sd1_cogs != . & sd2_cogs != ., det
    	signtest sd1_cogs = sd2_cogs
    	signrank sd1_cogs = sd2_cogs
    	swilk sd1_cogs sd2_cogs if sd1_cogs != . & sd2_cogs != .
    	
    	ttest sd1_cogs == sd2_cogs
    	restore
}

**** Conduct placebo analysis on SWAP events
	use "alljumps_placebo.dta", clear
	
	keep if main_jump == 1
	drop other_jump

**** Run the placebo test based on the customer change date occurring at some random period prior at least "k" periods prior to the actual date 
**** (but not so early that it conflicts with the exclusion period of any prior customer change event for this supplier)
    * Set range of time surrounding the ADD that a DROP must occur (in this case (-4,4) 
        local p = -4
        local q = 4

    forvalues k = 4(4)12 {
	preserve
	
    * Delete overlap, [-k,k] window with Add duplications
    	bysort s_cusip (datacqtr): gen To_last = datacqtr - datacqtr[_n-1]
    	bysort s_cusip (datacqtr): gen To_next = datacqtr[_n+1] - datacqtr
    	
        gen if_overlap = To_last <= `k' | To_next <= `k'
        drop if if_overlap
        drop if_overlap To_next
    	
    * The beginning quarter is 200302, so no jumps should be defined before 200402
        drop if datacqtr < tq(2004q2)
    
    * Merge minimum date for COMPUSTAT coverage
    	merge m:1 s_cusip using "compustat_earliest_qtr.dta", keepusing(minqtr) keep(master matched)
    	drop _merge
    
    * Create a date range that has as a minimum the number of quarter until the prior event for the supplier + k
    * and a maximum of the current event quarter - (k+1)
        gen earliestqtr = datacqtr - To_last + `k'
        gen latestqtr = datacqtr- `k'
        format earliestqtr latestqtr %tq
        gen count = latestqtr- earliestqtr
        replace count = . if count <0
        set seed 20140704
        generate randominteger = floor((count+1)*runiform())
        replace datacqtr = datacqtr - `k' - randominteger
        drop To_last minqtr earliestqtr latestqtr count randominteger

    * Create an index tracking all of the events and a variable to identify original event quarter before expanding the data set for the volatility calculaitons
        egen n_event=group(s_cusip datacqtr)
        gen jumpqtr=datacqtr
    	format jumpqtr %tq

    * expand to [-k-1,k] window
        local d = 2 * `k' + 2
        expand `d'
        sort s_cusip datacqtr
        bys s_cusip datacqtr: gen n = _n
    	replace datacqtr = datacqtr + n -`k'- 2
    
    	drop n
    	replace main_jump = 0 if datacqtr!= jumpqtr

    * Swap analysis: only keep add-jumps when there is "DROP" in window [p,q]
    
    	merge m:1 s_cusip datacqtr using "alljumps_placebo.dta", keepusing(other_jump)
    	drop if _merge==2	
    	drop _merge
    	replace other_jump = 0 if other_jump ==.	
    
    	gen otherqtr = datacqtr if other_jump == 1
    	format otherqtr %tq
    	gen gap = otherqtr - jumpqtr
    	keep if gap !=.
    	keep if gap >= `p' & gap <= `q'
    	drop datacqtr 
    	
        sort n_event
    	duplicates drop n_event, force
    	rename jumpqtr datacqtr
    
        expand `d'
    	gen jumpqtr = datacqtr
    	bys s_cusip datacqtr: gen n=_n
    	replace datacqtr = datacqtr + n -`k'- 2

    * Calculate log-differenced supplier COGS (pre/post) and t-test
    	merge m:1 s_cusip datacqtr using "compustat_placebo.dta"
    	keep if _merge==3
    	drop _merge

        * Since companies with multiple change events have those events shifted back in time randomly, there is a chance that they could overlap by a quarter
        * in the COMPUSTAT data. To addess this, force a duplicates drop.
            sort s_cusip datacqtr
            duplicates drop s_cusip datacqtr, force
            
        * Set time series structure. The following treats every customer event within a company as separate                
        	egen ns=group(s_cusip n_event)
    	   	xtset ns datacqtr
    	
        gen ld_cogsq = ln(sup_cogsq)-ln(l.sup_cogsq)
    	gen pre_t0= datacqtr< jumpqtr
    	gen post_t0= datacqtr> jumpqtr
    	egen sd_cogsq = sd(ld_cogsq) if pre_t0, by(ns)
    	egen sd1_cogs=max(sd_cogsq), by(ns)
    	egen sd_cogsq2 = sd(ld_cogsq) if post_t0, by(ns)
    	egen sd2_cogs=max(sd_cogsq2), by(ns)
    
        sort ns datacqtr
    	duplicates drop ns, force
    	save "mf_swap_placebo`k'", replace
     	
        display "modelfree_SWAP"
    	display "Non-overlap Window = [-`k',`k']"
    	sum sd1_cogs sd2_cogs if sd1_cogs != . & sd2_cogs != ., det
    	signtest sd1_cogs = sd2_cogs
    	signrank sd1_cogs = sd2_cogs
    	swilk sd1_cogs sd2_cogs if sd1_cogs != . & sd2_cogs != .
    	
    	ttest sd1_cogs == sd2_cogs
    	restore
}

log close
