Hi,

I'm trying to calculate weights for constituents in an index using Stata's optimize() function. I have some code so far, but I'm having some difficulties getting it to run.

The equation I am trying to minimize is W=min

X is a 134 x 4 matrix with rows for each constituent and columns for a geographic region (E, M, S, W). The data in X is the percent of each constituent's assets in each geographic region. Therefore, each row in matrix X adds to 1.

X

Omega

I also have variables in my data for the upper and lower bounds for each constituent.

W is what I am trying to minimize.

Ideally, I would like to have the results be in matrix form/some form that I could export back to Stata (maybe using the st_store option?). I've pasted my code below. When I try and run the last line, I get error r3200 (conformability). I don't know if I've chosen the correct option for any of the commands, particularly the evaluatortype() and params() lines. I was also having a difficulty understanding how to add the upper and lower bound constraints for each constituent.

CODE:

use indexdata

/* start optimization process */

mata

X=st_data(., "XRegE XRegM XRegS XRegW") /* to create X matrix */

Xt=X' /* transpose */

omegadiag=st_data(., "omegadiag")

omega=diag(omegadiag) /* create Omega

C=Xt*omega

D=C*X

LB=st_data(., "LB") /* lower bound constraint for each constituent 134 x 1 matrix */

UB=st_data(., "UB") /* upper bound constraint for each constituent 134 x 1 matrix */

void weights(todo, p, W, g, H)

{

W=(D*W - C)^2

}

S = optimize_init()

optimize_init_which(S,"min")

optimize_init_evaluator(S, &weights())

optimize_init_evaluatortype(S, "gf2")

optimize_init_params(S, 0)

W=optimize(S)

I'm trying to calculate weights for constituents in an index using Stata's optimize() function. I have some code so far, but I'm having some difficulties getting it to run.

The equation I am trying to minimize is W=min

_{w}|| (X^{T}*Omega^{-1}*X)*W - X^{T}*Omega^{-1 }||^{2 }subject to an upper bound and a lower bound for each constituent. This was previously done in Matlab using the command lsqlin.X is a 134 x 4 matrix with rows for each constituent and columns for a geographic region (E, M, S, W). The data in X is the percent of each constituent's assets in each geographic region. Therefore, each row in matrix X adds to 1.

X

^{T}is the transpose of X.Omega

^{-1}is a 134 x 134 diagonal matrix with a constituent's enterprise value along the diagonal.I also have variables in my data for the upper and lower bounds for each constituent.

W is what I am trying to minimize.

Ideally, I would like to have the results be in matrix form/some form that I could export back to Stata (maybe using the st_store option?). I've pasted my code below. When I try and run the last line, I get error r3200 (conformability). I don't know if I've chosen the correct option for any of the commands, particularly the evaluatortype() and params() lines. I was also having a difficulty understanding how to add the upper and lower bound constraints for each constituent.

CODE:

use indexdata

/* start optimization process */

mata

X=st_data(., "XRegE XRegM XRegS XRegW") /* to create X matrix */

Xt=X' /* transpose */

omegadiag=st_data(., "omegadiag")

omega=diag(omegadiag) /* create Omega

^{-1}*/C=Xt*omega

D=C*X

LB=st_data(., "LB") /* lower bound constraint for each constituent 134 x 1 matrix */

UB=st_data(., "UB") /* upper bound constraint for each constituent 134 x 1 matrix */

void weights(todo, p, W, g, H)

{

W=(D*W - C)^2

}

S = optimize_init()

optimize_init_which(S,"min")

optimize_init_evaluator(S, &weights())

optimize_init_evaluatortype(S, "gf2")

optimize_init_params(S, 0)

W=optimize(S)

## Comment