我有N股的时间序列数据。
sample.data<-replicate(10,rnorm(1000))
,其中每列显示不同股票随时间的回报。
我正在尝试构建投资组合权重向量,以最小化回报的方差。
目标函数:
min w^{T}\sum w
s.t. e_{n}^{T}w=1
\left \| w \right \|\leq C
其中w是权重向量,\sum
是协方差矩阵,e_{n}^{T}
是1的向量,C
是常数。第二个约束(\left \| w \right \|
)是不等式约束。
我使用以下代码来解决此问题:
library(Rsolnp)
gamma<-1
fn<-function(x) {cov.Rt<-cov(sample.data); return(t(x)%*%cov.Rt%*%x)} #OBJECTIVE FUNCTION TO MINIMIZE
eqn<-function(x){one.vec<-matrix(1,ncol=10,nrow=1); return(one.vec%*%x)} #EQUALITY CONSTRAINT
constraints<-1 #EQUALITY CONSTRAINT
ineq<-function(x){one.vec<-matrix(1,ncol=10,nrow=1); #INEQUALITY CONSTRAINT
z1<-one.vec%*%abs(x)
return(z1)
}
uh<-gamma #UPPER BOUND
lb<-0 #LOWER BOUND
x0<-matrix(0,10,1) #STARTING PARAMETER VECTOR (NOT SURE WHAT STARTING VALUES TO PUT HERE)
sol1<-solnp(x0,fun=fn,eqfun=eqn,eqB=constraints, ineqfun=ineq,ineqUB=gamma,ineqLB=lb)
运行此代码时,我收到以下错误消息:
Error in solve.default(cz,tol = 1e-25) : system is computationally singular: reciprocal condition number = 0 In addition: There were 50 warnings (use warnings() to see the first 50)
warnings()
1: In cbind(temp, funv): number of rows of result is not a multiple of vector length
任何想法我可能做错了什么?
启动参数向量x0
是否存在问题?
答案 0 :(得分:4)
我稍微改了你的代码:
library(Rsolnp)
set.seed(1)
sample.data <- matrix(rnorm(10*1000),ncol=10)
gamma <- 1
#OBJECTIVE FUNCTION TO MINIMIZE
fn <- function(x){
cov.Rt <- cov(sample.data);
as.numeric(t(x) %*% cov.Rt %*% x)
}
#EQUALITY CONSTRAINT
eqn <- function(x){
one.vec <- matrix(1, ncol=10, nrow=1)
as.numeric(one.vec %*% x)
}
constraints <- 1
#INEQUALITY CONSTRAINT
ineq <- function(x){
one.vec <- matrix(1, ncol=10, nrow=1);
z1<-one.vec %*% abs(x)
as.numeric(z1)
}
uh <- gamma #UPPER BOUND
lb <- 0 #LOWER BOUND
x0 <- matrix(1, 10, 1) #STARTING PARAMETER VECTOR (NOT SURE WHAT STARTING VALUES TO PUT HERE)
sol1 <- solnp(x0, fun=fn, eqfun=eqn, eqB=constraints)
当我们运行上面的代码时,我们得到了一个解决方案:
Iter: 1 fn: 0.09624 Pars: 0.08355 0.08307 0.10154 0.09108 0.11745 0.12076 0.09020 0.09435 0.10884 0.10918
Iter: 2 fn: 0.09624 Pars: 0.08354 0.08308 0.10153 0.09107 0.11746 0.12078 0.09021 0.09434 0.10883 0.10918
solnp--> Completed in 2 iterations
但是当我们将不等式限制添加到优化问题时,我们遇到了问题:
> sol2 <- solnp(x0, fun=fn, eqfun=eqn, eqB=constraints,
ineqfun=ineq, ineqUB=gamma, ineqLB=lb)
Iter: 1 fn: 0.09624 Pars: 0.08356 0.08305 0.10153 0.09111 0.11748 0.12078 0.09021 0.09431 0.10881 0.10916
solnp-->The linearized problem has no feasible
solnp-->solution. The problem may not be feasible.
Iter: 2 fn: 272.5459 Pars: 4.44541 4.42066 5.40272 4.84595 6.25082 6.42718 4.80020 5.02004 5.79138 5.81029
solnp-->The linearized problem has no feasible
solnp-->solution. The problem may not be feasible.
Iter: 3 fn: 272.5459 Pars: 4.44547 4.42070 5.40274 4.84596 6.25078 6.42712 4.80023 5.02006 5.79138 5.81023
Iter: 4 fn: 0.09624 Pars: 0.08357 0.08304 0.10157 0.09106 0.11744 0.12074 0.09021 0.09432 0.10886 0.10918
Iter: 5 fn: 0.09625 Pars: 0.08354 0.08308 0.10153 0.09107 0.11747 0.12078 0.09021 0.09434 0.10883 0.10919
Iter: 6 fn: 0.09717 Pars: 0.08394 0.08347 0.10201 0.09150 0.11803 0.12135 0.09064 0.09479 0.10935 0.10971
Iter: 7 fn: 0.09624 Pars: 0.08353 0.08307 0.10153 0.09106 0.11747 0.12078 0.09020 0.09433 0.10883 0.10919
Iter: 8 fn: 0.09624 Pars: 0.08353 0.08307 0.10153 0.09106 0.11747 0.12078 0.09020 0.09433 0.10883 0.10919
solnp--> Solution not reliable....Problem Inverting Hessian.
Warning message:
In p0 * vscale[(neq + 2):(nc + np + 1)] :
longer object length is not a multiple of shorter object length
让我们尝试稍微更改一下伽玛:
gamma <- 1.01
> sol2 <- solnp(x0, fun=fn, eqfun=eqn, eqB=constraints,
ineqfun=ineq, ineqUB=gamma, ineqLB=lb)
Iter: 1 fn: 0.09624 Pars: 0.08355 0.08307 0.10153 0.09108 0.11745 0.12076 0.09020 0.09435 0.10884 0.10918
Iter: 2 fn: 0.09624 Pars: 0.08354 0.08308 0.10153 0.09107 0.11746 0.12078 0.09021 0.09434 0.10883 0.10918
solnp--> Completed in 2 iterations
因此,您的不等式约束似乎完全围绕等式约束。同时看两个约束,对我来说似乎有点奇怪。我的猜测是,你可能指出了你的不等式约束是错误的,只是想要一个像卖空约束的东西,例如。权重在0和1之间。这可以通过以下方法实现:
ineq <- function(x){ return(x) }
uh <- rep(gamma, 10) #UPPER BOUND
lb <- rep(0, 10) #LOWER BOUND
sol3 <- solnp(x0, fun=fn, eqfun=eqn, eqB=constraints, ineqfun=ineq, ineqUB=uh, ineqLB=lb)