我正在学习如何PortfolioAnalytics
在 R 中使用包来重新平衡每个季度的投资组合。我使用 package 收集了美国和中国 19 只大盘股的数据qmao
,然后计算了季度回报率。一切正常,直到我调用该函数optimize.portfolio.rebalancing()
。它不断给我以下错误消息,NaN
结果是Annualized Portfolio Rebalancing Return
:
VaR计算产生不可靠的结果(风险超过100%):1:1.12020244277128 ES计算产生不可靠的结果(风险超过100%):1:1.14791543855955 VaR计算产生不可靠的结果(风险超过100%):1: 1.13372707060527 ES 计算为列产生不可靠的结果(风险超过 100%):1:1.14960946412769
复制的完整代码
#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
我的问题:
(1) 在我看来,我们也不知道哪些给定的约束和目标会导致可行的解决方案(例如,我们可以有10
约束、2
简单的目标(最小化风险,并在给定一定风险水平的情况下最大化回报) ,然后结果是NaN
分配给19
上述股票的权重)。那么我们如何确定添加哪个约束以使其可行呢?
(2)如果我想每季度绘制一次有效边界,我该怎么做?我尝试这样做,meanSigma.ef = create.EfficientFrontier(R=pfMthRets, portfolio=portf.dn, type="mean-StdDev", n.portfolios = 25)
但我不断收到错误消息“seq.default 中的错误(来自 = minret,to = maxret,length.out = n.portfolios):'from' 不能是 NA、NaN 或无限。”