在没有xts对象的PortfolioAnalytics中创建有效的边界

时间:2014-11-04 22:07:27

标签: r finance portfolio quantitative-finance r-portfolioanalytics

有没有办法在PortfolioAnalytics包中创建有效的边界,而无需指定资产回报的xts对象?相反,我想提供预期收益的向量和协方差矩阵。

1 个答案:

答案 0 :(得分:6)

有两种方法。首先,您可以提供一个包含您的矩阵的列表,其结构如下所示,然后调用optimize.portfolio,包括此列表作为参数。

# 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, ...)

或者,您可以提供自己的功能来计算时刻。再现一些PortfolioAnalytics选项的简单示例如下所示。

 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)
 }

然后,您将使用

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

作为一个例子。