【发布时间】:2015-06-03 08:15:31
【问题描述】:
我正在尝试使用 R 中的投资组合分析包中的 chart.EfficientFrontier 函数来绘制我创建的有效边界对象,但它一直失败。基本上,我试图找到一个可以最小化 annaulized 标准偏差的边界。最终,一旦我完成这项工作,我还希望最大化年化回报。
首先我使用这段代码创建了一个年化标准差函数
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 文件,我的投资组合对象如下所示:
> 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
现在我使用这条线成功地创建了一个高效的边界对象:
prt.ef <- create.EfficientFrontier(R = returns, portfolio = prt, type = "DEoptim", match.col = "pasd")
但是当我尝试绘制它时,我收到以下错误消息。
> 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 函数失败了?
【问题讨论】:
-
chart.EfficientFrontier期望能够在没有权重的情况下调用pasd来计算每个资产的 sigma,并使用权重来计算投资组合的 sigma。要消除错误消息,请使用pasd <- function(R, weights=NULL) as.numeric(StdDev(R=R, weights=weights)*sqrt(12))使pasd像StdDev一样工作。此外,为了进行一致的分析,您应该将回报年化,然后优化将为月度和年化投资组合计算相同的权重。 -
我按照您的建议更改了
pasd函数,但 chart.EfficientFrontier 仍然给我错误。chart.EfficientFrontier(prt.ef, match.col="pasd") Error in StdDev(R = R, weights = weights) : object 'NuLL' not found Error in StdDev(R = R, weights = weights) : object 'NuLL' not found Error in StdDev(R = R, weights = weights) : object 'NuLL' not found Error in xlim[2] * 1.15 : non-numeric argument to binary operator
标签: r mathematical-optimization portfolio