//run vc_random_vs_selected_by_sic2 OR vc_random_vs_selected_by_sic2_layer or first
clear all
mata mata clear
capture log close
set more off
cls

cd .

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

use psd_input.dta

//random vs selected
drop sic2 rc2_cusip rc2_prod

//PSD tests
//comparing sd and coef of variation
tempfile results
tempname hold
postfile `hold' rcode str10 s_cusip normV1 normV2 lpos lneg using `results'


qui levelsof s_cusip, local(levels)

foreach cusipval of local levels { 

preserve
disp "`cusipval'"
keep if s_cusip == "`cusipval'"

egen c_id = group(c_cusip)

drop c_cusip rc_cusip
qui reshape wide c_prod rc_prod, i(data_quarter) j(c_id)

qui corr c_prod*, c
matrix V1 = r(C)
qui corr rc_prod*, c
matrix V2 = r(C)

matrix D = V2-V1

matrix symeigen X lambda = D

mat list lambda
order c_* rc_*

mata: 	np = sum(st_matrix("lambda") :> 0)
mata: 	nn = sum(st_matrix("lambda") :< 0)
mata: 	st_matrix("npos", np)
mata: 	st_matrix("nneg", nn)
 
mata:	nv1 = norm(st_matrix("V1"))
mata:	nv2 = norm(st_matrix("V2"))
mata:	st_matrix("normv1", nv1)
mata:	st_matrix("normv2", nv2)

cap noisily matrix R = cholesky(D)

disp _rc

local pos=el(npos, 1,1)
local neg=el(nneg, 1,1)

local n1=el(normv1, 1,1)
local n2=el(normv2, 1,1)

post `hold' (_rc) ("`cusipval'")  (`n1') (`n2') (`pos') (`neg')

restore
}

postclose `hold'
use `results', clear
save "D_psd.dta", replace
tab rcode
gen pos = normV2 >= normV1
tab pos
tab rcode if pos

count if (lneg ==0)&(lpos>=0) //PSD
count if (lneg>0)&(lpos==0) //NegD
count if (lneg>0)&(lpos>0) //NonDeff




//random vs random
use psd_input.dta, clear

drop sic2 c_cusip c_prod
rename (rc2_cusip rc2_prod) (c_cusip c_prod)

duplicates drop s_cusip c_cusip data_quarter, force


//PSD tests
//comparing sd and coef of variation
tempfile results
tempname hold
postfile `hold' rcode str10 s_cusip normV1 normV2 lpos lneg using `results'


qui levelsof s_cusip, local(levels)

foreach cusipval of local levels { 

preserve
disp "`cusipval'"
keep if s_cusip == "`cusipval'"

egen c_id = group(c_cusip)

drop c_cusip rc_cusip
reshape wide c_prod rc_prod, i(data_quarter) j(c_id)

qui corr c_prod*, c
matrix V1 = r(C)
qui corr rc_prod*, c
matrix V2 = r(C)

matrix D = V2-V1

matrix symeigen X lambda = D

mat list lambda
order c_* rc_*

mata: 	np = sum(st_matrix("lambda") :> 0)
mata: 	nn = sum(st_matrix("lambda") :< 0)
mata: 	st_matrix("npos", np)
mata: 	st_matrix("nneg", nn)
 
mata:	nv1 = norm(st_matrix("V1"))
mata:	nv2 = norm(st_matrix("V2"))
mata:	st_matrix("normv1", nv1)
mata:	st_matrix("normv2", nv2)

cap noisily matrix R = cholesky(D)

disp _rc

local pos=el(npos, 1,1)
local neg=el(nneg, 1,1)

local n1=el(normv1, 1,1)
local n2=el(normv2, 1,1)

post `hold' (_rc) ("`cusipval'")  (`n1') (`n2') (`pos') (`neg')

restore
}

postclose `hold'
use `results', clear
save "D_psdR.dta", replace
disp "Random vs Random"

//tab rcode
gen pos = normV2 >= normV1
tab pos
//tab rcode if pos

count if (lneg ==0)&(lpos>=0) //PSD
count if (lneg>0)&(lpos==0) //NegD
count if (lneg>0)&(lpos>0) //NonDeff

use "D_psd.dta", clear
disp "Random vs Actual"

//tab rcode
gen pos = normV2 >= normV1
tab pos
//tab rcode if pos

count if (lneg ==0)&(lpos>=0) //PSD
count if (lneg>0)&(lpos==0) //NegD
count if (lneg>0)&(lpos>0) //NonDeff

log close



