Create efficient frontier in PortfolioAnalytics without an xts object

十年热恋 提交于 2019-12-07 07:25:15

问题


Is there a way to create an efficient frontier in the PortfolioAnalytics package without specifying an xts object of asset returns? Instead I'd like to supply the vector of expected returns and the covariance matrix.


回答1:


There are two ways. First you can supply a list containing containing your matrices with the structure shown below and then call optimize.portfolio including this list as an argument.

# num_assets is the number of assets in the portfolio
  momentargs <- list()
  momentargs$mu <- matrix(0, nrow=num_assets, ncol=1 )
  momentargs$sigma <- matrix(0, nrow=num_assets, ncol=num_assets)
  momentargs$m3 <- matrix(0, nrow=num_assets, ncol=num_assets^2)
  momentargs$m4 <- matrix(0, nrow=num_assets, ncol=num_assets^3)

  optimize.portfolio(R, portfolio, momentargs=momentargs, ...)

Alternatively, you can supply your own function to calculate the moments. A simple example reproducing some of the PortfolioAnalytics options is shown below.

 set.portfolio.moments.user=function(R, portfolio, user_moments=NULL, user_method=c(returns, input, two_moment)) {
 #  
 #  Sets portfolio moments to user specified values
 #
 #  R               asset returns as in PortfoloAnalytics
 #  portfolio       a portfolio object as in PortfolioAnalytics
 #  user_moments    a list of four matices containing user-specified
 #                  values for the first four return moments
 #  user_method     user-specified method for computing moment matrices
 #                  defaults to calculation used by PortfolioAnalytics "sample" method
 #                  which uses PerformanceAnalytics functions to computer the higher-order moments

 if( !hasArg(user_method) | is.null(user_method)) user_method <- "returns" 
  tmpR <- R
  switch( user_method,  returns = { 
     momentargs <- list()
     momentargs$mu  <-  matrix(as.vector(apply(tmpR,2, "mean")), ncol = 1)
     momentargs$sigma  <-  cov(tmpR)
     momentargs$m3  <-  PerformanceAnalytics:::M3.MM(tmpR)
     momentargs$m4  <-  PerformanceAnalytics:::M4.MM(tmpR)
  }, input = {
     momentargs <- user_moments
  }, two_moment = {
     momentargs <- list()
     momentargs$mu <- matrix(as.vector(apply(tmpR,2, "mean")), ncol = 1)
     momentargs$sigma <- cov(tmpR)
     momentargs$m3 <- matrix(0, nrow=ncol(R), ncol=ncol(R)^2)
     momentargs$m4 <- matrix(0, nrow=ncol(R), ncol=ncol(R)^3)
   } )

   return(momentargs)
 }

You would then call PortfolioAnalytics with

 optimize.portfolio(R, portfolio, momentFUN = "set.portfolio.moments.user", ...)

as an example.



来源:https://stackoverflow.com/questions/26745976/create-efficient-frontier-in-portfolioanalytics-without-an-xts-object

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