[Returnanalytics-commits] r2451 - pkg/FactorAnalytics/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Jun 27 00:05:01 CEST 2013
Author: chenyian
Date: 2013-06-27 00:05:00 +0200 (Thu, 27 Jun 2013)
New Revision: 2451
Modified:
pkg/FactorAnalytics/R/fitTimeSeriesFactorModel.R
pkg/FactorAnalytics/R/impliedFactorReturns.R
pkg/FactorAnalytics/R/predict.TimeSeriesFactorModel.r
Log:
1. modify predict.TimeSeriesFactorModel.r to adapt impliedFactorReturns.R
2. edit examples of impliedFactorReturns.R
3. add output of fitTimeSeriesFactorModel.R
Modified: pkg/FactorAnalytics/R/fitTimeSeriesFactorModel.R
===================================================================
--- pkg/FactorAnalytics/R/fitTimeSeriesFactorModel.R 2013-06-26 20:19:19 UTC (rev 2450)
+++ pkg/FactorAnalytics/R/fitTimeSeriesFactorModel.R 2013-06-26 22:05:00 UTC (rev 2451)
@@ -367,7 +367,9 @@
beta = Betas,
r2 = R2values,
resid.variance = ResidVars,
- call = this.call )
+ call = this.call,
+ data = data,
+ factors.names = factors.names)
class(ans) = "TimeSeriesFactorModel"
return(ans)
}
Modified: pkg/FactorAnalytics/R/impliedFactorReturns.R
===================================================================
--- pkg/FactorAnalytics/R/impliedFactorReturns.R 2013-06-26 20:19:19 UTC (rev 2450)
+++ pkg/FactorAnalytics/R/impliedFactorReturns.R 2013-06-26 22:05:00 UTC (rev 2451)
@@ -1,68 +1,71 @@
-#' Compute Implied Factor Returns Using Covariance Matrix Approach
-#'
-#' Compute risk factor conditional mean returns for a one group of risk factors
-#' given specified returns for another group of risk factors based on the
-#' assumption that all risk factor returns are multivariately normally
-#' distributed.
-#'
-#' Let \code{y} denote the \code{m x 1} vector of factor scenarios and \code{x}
-#' denote the \code{(n-m) x 1} vector of other factors. Assume that \code{(y',
-#' x')'} has a multivariate normal distribution with mean \code{(mu.y',
-#' mu.x')'} and covariance matrix partitioned as \code{(cov.yy, cov.yx, cov.xy,
-#' cov.xx)}. Then the implied factor scenarios are computed as \code{E[x|y] =
-#' mu.x + cov.xy*cov.xx^-1 * (y - mu.y)}
-#'
-#' @param factor.scenarios \code{m x 1} vector of factor mean returns of
-#' scenario. m is a subset of the n, where n is risk factors and \code{n > m}.
-#' @param mu.factors \code{n x 1} vector of factor mean returns.
-#' @param cov.factors \code{n x n} factor covariance matrix.
-#' @return \code{(n - m) x 1} vector of implied factor returns
-#' @author Eric Zivot and Yi-An Chen.
-#' @examples
-#'
-#' # get data
-#' data(managers.df)
-#' factors = managers.df[,(7:9)]
-#' # make up a factor mean returns scenario for factor SP500.TR
-#' factor.scenarios <- 0.001
-#' names(factor.scenarios) <- "SP500.TR"
-#' mu.factors <- mean(factors)
-#' cov.factors <- var(factors)
-#' # implied factor returns
-#' impliedFactorReturns(factor.scenarios,mu.factors,cov.factors)
-#'
-impliedFactorReturns <-
-function(factor.scenarios, mu.factors, cov.factors) {
-## inputs:
-## factor.scenarios m x 1 vector of factor mean returns of scenario. m is a subset of the n, where n is
-## risk factors and n > m.
-## mu.factors n x 1 vector of factor mean returns
-## cov.factors n x n factor covariance matrix
-## outputs:
-## (n - m) x 1 vector of implied factor returns
-## details
-## Let y denote the m x 1 vector of factor scenarios and x denote the (n-m) x 1
-## vector of other factors. Assume that (y', x')' has a multivariate normal
-## distribution with mean (mu.y', mu.x')' and covariance matrix
-##
-## | cov.yy, cov.yx |
-## | cov.xy, cov.xx |
-##
-## Then the implied factor scenarios are computed as
-##
-## E[x|y] = mu.x + cov.xy*cov.xx^-1 * (y - mu.y)
-##
- factor.names = colnames(cov.factors)
- scenario.names = names(factor.scenarios)
- non.scenario.names = setdiff(factor.names, scenario.names)
- # m x m matrix
- cov.scenarios = cov.factors[scenario.names, scenario.names]
- # (n-m) x m matrix
- cov.non.scenarios.scenarios = cov.factors[non.scenario.names, scenario.names]
- # compute (n-m) x 1 vector of implied factor returns from conditional distribution
- mu.non.scenarios = mu.factors[non.scenario.names] + cov.non.scenarios.scenarios %*% solve(cov.scenarios) %*% (factor.scenarios - mu.factors[scenario.names])
- mu.non.scenarios = as.numeric(mu.non.scenarios)
- names(mu.non.scenarios) = non.scenario.names
- return(mu.non.scenarios)
-}
-
+#' Compute Implied Factor Returns Using Covariance Matrix Approach
+#'
+#' Compute risk factor conditional mean returns for a one group of risk factors
+#' given specified returns for another group of risk factors based on the
+#' assumption that all risk factor returns are multivariately normally
+#' distributed.
+#'
+#' Let \code{y} denote the \code{m x 1} vector of factor scenarios and \code{x}
+#' denote the \code{(n-m) x 1} vector of other factors. Assume that \code{(y',
+#' x')'} has a multivariate normal distribution with mean \code{(mu.y',
+#' mu.x')'} and covariance matrix partitioned as \code{(cov.yy, cov.yx, cov.xy,
+#' cov.xx)}. Then the implied factor scenarios are computed as \code{E[x|y] =
+#' mu.x + cov.xy*cov.xx^-1 * (y - mu.y)}
+#'
+#' @param factor.scenarios m x 1 vector of scenario values for a subset
+#' of the n > m risk factors
+#' @param mu.factors \code{n x 1} vector of factor mean returns.
+#' @param cov.factors \code{n x n} factor covariance matrix.
+#' @return \code{(n - m) x 1} vector of implied factor returns
+#' @author Eric Zivot and Yi-An Chen.
+#' @examples
+#'
+#' # get data
+#' data(managers.df)
+#' factors = managers.df[,(7:9)]
+#' # make up a factor mean returns scenario for factor SP500.TR
+#' factor.scenarios <- 0.1
+#' names(factor.scenarios) <- "SP500.TR"
+#' mu.factors <- mean(factors)
+#' cov.factors <- var(factors)
+#' # implied factor returns
+#' impliedFactorReturns(factor.scenarios,mu.factors,cov.factors)
+#'
+impliedFactorReturns <-
+function(factor.scenarios, mu.factors, cov.factors) {
+## inputs:
+## factor.scenarios m x 1 vector of factor mean returns of scenario.
+## m is a subset of the n, where n is
+## risk factors and n > m.
+## mu.factors n x 1 vector of factor mean returns
+## cov.factors n x n factor covariance matrix
+## outputs:
+## (n - m) x 1 vector of implied factor returns
+## details
+## Let y denote the m x 1 vector of factor scenarios and x denote the (n-m) x 1
+## vector of other factors. Assume that (y', x')' has a multivariate normal
+## distribution with mean (mu.y', mu.x')' and covariance matrix
+##
+## | cov.yy, cov.yx |
+## | cov.xy, cov.xx |
+##
+## Then the implied factor scenarios are computed as
+##
+## E[x|y] = mu.x + cov.xy*cov.xx^-1 * (y - mu.y)
+##
+ factor.names = colnames(cov.factors)
+ scenario.names = names(factor.scenarios)
+ non.scenario.names = setdiff(factor.names, scenario.names)
+ # m x m matrix
+ cov.scenarios = cov.factors[scenario.names, scenario.names]
+ # (n-m) x m matrix
+ cov.non.scenarios.scenarios = cov.factors[non.scenario.names, scenario.names]
+ # compute (n-m) x 1 vector of implied factor returns from conditional distribution
+ mu.non.scenarios = mu.factors[non.scenario.names] +
+ cov.non.scenarios.scenarios %*% solve(cov.scenarios) %*%
+ (factor.scenarios - mu.factors[scenario.names])
+ mu.non.scenarios = as.numeric(mu.non.scenarios)
+ names(mu.non.scenarios) = non.scenario.names
+ return(mu.non.scenarios)
+}
+
Modified: pkg/FactorAnalytics/R/predict.TimeSeriesFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/predict.TimeSeriesFactorModel.r 2013-06-26 20:19:19 UTC (rev 2450)
+++ pkg/FactorAnalytics/R/predict.TimeSeriesFactorModel.r 2013-06-26 22:05:00 UTC (rev 2451)
@@ -25,6 +25,34 @@
#' @export
#'
-predict.TimeSeriesFactorModel <- function(fit,...){
- lapply(fit[[1]], predict,...)
+predict.TimeSeriesFactorModel <- function(fit,newdata,...){
+ if (missing(newdata) || is.null(newdata) ) {
+ lapply(fit$asset.fit, predict,...)
+ }
+ if ( !(missing(newdata) && !is.null(newdata) )) {
+ numAssets <- length(names(fit$asset.fit))
+
+ data <- fit$data
+ factors <- data[,fit$factors.names]
+ mu.factors <- apply(factors,2,mean)
+ cov.factors <- cov(factors)
+
+ for (i in 1:numAssets)
+ if (dim(newdata)[1] < length(residuals(fit$asset.fit[[1]])) ){
+
+
+ newdata <- data.frame(EDHEC.LS.EQ = rnorm(n=100), SP500.TR = rnorm(n=100) )
+ newdata.mat <- as.matrix(newdata)
+ factor.scenarios <- 0.001
+ names(factor.scenarios) <- "SP500.TR"
+
+ impliedFactorReturns(factor.scenarios, mu.factors, cov.factors)
+
+ }
+
+
+
+ }
+
+
}
\ No newline at end of file
More information about the Returnanalytics-commits
mailing list