[Returnanalytics-commits] r2456 - in pkg/FactorAnalytics: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 28 00:39:32 CEST 2013


Author: chenyian
Date: 2013-06-28 00:39:32 +0200 (Fri, 28 Jun 2013)
New Revision: 2456

Added:
   pkg/FactorAnalytics/R/summary.FundamentalFactorModel.r
   pkg/FactorAnalytics/man/summary.FundamentalFactorModel.Rd
Modified:
   pkg/FactorAnalytics/R/factorModelCovariance.r
   pkg/FactorAnalytics/R/fitStatisticalFactorModel.R
Log:
1. modify factorModelCovariance.r
2. create skeleton of summary.FundamentalFactorModel.Rd and summary.FundamentalFactorModel.r

Modified: pkg/FactorAnalytics/R/factorModelCovariance.r
===================================================================
--- pkg/FactorAnalytics/R/factorModelCovariance.r	2013-06-27 22:14:44 UTC (rev 2455)
+++ pkg/FactorAnalytics/R/factorModelCovariance.r	2013-06-27 22:39:32 UTC (rev 2456)
@@ -1,61 +1,75 @@
-#' Compute Factor Model Covariance Matrix.
-#' 
-#' Compute asset return covariance matrix from factor model parameters.
-#' 
-#' The return on asset \code{i} (\code{i = 1,...,N}) is assumed to follow the
-#' factor model \cr \code{R(i,t) = alpha + t(beta)*F(t) + e(i,t), e(i,t) ~ iid
-#' (0, sig(i)^2)} \cr where \code{beta} is a \code{K x 1} vector of factor
-#' exposures. The return variance is then \cr \code{var(R(i,t) =
-#' t(beta)*var(F(t))*beta + sig(i)^2}, \cr and the \code{N x N} covariance
-#' matrix of the return vector \code{R} is \cr \code{var(R) = B*var(F(t))*t(B)
-#' + D} \cr where B is the \code{N x K} matrix of asset betas and \code{D} is a
-#' diagonal matrix with \code{sig(i)^2} values along the diagonal.
-#' 
-#' @param beta.mat \code{N x K} matrix of factor betas, where \code{N} is the
-#' number of assets and \code{K} is the number of factors.
-#' @param factor.cov \code{K x K} factor return covariance matrix.
-#' @param residVars.vec \code{N x 1} vector of asset specific residual
-#' variances from the factor model.
-#' @return \code{N x N} return covariance matrix based on factor model
-#' parameters.
-#' @author Eric Zivot and Yi-An Chen.
-#' @references Zivot, E. and J. Wang (2006), \emph{Modeling Financial Time
-#' Series with S-PLUS, Second Edition}, Springer-Verlag.
-#' @examples
-#' 
-#' # factorModelCovariance
-#' data(managers.df)
-#' factors    = managers.df[,(7:9)]
-#' ret.assets = managers.df[,(1:6)]
-#' fit <-fitMacroeconomicFactorModel(ret.assets,factors,fit.method="OLS",
-#'                                   variable.selection="all subsets", factor.set = 3)
-#' factorModelCovariance(fit$beta.mat,var(factors),fit$residVars.vec)
-#' 
-factorModelCovariance <-
-function(beta.mat, factor.cov, residVars.vec) {
-## Inputs:
-## beta.mat		   	n x k matrix of factor betas
-## factor.cov		  k x k factor return covariance matrix
-## residVars.vec  n x 1 vector of residual variances from factor model
-## Output:
-## cov.fm			    n x n return covariance matrix based on
-##				        estimated factor model.
-	beta.mat = as.matrix(beta.mat)
-	factor.cov = as.matrix(factor.cov)
-	sig.e = as.vector(residVars.vec)
-	if (length(sig.e) > 1) {
-	 D.e = diag(as.vector(sig.e))
-  } else {
-   D.e =  as.matrix(sig.e)
-  }
-	if (ncol(beta.mat) != ncol(factor.cov))
-		stop("beta.mat and factor.cov must have same number of columns")
-		
-	if (nrow(D.e) != nrow(beta.mat))
-		stop("beta.mat and D.e must have same number of rows")
-	cov.fm = beta.mat %*% factor.cov %*% t(beta.mat) + D.e
-	if (any(diag(chol(cov.fm)) == 0))
-    warning("Covariance matrix is not positive definite")
-	return(cov.fm)
-}
-
+#' Compute Factor Model Covariance Matrix.
+#' 
+#' Compute asset return covariance matrix from factor model parameters.
+#' 
+#' The return on asset \code{i} (\code{i = 1,...,N}) is assumed to follow the
+#' factor model \cr \code{R(i,t) = alpha + t(beta)*F(t) + e(i,t), e(i,t) ~ iid
+#' (0, sig(i)^2)} \cr where \code{beta} is a \code{K x 1} vector of factor
+#' exposures. The return variance is then \cr \code{var(R(i,t) =
+#' t(beta)*var(F(t))*beta + sig(i)^2}, \cr and the \code{N x N} covariance
+#' matrix of the return vector \code{R} is \cr \code{var(R) = B*var(F(t))*t(B)
+#' + D} \cr where B is the \code{N x K} matrix of asset betas and \code{D} is a
+#' diagonal matrix with \code{sig(i)^2} values along the diagonal.
+#' 
+#' @param beta \code{N x K} matrix of factor betas, where \code{N} is the
+#' number of assets and \code{K} is the number of factors.
+#' @param factor.cov \code{K x K} factor return covariance matrix.
+#' @param resid.variance \code{N x 1} vector of asset specific residual
+#' variances from the factor model.
+#' @return \code{N x N} return covariance matrix based on factor model
+#' parameters.
+#' @author Eric Zivot and Yi-An Chen.
+#' @references Zivot, E. and J. Wang (2006), \emph{Modeling Financial Time
+#' Series with S-PLUS, Second Edition}, Springer-Verlag.
+#' @examples
+#' 
+#' # Time Series model
+#' 
+#' data(managers.df)
+#' factors    = managers.df[,(7:9)]
+#' fit <- fitTimeseriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
+#'                                 factors.names=c("EDHEC.LS.EQ","SP500.TR"),
+#'                                 data=managers.df,fit.method="OLS")
+#' factorModelCovariance(fit$beta,var(factors),fit$resid.variance)
+#' 
+#' # Statistical Model
+#' data(stat.fm.data)
+#' fit <- fitStatisticalFactorModel(sfm.dat,k=2,
+#'                                   ckeckData.method="data.frame")
+#' 
+#' factorModelCovariance(t(sfm.pca.fit$loadings),var(sfm.pca.fit$factors),sfm.pca.fit$resid.variance)
+#' 
+#' sfm.apca.fit <- fitStatisticalFactorModel(sfm.apca.dat,k=2
+#' ,ckeckData.method="data.frame")
+#' 
+#' factorModelCovariance(t(sfm.apca.fit$loadings),
+#'                        var(sfm.apca.fit$factors),sfm.apca.fit$resid.variance)
+#'
+factorModelCovariance <-
+function(beta.mat, factor.cov, residVars.vec) {
+## Inputs:
+## beta.mat		   	n x k matrix of factor betas
+## factor.cov		  k x k factor return covariance matrix
+## residVars.vec  n x 1 vector of residual variances from factor model
+## Output:
+## cov.fm			    n x n return covariance matrix based on
+##				        estimated factor model.
+	beta.mat = as.matrix(beta.mat)
+	factor.cov = as.matrix(factor.cov)
+	sig.e = as.vector(residVars.vec)
+	if (length(sig.e) > 1) {
+	 D.e = diag(as.vector(sig.e))
+  } else {
+   D.e =  as.matrix(sig.e)
+  }
+	if (ncol(beta.mat) != ncol(factor.cov))
+		stop("beta.mat and factor.cov must have same number of columns")
+		
+	if (nrow(D.e) != nrow(beta.mat))
+		stop("beta.mat and D.e must have same number of rows")
+	cov.fm = beta.mat %*% factor.cov %*% t(beta.mat) + D.e
+	if (any(diag(chol(cov.fm)) == 0))
+    warning("Covariance matrix is not positive definite")
+	return(cov.fm)
+}
+

