无法在PortfolioAnalytics中使用SharpeRatio来优化投资组合

时间:2018-01-30 16:37:12

标签: r performanceanalytics r-portfolioanalytics

我尝试使用SharpeRatio有一个目标函数来优化我的投资组合,但是我得到以下错误:

objective name SharpeRatio generated an error or warning: Error in t(w) %*% M3 : requires numeric/complex matrix/vector arguments

我已经搜索过,似乎问题与权重有关,但我无法找到解决问题的方法。

下一个代码复制错误:

library(PortfolioAnalytics)
data(edhec)
asset_names <- colnames(edhec)
port_spec <- portfolio.spec(asset_names)
port_spec <- add.constraint(portfolio = port_spec, type = "weight_sum", min_sum = 0.99, max_sum = 1.01)
port_spec <- add.constraint(portfolio = port_spec, type = "long_only")
port_spec <- add.objective(portfolio = port_spec, type = "return", name = "SharpeRatio", FUN = "StdDev")
opt_DE <- optimize.portfolio(R = edhec, portfolio = port_spec, optimize_method = "DEoptim", search_size=5000, trace = TRUE, traceDE = 0)

已请求,sessionInfo()

R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default 

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] FactoMineR_1.39               nFactors_2.3.3                lattice_0.20-35              
 [4] boot_1.3-20                   psych_1.7.8                   MASS_7.3-47                  
 [7] PortfolioAnalytics_1.0.3636   PerformanceAnalytics_1.4.3541 foreach_1.4.4                
[10] xts_0.10-1                    zoo_1.8-0                    

loaded via a namespace (and not attached):
 [1] cluster_2.0.6             leaps_3.0                 mnormt_1.5-5              scatterplot3d_0.3-40     
 [5] quadprog_1.5-5            ROI_0.3-0                 TTR_0.23-2                tools_3.4.3              
 [9] quantmod_0.4-12           parallel_3.4.3            grid_3.4.3                nlme_3.1-131             
[13] registry_0.5              iterators_1.0.9           yaml_2.1.16               GenSA_1.1.7              
[17] codetools_0.2-15          curl_3.1                  slam_0.1-42               ROI.plugin.quadprog_0.2-5
[21] compiler_3.4.3            flashClust_1.01-2         DEoptim_2.2-4             foreign_0.8-69           
  

1 个答案:

答案 0 :(得分:0)

我建议查看PortfolioAnalytics演示文件。其中一个特别是

Demo Max Sharpe比率:
https://github.com/R-Finance/PortfolioAnalytics/blob/master/demo/demo_max_Sharpe.R

对于参考特别有用。阅读完一些代码和注释后,您会看到一些内容。首先,您指定了冲突的参数,例如type = "return", name = "SharpeRatio", FUN = "StdDev"

"return"是一种约束,"StdDev""risk"约束的名称,"SharpeRatio"是您要解决的问题。

如果您使用"ROI"方法进行优化,则需要指定要在优化"maxSR=TRUE"中最大化夏普比率,以便使用"DEOptim"优化方法,你需要放松你的杠杆约束。

每个的例子可以在下面找到。它们直接取自上面引用的演示文件。

library(PortfolioAnalytics)

# Examples of solving optimization problems to maximize mean return per unit StdDev

data(edhec)
R <- edhec[, 1:8]
funds <- colnames(R)

# Construct initial portfolio
init.portf <- portfolio.spec(assets=funds)
init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
init.portf <- add.constraint(portfolio=init.portf, type="long_only")
init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
init.portf


# The default action if "mean" and "StdDev" are specified as objectives with
# optimize_method="ROI" is to maximize quadratic utility. If we want to maximize
# Sharpe Ratio, we need to pass in maxSR=TRUE to optimize.portfolio.

maxSR.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
                                   optimize_method="ROI", 
                                   maxSR=TRUE, trace=TRUE)
maxSR.lo.ROI

# Although the maximum Sharpe Ratio objective can be solved quickly and accurately
# with optimize_method="ROI", it is also possible to solve this optimization
# problem using other solvers such as random portfolios or DEoptim. These
# solvers have the added flexibility of using different methods to calculate
# the Sharpe Ratio (e.g. we could specify annualized measures of risk and return).

# For random portfolios and DEoptim, the leverage constraints should be 
# relaxed slightly.
init.portf$constraints[[1]]$min_sum=0.99
init.portf$constraints[[1]]$max_sum=1.01


# Use DEoptim
maxSR.lo.DE <- optimize.portfolio(R=R, portfolio=init.portf, 
                                  optimize_method="DEoptim",
                                  search_size=2000,
                                  trace=TRUE) 

希望这会有所帮助;通常我发现R中许多更复杂的软件包都有演示文件来帮助你入门。