********************************************************************************
* PROGRAM "opl_tb"
********************************************************************************
*! opl_tb, v1, GCerulli, 04June2022
program opl_tb , eclass
version 16
syntax  ,  ///
xlist(varlist max=2 min=2) cate(varlist max=1 min=1)
marksample touse
markout `touse' `xlist'
********************************************************************************
ereturn local sel_vars "`xlist'"
********************************************************************************
qui{ // start quietly
********************************************************************************
local R1 "0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0"
local M1: word count `R1'
********************************************************************************
local R2 "0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0"
local M2: word count `R2'
********************************************************************************
local _M =`M1'*`M2'
tempname A
mat `A' = J(`_M',3,.)
********************************************************************************
local i=1
foreach k of local R1{
foreach j of local R2{
********************************************************************************
foreach x of local xlist{
cap drop `x'_std
}
********************************************************************************
opl_tb_c , xlist(`xlist') c1(`k') c2(`j') cate(`cate')
mat `A'[`i',1] =`k'  // value of "X1" in the grid
mat `A'[`i',2] =`j'  // value of "X2" in the grid
mat `A'[`i',3] =e(W_constr) // optimal welfare at c=j
local i=`i'+1
}
}
********************************************************************************
preserve
ereturn clear
svmat `A'
tempname max_w
egen `max_w'=max(`A'3)
qui sum `A'1 if `A'3==`max_w'
ereturn scalar best_c1=r(mean)
qui sum `A'2 if `A'3==`max_w'
ereturn scalar best_c2=r(mean)
restore
********************************************************************************
} // end quietly
********************************************************************************
cap drop _units_to_be_treated
********************************************************************************
********************************************************************************
end