Modified: pkg/FactorAnalytics/R/fitStatisticalFactorModel.R
===================================================================
--- pkg/FactorAnalytics/R/fitStatisticalFactorModel.R	2013-06-27 22:14:44 UTC (rev 2455)
+++ pkg/FactorAnalytics/R/fitStatisticalFactorModel.R	2013-06-27 22:39:32 UTC (rev 2456)
@@ -303,7 +303,7 @@
 	dimnames(f) <- list(dimnames(data)[[1]], paste("F", 1:k, sep = "."))
 	names(alpha) <- data.names
 	resid <- t(t(data) - alpha) - f %*% B
-	r2 <- (1 - colSums(res^2)/colSums(xc^2))
+	r2 <- (1 - colSums(resid^2)/colSums(xc^2))
   
   if (ckeckData.method == "xts" | ckeckData.method == "zoo" ) {
     f <- xts(f,index(data.xts))

Added: pkg/FactorAnalytics/R/summary.FundamentalFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/summary.FundamentalFactorModel.r	                        (rev 0)
+++ pkg/FactorAnalytics/R/summary.FundamentalFactorModel.r	2013-06-27 22:39:32 UTC (rev 2456)
@@ -0,0 +1,18 @@
+#' summary method for FundamentalFactorModel
+#' 
+#' Generic function of summary method for fitTimeSeriesFactorModel.
+#' 
+#' @param fit it object created by fitFundamentalFactorModel.
+#' 
+#' @author Yi-An Chen
+#' 
+#' 
+#' 
+#' @export
+#' 
+
+summary.FundamentalFactorModel <- function(fit) {
+dim(fit$factors)
+print(fit$factors) 
+  
+}
\ No newline at end of file

Added: pkg/FactorAnalytics/man/summary.FundamentalFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/summary.FundamentalFactorModel.Rd	                        (rev 0)
+++ pkg/FactorAnalytics/man/summary.FundamentalFactorModel.Rd	2013-06-27 22:39:32 UTC (rev 2456)
@@ -0,0 +1,18 @@
+\name{summary.FundamentalFactorModel}
+\alias{summary.FundamentalFactorModel}
+\title{summary method for FundamentalFactorModel}
+\usage{
+  summary.FundamentalFactorModel(fit)
+}
+\arguments{
+  \item{fit}{it object created by
+  fitFundamentalFactorModel.}
+}
+\description{
+  Generic function of summary method for
+  fitTimeSeriesFactorModel.
+}
+\author{
+  Yi-An Chen
+}
+



More information about the Returnanalytics-commits mailing list