現代投資組合理論

投資組合分析RRR- optimize.portfolio.rebalancing 錯誤

  • January 10, 2017

剛開始使用 PortfolioAnalytics(一般來說對 R 來說相當新)並且在執行時遇到錯誤optimize.portfolio.rebalance——見下文:

UseMethod(“extractObjectiveMeasures”)中的錯誤:沒有適用於“extractObjectiveMeasures”的適用方法應用於類“c(‘simpleError’,’error’,‘condition’)”的對象

我查看了 CRAN 上的小插圖和包裝文件,但無法弄清楚可能導致此特定問題的原因。optimize.portfolio似乎在相同的約束和目標下工作正常。也許我還沒有載入所有必需的庫?接受任何其他建議。謝謝你。

# Load packages
library(xts)
library(ROI)
library(doParallel)
library(PortfolioAnalytics)

# Define pseudo dates and returns (format as xts)
dates <- seq(from = as.Date("2016-01-01"), to = as.Date("2016-12-01"), by = "month")
returns <- as.xts(data.frame("A" = runif(12, -.05, .05), 
                        "B" = runif(12, -.10, .20), 
                        "C" = runif(12, -.15, .30)), 
                 order.by = dates)

# Register doParalell session (PortfolioAnalytics suggests this when running optimizations w/ rebals)
registerDoParallel(cores = 2)

# Initiate portfolio object
port <- portfolio.spec(assets = colnames(returns))

# Add constraints 
port <- add.constraint(portfolio = port, type = "long_only")
port <- add.constraint(portfolio = port, type = "full_investment")
port <- add.constraint(portfolio = port, type = "box", min = 0.02, max = 0.40)

# Add objectives - want to maximize Sharpe Ratio
port <- add.objective(portfolio = port, type = "risk", name = "StdDev")
port <- add.objective(portfolio = port, type = "return", name = "mean")

# Run optimization WITHOUT rebalancing - THIS SEEMS TO RUN FINE...
portOPT <- optimize.portfolio(R = returns,
                             portfolio = port,
                             optimize_method = "ROI",
                             maxSR = TRUE)
portOPT

# Run optimization with rebalancing
portOPT.R <- optimize.portfolio.rebalancing(R = returns,
                                           portfolio = port,
                                           optimize_method = "ROI",
                                           maxSR = TRUE,
                                           training_period = 6,
                                           rebalance_on = "months")
portOPT.R

編輯

此外,我正在執行 Windows 10,並認為包含我目前安裝的軟體包的列表會有所幫助:

https://docs.google.com/spreadsheets/d/1JLauyCpmDg3DOztRJKntoZ2LFMxV4_ouLOKYUVenEAA/edit?usp=sharing

ROI 軟體包的 0.2 版本進行了重大更改。我們正在努力修復。同時,我建議降級到 ROI v0.1-0 和 ROI.plugin.* 到 0.0-2。

您還可以使用不同的優化方法執行優化,例如optimize_method = "random"optimize_method = "DEoptim"

引用自:https://quant.stackexchange.com/questions/31752