Hello, I'm creating an index based on corporate financial, stock, and emissions data. I'm currently using Stata version 14.1.
I've finished processing the data, and I'm trying to calculate company-specific weights by running optimizations monthly. Below is the code I wrote for this process.
I'm running mata within a loop, and I'm getting a conformability error (r3200). I thought the loop was the problem, but I keep getting the same error when I run the code for specific months and years.
When I specify w as a colvector (i.e., void obj(todo, real colvector w, f, g, H), I get the following error:
obj(): 3203 <tmp>[1,77] found where colvector required
I can't figure out where the problem is. Please help.
I've finished processing the data, and I'm trying to calculate company-specific weights by running optimizations monthly. Below is the code I wrote for this process.
I'm running mata within a loop, and I'm getting a conformability error (r3200). I thought the loop was the problem, but I keep getting the same error when I run the code for specific months and years.
When I specify w as a colvector (i.e., void obj(todo, real colvector w, f, g, H), I get the following error:
obj(): 3203 <tmp>[1,77] found where colvector required
I can't figure out where the problem is. Please help.
Code:
capture erase "results_intermediate.dta"
capture erase "barra_pab_final_prevyear_penalty.dta"
save "results_intermediate.dta", replace emptyok
levelsof mdate, local(months)
local WACI_prev = .
local end_mata end
foreach t of local months {
di as txt "===== 최적화 시작: `t' ====="
preserve
keep if mdate == `t' & exclusion == 0
keep company_id mdate intensity kospi_intensity ret_kospi ret_company w_b manufacturing style*
mata:
WB = st_data(., "w_b")
R = st_data(., "ret_company")
CI = st_data(., "intensity")
CIK = st_data(., "kospi_intensity")
MAN = st_data(., "manufacturing")
E = st_data(., ("style1","style2","style3","style4","style5","style6","style7","style8","style9","style10","style11"))
N = rows(WB)
Rb_vec = st_data(., "ret_kospi")
WACI_prev = st_numscalar("WACI_prev")
Rb=Rb_vec[1]
void obj(todo, real colvector w, f, g, H)
{
f = (R'*w - Rb)^2
pen = 0
// (1)
man_bench = MAN' * WB
man_weight = MAN' * w
if (man_weight < man_bench) pen = pen + 1000 * (man_bench - man_weight)
// (2)
waci_bench = WB' * CIK
carbon_weight = CI' * w
if (carbon_weight > 0.5 * waci_bench) pen = pen + 1000 * (carbon_weight - 0.5 * waci_bench)
// (3)
if (WACI_prev != .) {
if (carbon_weight > 0.93 * WACI_prev) pen = pen + 1000*(carbon_weight - 0.93*WACI_prev)
}
f = f + pen
if (todo >= 1) g = 2*(R'*w - Rb)*R
if (todo >= 2) H = 2*(R*R')
}
M = optimize_init()
optimize_init_evaluator(M, &obj())
optimize_init_which(M, "min")
optimize_init_params(M, WB') // 행벡터 입력
optimize_init_conv_maxiter(M, 1000)
Aeq = J(1, N, 1)
beq = (1)
Ftarget = E' * WB //(N x 11)' * (N x 1) = (11 x 1)
Aeq = Aeq \ E' //(12 x 77) (\는 행결합, 즉 세로 머지)
beq = beq \ Ftarget //(12 x 1)
Cc = Aeq, beq //(12 x 78)
optimize_init_constraints(M, Cc)
w_opt = optimize(M)
st_store(., "w_opt", w_opt)
WACI_now = (CI'*w_opt)
st_numscalar("WACI_now", WACI_now)
`end_mata'
scalar WACI_prev = r(WACI_now)
gen mdate = `t'
append using "results_intermediate.dta"
save "results_intermediate.dta", replace
restore
}
use "results_intermediate.dta", clear
order mdate company_id w_opt
save "barra_pab_final_prevyear_penalty.dta", replace
export delimited using "barra_pab_final_prevyear_penalty.csv", replace

Comment