即使与PerformanceAnalytics的作品相同,r中的PortfolioAnalytics也不会接受自定义的return_objective?

时间:2019-05-06 18:59:12

标签: r r-portfolioanalytics

我无法使PortfolioAnalytics中的自定义“返回”目标正常工作。

下面,我展示了一个可重复的示例,其中SortinoRatio起作用,但是简单地重命名该函数会使它返回错误。好像是个错误,但我可能错过了识别自定义目标的步骤。

library(PortfolioAnalytics)
library(PerformanceAnalytics)
require(xts)
require(doParallel)
registerDoParallel()

data(indexes)
R <- indexes[,1:4]
MAR = 0.005

重命名Sortino

SortinoRatioRenamed <- SortinoRatio
> identical(SortinoRatio,SortinoRatioRenamed)
[1] TRUE

SortinoRatio(R,MAR=MAR,weights=(rep(1/(ncol(R)),ncol(R))))
SortinoRatioRenamed(R,MAR=MAR,weights=(rep(1/(ncol(R)),ncol(R))))

#Both version return:
> SortinoRatio(R,MAR=MAR,weights=(rep(1/(ncol(R)),ncol(R))))
                           portfolio.returns
Sortino Ratio (MAR = 0.5%)        0.06213398
> SortinoRatioRenamed(R,MAR=MAR,weights=(rep(1/(ncol(R)),ncol(R))))
                           portfolio.returns
Sortino Ratio (MAR = 0.5%)        0.06213398

因此重命名的功能可以按预期工作

使用SortinoRatio的返回目标运行随机优化:

SortinoConstr <- NULL
SortinoConstr <- constraint_v1(assets = colnames(indexes[,1:4]), min = 0.05, max = 1, min_sum=.99, max_sum=1.01, weight_seq = generatesequence(by=.001))
SortinoConstr <- add.objective(constraints=SortinoConstr, type="return", name="SortinoRatio",  enabled=TRUE, arguments = list(MAR=MAR))
SortinoConstr <- add.objective(constraints=SortinoConstr, type="return", name="mean",  enabled=TRUE, multiplier=0) # multiplier 0 makes it available for plotting, but not affect optimization

SortinoResult<-optimize.portfolio(R=indexes[,1:4], constraints=SortinoConstr, optimize_method='random', search_size=2000, trace=TRUE, verbose=TRUE)
SortinoResult

***********************************
PortfolioAnalytics Optimization
***********************************

Call:
optimize.portfolio(R = indexes[, 1:4], constraints = SortinoConstr, 
    optimize_method = "random", search_size = 2000, trace = TRUE, 
    verbose = TRUE)

Optimal Weights:
      US Bonds    US Equities Int'l Equities    Commodities 
         0.841          0.067          0.050          0.050 

Objective Measures:
SortinoRatio 
      0.1609 


    mean 
0.006832 

按预期工作。

但是: 使用SortinoRatioRenamed的返回目标运行随机优化:(唯一的区别是函数名称)

SortinoConstr2 <- NULL
SortinoConstr2 <- constraint_v1(assets = colnames(indexes[,1:4]), min = 0.05, max = 1, min_sum=.99, max_sum=1.01, weight_seq = generatesequence(by=.001))
SortinoConstr2 <- add.objective(constraints=SortinoConstr2, type="return", name="SortinoRatioRenamed",  enabled=TRUE, arguments = list(MAR=MAR))
SortinoConst2r <- add.objective(constraints=SortinoConstr2, type="return", name="mean",  enabled=TRUE, multiplier=0) # multiplier 0 makes it available for plotting, but not affect optimization

SortinoResult2<-optimize.portfolio(R=indexes[,1:4], constraints=SortinoConstr2, optimize_method='random', search_size=2000, trace=TRUE, verbose=TRUE)
SortinoResult2

Error in search[i] <- ifelse(try(rp_objective_results[[i]]$out), rp_objective_results[[i]]$out,  : 
  replacement has length zero

并返回错误:“搜索[i]中的错误<-ifelse(try(rp_objective_results [[i]] $ out),rp_objective_results [[i]] $ out,:   替换的长度为零”

我已经看到了很多创建自定义函数的示例,但是还没有运气使任何演示函数都能正常工作-关于如何实现此功能的任何想法?

编辑:上面包含“相同”以进一步证明等效性

0 个答案:

没有答案