简体   繁体   中英

R/optimize.portfolio {PortfolioAnalytics} gives only NA weights

I am having a question and can't think of a reasonable answer. I hope someone can help me! Many thanks in advance!

I am doing a portfolio optimization maximizing the Sharpe Ratio with a rolling window approach. Afterwards, I am writing the portfolio weights in a new Matrix.

With several datatsets, everything is working fine. Although with two other datasets, one issues arises: Two or three rows get only NA weights.

There should not be an issue with the data, because there are only so few NAs. Furthermore, there can't be an issue with one row of returns for the portfolio estimation as every row of returns is used several times in a rolling window approach. Do you maybe know why one row in particular could be filled with Nas?

Many thanks in advance and best regards,

Sebastian

# Kenneth French Dates
DateKFStart <- '1963-07' #'1969-07'
DateKFEnd   <- '2004-11' #'2017-06'

R.FF3=R.FF3[paste(DateKFStart, DateKFEnd, sep='/'),]
R.FF5=R.FF5[paste(DateKFStart, DateKFEnd, sep='/'),]
R.KFInd10=R.KFInd10[paste(DateKFStart, DateKFEnd, sep='/'),]
R.KFInd49=R.KFInd49[paste(DateKFStart, DateKFEnd, sep='/'),]
R.KFSibo=R.KFSibo[paste(DateKFStart, DateKFEnd, sep='/'),]

#--------------------#
#### Optimization ####
#--------------------#

#------------------------------#
assets=R.KFInd10               # R.FF3
is.estimation.window='Rolling' # 'Expanding'
M=60                           # Estimation Window Length


# Construct initial portfolio

assetnames=colnames(assets)
returns=assets

w.sharpe=returns*0
r.sharpe.NC=w.sharpe[,1]
colnames(r.sharpe.NC)='r.sharpe.NC'


sharpe.portf <- portfolio.spec(assets = assetnames)

sharpe.portf <- add.constraint(sharpe.portf, type= 'full_investment')
sharpe.portf <- add.constraint(sharpe.portf, type= 'long_only')

sharpe.portf <- add.objective(sharpe.portf, type='risk',   name='StdDev')
sharpe.portf <- add.objective(sharpe.portf, type='return', name='mean'  )

sharpe.portf

  for (n in (M+1):nrow(returns))
  {
  max_sharpe_opt <- optimize.portfolio(R=returns[(n-M):(n-1),],portfolio=sharpe.portf, 
                                       optimize_method='ROI',maxSR=TRUE, message=TRUE,trace=TRUE)
  w.sharpe[n,]=max_sharpe_opt$weights
  }

r.sharpe.NC[,1]=rowSums(returns*w.sharpe)

As already said, I don't know one single explanation. The whole code works for several datasets. And for only 2 two datasets, two rows of weights are NA, the rest works fine. So there should not be an issue with the data?

I get the same problem, and i think it's because it doesn't find a solution sometimes... Try the method "random", it will take more time but you will get something normally... Or try with the other method of optimization.

If you found something about your problem, please give me your solution.

max_sharpe_opt <- optimize.portfolio(R=returns[(n-M):(n-1),],portfolio=sharpe.portf, 
                                       optimize_method='random',maxSR=TRUE, message=TRUE)

or

max_sharpe_opt <- optimize.portfolio(R=returns[(n-M):(n-1),],portfolio=sharpe.portf, 
                                       optimize_method='GenSA',maxSR=TRUE, message=TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM