R投资组合分析图表.EfficientFrontier功能 [英] R portfolio analytics chart.EfficientFrontier function
问题描述
我正在尝试使用R中的Portfolioanalytics包中的chart.EfficientFrontier函数来绘制我创建的有效边界对象的图表,但该对象一直失败.基本上,我正在尝试找到一个边界,该边界将年化标准偏差最小化.最终,一旦我开始工作,我还希望最大化年化回报.
I am trying to use the chart.EfficientFrontier function in the portfolioanalytics package in R to chart an efficient frontier object that I have created but it keeps failing. Basically I am trying to find a frontier that will minimize annaulized standard deviation. Eventually once I get this working I would also like to maximize annualized return.
首先,我使用此代码创建了一个年度标准差函数
Firstly I created an annualized standard deviation function using this code
pasd <- function(R, weights){
as.numeric(StdDev(R=R, weights=weights)*sqrt(12)) # hardcoded for monthly data
# as.numeric(StdDev(R=R, weights=weights)*sqrt(4)) # hardcoded for quarterly data
}
我导入了一个具有月收益的csv文件,我的投资组合对象如下所示:
I imported a csv file with monthly returns and my portfolio object looks like this:
> prt
**************************************************
PortfolioAnalytics Portfolio Specification
**************************************************
Call:
portfolio.spec(assets = colnames(returns))
Number of assets: 3
Asset Names
[1] "Global REITs" "Au REITs" "Au Util and Infra"
Constraints
Enabled constraint types
- leverage
- long_only
Objectives:
Enabled objective names
- mean
- pasd
现在,我使用此行成功创建了一个有效的边界对象:
Now I successfully create an efficient frontier object using this line:
prt.ef <- create.EfficientFrontier(R = returns, portfolio = prt, type = "DEoptim", match.col = "pasd")
但是当我尝试绘制它时,我收到以下错误消息.
But when I try to plot it I am getting the following error messages.
> chart.EfficientFrontier(prt.ef, match.col="pasd")
Error in StdDev(R = R, weights = weights) :
argument "weights" is missing, with no default
In addition: There were 26 warnings (use warnings() to see them)
Error in StdDev(R = R, weights = weights) :
argument "weights" is missing, with no default
Error in StdDev(R = R, weights = weights) :
argument "weights" is missing, with no default
Error in xlim[2] * 1.15 : non-numeric argument to binary operator
有人知道为什么会这样吗?当我使用summary(prt.ef)时,我可以看到权重,但是为什么chart.EfficientFrontier函数会失败?
Anyone know why this is the case? When I use summary(prt.ef) I can see the weights, but why is the chart.EfficientFrontier function failing?
推荐答案
正如@WaltS所建议的那样,您需要在实现函数上保持一致,以对均值和风险回报进行年度化处理.
As @WaltS suggested, you need to be consistent in implementing functions to annualize mean and risk returns.
但是实际上要获得年度统计数据,您有两个选择,您没有使用任何选择:
But actually to get annualized statistics you have two options, you are not using any:
1)使用月度数据进行优化,并使用规范中的原始风险回报功能.对于绘图,您可以分析
1) Make the optimization with monthly data, with the original risk return functions in the specification. For plotting you can anualize making
Port.Anua.Returns=prt.ef$frontier[,1]*12
Port.Anua.StDev=prt.ef$frontier[,2]*12^.5
月度或年度化投资组合的权重将相同.
The weights will be the same for monthly or annualized portfolios.
prt.ef$frontier[,-(1:3)]
2)将您的每月回报转换成年化回报乘以12.然后按照常规过程进行优化,所有风险和回报都已经在prt.ef$frontier
中进行了年化.
2) Transform your monthly returns in annualized returns multiplying by 12. Then do the optimization with the usual procedure, all risk and return will be already annualized in prt.ef$frontier
.
与EF中的锯齿线有关.使用您的投资组合规范,我还能够重新创建相同的行为.对于下面的绘图,我使用了edhec
数据,即您在物镜中使用原始mean
和StdDev
的规范:
Related to the jagged line in EF. Using your portfolio specification I was also able to recreate the same behavior. For the following plot I used edhec
data, your specification with original mean
and StdDev
in the objectives:
data(edhec)
returns <- edhec[,1:3]
该行为必须受到所使用的规范或优化算法的影响.我对软件包quadprog
中的solve.QP
做了相同的优化.这就是结果.
That behavior must be influenced by the specification or the optimization algorithm you are using. I did the same optimization with solve.QP
from package quadprog
. This is the result.
更新
代码在这里:
require(quadprog)
#min_x(-d^T x + 1/2 b^T D x) r.t A.x>=b
MV_QP<-function(nx, tarRet, Sig=NULL,long_only=FALSE){
if (is.null(Sig)) Sig=cov(nx)
dvec=rep(0,ncol(Sig))
meq=2
Amat=rbind(rep(1,ncol(Sig)),
apply(nx,2,mean) )
bvec=c(1,tarRet )
if (long_only) {
meq=1
Amat=Amat[-1,]
Amat=rbind(Amat,
diag(1,ncol(Sig)),
rep(1,ncol(Sig)),
rep(-1,ncol(Sig)))
bvec=bvec[-1]
bvec=c(bvec,
rep(0,ncol(Sig)),.98,-1.02)
}
sol <- solve.QP(Dmat=Sig, dvec, t(Amat), bvec, meq=meq)$solution
}
steps=50
x=returns
µ.b <- apply(X = x, 2, FUN = mean)
long_only=TRUE
range.bl <- seq(from = min(µ.b), to = max(µ.b)*ifelse(long_only,1,1.6), length.out = steps)
risk.bl <- t(sapply(range.bl, function(targetReturn) {
w <- MV_QP(x, targetReturn,long_only=long_only)
c(sd(x %*% w),w) }))
weigthsl=round(risk.bl[,-1],4)
colnames(weigthsl)=colnames(x)
weigthsl
risk.bl=risk.bl[,1]
rets.bl= weigthsl%*%µ.b
fan=12
plot(x = risk.bl*fan^.5, y = rets.bl*fan,col=2,pch=21,
xlab = "Annualized Risk ",
ylab = "Annualized Return", main = "long only EF with solve.QP")
这篇关于R投资组合分析图表.EfficientFrontier功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!