How to put mathematical constraints with GenSA function in R

旧街凉风 提交于 2019-12-18 08:55:07

问题


I am currently trying to use Simulated Annealing package GenSA in order to minimize the function below :

efficientFunction <- function(v) {
  t(v)  %*% Cov_Mat   %*%  v
 }

Where Cov_Mat is a covariance matrix obtained from 4 assets and v is a weight vector of dimension 4.

I'm trying to solve the Markowitz asset allocation approach this way and I would like to know how I could introduce mathematical constraint such as the sum of all coefficients have to equal 1 :

sum(v) = 1

Moreover since I intend to rely on the GenSA function, I would like to use something like this with the constraint :

v <- c(0.25, 0.25, 0.25, 0.25)
dimension <- 4
lower <- rep(0, dimension)
upper <- rep(1, dimension)

out <- GenSA(v, lower = lower, upper = upper, fn = efficientFunction)

I have found in this paper : http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.97.6091&rep=rep1&type=pdf how to handle such constraint within the Simulated Annealing Algorithm but I don't know how I could implement it in R.

I'd be very grateful for any advice. It is my first time using SO so don't hesitate to tell me if I have the wrong approach in the way I ask question.


回答1:


A possible approach would be to make use of so-called Lagrange multipliers (cf., http://en.wikipedia.org/wiki/Lagrange_multiplier). For example, set

efficientFunction <- function(v) {
  lambda <- 100
  t(v)  %*% Cov_Mat   %*%  v + lambda * abs( sum(v) - 1 )
}

, so that in order to minimize the objective function efficientFunction the resulting parameter also minimize the penalty term lambda * abs( sum(v) - 1 ). The Lagrange multiplier lambda is set to an arbitrary but sufficiently high level.




回答2:


So the function itself doesn't appear to have any constraints that you can set. However, you can reparameterize your function to force the constraint. How about

efficientFunction <- function(v) {
    v <- v/sum(v)
    t(v) %*% Cov_Mat %*%  v
}

Here we normalize the values of v so that they will sum to 1. Then, when we get the output parameters, we need to perform the same transformation

out <- GenSA(v, lower = lower, upper = upper, fn = efficientFunction)
out$par/sum(out$par)


来源:https://stackoverflow.com/questions/23808693/how-to-put-mathematical-constraints-with-gensa-function-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!