PortfolioAnalytics in R - optimize.portfolio() results in NaN for annualized return

Deadly 提交于 2019-12-08 05:13:58

问题


I am learning how to use PortfolioAnalytics package in R to do rebalancing a portfolio every quarters. I collected the data on 19 large-cap stocks in US and China using qmao package, and then compute the quarterly return. Everything works fine until I call the function optimize.portfolio.rebalancing(). It kept giving me the following error message, together with NaN as a result for Annualized Portfolio Rebalancing Return:

VaR calculation produces unreliable result (risk over 100%) for column: 1 : 1.12020244277128 ES calculation produces unreliable result (risk over 100%) for column: 1 : 1.14791543855955 VaR calculation produces unreliable result (risk over 100%) for column: 1 : 1.13372707060527 ES calculation produces unreliable result (risk over 100%) for column: 1 : 1.14960946412769

Full code for reproducing

#get the stock price data for 19 large-cap stocks from 2008/10/29 to 2018/11/04 and compute its quarterly return.
#Make the boxplot of the quarterly return for all 19 stocks. 
library(qmao)
library(quantmod)
library(data.table)
library(scales)
library(ggplot2)
library(car)
library(PortfolioAnalytics)
library(foreach)
library(iterators)
library(ROI)
library(ROI.plugin.quadprog)
library(ROI.plugin.glpk)
library(doParallel)
registerDoParallel()

symbols <- c("IBM", "GOOG", "JPM", "BAC", "TGT", "WMT", "AAPL", "TSLA", "AMZN", "PG", "EBAY", "NKE", "MSFT", "GE", "TCEHY", "SBUX", "GM", "TM", "BABA")
data.env <- new.env()
startDate = as.Date("2008-10-29") #Specify what date to get the prices from
endDate = as.Date("2018-11-04")
getSymbols(symbols, env = data.env ,from= startDate ,to= endDate)
pf <- PF(symbols,env = data.env,silent=TRUE) # consolidated xts-object
pfMth <- pf[endpoints(pf,on='quarters'),]   # get quarterly endpoints
pfMthRets <- ROC(pfMth,type='discrete')   
pfMthRets <- pfMthRets[complete.cases(pfMthRets),]
tail(pfMthRets)

#compute S&P 500 quarterly return from 2008/10/29 to 2018/11/04
startDate = as.Date("2008-10-29") #Specify what date to get the prices from
endDate = as.Date("2018-11-04")
sp500 <- getSymbols("^GSPC",auto.assign = FALSE ,from= startDate ,to= endDate)
sp500 <- PF("sp500",env = sp500,silent=TRUE) # consolidated xts-object
sp500Mth <- sp500[endpoints(sp500,on='quarters'),]   # get monthly endpoints
sp500MthRets <- ROC(sp500Mth,type='discrete')   
sp500MthRets <- sp500MthRets[complete.cases(sp500MthRets),]

#constraint the portfolio of stock with certain constraints and solve for the optimal portfolio (in terms of minimum variance and maximum return)
returns <- pfMthRets[, 1:ncol(pfMthRets)]
colnames(returns) <- c("IBM", "GOOG", "JPM", "BAC", "TGT", "WMT", "AAPL", "TSLA", "AMZN", "PG", "EBAY", "NKE", "MSFT", "GE", "TCEHY", "SBUX", "GM", "TM", "BABA")
portf.dn <- portfolio.spec(assets = colnames(returns))
# Add constraint such that the portfolio weights sum to 0*
portf.dn <- add.constraint(portf.dn, type="active")
# Add box constraint such that no asset can have a weight of greater than
# 20% or less than -20%
portf.dn <- add.constraint(portf.dn, type="box", min=-0.2, max=0.2)
# Add constraint such that the portfolio beta is between -0.25 and 0.25
betas <- t(CAPM.beta(pfMthRets,sp500MthRets)) #beta = cov(Ra,Rb)/var(R)
portf.dn <- add.constraint(portf.dn, type="factor_exposure", B=betas,
                           lower=-0.25, upper=0.25)

# Add objective to maximize portfolio return with a target of 0.15
portf.dn <- add.objective(portf.dn, type="return", name="mean")
# Add objective to minimize portfolio StdDev with a target of 0.005
portf.dn <- add.objective(portf.dn, type="risk", name="var")
# transaction cost constraint
portf.dn <- add.constraint(portfolio = portf.dn, type = "transaction_cost", ptc=0.01)

# Run the optimization with rebalancing every quarters
opt.dn <- optimize.portfolio.rebalancing(R=pfMthRets, portf.dn,
                             optimize_method="random",rebalance_on = "quarters",maxSR = TRUE,
                             training_period = 3)
opt.dn

My questions:

(1) It also seems to me that we have no clue to know which of the given constraints and objectives result in feasible solutions (for example, we can have 10 constraints, 2 simple objectives (minimize risk, and maximize return given certain level of risk), and then the result is NaN for weights assigned to 19 stocks above). So how do we determine which constraint to add to make it feasible?

(2) If I want to plot the efficient frontier every quarters, how can I do that? I tried doing this with meanSigma.ef = create.EfficientFrontier(R=pfMthRets, portfolio=portf.dn, type="mean-StdDev", n.portfolios = 25) but I keeps getting the error message "Error in seq.default(from = minret, to = maxret, length.out = n.portfolios) : 'from' cannot be NA, NaN or infinite."

来源:https://stackoverflow.com/questions/53146031/portfolioanalytics-in-r-optimize-portfolio-results-in-nan-for-annualized-ret

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!