Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Re: Inequality constraints with optimization


From   Alberto Dorantes <alberto.dorantes@finanzastec.net>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Re: Inequality constraints with optimization
Date   Sat, 25 Aug 2012 14:35:20 -0500

Hi Jane.
For your evaluation function, try this (I'm assuming you have 11
instruments in the portfolio):
void myeval(todo, p, s, fml, g, H)

 { real matrix q
 q=J(1,11,0)
 q=ln(p - 0.02)

 Return=variance(s)

  fml=(p)*Return*(p)'

  }

Now, it is possible that optimize cannot find a solution after many
interations, so you can use _optimize instead of optimize (to avoid
the program to halt), and use the optimize_result_errorcode function
to get the error number, and then if the error is not zero, initialize
the optimizer again but changing the optimization technique wih the
function optimize_init_technique (there are 4 types of techniques in
Mata).  Also, you can change the criteria for convergence using the
functions: optimize_init_conv_ptol, optimize_init_conv_vtol and
optimize_init_nrtol.

Late, but I hope this help.

Alberto.

2012/7/30 Jane Ross <jross5137@gmail.com>:
>> 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/

*
*   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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index