1

我尝试使用 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           

4

1 回答 1

2

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

演示最大夏普比率:
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 中许多更复杂的包都有演示文件来帮助您入门。

于 2018-06-08T15:34:45.767 回答