如何在R中使用GenSA函数放置数学约束 [英] How to put mathematical constraints with GenSA function in R
问题描述
我目前正在尝试使用模拟退火程序包GenSA,以最小化以下功能:
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
}
其中Cov_Mat是从4种资产获得的协方差矩阵,而v是维度4的权重向量.
Where Cov_Mat is a covariance matrix obtained from 4 assets and v is a weight vector of dimension 4.
我正在尝试以这种方式解决Markowitz资产分配方法,并且我想知道如何引入数学约束,例如所有系数之和必须等于1:
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
此外,由于我打算依靠GenSA功能,因此我想在约束条件下使用类似的东西:
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.
对于任何建议,我将不胜感激.这是我第一次使用SO,所以请不要犹豫告诉我我的提问方式是否错误.
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.
推荐答案
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 )
}
,因此为了最小化目标函数efficientFunction
,生成的参数也将惩罚项lambda * abs( sum(v) - 1 )
最小化.拉格朗日乘数lambda
设置为任意但足够高的水平.
, 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.
这篇关于如何在R中使用GenSA函数放置数学约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!