如何在PortfolioAnalytics R软件包中修复风险规避参数?

时间:2019-06-14 11:36:19

标签: portfolio r-portfolioanalytics

我正在使用PortfolioAnalytics R包来创建有效的边界,并针对固定的风险规避参数获得最佳的投资组合。 (过去,我使用“ tseries”程序包和Portfolio.optim来创建有效的边界,但这不能解决风险规避问题。因此,我试图转向PortfolioAnalytics) 我已将数据转换为时间序列数据框并创建了投资组合对象。然后,我添加一些约束和目标。但是当我开始优化create.EfficientFrontier的均值方差公式时,它显示了一个错误。我正在努力理解错误。

***在有效边界上设置风险规避参数

options(max.print=999999)
library(tseries)
library(MASS)
library(ggplot2)
library(quadprog)
library(PortfolioAnalytics)
library(gdata)
library(DEoptim)
library(ROI)
require(ROI.plugin.glpk)
require(ROI.plugin.quadprog)
library(foreach)
library(DEoptim)
library(iterators)
library(fGarch)
library(Rglpk)
library(quadprog)
library(ROI)
library(ROI.plugin.glpk)
library(ROI.plugin.quadprog)
library(ROI.plugin.symphony)
library(pso)
library(GenSA)
library(corpcor)
library(testthat)
library(nloptr)
library(MASS)
library(robustbase)

##Rownames converted to year-month-date## 

mydata1 = bird2
mydata1 = xts(mydata1, order.by=as.Date(mydata1$Date))
mydata1 = read.table("bird return by price_in0070.txt") 

##Name the assets for plotting##
fund.names <- colnames(bird2)

##Create Portfolio Object## 
pspec <- portfolio.spec(assets=fund.names)
print.default(pspec)

##Add Constraints## 
pspec <- add.constraint(portfolio=pspec,
                         type="weight_sum",
                         min_sum=1,
                         max_sum=1)

summary(pspec)

##Declare as time series##



##Add Objectives## 
pspec <- add.objective(portfolio=pspec,
                        type='risk',
                        name='ETL',
                        arguments=list(p=0.95))

pspec <- add.objective(portfolio=pspec,
                        type='return',
                        name='mean')


qu <- add.objective(portfolio=pspec, type="return", name="mean")
qu <- add.objective(portfolio=qu, type="risk", name="var", risk_aversion=0.25)


opt_qu <- optimize.portfolio(R=dfx, portfolio=qu,
                             optimize_method="ROI",
                             trace=TRUE)



# mean-var efficient frontier
meanvar.ef <- create.EfficientFrontier(R=dfx, portfolio=pspec, type="mean-StdDev")
meanvar.ef
summary(meanvar.ef, digits=2)
meanvar.ef$frontier


###Fix the risk aversion parameter### 

meanvar.portf$objectives[[2]]$risk_aversion=0.25
opt_meanvar <- optimize.portfolio(R=dfx, portfolio=meanvar.portf, optimize_method="ROI", trace=TRUE)
chart.EfficientFrontier(opt_meanvar, match.col="StdDev", n.portfolios=25, type="l")

This is the error I get from the code: 

meanvar.ef <- create.EfficientFrontier(R=dfx, portfolio=pspec, type="mean-StdDev")

**portfolio moment function failed with message Error in cov(tmpR, use = "pairwise.complete.obs") : 
  is.numeric(x) || is.logical(x) is not TRUE

Error: $ operator is invalid for atomic vectors
In addition: Warning messages:
1: In mean.default(newX[, i], ...) :
  argument is not numeric or logical: returning NA
2: In mean.default(newX[, i], ...) :
  argument is not numeric or logical: returning NA
3: In mean.default(newX[, i], ...) :
  argument is not numeric or logical: returning NA
4: In mean.default(newX[, i], ...) :
  argument is not numeric or logical: returning NA
5: In mean.default(newX[, i], ...) :
  argument is not numeric or logical: returning NA
6: In mean.default(newX[, i], ...) :
  argument is not numeric or logical: returning NA
7: In mean.default(newX[, i], ...) :
  argument is not numeric or logical: returning NA
8: In mean.default(newX[, i], ...) :
  argument is not numeric or logical: returning NA**

0 个答案:

没有答案