Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: Re: Inequality constraints with optimization

 From Jane Ross <[email protected]> To [email protected] Subject st: Re: Inequality constraints with optimization Date Mon, 30 Jul 2012 17:18:43 -0400

```> Im currently trying to construct the efficient frontier in a Markowitz portfolio using the command optimize-. I have a data set of returns:
>
>
>
>                 1       2       3       4
>
>                 +---------------------------------+
>
> 1              .111    .223    .122     .05
>
> 2              .114     .46    .003     .05
>
> 3              .323    -.09    .111     .05
>
> 4              .001   -.107    .054     .05
>
> 5              -.209     .12    .169     .05
>
> 6              .223    .309   -.035     .05
>
> 7              .26    .411    .133     .05
>
> 8              .21     .05    .732     .05
>
> 9              .144      .1    .021     .05
>
> 10           .412    .445    .131     .05
>
> 11           -.013    .123    .006     .05
>
> 12           .553     .55    .908     .05
>
>
>
> which I use to calculate the mean return (b) and the covariance (s) of the portfolio. I am trying to find the set of weights (p) which give the lowest variance of the portfolio = p*cov*p’
>
> subject to
>
> 1.       p*b’=.08
>
> 2.       p>=.02 for all p’s
>
> 3.       and sum(p)=1
>
>
>
> my code is:
>
>
>
> mata:
>
> mata clear
>
> void myeval(todo, p, s, fml, g, H)
>
> {
>
>   Return=variance(s)
>
> fml=(p)*Return*(p)'
>
>  }
>
> end
>
> import excel "C:\Users\New\Documents\MarkowitzOptimization.xlsx", sheet("Sheet2") firstrow clear \\this is the returns dataset mentioned above\\
>
> mkmat ATT GMC USX TBILL, mat(Ret)
>
> mean ATT GMC USX TBILL
>
> matrix b=e(b)
>
> mata:
>
> s=st_matrix("Ret")
>
> b=st_matrix("b")
>
> S=optimize_init()
>
> C=J(2,4,0)
>
> C[1,1..4]=b[1,1..4]
>
> C[2,1..4]=J(1,4,1)
>
> c = (.10\1)
>
> Cc = (C, c)
>
> optimize_init_constraints(S,Cc)
>
> optimize_init_which(S, "min")
>
> optimize_init_evaluator(S, &myeval())
>
> optimize_init_evaluatortype(S,"d0")
>
> optimize_init_params(S, J(1,4,.25))
>
> optimize_init_conv_maxiter(S, 100000000000000)
>
> optimize_init_argument(S, 1, s)
>
> p=optimize(S)
>
> end
>
>
>
>
>
> For this specific data set, I get a p vector of:
>
>                 +---------------------------------------------------------+
>
> 1              .0680515463       .1961302652       .0597523857       .6760658028
>
>
>
> And a total portfolio variance of .00356167 which fit my constraints.
>
>
>
> However, with larger more varied data sets, I would like to be able to constrain p in the optimization so that all the weights are >=.02. I have tried parameterization using the code below but I’m unsure how to get the variance back out of the equation after changing p and I don’t know if I need to reset the constraint matrix now that p is different. Does anyone have suggestions on how I should proceed?
>
>
>
> mata:
>
> mata clear
>
> void myeval(todo, p, s, fml, g, H)
>
> {
>
>   Return=variance(s)
>
> m=J(1,11,.02)
>
>   l=ln(p - m)
>
>  fml=(l)*Return*(l)'
>
>  }
>
> End
>
> import excel "C:\Users\New\Documents\MarkowitzOptimization.xlsx", sheet("Sheet2") firstrow clear
>
> mkmat ATT GMC USX TBILL, mat(Ret)
>
> mean ATT GMC USX TBILL
>
> matrix b=e(b)
>
> mata:
>
> s=st_matrix("Ret")
>
> b=st_matrix("b")
>
> S=optimize_init()
>
> C=J(2,4,0)
>
> C[1,1..4]=b[1,1..4]
>
> C[2,1..4]=J(1,4,1)
>
> c = (.10\1)
>
> Cc = (C, c)
>
> optimize_init_constraints(S,Cc)
>
> optimize_init_which(S, "min")
>
> optimize_init_evaluator(S, &myeval())
>
> optimize_init_evaluatortype(S,"d0")
>
> optimize_init_params(S, J(1,4,.25))
>
> optimize_init_conv_maxiter(S, 100000000000000)
>
> optimize_init_argument(S, 1, s)
>
> p=optimize(S)
>
> end
>
>  Thanks for any input

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```