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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Aug 21 20:26:19 CEST 2013


Author: chenyian
Date: 2013-08-21 20:26:19 +0200 (Wed, 21 Aug 2013)
New Revision: 2845

Modified:
   pkg/FactorAnalytics/R/factorModelVaRDecomposition.R
   pkg/FactorAnalytics/R/plot.StatFactorModel.r
   pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd
   pkg/FactorAnalytics/vignettes/fundamentalFM.Rnw
Log:
add risk analysis in vignette. modify some codes.

Modified: pkg/FactorAnalytics/R/factorModelVaRDecomposition.R
===================================================================
--- pkg/FactorAnalytics/R/factorModelVaRDecomposition.R	2013-08-21 17:36:36 UTC (rev 2844)
+++ pkg/FactorAnalytics/R/factorModelVaRDecomposition.R	2013-08-21 18:26:19 UTC (rev 2845)
@@ -44,7 +44,7 @@
 #' fit.macro <- fitTimeSeriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
 #'                                      factors.names=c("EDHEC.LS.EQ","SP500.TR"),
 #'                                      data=managers.df,fit.method="OLS")
-#' # risk factor contribution to ETL
+#' # risk factor contribution to VaR
 #' # combine fund returns, factor returns and residual returns for HAM1
 #' tmpData = cbind(managers.df[,1],managers.df[,c("EDHEC.LS.EQ","SP500.TR")] ,
 #' residuals(fit.macro$asset.fit$HAM1)/sqrt(fit.macro$resid.variance[1]))

Modified: pkg/FactorAnalytics/R/plot.StatFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/plot.StatFactorModel.r	2013-08-21 17:36:36 UTC (rev 2844)
+++ pkg/FactorAnalytics/R/plot.StatFactorModel.r	2013-08-21 18:26:19 UTC (rev 2845)
@@ -389,7 +389,7 @@
                    factorModelSdDecomposition(x$loadings[,i],
                                               cov.factors, x$resid.variance[i])
                }
-               # function to extract contribution to sd from list
+               # function to extract component contribution to sd from list
                getCSD = function(x) {
                  x$cr.fm
                }
@@ -397,8 +397,8 @@
                cr.sd = sapply(factor.sd.decomp.list, getCSD)
                rownames(cr.sd) = c(colnames(x$factors), "residual")
                # create stacked barchart
-               barplot(cr.sd[,(1:max.show)], main="Factor Contributions to SD",
-                       legend.text=T, args.legend=list(x="topleft"))
+               barplot(cr.sd[,(1:max.show)], main="Factor Contributions to SD",...)
+#                        legend.text=T, args.legend=list(x="topright"))
              } ,
              "7L" ={
                factor.es.decomp.list = list()
@@ -416,15 +416,15 @@
                }
                
                
-               # stacked bar charts of percent contributions to ES 
+               # stacked bar charts of component contributions to ES 
                getCETL = function(x) {
                  x$cES
                }
                # report as positive number
                cr.etl = sapply(factor.es.decomp.list, getCETL)
                rownames(cr.etl) = c(colnames(x$factors), "residual")
-               barplot(cr.etl[,(1:max.show)], main="Factor Contributions to ES",
-                       legend.text=T, args.legend=list(x="topleft") )
+               barplot(cr.etl[,(1:max.show)], main="Factor Contributions to ES",...)
+#                        legend.text=T, args.legend=list(x="topright") )
              },
              "8L" =  {
                factor.VaR.decomp.list = list()
@@ -442,15 +442,15 @@
                }
                
                
-               # stacked bar charts of percent contributions to VaR
+               # stacked bar charts of component contributions to VaR
                getCVaR = function(x) {
                  x$cVaR.fm
                }
                # report as positive number
                cr.var = sapply(factor.VaR.decomp.list, getCVaR)
                rownames(cr.var) = c(colnames(x$factors), "residual")
-               barplot(cr.var[,(1:max.show)], main="Factor Contributions to VaR",
-                       legend.text=T, args.legend=list(x="topleft"))
+               barplot(cr.var[,(1:max.show)], main="Factor Contributions to VaR",...)
+#                        legend.text=T, args.legend=list(x="topright"))
              }, invisible()
              
       )

Modified: pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd
===================================================================
--- pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd	2013-08-21 17:36:36 UTC (rev 2844)
+++ pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd	2013-08-21 18:26:19 UTC (rev 2845)
@@ -57,7 +57,7 @@
 fit.macro <- fitTimeSeriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
                                      factors.names=c("EDHEC.LS.EQ","SP500.TR"),
                                      data=managers.df,fit.method="OLS")
