Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 预期收益和收益时间序列的协方差_R_Matlab_Portfolio - Fatal编程技术网

R 预期收益和收益时间序列的协方差

R 预期收益和收益时间序列的协方差,r,matlab,portfolio,R,Matlab,Portfolio,我试图模拟此处定义的Matlabewstats函数: Matlab给出的结果如下: > ExpReturn = 1×2 0.1995 0.1002 > ExpCovariance = 2×2 0.0032 -0.0017 -0.0017 0.0010 我试图用Riskr包复制这个例子: 我使用的R代码是: library(RiskPortfolios) rets <- as.matrix(cbind(c(0.24, 0.15, 0.27, 0.14),

我试图模拟此处定义的Matlab
ewstats
函数:

Matlab给出的结果如下:

> ExpReturn = 1×2
0.1995    0.1002

> ExpCovariance = 2×2
0.0032   -0.0017
-0.0017    0.0010
我试图用Riskr包复制这个例子:

我使用的R代码是:

library(RiskPortfolios)

rets <- as.matrix(cbind(c(0.24, 0.15, 0.27, 0.14), c(0.08, 0.13, 0.06, 0.13)))
w <- 0.98

rets
w
meanEstimation(rets, control = list(type = 'ewma', lambda = w))
covEstimation(rets, control = list(type = 'ewma', lambda = w))
我错过什么了吗?
谢谢

他们正在使用不同的算法。从RiskPortfolio手册:

ewma
。。。见RiskMetrics(1996年)

从Matlab hlp页面:

ewstats函数与RiskMetrics®方法之间没有关系,该方法用于确定收益时间序列的预期收益和协方差


不幸的是,Matlab没有告诉我们使用了哪种算法。

如果使用
type=“lw”
,他们给出了相同的答案:

round(covEstimation(rets, control = list(type = 'lw')), 4)

##   0.0032 -0.0017
##  -0.0017  0.0010

对于那些最终需要在R中使用等效的
ewstats
函数的人,这里是我编写的代码:

ewstats <- function(RetSeries, DecayFactor=NULL, WindowLength=NULL){

  #EWSTATS Expected return and covariance from return time series.  
  #   Optional exponential weighting emphasizes more recent data.
  # 
  #   [ExpReturn, ExpCovariance, NumEffObs] = ewstats(RetSeries, ...
  #                                           DecayFactor, WindowLength)
  #  
  #   Inputs:
  #     RetSeries : NUMOBS by NASSETS matrix of equally spaced incremental 
  #     return observations.  The first row is the oldest observation, and the
  #     last row is the most recent.
  #
  #     DecayFactor : Controls how much less each observation is weighted than its
  #     successor.  The k'th observation back in time has weight DecayFactor^k.
  #     DecayFactor must lie in the range: 0 < DecayFactor <= 1. 
  #     The default is DecayFactor = 1, which is the equally weighted linear
  #     moving average Model (BIS).  
  #
  #     WindowLength: The number of recent observations used in
  #     the computation.  The default is all NUMOBS observations.
  #
  #   Outputs:
  #     ExpReturn : 1 by NASSETS estimated expected returns.
  # 
  #     ExpCovariance : NASSETS by NASSETS estimated covariance matrix.  
  #
  #     NumEffObs: The number of effective observations is given by the formula:
  #     NumEffObs = (1-DecayFactor^WindowLength)/(1-DecayFactor).  Smaller
  #     DecayFactors or WindowLengths emphasize recent data more strongly, but
  #     use less of the available data set.
  #
  #   The standard deviations of the asset return processes are given by:
  #   STDVec = sqrt(diag(ECov)).  The correlation matrix is :
  #   CorrMat = VarMat./( STDVec*STDVec' )
  #
  #   See also MEAN, COV, COV2CORR.


  NumObs <- dim(RetSeries)[1]
  NumSeries <- dim(RetSeries)[2]

  # size the series and the window
  if (is.null(WindowLength)) {
    WindowLength <- NumObs
  }

  if (is.null(DecayFactor)) {
    DecayFactor = 1
  }


  if (DecayFactor <= 0 | DecayFactor > 1) {
    stop('Must have 0< decay factor <= 1.')
  }


  if (WindowLength > NumObs){
    stop(sprintf('Window Length #d must be <= number of observations #d',
                 WindowLength, NumObs))
  }


  # ------------------------------------------------------------------------
  # size the data to the window
  RetSeries <- RetSeries[NumObs-WindowLength+1:NumObs, ]

  # Calculate decay coefficients
  DecayPowers <- seq(WindowLength-1, 0, by = -1)
  VarWts <- sqrt(DecayFactor)^DecayPowers
  RetWts <- (DecayFactor)^DecayPowers

  NEff = sum(RetWts) # number of equivalent values in computation

  # Compute the exponentially weighted mean return
  WtSeries <- matrix(rep(RetWts, times = NumSeries),
                     nrow = length(RetWts), ncol = NumSeries) * RetSeries

  ERet <- colSums(WtSeries)/NEff;

  # Subtract the weighted mean from the original Series
  CenteredSeries <- RetSeries - matrix(rep(ERet, each = WindowLength),
                                       nrow = WindowLength, ncol = length(ERet))

  # Compute the weighted variance
  WtSeries <- matrix(rep(VarWts, times = NumSeries),
                     nrow = length(VarWts), ncol = NumSeries) * CenteredSeries

  ECov <- t(WtSeries) %*% WtSeries / NEff

  list(ExpReturn = ERet, ExpCovariance = ECov, NumEffObs = NEff)
}

ewstats我担心是这样的。我试着问这里使用的算法是什么:。但还是什么都没有。我想知道人们如何在不知道底层算法的情况下使用函数。奇怪的是“lw”方法不需要权重参数。ewstats函数使用它。这太奇怪了!
ewstats <- function(RetSeries, DecayFactor=NULL, WindowLength=NULL){

  #EWSTATS Expected return and covariance from return time series.  
  #   Optional exponential weighting emphasizes more recent data.
  # 
  #   [ExpReturn, ExpCovariance, NumEffObs] = ewstats(RetSeries, ...
  #                                           DecayFactor, WindowLength)
  #  
  #   Inputs:
  #     RetSeries : NUMOBS by NASSETS matrix of equally spaced incremental 
  #     return observations.  The first row is the oldest observation, and the
  #     last row is the most recent.
  #
  #     DecayFactor : Controls how much less each observation is weighted than its
  #     successor.  The k'th observation back in time has weight DecayFactor^k.
  #     DecayFactor must lie in the range: 0 < DecayFactor <= 1. 
  #     The default is DecayFactor = 1, which is the equally weighted linear
  #     moving average Model (BIS).  
  #
  #     WindowLength: The number of recent observations used in
  #     the computation.  The default is all NUMOBS observations.
  #
  #   Outputs:
  #     ExpReturn : 1 by NASSETS estimated expected returns.
  # 
  #     ExpCovariance : NASSETS by NASSETS estimated covariance matrix.  
  #
  #     NumEffObs: The number of effective observations is given by the formula:
  #     NumEffObs = (1-DecayFactor^WindowLength)/(1-DecayFactor).  Smaller
  #     DecayFactors or WindowLengths emphasize recent data more strongly, but
  #     use less of the available data set.
  #
  #   The standard deviations of the asset return processes are given by:
  #   STDVec = sqrt(diag(ECov)).  The correlation matrix is :
  #   CorrMat = VarMat./( STDVec*STDVec' )
  #
  #   See also MEAN, COV, COV2CORR.


  NumObs <- dim(RetSeries)[1]
  NumSeries <- dim(RetSeries)[2]

  # size the series and the window
  if (is.null(WindowLength)) {
    WindowLength <- NumObs
  }

  if (is.null(DecayFactor)) {
    DecayFactor = 1
  }


  if (DecayFactor <= 0 | DecayFactor > 1) {
    stop('Must have 0< decay factor <= 1.')
  }


  if (WindowLength > NumObs){
    stop(sprintf('Window Length #d must be <= number of observations #d',
                 WindowLength, NumObs))
  }


  # ------------------------------------------------------------------------
  # size the data to the window
  RetSeries <- RetSeries[NumObs-WindowLength+1:NumObs, ]

  # Calculate decay coefficients
  DecayPowers <- seq(WindowLength-1, 0, by = -1)
  VarWts <- sqrt(DecayFactor)^DecayPowers
  RetWts <- (DecayFactor)^DecayPowers

  NEff = sum(RetWts) # number of equivalent values in computation

  # Compute the exponentially weighted mean return
  WtSeries <- matrix(rep(RetWts, times = NumSeries),
                     nrow = length(RetWts), ncol = NumSeries) * RetSeries

  ERet <- colSums(WtSeries)/NEff;

  # Subtract the weighted mean from the original Series
  CenteredSeries <- RetSeries - matrix(rep(ERet, each = WindowLength),
                                       nrow = WindowLength, ncol = length(ERet))

  # Compute the weighted variance
  WtSeries <- matrix(rep(VarWts, times = NumSeries),
                     nrow = length(VarWts), ncol = NumSeries) * CenteredSeries

  ECov <- t(WtSeries) %*% WtSeries / NEff

  list(ExpReturn = ERet, ExpCovariance = ECov, NumEffObs = NEff)
}