r-投资组合优化-Solve.QP-约束不一致 [英] r - Portfolio Optimization - solve.QP - Constraints are Inconsistent

查看:906
本文介绍了r-投资组合优化-Solve.QP-约束不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Solve.QP解决投资组合优化问题(二次问题)

I am trying to use solve.QP to solve a portfolio optimization problem (quadratic problem)

共有3个资产

有4个约束条件:

  1. 权重之和等于1
  2. 投资组合的预期回报率等于5.2%
  3. 每项资产权重均大于0
  4. 每个资产权重小于.5

Dmat是协方差矩阵

Dmat is the covariance matrix

Dmat <- matrix(c(356.25808, 12.31581, 261.8830, 212.31581, 27.24840, 18.50515, 261.88302, 18.50515,535.45960), nrow=3, ncol=3)

dvec是每种资产的预期收益

dvec is each asset's expected return

dvec <- matrix(c(9.33, 3.33, 9.07), nrow=3, ncol=1)

Amat是约束矩阵

A.Equality <- matrix(c(1,1,1), ncol=1)
Amat <- cbind(A.Equality, dvec, diag(3), -diag(3))

约束A ^ T b> = b_0,b向量

constraint A^T b >= b_0, b vector

bvec <- c(1, 5.2, rep(0, 3), rep(-0.5, 3))

meq = 2,因为有两个相等约束,所以第一个和第二个约束是相等的

meq=2, since there are two equality constraints, first and second constraints are equality

然后我运行功能solve.QP

Then I run the function solve.QP

library(quadprog)
qp <- solve.QP(Dmat, dvec, Amat, bvec, meq=2)

但是它给出了错误

Error in solve.QP(Dmat, dvec, Amat, bvec, meq = 2) : constraints are inconsistent, no solution!

我不确定我做错了什么地方.

I am not sure where I did wrong.

推荐答案

您发布的代码存在两个问题:

There were two issues with the code you posted:

  • 发布的Dmat实际上不是对称的;您不小心包含了值212.31581而不是12.31581
  • meq=2选项意味着您的前两个约束保持相等,这意味着您的权重之和为1,而收益率恰好是5.2%.第二个约束显然是导致不可行的约束.鉴于您的其他限制条件,似乎没有有效的投资组合的收益率完全等于5.2%.实际上,由于不超过一半的投资组合可以拥有3.33%的回报,其余投资组合必须至少具有9.07%的回报,因此回报率必须为6.2%或更高.因此,您应该通过设置meq=1将其放宽到> =约束.
  • The posted Dmat is not actually symmetric; you had accidentally included value 212.31581 instead of 12.31581
  • The meq=2 option means your first two constraints are held at equality, meaning your weights sum to 1 and your return is exactly 5.2%. The second constraint is apparently the one causing the infeasibility; it seems there are no valid portfolios that have return exactly equal to 5.2% given your other constraints. Indeed, since no more than half the portfolio can have return 3.33% and the rest must have return of at least 9.07%, the return must be 6.2% or greater. Therefore, you should relax this to a >= constraint by setting meq=1.

这是工作代码:

library(quadprog)
Dmat <- matrix(c(356.25808, 12.31581, 261.88302, 12.31581, 27.24840, 18.50515, 261.88302, 18.50515,535.45960), nrow=3, ncol=3)
dvec <- matrix(c(9.33, 3.33, 9.07), nrow=3, ncol=1)
A.Equality <- matrix(c(1,1,1), ncol=1)
Amat <- cbind(A.Equality, dvec, diag(3), -diag(3))
bvec <- c(1, 5.2, rep(0, 3), rep(-0.5, 3))
qp <- solve.QP(Dmat, dvec, Amat, bvec, meq=1)
qp$solution
# [1] 0.3808733 0.5000000 0.1191267

最佳解决方案实际上带来了6.3%的回报.

The optimal solution is actually associated with a 6.3% return.

这篇关于r-投资组合优化-Solve.QP-约束不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