[Returnanalytics-commits] r3502 - in pkg/FactorAnalytics: R man vignettes
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Aug 8 04:38:35 CEST 2014
Author: pragnya
Date: 2014-08-08 04:38:34 +0200 (Fri, 08 Aug 2014)
New Revision: 3502
Added:
pkg/FactorAnalytics/vignettes/FA.bib
pkg/FactorAnalytics/vignettes/fitTsfm.pdf
pkg/FactorAnalytics/vignettes/fundamentalFM.pdf
Modified:
pkg/FactorAnalytics/R/fitTsfm.R
pkg/FactorAnalytics/R/fmCov.R
pkg/FactorAnalytics/R/fmEsDecomp.R
pkg/FactorAnalytics/R/fmSdDecomp.R
pkg/FactorAnalytics/R/fmVaRDecomp.R
pkg/FactorAnalytics/R/plot.tsfm.r
pkg/FactorAnalytics/R/predict.tsfm.r
pkg/FactorAnalytics/R/print.tsfm.r
pkg/FactorAnalytics/R/summary.tsfm.r
pkg/FactorAnalytics/man/fitTsfm.Rd
pkg/FactorAnalytics/man/fmCov.Rd
pkg/FactorAnalytics/man/fmEsDecomp.Rd
pkg/FactorAnalytics/man/fmSdDecomp.Rd
pkg/FactorAnalytics/man/fmVaRDecomp.Rd
pkg/FactorAnalytics/man/plot.tsfm.Rd
Log:
Added vignette for fitTsfm, fixed some typos
Modified: pkg/FactorAnalytics/R/fitTsfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.R 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/R/fitTsfm.R 2014-08-08 02:38:34 UTC (rev 3502)
@@ -26,20 +26,20 @@
#' improves. And, "subsets" enables subsets selection using
#' \code{\link[leaps]{regsubsets}}; chooses the best performing subset of any
#' given size. See \code{\link{fitTsfm.control}} for more details on the
-#' control arguments. \code{varaible.selection="lars"} corresponds to least
+#' control arguments. \code{variable.selection="lars"} corresponds to least
#' angle regression using \code{\link[lars]{lars}} with variants "lasso",
#' "lar", "forward.stagewise" or "stepwise". Note: If
#' \code{variable.selection="lars"}, \code{fit.method} will be ignored.
#'
-#' \code{mkt.timing} allows for market-timing factors to be added to any of the
-#' above methods. Market timing accounts for the price movement of the general
-#' stock market relative to fixed income securities. "HM" follows
-#' Henriksson & Merton (1981) and \code{up-market=max(0,Rm-Rf)}, is added to
-#' the regression. The coefficient of this up-market factor can be
-#' interpreted as the number of free put options. Similarly, "TM" follows
-#' Treynor-Mazuy (1966), to account for market timing with respect to
-#' volatility, and \code{market.sqd=(Rm-Rf)^2} is added as a factor in the
-#' regression. Option "both" adds both of these factors.
+#' Arguments \code{mkt.name} and \code{mkt.timing} allow for market-timing
+#' factors to be added to any of the above methods. Market timing accounts for
+#' the price movement of the general stock market relative to fixed income
+#' securities. "HM" follows Henriksson & Merton (1981) and
+#' \code{up.market=max(0,Rm-Rf)}, is added to the regression. The coefficient
+#' of this up-market factor can be interpreted as the number of free put
+#' options. Similarly, "TM" follows Treynor-Mazuy (1966), to account for market
+#' timing with respect to volatility, and \code{market.sqd=(Rm-Rf)^2} is added
+#' as a factor in the regression. Option "both" adds both of these factors.
#'
#' \subsection{Data Processing}{
#'
@@ -56,9 +56,8 @@
#' @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 mkt.name name of the column for market excess returns (Rm-Rf).
-#' Is required if \code{mkt.timing} or \code{add.market.sqd}
-#' are \code{TRUE}. Default is NULL.
+#' @param mkt.name name of the column for market excess returns (Rm-Rf); this
+#' is necessary to add market timing factors. Default is 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 NULL, and no action is taken.
@@ -69,8 +68,8 @@
#' See details. Default is "OLS".
#' @param variable.selection the variable selection method, one of "none",
#' "stepwise","subsets","lars". See details. Default is "none".
-#' @param mkt.timing one of "HM", "TM" or "both". Default is NULL. See Details.
-#' \code{mkt.name} is required if any of these options are specified.
+#' @param mkt.timing one of "HM", "TM" or "both". Default is NULL. See Details.
+#' \code{mkt.name} is required if any of these options are to be implemented.
#' @param control list of control parameters. The default is constructed by
#' the function \code{\link{fitTsfm.control}}. See the documentation for
#' \code{\link{fitTsfm.control}} for details.
@@ -165,8 +164,8 @@
#' @export
fitTsfm <- function(asset.names, factor.names, mkt.name=NULL, rf.name=NULL,
- data=data, fit.method=c("OLS","DLS","Robust"),
- variable.selection=c("none","stepwise","subsets","lars"),
+ data=data, fit.method=c("OLS","DLS","Robust"),
+ variable.selection=c("none","stepwise","subsets","lars"),
mkt.timing=NULL, control=fitTsfm.control(...), ...) {
# record the call as an element to be returned
@@ -230,7 +229,7 @@
}
# opt add mkt-timing factors: up.market=max(0,Rm-Rf), market.sqd=(Rm-Rf)^2
- if (!is.null(mkt.timing)) {
+ if (!is.null(mkt.name)) {
if(mkt.timing=="HM" || mkt.timing=="both") {
up.market <- data.xts[,mkt.name]
up.market [up.market < 0] <- 0
@@ -277,9 +276,10 @@
# from returned factor model fits above
coef.mat <- makePaddedDataFrame(lapply(reg.list, coef))
alpha <- coef.mat[, 1, drop=FALSE]
- # to make class of alpha numeric instead of matrix
- # aplha <- coef.mat[,1]
+ # to get alpha of class numeric, do: aplha <- coef.mat[,1]
beta <- coef.mat[, -1, drop=FALSE]
+ # reorder the columns to match factor names vector
+ beta <- subset(beta, select=factor.names)
r2 <- sapply(reg.list, function(x) summary(x)$r.squared)
resid.sd <- sapply(reg.list, function(x) summary(x)$sigma)
# create list of return values.
@@ -493,14 +493,10 @@
#' @export
coef.tsfm <- function(object, ...) {
- if (object$variable.selection=="lars") {
- # generic method 'coef' does not exist for "lars" fit objects
- # so, use cbind to form coef matrix
- coef.mat <- cbind(object$alpha, object$beta)
- colnames(coef.mat)[1] <- "(Intercept)"
- } else {
- coef.mat <- t(sapply(object$asset.fit, coef, ...))
- }
+ # cbind alpha and beta; works for all fit and var selection methods
+ coef.mat <- cbind(object$alpha, object$beta)
+ # name for alpha/intercept column
+ colnames(coef.mat)[1] <- "(Intercept)"
return(coef.mat)
}
Modified: pkg/FactorAnalytics/R/fmCov.R
===================================================================
--- pkg/FactorAnalytics/R/fmCov.R 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/R/fmCov.R 2014-08-08 02:38:34 UTC (rev 3502)
@@ -6,16 +6,16 @@
#'
#' @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*F(t) + e(i,t)}, \cr \cr
-#' where, \code{alpha(i)} is the intercept, \code{F(t)} is a {K x 1} vector of
-#' the \code{K} factor values at time \code{t}, \code{beta} is a \code{1 x K}
-#' vector of factor exposures and the error terms \code{e(i,t)} are serially
+#' \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,t)) = beta*var(F(t))*tr(beta) + sig(i)^2}. \cr \cr
-#' And, the \code{N x N} covariance matrix of N asset returns is \cr \cr
-#' \code{var(R) = B*var(F(t))*tr(B) + D}, \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.
#'
@@ -98,6 +98,7 @@
# 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])
Modified: pkg/FactorAnalytics/R/fmEsDecomp.R
===================================================================
--- pkg/FactorAnalytics/R/fmEsDecomp.R 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/R/fmEsDecomp.R 2014-08-08 02:38:34 UTC (rev 3502)
@@ -8,8 +8,8 @@
#' simulated data.
#'
#' @details The factor model for an asset's return at time \code{t} has the
-#' form \cr \cr \code{R(t) = beta'F(t) + e(t) = beta.star'F.star(t)} \cr \cr
-#' where, \code{beta.star=(beta,sig.e)} and \code{F.star(t)=[F(t)',z(t)]'}. By
+#' form \cr \cr \code{R(t) = beta'f(t) + e(t) = beta.star'f.star(t)} \cr \cr
+#' where, \code{beta.star=(beta,sig.e)} and \code{f.star(t)=[f(t)',z(t)]'}. By
#' Euler's theorem, the ES of the asset's return is given by:
#' \cr \cr \code{ES.fm = sum(cES_k) = sum(beta.star_k*mES_k)} \cr \cr
#' where, summation is across the \code{K} factors and the residual,
@@ -105,7 +105,9 @@
}
# get beta.star
- beta.star <- as.matrix(cbind(object$beta, object$resid.sd))
+ beta <- object$beta
+ beta[is.na(beta)] <- 0
+ beta.star <- as.matrix(cbind(beta, object$resid.sd))
colnames(beta.star)[dim(beta.star)[2]] <- "residual"
# factor returns and residuals data
@@ -155,13 +157,13 @@
mES[i,] <- inv * colMeans(factor.star[idx,], na.rm =TRUE)
# correction factor to ensure that sum(cES) = portfolio ES
- cf <- as.numeric( ES.fm[i] / sum(mES[i,]*beta.star[i,]) )
+ cf <- as.numeric( ES.fm[i] / sum(mES[i,]*beta.star[i,], na.rm=TRUE) )
# compute marginal, component and percentage contributions to ES
# each of these have dimensions: N x (K+1)
mES[i,] <- cf * mES[i,]
cES[i,] <- mES[i,] * beta.star[i,]
- pcES[i,] <- cES[i,] / ES.fm[i]
+ pcES[i,] <- 100* cES[i,] / ES.fm[i]
}
fm.ES.decomp <- list(VaR.fm=VaR.fm, n.exceed=n.exceed, idx.exceed=idx.exceed,
Modified: pkg/FactorAnalytics/R/fmSdDecomp.R
===================================================================
--- pkg/FactorAnalytics/R/fmSdDecomp.R 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/R/fmSdDecomp.R 2014-08-08 02:38:34 UTC (rev 3502)
@@ -4,8 +4,8 @@
#' assets' returns based on Euler's theorem, given the fitted factor model.
#'
#' @details The factor model for an asset's return at time \code{t} has the
-#' form \cr \cr \code{R(t) = beta'F(t) + e(t) = beta.star'F.star(t)} \cr \cr
-#' where, \code{beta.star=(beta,sig.e)} and \code{F.star(t)=[F(t)',z(t)]'}.
+#' form \cr \cr \code{R(t) = beta'f(t) + e(t) = beta.star'f.star(t)} \cr \cr
+#' where, \code{beta.star=(beta,sig.e)} and \code{f.star(t)=[f(t)',z(t)]'}.
#' \cr \cr By Euler's theorem, the standard deviation of the asset's return
#' is given as: \cr \cr
#' \code{Sd.fm = sum(cSd_k) = sum(beta.star_k*mSd_k)} \cr \cr
@@ -93,7 +93,9 @@
fmSdDecomp.tsfm <- function(object, use="pairwise.complete.obs", ...) {
# get beta.star: N x (K+1)
- beta.star <- as.matrix(cbind(object$beta, object$resid.sd))
+ beta <- object$beta
+ beta[is.na(beta)] <- 0
+ beta.star <- as.matrix(cbind(beta, object$resid.sd))
colnames(beta.star)[dim(beta.star)[2]] <- "residual"
# get cov(F): K x K
@@ -108,15 +110,15 @@
rownames(factor.star.cov) <- c(colnames(factor.cov),"residuals")
# compute factor model sd; a vector of length N
- sd.fm <- sqrt(rowSums(beta.star %*% factor.star.cov * beta.star))
+ Sd.fm <- sqrt(rowSums(beta.star %*% factor.star.cov * beta.star))
# compute marginal, component and percentage contributions to sd
# each of these have dimensions: N x (K+1)
- mSd <- (t(factor.star.cov %*% t(beta.star)))/sd.fm
+ mSd <- (t(factor.star.cov %*% t(beta.star)))/Sd.fm
cSd <- mSd * beta.star
- pcSd = cSd/sd.fm
+ pcSd = 100* cSd/Sd.fm
- fm.sd.decomp <- list(sd.fm=sd.fm, mSd=mSd, cSd=cSd, pcSd=pcSd)
+ fm.sd.decomp <- list(Sd.fm=Sd.fm, mSd=mSd, cSd=cSd, pcSd=pcSd)
return(fm.sd.decomp)
}
Modified: pkg/FactorAnalytics/R/fmVaRDecomp.R
===================================================================
--- pkg/FactorAnalytics/R/fmVaRDecomp.R 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/R/fmVaRDecomp.R 2014-08-08 02:38:34 UTC (rev 3502)
@@ -8,8 +8,8 @@
#' estimated quantile using the Cornish-Fisher expansion.
#'
#' @details The factor model for an asset's return at time \code{t} has the
-#' form \cr \cr \code{R(t) = beta'F(t) + e(t) = beta.star'F.star(t)} \cr \cr
-#' where, \code{beta.star=(beta,sig.e)} and \code{F.star(t)=[F(t)',z(t)]'}. By
+#' form \cr \cr \code{R(t) = beta'f(t) + e(t) = beta.star'f.star(t)} \cr \cr
+#' where, \code{beta.star=(beta,sig.e)} and \code{f.star(t)=[f(t)',z(t)]'}. By
#' Euler's theorem, the VaR of the asset's return is given by:
#' \cr \cr \code{VaR.fm = sum(cVaR_k) = sum(beta.star_k*mVaR_k)} \cr \cr
#' where, summation is across the \code{K} factors and the residual,
@@ -100,12 +100,14 @@
}
# get beta.star
- beta.star <- as.matrix(cbind(object$beta, object$resid.sd))
+ beta <- object$beta
+ beta[is.na(beta)] <- 0
+ beta.star <- as.matrix(cbind(beta, object$resid.sd))
colnames(beta.star)[dim(beta.star)[2]] <- "residual"
# factor returns and residuals data
factors.xts <- object$data[,object$factor.names]
- resid.xts <- checkData(t(t(residuals(object))/object$resid.sd))
+ resid.xts <- as.xts(t(t(residuals(object))/object$resid.sd))
time(resid.xts) <- as.Date(time(resid.xts))
# initialize lists and matrices
@@ -156,13 +158,13 @@
mVaR[i,] <- inv * colMeans(factor.star*k.weight, na.rm =TRUE)
# correction factor to ensure that sum(cVaR) = portfolio VaR
- cf <- as.numeric( VaR.fm[i] / sum(mVaR[i,]*beta.star[i,]) )
+ cf <- as.numeric( VaR.fm[i] / sum(mVaR[i,]*beta.star[i,], na.rm=TRUE) )
# compute marginal, component and percentage contributions to VaR
# each of these have dimensions: N x (K+1)
mVaR[i,] <- cf * mVaR[i,]
cVaR[i,] <- mVaR[i,] * beta.star[i,]
- pcVaR[i,] <- cVaR[i,] / VaR.fm[i]
+ pcVaR[i,] <- 100* cVaR[i,] / VaR.fm[i]
}
fm.VaR.decomp <- list(VaR.fm=VaR.fm, n.exceed=n.exceed, idx.exceed=idx.exceed,
Modified: pkg/FactorAnalytics/R/plot.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/plot.tsfm.r 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/R/plot.tsfm.r 2014-08-08 02:38:34 UTC (rev 3502)
@@ -68,9 +68,6 @@
#' to suppress the legend.
#' @param las one of {0, 1, 2, 3} to set the direction of axis labels, same as
#' in \code{plot}. Default here is 1.
-#' @param horiz a logical value. If \code{FALSE}, the bars are drawn vertically
-#' with the first bar to the left. If \code{TRUE}, the bars are drawn
-#' horizontally with the first at the bottom. Default here is \code{TRUE}.
#' @param VaR.method a method for computing VaR; one of "modified", "gaussian",
#' "historical" or "kernel". VaR is computed using
#' \code{\link[PerformanceAnalytics]{VaR}}. Default is "historical".
@@ -91,7 +88,7 @@
#' \code{\link[PerformanceAnalytics]{chart.ACFplus}},
#' \code{\link[PerformanceAnalytics]{chart.Histogram}},
#' \code{\link[PerformanceAnalytics]{chart.QQPlot}},
-#' \code{\link[graphics]{barplot}} and
+#' \code{\link[graphics]{barplot}}, \code{\link[lattice]{barchart}} and
#' \code{\link[corrplot]{corrplot}} for plotting methods used.
#'
#' \code{\link{fmSdDecomp}}, \code{\link{fmEsDecomp}},
@@ -123,8 +120,8 @@
plot.tsfm <- function(x, which.plot.group=NULL, max.show=6, plot.single=FALSE,
asset.name, which.plot.single=NULL, colorset=(1:12),
- legend.loc="topleft", las=1, horiz=TRUE,
- VaR.method="historical", loop=TRUE, ...) {
+ legend.loc="topleft", las=1, VaR.method="historical",
+ loop=TRUE, ...) {
if (plot.single==TRUE) {
@@ -249,7 +246,7 @@
}
reg.z <- zoo(fit$model, as.Date(rownames(fit$model)))
rollReg.z <- rollapply(reg.z, FUN=rollReg, formula(fit),
- width=24, by.column=FALSE, align="right")
+ width=24, by.column=FALSE, align="right")
} else if (x$fit.method=="DLS") {
# get decay factor
if (as.character(x$call["decay"])=="NULL") {
@@ -264,17 +261,17 @@
coef(lm(formula, weights=w, data=as.data.frame(data.z)))
}
reg.z <- zoo(fit$model[-length(fit$model)],
- as.Date(rownames(fit$model)))
+ as.Date(rownames(fit$model)))
rollReg.z <- rollapply(reg.z, FUN=rollReg.w, formula(fit), w,
- width=24, by.column=FALSE, align="right")
+ width=24, by.column=FALSE, align="right")
} else if (x$fit.method=="Robust") {
rollReg.Rob <- function(data.z, formula) {
coef(lmRob(formula=formula, data=as.data.frame(data.z)))
}
reg.z <- zoo(fit$model, as.Date(rownames(fit$model)))
rollReg.z <- rollapply(reg.z, width=24, FUN=rollReg.Rob,
- formula(fit), by.column=FALSE,
- align="right")
+ formula(fit), by.column=FALSE,
+ align="right")
} else if (is.null(x$fit.method)) {
stop("Rolling estimates is not available for 'lars' fits.")
}
@@ -313,9 +310,9 @@
switch(which.plot.group,
"1L" = {
## Factor model coefficients: Alpha
- # xlab="Intercept estimate", ylab="Assets"
barplot(coef(x)[,1], main="Factor model Alpha (Intercept)",
- col="darkblue", las=las, horiz=horiz, ...)
+ names.arg=rownames(coef(x)), col="darkblue", las=las,
+ horiz=TRUE, ...)
abline(v=0, lwd=1, lty=1, col=1)
},
"2L" = {
@@ -329,9 +326,8 @@
par(mfrow=c(ceiling(k/2),2))
for (i in 2:(k+1)) {
main=paste(colnames(coef(x))[i], "factor Betas")
- # xlab="Beta estimate", ylab="Assets"
- barplot(coef(x)[,i], main=main, col="darkblue", las=las,
- horiz=horiz, ...)
+ barplot(coef(x)[,i], main=main, names.arg=rownames(coef(x)),
+ col="darkblue", las=las, horiz=TRUE, ...)
abline(v=0, lwd=1, lty=1, col=1)
}
par(mfrow=c(1,1))
@@ -357,51 +353,65 @@
},
"4L" ={
## R-squared
- # ylab="Assets", xlab="R-squared"
- barplot(x$r2, main="R-squared values for factor model fits",
- col="darkblue", las=las, horiz=horiz, ...)
- abline(v=0, lwd=1, lty=1, col=1)
+ plot(
+ barchart(x$r2, main="R-squared values", xlab="",
+ col="darkblue", ...)
+ )
},
"5L" = {
## Residual Volatility
- # ylab="Assets", xlab="Residual volatility"
- barplot(x$resid.sd, col="darkblue", las=las, horiz=horiz,
- main="Residual volatility for factor model fits", ...)
- abline(v=0, lwd=1, lty=1, col=1)
+ plot(
+ barchart(x$resid.sd, main="Residual volatility", xlab="",
+ col="darkblue", ...)
+ )
},
"6L" = {
## Factor Model Residual Correlation
cor.resid <- cor(residuals(x), use="pairwise.complete.obs")
corrplot::corrplot(cor.resid, ...)
+ # mtext("pairwise complete obs", line=0.5)
},
"7L" = {
## Factor Model Return Correlation
cor.fm <- cov2cor(fmCov(x))
corrplot::corrplot(cor.fm, ...)
+ # mtext("pairwise complete obs", line=0.5)
},
"8L" = {
- ## Factor Contribution to SD
- cSd.fm <- fmSdDecomp(x)$cSd
- barplot(t(cSd.fm), main="Factor Contributions to SD",
- legend.text=T, args.legend=legend.loc, col=colorset,
- horiz=horiz, ...)
- mtext("(pairwise complete obs)", line=0.5)
+ ## Factor Percentage Contribution to SD
+ pcSd.fm <- fmSdDecomp(x)$pcSd
+ plot(
+ barchart(pcSd.fm, main="Factor % Contribution to SD", xlab="",
+ auto.key=list(space="bottom",columns=3,
+ points=FALSE,rectangles=TRUE),
+ par.settings=list(superpose.polygon=list(col=colorset)),
+ panel=function(...){panel.grid(h=0, v=-1);
+ panel.barchart(...)}, ...)
+ )
},
"9L"={
- ## Factor Contribution to ES
- cES.fm <- fmEsDecomp(x)$cES
- barplot(t(cES.fm), main="Factor Contributions to ES",
- legend.text=T, args.legend=legend.loc, col=colorset,
- horiz=horiz, ...)
- mtext("(pairwise complete obs)", line=0.5)
+ ## Factor Percentage Contribution to ES
+ pcES.fm <- fmEsDecomp(x, method=VaR.method)$pcES
+ plot(
+ barchart(pcES.fm, main="Factor % Contribution to ES", xlab="",
+ auto.key=list(space="bottom",columns=3,
+ points=FALSE,rectangles=TRUE),
+ par.settings=list(superpose.polygon=list(col=colorset)),
+ panel=function(...){panel.grid(h=0, v=-1);
+ panel.barchart(...)}, ...)
+ )
},
"10L" ={
- ## Factor Contribution to VaR
- cVaR.fm <- fmVaRDecomp(x)$cVaR
- barplot(t(cVaR.fm), main="Factor Contributions to VaR",
- legend.text=T, args.legend=legend.loc, col=colorset,
- horiz=horiz, ...)
- mtext("(pairwise complete obs)", line=0.5)
+ ## Factor Percentage Contribution to VaR
+ pcVaR.fm <- fmVaRDecomp(x, method=VaR.method)$pcVaR
+ plot(
+ barchart(pcVaR.fm, main="Factor % Contribution to VaR",
+ xlab="", auto.key=list(space="bottom",columns=3,
+ points=FALSE,rectangles=TRUE),
+ par.settings=list(superpose.polygon=list(col=colorset)),
+ panel=function(...){panel.grid(h=0, v=-1);
+ panel.barchart(...)}, ...)
+ )
},
invisible()
)
Modified: pkg/FactorAnalytics/R/predict.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/predict.tsfm.r 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/R/predict.tsfm.r 2014-08-08 02:38:34 UTC (rev 3502)
@@ -43,31 +43,3 @@
lapply(object$asset.fit, predict, newdata, ...)
}
}
-
-#
-# if ( !(missing(newdata) && !is.null(newdata) )) {
-# numAssets <- length(names(fit.macro$asset.fit))
-#
-# data <- fit.macro$data
-# factors <- data[,fit.macro$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)
-#
-# }
-#
-#
-#
-# }
-
-
Modified: pkg/FactorAnalytics/R/print.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/print.tsfm.r 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/R/print.tsfm.r 2014-08-08 02:38:34 UTC (rev 3502)
@@ -34,11 +34,11 @@
names(tmp) <- c("Factors", "Assets", "Periods")
print(tmp)
cat("\nRegression Alphas:\n")
- print(x$alpha, digits = digits, ...)
+ print(t(x$alpha), digits=digits, ...)
cat("\nFactor Betas:\n")
- print(x$beta, digits = digits, ...)
+ print(x$beta, digits=digits, ...)
cat("\nR-squared values:\n")
- print(x$r2, digits = digits, ...)
+ print(x$r2, digits=digits, ...)
cat("\nResidual Volatilities:\n")
- print(x$resid.sd, digits = digits, ...)
+ print(x$resid.sd, digits=digits, ...)
}
Modified: pkg/FactorAnalytics/R/summary.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.tsfm.r 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/R/summary.tsfm.r 2014-08-08 02:38:34 UTC (rev 3502)
@@ -73,9 +73,9 @@
# convert to HC/HAC standard errors and t-stats if specified
# extract coefficients separately for "lars" variable.selection method
for (i in object$asset.names) {
- if (se.type == "HC") {
+ if (se.type=="HC") {
sum[[i]]$coefficients <- coeftest(object$asset.fit[[i]], vcovHC)[,1:4]
- } else if (se.type == "HAC") {
+ } else if (se.type=="HAC") {
sum[[i]]$coefficients <- coeftest(object$asset.fit[[i]], vcovHAC)[,1:4]
}
}
@@ -92,7 +92,7 @@
}
# include the call and se.type to fitTsfm
- sum <- c(list(call=object$call, Type=se.type), sum)
+ sum <- c(list(call=object$call, se.type=se.type), sum)
class(sum) <- "summary.tsfm"
return(sum)
}
@@ -112,7 +112,7 @@
n <- length(x)
for (i in 3:n) {
options(digits = digits)
- cat("\nAsset", i-2, ": ", names(x[i]), "\n(",x$Type,
+ cat("\nAsset", i-2, ": ", names(x[i]), "\n(",x$se.type,
" Standard Errors & T-stats)\n\n", sep = "")
table.coef <- x[[i]]$coefficients
print(table.coef, digits = digits, ...)
Modified: pkg/FactorAnalytics/man/fitTsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitTsfm.Rd 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/man/fitTsfm.Rd 2014-08-08 02:38:34 UTC (rev 3502)
@@ -23,9 +23,8 @@
\item{factor.names}{vector containing names of the macroeconomic factors.}
-\item{mkt.name}{name of the column for market excess returns (Rm-Rf).
-Is required if \code{mkt.timing} or \code{add.market.sqd}
-are \code{TRUE}. Default is NULL.}
+\item{mkt.name}{name of the column for market excess returns (Rm-Rf); this
+is necessary to add market timing factors. Default is 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
@@ -42,7 +41,7 @@
"stepwise","subsets","lars". See details. Default is "none".}
\item{mkt.timing}{one of "HM", "TM" or "both". Default is NULL. See Details.
-\code{mkt.name} is required if any of these options are specified.}
+\code{mkt.name} is required if any of these options are to be implemented.}
\item{control}{list of control parameters. The default is constructed by
the function \code{\link{fitTsfm.control}}. See the documentation for
@@ -110,20 +109,20 @@
improves. And, "subsets" enables subsets selection using
\code{\link[leaps]{regsubsets}}; chooses the best performing subset of any
given size. See \code{\link{fitTsfm.control}} for more details on the
-control arguments. \code{varaible.selection="lars"} corresponds to least
+control arguments. \code{variable.selection="lars"} corresponds to least
angle regression using \code{\link[lars]{lars}} with variants "lasso",
"lar", "forward.stagewise" or "stepwise". Note: If
\code{variable.selection="lars"}, \code{fit.method} will be ignored.
-\code{mkt.timing} allows for market-timing factors to be added to any of the
-above methods. Market timing accounts for the price movement of the general
-stock market relative to fixed income securities. "HM" follows
-Henriksson & Merton (1981) and \code{up-market=max(0,Rm-Rf)}, is added to
-the regression. The coefficient of this up-market factor can be
-interpreted as the number of free put options. Similarly, "TM" follows
-Treynor-Mazuy (1966), to account for market timing with respect to
-volatility, and \code{market.sqd=(Rm-Rf)^2} is added as a factor in the
-regression. Option "both" adds both of these factors.
+Arguments \code{mkt.name} and \code{mkt.timing} allow for market-timing
+factors to be added to any of the above methods. Market timing accounts for
+the price movement of the general stock market relative to fixed income
+securities. "HM" follows Henriksson & Merton (1981) and
+\code{up.market=max(0,Rm-Rf)}, is added to the regression. The coefficient
+of this up-market factor can be interpreted as the number of free put
+options. Similarly, "TM" follows Treynor-Mazuy (1966), to account for market
+timing with respect to volatility, and \code{market.sqd=(Rm-Rf)^2} is added
+as a factor in the regression. Option "both" adds both of these factors.
\subsection{Data Processing}{
Modified: pkg/FactorAnalytics/man/fmCov.Rd
===================================================================
--- pkg/FactorAnalytics/man/fmCov.Rd 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/man/fmCov.Rd 2014-08-08 02:38:34 UTC (rev 3502)
@@ -31,16 +31,16 @@
\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*F(t) + e(i,t)}, \cr \cr
-where, \code{alpha(i)} is the intercept, \code{F(t)} is a {K x 1} vector of
-the \code{K} factor values at time \code{t}, \code{beta} is a \code{1 x K}
-vector of factor exposures and the error terms \code{e(i,t)} are serially
+\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,t)) = beta*var(F(t))*tr(beta) + sig(i)^2}. \cr \cr
-And, the \code{N x N} covariance matrix of N asset returns is \cr \cr
-\code{var(R) = B*var(F(t))*tr(B) + D}, \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.
Modified: pkg/FactorAnalytics/man/fmEsDecomp.Rd
===================================================================
--- pkg/FactorAnalytics/man/fmEsDecomp.Rd 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/man/fmEsDecomp.Rd 2014-08-08 02:38:34 UTC (rev 3502)
@@ -45,8 +45,8 @@
}
\details{
The factor model for an asset's return at time \code{t} has the
-form \cr \cr \code{R(t) = beta'F(t) + e(t) = beta.star'F.star(t)} \cr \cr
-where, \code{beta.star=(beta,sig.e)} and \code{F.star(t)=[F(t)',z(t)]'}. By
+form \cr \cr \code{R(t) = beta'f(t) + e(t) = beta.star'f.star(t)} \cr \cr
+where, \code{beta.star=(beta,sig.e)} and \code{f.star(t)=[f(t)',z(t)]'}. By
Euler's theorem, the ES of the asset's return is given by:
\cr \cr \code{ES.fm = sum(cES_k) = sum(beta.star_k*mES_k)} \cr \cr
where, summation is across the \code{K} factors and the residual,
Modified: pkg/FactorAnalytics/man/fmSdDecomp.Rd
===================================================================
--- pkg/FactorAnalytics/man/fmSdDecomp.Rd 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/man/fmSdDecomp.Rd 2014-08-08 02:38:34 UTC (rev 3502)
@@ -33,8 +33,8 @@
}
\details{
The factor model for an asset's return at time \code{t} has the
-form \cr \cr \code{R(t) = beta'F(t) + e(t) = beta.star'F.star(t)} \cr \cr
-where, \code{beta.star=(beta,sig.e)} and \code{F.star(t)=[F(t)',z(t)]'}.
+form \cr \cr \code{R(t) = beta'f(t) + e(t) = beta.star'f.star(t)} \cr \cr
+where, \code{beta.star=(beta,sig.e)} and \code{f.star(t)=[f(t)',z(t)]'}.
\cr \cr By Euler's theorem, the standard deviation of the asset's return
is given as: \cr \cr
\code{Sd.fm = sum(cSd_k) = sum(beta.star_k*mSd_k)} \cr \cr
Modified: pkg/FactorAnalytics/man/fmVaRDecomp.Rd
===================================================================
--- pkg/FactorAnalytics/man/fmVaRDecomp.Rd 2014-08-06 20:34:32 UTC (rev 3501)
+++ pkg/FactorAnalytics/man/fmVaRDecomp.Rd 2014-08-08 02:38:34 UTC (rev 3502)
@@ -44,8 +44,8 @@
}
\details{
The factor model for an asset's return at time \code{t} has the
-form \cr \cr \code{R(t) = beta'F(t) + e(t) = beta.star'F.star(t)} \cr \cr
-where, \code{beta.star=(beta,sig.e)} and \code{F.star(t)=[F(t)',z(t)]'}. By
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/returnanalytics -r 3502
More information about the Returnanalytics-commits
mailing list