[Returnanalytics-commits] r3928 - in pkg/FactorAnalytics: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Aug 8 20:42:16 CEST 2015
Author: pragnya
Date: 2015-08-08 20:42:15 +0200 (Sat, 08 Aug 2015)
New Revision: 3928
Modified:
pkg/FactorAnalytics/DESCRIPTION
pkg/FactorAnalytics/R/fitTsfm.R
pkg/FactorAnalytics/R/fmCov.R
pkg/FactorAnalytics/R/fmmcSemiParam.R
pkg/FactorAnalytics/man/fitTsfm.Rd
pkg/FactorAnalytics/man/fmCov.Rd
Log:
Updtaed Description; Add option to pass factor.cov to fmCov; Edits to fitTsfm, fmmcSemiParam.
Modified: pkg/FactorAnalytics/DESCRIPTION
===================================================================
--- pkg/FactorAnalytics/DESCRIPTION 2015-08-07 15:06:53 UTC (rev 3927)
+++ pkg/FactorAnalytics/DESCRIPTION 2015-08-08 18:42:15 UTC (rev 3928)
@@ -1,19 +1,16 @@
Package: factorAnalytics
Type: Package
Title: Factor Analytics
-Version:2.0.23
-Date:2015-07-24
+Version: 2.0.24
+Date: 2015-08-08
Author: Eric Zivot, Sangeetha Srinivasan and Yi-An Chen
Maintainer: Sangeetha Srinivasan <sangee at uw.edu>
-Description: An R package for the estimation and risk analysis of linear factor
- models for asset returns and portfolios. It contains model fitting methods
- for the three major types of factor models: time series (or, macroeconomic)
- factor model, fundamental factor model and statistical factor model. They
- allow for different types of distributions to be specified for modeling the
- fat-tailed behavior of financial returns, including Edgeworth expansions.
- Risk analysis measures such as VaR and ES, as well as performance
- attribution for factor models (factor-contributed vs idiosyncratic returns)
- are included.
+Description: Linear factor model fitting for asset returns (three major types-
+ time series, fundamental and statistical factor models); related risk
+ (volatility, VaR and ES) and performance attribution (factor-contributed vs
+ idiosyncratic returns); tabular displays of risk and performance reports;
+ factor model Monte Carlo, single and multiple imputation methods for
+ simulating returns and backfilling unequal histories.
License: GPL-2
Depends:
R (>= 3.0.0),
Modified: pkg/FactorAnalytics/R/fitTsfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.R 2015-08-07 15:06:53 UTC (rev 3927)
+++ pkg/FactorAnalytics/R/fitTsfm.R 2015-08-08 18:42:15 UTC (rev 3928)
@@ -27,7 +27,7 @@
#' \code{\link[leaps]{regsubsets}}; chooses the best performing subset of any
#' given size or within a range of subset sizes. Different methods such as
#' exhaustive search (default), forward or backward stepwise, or sequential
-#' replacement can be employed.See \code{\link{fitTsfm.control}} for more
+#' replacement can be employed. See \code{\link{fitTsfm.control}} for more
#' details on the control arguments.
#'
#' \code{variable.selection="lars"} corresponds to least angle regression
@@ -51,21 +51,19 @@
#' with \code{xts} objects internally and colnames won't be left as they are.
#' }
#'
-#' @param asset.names vector containing names of assets, whose returns or
-#' excess returns are the dependent variable.
-#' @param factor.names vector containing names of the macroeconomic factors.
+#' @param asset.names vector of asset names, whose returns are the dependent
+#' variable in the factor model.
+#' @param factor.names vector containing names of the factors.
#' @param mkt.name name of the column for market returns. Default is \code{NULL}.
-#' @param rf.name name of the column of risk free rate variable to calculate
-#' excess returns for all assets (in \code{asset.names}) and factors (in
-#' \code{factor.names}). Default is \code{NULL}, and no action is taken.
+#' @param rf.name name of the column for the risk free rate; if excess returns
+#' should be calculated for all assets and factors. Default is \code{NULL}.
#' @param data vector, matrix, data.frame, xts, timeSeries or zoo object
-#' containing column(s) named in \code{asset.names}, \code{factor.names} and
+#' containing the columns \code{asset.names}, \code{factor.names}, and
#' optionally, \code{mkt.name} and \code{rf.name}.
#' @param fit.method the estimation method, one of "LS", "DLS" or "Robust".
#' See details. Default is "LS".
#' @param variable.selection the variable selection method, one of "none",
#' "stepwise","subsets","lars". See details. Default is "none".
-#' \code{mkt.name} is required if any of these options are to be implemented.
#' @param control list of control parameters. Refer to
#' \code{\link{fitTsfm.control}} for details.
#' @param ... arguments passed to \code{\link{fitTsfm.control}}
@@ -76,7 +74,7 @@
#' The generic accessor functions \code{coef}, \code{fitted} and
#' \code{residuals} extract various useful features of the fit object.
#' Additionally, \code{fmCov} computes the covariance matrix for asset returns
-#' based on the fitted factor model
+#' based on the fitted factor model.
#'
#' An object of class \code{"tsfm"} is a list containing the following
#' components:
Modified: pkg/FactorAnalytics/R/fmCov.R
===================================================================
--- pkg/FactorAnalytics/R/fmCov.R 2015-08-07 15:06:53 UTC (rev 3927)
+++ pkg/FactorAnalytics/R/fmCov.R 2015-08-08 18:42:15 UTC (rev 3928)
@@ -1,128 +1,132 @@
-#' @title Covariance Matrix for assets' returns from fitted factor model.
-#'
-#' @description Computes the covariance matrix for assets' returns based on a
-#' fitted factor model. This is a generic function with methods for classes
-#' \code{tsfm}, \code{sfm} and \code{ffm}.
-#'
-#' @details \code{R(i, t)}, the return on asset \code{i} at time \code{t},
-#' is assumed to follow a factor model of the form, \cr \cr
-#' \code{R(i,t) = alpha(i) + beta(i)*f(t) + e(i,t)}, \cr \cr
-#' where, \code{alpha(i)} is the intercept, \code{f(t)} is a {K x 1} vector of
-#' factor returns at time \code{t}, \code{beta(i)} is a \code{1 x K} vector of
-#' factor exposures and the error terms \code{e(i,t)} are serially
-#' uncorrelated across time and contemporaneously uncorrelated across assets
-#' so that \code{e(i,t) ~ iid(0,sig(i)^2)}. Thus, the variance of asset
-#' \code{i}'s return is given by \cr \cr
-#' \code{var(R(i)) = beta(i)*cov(F)*tr(beta(i)) + sig(i)^2}. \cr \cr
-#' And, the \code{N x N} covariance matrix of asset returns is \cr \cr
-#' \code{var(R) = B*cov(F)*tr(B) + D}, \cr \cr
-#' where, B is the \code{N x K} matrix of factor betas and \code{D} is a
-#' diagonal matrix with \code{sig(i)^2} along the diagonal.
-#'
-#' The method for computing covariance can be specified via the \dots
-#' argument. Note that the default of \code{use="pairwise.complete.obs"} for
-#' handling NAs restricts the method to "pearson".
-#'
-#' @param object fit object of class \code{tsfm}, \code{sfm} or \code{ffm}.
-#' @param use an optional character string giving a method for computing
-#' covariances in the presence of missing values. This must be (an
-#' abbreviation of) one of the strings "everything", "all.obs",
-#' "complete.obs", "na.or.complete", or "pairwise.complete.obs". Default is
-#' "pairwise.complete.obs".
-#' @param ... optional arguments passed to \code{\link[stats]{cov}}.
-#'
-#' @return The computed \code{N x N} covariance matrix for asset returns based
-#' on the fitted factor model.
-#'
-#' @author Eric Zivot, Yi-An Chen and Sangeetha Srinivasan.
-#'
-#' @references
-#' Zivot, E., & Jia-hui, W. A. N. G. (2006). Modeling Financial Time
-#' Series with S-Plus Springer-Verlag.
-#'
-#' @seealso \code{\link{fitTsfm}}, \code{\link{fitSfm}}, \code{\link{fitFfm}}
-#'
-#' \code{\link[stats]{cov}} for more details on arguments \code{use} and
-#' \code{method}.
-#'
-#' @examples
-#' # Time Series Factor model
-#' data(managers)
-#' fit <- fitTsfm(asset.names=colnames(managers[, (1:6)]),
-#' factor.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers)
-#' fmCov(fit)
-#'
-#' # Statistical Factor Model
-#' data(StockReturns)
-#' sfm.pca.fit <- fitSfm(r.M, k=2)
-#' fmCov(sfm.pca.fit)
-#'
-#' \dontrun{
-#' # Fundamental Factor Model
-#' data(stock)
-#' # there are 447 assets
-#' exposure.names <- c("BOOK2MARKET", "LOG.MARKETCAP")
-#' beta.mat <- subset(stock, DATE=="2003-12-31")[, exposure.names]
-#' beta.mat1 <- cbind(rep(1, 447), beta.mat1)
-#' # FM return covariance
-#' fit.fund <- fitFfm(exposure.names=c("BOOK2MARKET", "LOG.MARKETCAP"),
-#' data=stock, returnsvar="RETURN", datevar="DATE",
-#' assetvar="TICKER", wls=TRUE, regression="classic",
-#' covariance="classic", full.resid.cov=FALSE)
-#' ret.cov.fundm <- fmCov(beta.mat1,fit.fund$factor.cov$cov,fit.fund$resid.sd)
-#' fit.fund$returns.cov$cov == ret.cov.fundm
-#' }
-#'
-#' @rdname fmCov
-#' @export
-
-fmCov <- function(object, ...){
- # check input object validity
- if (!inherits(object, c("tsfm", "sfm", "ffm"))) {
- stop("Invalid argument: Object should be of class 'tsfm', 'sfm' or 'ffm'.")
- }
- UseMethod("fmCov")
-}
-
-#' @rdname fmCov
-#' @method fmCov tsfm
-#' @export
-
-fmCov.tsfm <- function(object, use="pairwise.complete.obs", ...) {
-
- # get parameters and factors from factor model
- beta <- as.matrix(object$beta)
- # convert NAs to 0 to enable matrix multiplication
- beta[is.na(beta)] <- 0
- sig2.e = object$resid.sd^2
- factor <- as.matrix(object$data[, object$factor.names])
-
- # factor covariance matrix
- factor.cov = cov(factor, use=use, ...)
-
- # residual covariance matrix D
- if (length(sig2.e) > 1) {
- D.e = diag(sig2.e)
- } else {
- D.e = as.vector(sig2.e)
- }
-
- cov.fm = beta %*% factor.cov %*% t(beta) + D.e
-
- if (any(diag(chol(cov.fm))==0)) {
- warning("Covariance matrix is not positive definite!")
- }
-
- return(cov.fm)
-}
-
-#' @rdname fmCov
-#' @method fmCov sfm
-#' @export
-
-fmCov.sfm <- function(object, use="pairwise.complete.obs", ...) {
-
- # already computed via fitSfm function
- return(object$Omega)
-}
-
+#' @title Covariance Matrix for assets' returns from fitted factor model.
+#'
+#' @description Computes the covariance matrix for assets' returns based on a
+#' fitted factor model. This is a generic function with methods for classes
+#' \code{tsfm}, \code{sfm} and \code{ffm}.
+#'
+#' @details \code{R(i, t)}, the return on asset \code{i} at time \code{t},
+#' is assumed to follow a factor model of the form, \cr \cr
+#' \code{R(i,t) = alpha(i) + beta(i)*f(t) + e(i,t)}, \cr \cr
+#' where, \code{alpha(i)} is the intercept, \code{f(t)} is a {K x 1} vector of
+#' factor returns at time \code{t}, \code{beta(i)} is a \code{1 x K} vector of
+#' factor exposures and the error terms \code{e(i,t)} are serially
+#' uncorrelated across time and contemporaneously uncorrelated across assets
+#' so that \code{e(i,t) ~ iid(0,sig(i)^2)}. Thus, the variance of asset
+#' \code{i}'s return is given by \cr \cr
+#' \code{var(R(i)) = beta(i)*cov(F)*tr(beta(i)) + sig(i)^2}. \cr \cr
+#' And, the \code{N x N} covariance matrix of asset returns is \cr \cr
+#' \code{var(R) = B*cov(F)*tr(B) + D}, \cr \cr
+#' where, B is the \code{N x K} matrix of factor betas and \code{D} is a
+#' diagonal matrix with \code{sig(i)^2} along the diagonal.
+#'
+#' The method for computing covariance can be specified via the \dots
+#' argument. Note that the default of \code{use="pairwise.complete.obs"} for
+#' handling NAs restricts the method to "pearson".
+#'
+#' @param object fit object of class \code{tsfm}, \code{sfm} or \code{ffm}.
+#' @param factor.cov factor covariance matrix (optional); defaults to the
+#' sample covariance matrix.
+#' @param use method for computing covariances in the presence of missing
+#' values; one of "everything", "all.obs", "complete.obs", "na.or.complete", or
+#' "pairwise.complete.obs". Default is "pairwise.complete.obs".
+#' @param ... optional arguments passed to \code{\link[stats]{cov}}.
+#'
+#' @return The computed \code{N x N} covariance matrix for asset returns based
+#' on the fitted factor model.
+#'
+#' @author Eric Zivot, Yi-An Chen and Sangeetha Srinivasan.
+#'
+#' @references
+#' Zivot, E., & Jia-hui, W. A. N. G. (2006). Modeling Financial Time
+#' Series with S-Plus Springer-Verlag.
+#'
+#' @seealso \code{\link{fitTsfm}}, \code{\link{fitSfm}}, \code{\link{fitFfm}}
+#'
+#' \code{\link[stats]{cov}} for more details on arguments \code{use} and
+#' \code{method}.
+#'
+#' @examples
+#' # Time Series Factor model
+#' data(managers)
+#' fit <- fitTsfm(asset.names=colnames(managers[, (1:6)]),
+#' factor.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers)
+#' fmCov(fit)
+#'
+#' # Statistical Factor Model
+#' data(StockReturns)
+#' sfm.pca.fit <- fitSfm(r.M, k=2)
+#' fmCov(sfm.pca.fit)
+#'
+#' \dontrun{
+#' # Fundamental Factor Model
+#' data(stock)
+#' # there are 447 assets
+#' exposure.names <- c("BOOK2MARKET", "LOG.MARKETCAP")
+#' beta.mat <- subset(stock, DATE=="2003-12-31")[, exposure.names]
+#' beta.mat1 <- cbind(rep(1, 447), beta.mat1)
+#' # FM return covariance
+#' fit.fund <- fitFfm(exposure.names=c("BOOK2MARKET", "LOG.MARKETCAP"),
+#' data=stock, returnsvar="RETURN", datevar="DATE",
+#' assetvar="TICKER", wls=TRUE, regression="classic",
+#' covariance="classic", full.resid.cov=FALSE)
+#' ret.cov.fundm <- fmCov(beta.mat1,fit.fund$factor.cov$cov,fit.fund$resid.sd)
+#' fit.fund$returns.cov$cov == ret.cov.fundm
+#' }
+#'
+#' @rdname fmCov
+#' @export
+
+fmCov <- function(object, ...){
+ # check input object validity
+ if (!inherits(object, c("tsfm", "sfm", "ffm"))) {
+ stop("Invalid argument: Object should be of class 'tsfm', 'sfm' or 'ffm'.")
+ }
+ UseMethod("fmCov")
+}
+
+#' @rdname fmCov
+#' @method fmCov tsfm
+#' @export
+
+fmCov.tsfm <- function(object, factor.cov, use="pairwise.complete.obs", ...) {
+
+ # get parameters and factors from factor model
+ beta <- as.matrix(object$beta)
+ # convert NAs to 0 to enable matrix multiplication
+ beta[is.na(beta)] <- 0
+ sig2.e = object$resid.sd^2
+ factor <- as.matrix(object$data[, object$factor.names])
+
+ # factor covariance matrix
+ if (missing(factor.cov)) {
+ factor.cov = cov(factor, use=use, ...)
+ } else {
+ identical(dim(factor.cov), as.integer(c(ncol(factor), ncol(factor))))
+ }
+
+ # residual covariance matrix D
+ if (length(sig2.e) > 1) {
+ D.e = diag(sig2.e)
+ } else {
+ D.e = as.vector(sig2.e)
+ }
+
+ cov.fm = beta %*% factor.cov %*% t(beta) + D.e
+
+ if (any(diag(chol(cov.fm))==0)) {
+ warning("Covariance matrix is not positive definite!")
+ }
+
+ return(cov.fm)
+}
+
+#' @rdname fmCov
+#' @method fmCov sfm
+#' @export
+
+fmCov.sfm <- function(object, use="pairwise.complete.obs", ...) {
+
+ # already computed via fitSfm function
+ return(object$Omega)
+}
+
Modified: pkg/FactorAnalytics/R/fmmcSemiParam.R
===================================================================
--- pkg/FactorAnalytics/R/fmmcSemiParam.R 2015-08-07 15:06:53 UTC (rev 3927)
+++ pkg/FactorAnalytics/R/fmmcSemiParam.R 2015-08-08 18:42:15 UTC (rev 3928)
@@ -102,16 +102,18 @@
} else {
fund.names <- rownames(beta)
N = nrow(beta)
- if (colnames(beta) != factor.names) {
+ if (all(colnames(beta)!=factor.names) || ncol(beta)!=length(factor.names)) {
stop("Invalid argument: beta and factor.ret should correspond to the same
set of factors")
}
}
resid.dist = resid.dist[1]
- if (!(resid.dist %in% c("normal","Cornish-Fisher","skew-t"))) {
- stop("Invalid argument: resid.dist must be 'normal','Cornish-Fisher' or
- 'skew-t'")
- }
+ switch(resid.dist,
+ "normal" = {if (ncol(resid.par)!=1) {stop("Invalid argument: resid.par")}},
+ "Cornish-Fisher" = {if (ncol(resid.par)!=3) {stop("Invalid argument: resid.par")}},
+ "skew-t" = {if (ncol(resid.par)!=4) {stop("Invalid argument: resid.par")}},
+ stop("Invalid argument: resid.dist must be 'normal', 'Cornish-Fisher' or 'skew-t'")
+ )
boot.method = boot.method[1]
if (!(boot.method %in% c("random","block"))) {
stop("Invalid argument: boot.method must be either 'random' or 'block'")
@@ -155,7 +157,7 @@
"normal" = {sim.resid[,i] <- rnorm(n=B, mean=0, sd=resid.par[i,]) }, # Bx1
"Cornish-Fisher" = {sim.resid[,i] <- rCornishFisher(n=B, dp=resid.par[i,])},
"skew-t" = {sim.resid[,i] <- rst(n=B, dp=resid.par[i,])}
- )
+ )
sim.fund.ret[,i] =
alpha[i,1] + boot.factor.ret %*% t(beta[i,,drop=FALSE]) + sim.resid[,i] # Bx1
}
Modified: pkg/FactorAnalytics/man/fitTsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitTsfm.Rd 2015-08-07 15:06:53 UTC (rev 3927)
+++ pkg/FactorAnalytics/man/fitTsfm.Rd 2015-08-08 18:42:15 UTC (rev 3928)
@@ -19,27 +19,25 @@
\method{residuals}{tsfm}(object, ...)
}
\arguments{
-\item{asset.names}{vector containing names of assets, whose returns or
-excess returns are the dependent variable.}
+\item{asset.names}{vector of asset names, whose returns are the dependent
+variable in the factor model.}
-\item{factor.names}{vector containing names of the macroeconomic factors.}
+\item{factor.names}{vector containing names of the factors.}
\item{mkt.name}{name of the column for market returns. Default is \code{NULL}.}
-\item{rf.name}{name of the column of risk free rate variable to calculate
-excess returns for all assets (in \code{asset.names}) and factors (in
-\code{factor.names}). Default is \code{NULL}, and no action is taken.}
+\item{rf.name}{name of the column for the risk free rate; if excess returns
+should be calculated for all assets and factors. Default is \code{NULL}.}
\item{data}{vector, matrix, data.frame, xts, timeSeries or zoo object
-containing column(s) named in \code{asset.names}, \code{factor.names} and
+containing the columns \code{asset.names}, \code{factor.names}, and
optionally, \code{mkt.name} and \code{rf.name}.}
\item{fit.method}{the estimation method, one of "LS", "DLS" or "Robust".
See details. Default is "LS".}
\item{variable.selection}{the variable selection method, one of "none",
-"stepwise","subsets","lars". See details. Default is "none".
-\code{mkt.name} is required if any of these options are to be implemented.}
+"stepwise","subsets","lars". See details. Default is "none".}
\item{control}{list of control parameters. Refer to
\code{\link{fitTsfm.control}} for details.}
@@ -56,7 +54,7 @@
The generic accessor functions \code{coef}, \code{fitted} and
\code{residuals} extract various useful features of the fit object.
Additionally, \code{fmCov} computes the covariance matrix for asset returns
-based on the fitted factor model
+based on the fitted factor model.
An object of class \code{"tsfm"} is a list containing the following
components:
@@ -108,7 +106,7 @@
\code{\link[leaps]{regsubsets}}; chooses the best performing subset of any
given size or within a range of subset sizes. Different methods such as
exhaustive search (default), forward or backward stepwise, or sequential
-replacement can be employed.See \code{\link{fitTsfm.control}} for more
+replacement can be employed. See \code{\link{fitTsfm.control}} for more
details on the control arguments.
\code{variable.selection="lars"} corresponds to least angle regression
Modified: pkg/FactorAnalytics/man/fmCov.Rd
===================================================================
--- pkg/FactorAnalytics/man/fmCov.Rd 2015-08-07 15:06:53 UTC (rev 3927)
+++ pkg/FactorAnalytics/man/fmCov.Rd 2015-08-08 18:42:15 UTC (rev 3928)
@@ -8,7 +8,7 @@
\usage{
fmCov(object, ...)
-\method{fmCov}{tsfm}(object, use = "pairwise.complete.obs", ...)
+\method{fmCov}{tsfm}(object, factor.cov, use = "pairwise.complete.obs", ...)
\method{fmCov}{sfm}(object, use = "pairwise.complete.obs", ...)
}
@@ -17,11 +17,12 @@
\item{...}{optional arguments passed to \code{\link[stats]{cov}}.}
-\item{use}{an optional character string giving a method for computing
-covariances in the presence of missing values. This must be (an
-abbreviation of) one of the strings "everything", "all.obs",
-"complete.obs", "na.or.complete", or "pairwise.complete.obs". Default is
-"pairwise.complete.obs".}
+\item{factor.cov}{factor covariance matrix (optional); defaults to the
+sample covariance matrix.}
+
+\item{use}{method for computing covariances in the presence of missing
+values; one of "everything", "all.obs", "complete.obs", "na.or.complete", or
+"pairwise.complete.obs". Default is "pairwise.complete.obs".}
}
\value{
The computed \code{N x N} covariance matrix for asset returns based
More information about the Returnanalytics-commits
mailing list