-# risk factor contribution to ETL
+# risk factor contribution to VaR
 # combine fund returns, factor returns and residual returns for HAM1
 tmpData = cbind(managers.df[,1],managers.df[,c("EDHEC.LS.EQ","SP500.TR")] ,
 residuals(fit.macro$asset.fit$HAM1)/sqrt(fit.macro$resid.variance[1]))

Modified: pkg/FactorAnalytics/vignettes/fundamentalFM.Rnw
===================================================================
--- pkg/FactorAnalytics/vignettes/fundamentalFM.Rnw	2013-08-21 17:36:36 UTC (rev 2844)
+++ pkg/FactorAnalytics/vignettes/fundamentalFM.Rnw	2013-08-21 18:26:19 UTC (rev 2845)
@@ -1,5 +1,7 @@
 \documentclass{article}
 \usepackage[utf8]{inputenc}
+\usepackage{amsmath}
+\usepackage{amsthm}
 % \VignetteIndexEntry{test file}
 % \VignetteKeywords{facor model, risk analytics}
 
@@ -248,7 +250,7 @@
 
 \begin{figure}
 \begin{center}
-<<label=fig2,fig=TRUE,echo=FALSE>>=
+<<label=fig3,fig=TRUE,echo=FALSE>>=
 <<plot.fit.stat2>>
 @
 \end{center}
@@ -259,7 +261,7 @@
 Similar to \verb at fitFundamentalFactorModel@, generic functions like \verb at summary@, \verb at print@, \verb at plot@ and \verb at predict@ can be used for statistical factor model. 
 
 \section{Time Series Factor Model}
-In Time Series facto model, factor returns $f_t$ is observed and takens as macroeconomic time series like GDP or other time series like market returns or credit spread. In our package, we have provided some common used times series in data set \verb at CommonFactors@. \verb at factors@ is monthly time series and \verb at factors.Q@ is quarterly time series.
+In Time Series factor model, factor returns $f_t$ is observed and taken as macroeconomic time series like GDP or other time series like market returns or credit spread. In our package, we have provided some common used times series in data set \verb at CommonFactors@. \verb at factors@ is monthly time series and \verb at factors.Q@ is quarterly time series.
 
 <<common.factors>>=
 data(CommonFactors)
@@ -277,17 +279,16 @@
 
 <<fit.ts>>=
 fit.time <- fitTimeSeriesFactorModel(assets.names=tic,factors.names=c("SP500","Term.Spread","dVIX"),data=ts.data,fit.method="OLS")
-names(fit.time)
 @
 
-\veb at asset.fit@ can show model fit of each assets, for example for asset \verb at AA@. 
+\verb at asset.fit@ can show model fit for each assets, for example for asset \verb at AA@. 
 <<single.asset.fit>>=
-summary(fit.time$asset.fit$AA)
+fit.time$asset.fit$AA
 @
-shows only beta of SP500 is significant.  
 
-\verb at fitTimeSeriesFactorModel@ also have various variable selection algorithm to choose. One can include all the factor and let the function to decide which one is the best model. For example, we inlcude all common factors and use method \verb at stepwise@ which utilizes \verb at step@ function in \verb at stat@ package 
 
+\verb at fitTimeSeriesFactorModel@ also have various variable selection algorithm to choose. One can include all the factor and let the function to decide which one is the best model. For example, we include all common factors and use method \verb at stepwise@ which utilizes \verb at step@ function in \verb at stat@ package 
+
 <<fit.ts>>=
 fit.time2 <- fitTimeSeriesFactorModel(assets.names=tic,factors.names=names(ts.factors),data=ts.data,fit.method="OLS",variable.selection = "stepwise")
 @
@@ -296,7 +297,83 @@
 fit.time2$asset.fit$AA
 @
 
-Generic functions like \verb at summary@, \verb at print@, \verb at plot@ and \verb at predict@ can be used for Time Series factor model as well like previous section.
+Generic functions like \verb at summary@, \verb at print@, \verb at plot@ and \verb at predict@ can also be used for time series factor model as previous section.
 
+\section{Risk Analysis}
 
