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 at the end of May, and its replacement, statalist.org is already up and running.


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

st: Re: optimize with unequality constraints


From   Alberto Dorantes <alberto.dorantes@finanzastec.net>
To   statalist@hsphsun2.harvard.edu
Subject   st: Re: optimize with unequality constraints
Date   Thu, 19 Apr 2012 11:30:48 -0500

For those who might be interested in my "optimize" problem, I found a
solution to include unequal constraints using optimize in Mata. Here
is an example that solve my problem based on the code I had posted

void retorno_ajustado(real scalar todo, real vector p,
                      real vector R, real matrix COV,
					  ret, grad, Hess)
{  real matrix q
    q=J(1,4,0)
  // I create a temporal matrix q, that will be a log transformation
of the vector p I want to solve. This helps me to add the constraints
// that all p's will be greater than zero, and I can also add the
constraint that a specific p can be greater to any point between
// 0 and 1. In the code I included the constraints that p[1]>0.10, and
p[4]>0.20:
   q[1,1]=ln(p[1]-0.1)
   q[1,2]=ln(p[2])
   q[1,3]=ln(p[3])
   q[1,4]=ln(p[4]-0.2)
	// Previous code I had was:
	//  ret= p*R' / sqrt(p*COV*p')
	// Here is the same, but more efficient, and I added the transformed
q variables without changing my objective function value:
   ret=quadcross(p',R') / sqrt(quadcross(p',COV)*p') + q[1,1] - q[1,1]
+ q[1,2] - q[1,2] + q[1,3] - q[1,3] + q[1,4] - q[1,4]
}

S = optimize_init()
optimize_init_evaluator(S, &retorno_ajustado())
optimize_init_evaluatortype(S, "d0")
optimize_init_params(S,(0,1))
C=J(1,4,1)
c=(1)
COV=st_matrix("COVA")
R = [0.28, 0.19, 0.05, 0.42]
optimize_init_argument(S,1,R)
optimize_init_argument(S,2,COV)
optimize_init_constraints(S,(C,c))
optimize(S)
optimize_result_params(S)
optimize_result_value(S)

retorno=p*R'
retorno
varianza=p*COV*p'
desv=sqrt(varianza)
desv
p
end

//(COVA is a Var-Cov matrix of 4x4)


Regards... Alberto.

2012/4/1 Alberto Dorantes <alberto.dorantes@finanzastec.net>:
> Hi all.
>  Is there a way to specify unequality constraints to the optimize function
> in Mata?
> I have f(p) as a function to maximize. I found a way to put an
> equality constraint
> (the sum of p[i] is equal to one), but I could not specify the non-negative
> constraint to each p[i] (p[i]>0).
>
> I read in Statalist that using logs can solve the problem, but I have not
> figure out how to transform my p vector to logs since I', afraid that my
> function will not work correctly.
>
> To be more specific, here is my code:
>
> mata:
> void retorno_ajustado(real scalar todo, real vector p,
>                      real vector R, real matrix COV,
>                    ret, grad, Hess)
> {
>    ret= p*R' / sqrt(p*COV*p')
> }
>
> S = optimize_init()
> optimize_init_evaluator(S, &retorno_ajustado())
> optimize_init_evaluatortype(S, "d0")
> optimize_init_params(S,(0,1))
> C=J(1,2,1)
> c=(1)
> COV=st_matrix("COVA")
> R = [0.28, 0.19]
> optimize_init_argument(S,1,R)
> optimize_init_argument(S,2,COV)
> optimize_init_constraints(S,(C,c))
> optimize(S)
> optimize_result_params(S)
> optimize_result_value(S)
> end
>
> (COVA is a Var-Cov matrix of 2x2)
>
> Thanks in advance!
>
> Carlos A. Dorantes

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