问题描述
我正在尝试在5个约束条件下最大化投资组合回报率:
1.-一定程度的投资组合风险
2.-与上述相同,但符号相反(我需要将风险准确地定为该数字)
3.-权重之和必须为1
4.-所有权重必须大于或等于cero
5.-所有权重必须最大为一个
我使用的是optiSolve软件包,因为我没有找到其他任何可以写这个问题的软件包(或者至少我知道如何使用它)。
我在这里遇到三个大问题,第一个是所得的权重向量之和大于1 ,第二个问题是我无法声明 t(w) %*%varcov_matrix%*%w == 0 在二次约束中,因为它仅允许“ ,最后我不知道如何设置约束以仅获得正数权重
有人知道如何解决前面提到的所有约束的最大化问题,或者告诉我我做错了什么?我真的很感激,因为结果似乎没有考虑约束。谢谢!vector_de_retornos <- rnorm(5)
matriz_de_varcov <- matrix(rnorm(25),ncol = 5)
library(optiSolve)
restriccion1 <- quadcon(Q = matriz_de_varcov,dir = "<=",val = 0.04237972)
restriccion1_neg <- quadcon(Q = -matriz_de_varcov,val = -mean(limite_inf,limite_sup))
restriccion2 <- lincon(t(vector_de_retornos),d=rep(0,nrow(t(vector_de_retornos))),dir=rep("==",val = rep(1,id=1:ncol(t(vector_de_retornos)),name = nrow(t(vector_de_retornos)))
restriccion_nonnegativa <- lbcon(rep(0,length(vector_de_retornos)))
restriccion_positiva <- ubcon(rep(1,length(vector_de_retornos)))
funcion_lineal <- linfun(vector_de_retornos,name = "lin.fun")
funcion_obj <- cop(funcion_lineal,max = T,ub = restriccion_positiva,lc = restriccion2,lb = restriccion_nonnegativa,restriccion1,restriccion1_neg)
porfavor_funciona <- solvecop(funcion_obj,solver = "alabama")
> porfavor_funciona$x
1 2 3 4 5
-3.243313e-09 -4.709673e-09 9.741379e-01 3.689040e-01 -1.685290e-09
> sum(porfavor_funciona$x)
[1] 1.343042
解决方法
- 您的
restriccion2
使x的加权和为1,如果还希望确保x的常规和为1,则可以如下修改约束:
restriccion2 <- lincon(rbind(t(vector_de_retornos),# make a second row of coefficients in the A matrix
t(rep(1,length(vector_de_retornos)))),d=rep(0,2),# the scalar value for both constraints is 0
dir=rep('==',# the direction for both constraints is '=='
val=rep(1,# the rhs value for both constraints is 1
id=1:ncol(t(vector_de_retornos)),# the number of columns is the same as before
name= 1:2)
如果只希望常规和为1,而不是加权和,则可以将lincon
函数中的第一个参数替换为您定义的t(rep(1,length(vector_de_retornos)))
,这将限制x
的常规和为1。
- 要仅使用不等式创建不等式约束,您需要两次使用相同的约束,但是两者之间的系数和右手边的值应具有相反的符号(例如:2 x x val参数提供了一个不同的值,因此,除非这两个约束匹配(除非相反的符号如下),否则这两个约束将不会组合成相等约束。
restriccion1_neg <- quadcon(Q = -matriz_de_varcov,dir = "<=",val = -0.04237972)
- 我不确定,因为我无法在包装文档中找到精度信息,但是x向量中的那些“负”值可能是由于舍入。它们是如此之小,实际上为0,所以我认为非负性约束功能正常。
restriccion_nonnegativa <- lbcon(rep(0,length(vector_de_retornos)))
形式的约束
x'Qx = a
非凸。 (更笼统:任何非线性等式约束都是非凸的)。非凸问题比凸问题更难解决,需要专门的全局求解器。对于凸问题,有很多求解器可用。非凸问题不是这种情况。大多数投资组合模型被表述为凸QP(二次规划,即风险-二次项-是目标)或凸QCP / SOCP问题(约束中的二次项,但以凸方式)。因此,约束条件
x'Qx <= a
只要Q是正半定数,就很容易(凸)。将x'Qx=a
重写为
x'Qx <= a
-x'Qx <= -a
不幸的是,不会消除不凸性,因为-Q
不是PSD。如果我们要最大化回报,通常只使用x'Qx <= a
来限制风险,而忽略了> =部分。更为流行的是将收益和风险都放在目标中(这是标准的均值-可变投资组合模型)。
在R下求解非凸二次问题的一种可能的求解器是Gurobi。