+\subsection{Factor Model Risk Budgeting}
+One can do risk analysis easily with factor model. According to Meucci (2007), factor model can be represented as 
+
+\begin{align}
+r_{it} &= \alpha_i + \beta_{i1}f_{1t} + \beta_{i2}f_{2t} + \cdots + \beta_{ik}f_{kt} + \sigma{i}z_{it},\;i=1 \cdots N,\;t=1 \cdots T \\
+       &= \alpha_i + \tilde{\beta_i}'\tilde{F_t}
+\end{align}
+
+where $z_{it}$ is the standardized residuals and $\epsilon_{it} / \sigma_i = z_{it}$, $\tilde{\beta_i} = [\beta_{1i},\dots,\beta_{ki}, \sigma_i]$, $\tilde{F_t}=[f_{1t},\dots,f_{kt}, z_{it}]$  
+
+Common risk measures like standard deviation, value-at-risk and expected shortfall are function of homogeneous of degree 1. By Euler theoreom, risk metrics (RM) can be decomposed to 
+\begin{align}
+RM_i = \beta_{1i}\frac{\partial RM_i}{\partial \beta_{1i}} + \beta_{2i}\frac{\partial RM_i}{\partial \beta_{2i}} + \cdots + \beta_{ki}\frac{\partial RM_i}{\partial \beta_{ki}} + \sigma_{i}\frac{\partial RM_i}{\partial \sigma_{i}}
+\end{align}
+
+where\\
+$\frac{\partial RM_i}{\partial \beta_{ki}}$ is called marginal contribution of factor k to $RM_i$ \\
+$\beta_{ki}\frac{\partial RM_i}{\partial \beta_{ki}}$ is called component contribution of factor k to $RM_i$ \\
+$\beta_{ki}\frac{\partial RM_i}{\partial \beta_{ki}}/RM_i$ is called percentage contribution of factor k to $RM_i$
+
+\verb at factorAnalytics@ package provide 3 different risk metrics decomposition, Standard deviation (Std), Value-at-Risk (VaR) and Expected Shortfall (ES) with historical distribution, Normal distribution and Cornish-Fisher distribution. 
+
+For example, factor model VaR decomposition with Normal distribution of asset AA for a statistical factor model.
+<<risk.decomp>>=
+data.rd <- cbind(ret[,"AA"],fit.stat$factors,fit.stat$residuals[,"AA"]/sqrt(fit.stat$resid.variance["AA"]))
+var.decp <- factorModelVaRDecomposition(data.rd,fit.stat$loadings[,"AA"],
+                            fit.stat$resid.variance["AA"],tail.prob=0.05,
+                             VaR.method="gaussian")
+names(var.decp)
+@
+
+VaR, number of exceed, id of exceed, marginal contribution to VaR, component contribution to VaR and percentage contribution to VaR are computed. Let see VaR and component contribution to VaR 
+<<mct.var>>=
+var.decp$VaR.fm
+var.decp$cVaR.fm
+@
+It looks like the second factor contributes the largest risk to asset AA. 
+
+One can use \verb at plot@ method to see barplot of risk budgeting. Default is to show 6 assets. Figure \ref{fig4} shows componenet contribution to VaR for several different assets. 
+
+<<label=plot.rb.stat,fig=TRUE,echo=FALSE,include=FALSE,results=hide>>=
+plot(fit.stat,which.plot=8,legend.text=TRUE, args.legend=list(x="topright"),
+     VaR.method="gaussian")
+@
+
+\begin{figure}
+\begin{center}
+<<label=fig4,fig=TRUE,echo=FALSE>>=
+<<plot.rb.stat>>
+@
+\end{center}
+\caption{Component Contribution to VaR for Statistical Factor Model. }
+\label{fig4}
+\end{figure}
+
+\subsection{Portfolio Risk Budgeting}
+
+Let $Rp_t = Rp_t(w)$ denote the portfolio return based on the vector of portfolio weights w. Let RM(w) denote a portfolio risk measure. 
+
+\begin{align}
+RM = w_{1}\frac{\partial RM}{\partial w_{1}} + w_{2}\frac{\partial RM}{\partial w_{2}} + \cdots + w_{N}\frac{\partial RM}{\partial w_{N}}
+\end{align}
+
+where\\
+$\frac{\partial RM}{\partial w_{i}}$ is called marginal contribution of asset i to RM \\
+$w_{i}\frac{\partial RM}{\partial w_{i}}$ is called component contribution of asset i to RM \\
+$w_{i}\frac{\partial RM}{\partial w_{i}}/RM$ is called percentage contribution of asset i to RM
+
+we can use function \verb at VaR()@ in \verb at PerformanceAnalytics@. Suppose we have an eqaully weighted portfolio of 63 assets in data set \verb at ret@. The following code can compute portfolio VaR, component contribution to VaR and percentage contribution to VaR
+
+<<port.var,results=hide>>=
+VaR(R=ret,method="gaussian",portfolio_method="component")
+@
+
+
 \end{document}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list