[Uwgarp-commits] r70 - in pkg/GARPFRM: . man vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Feb 8 18:05:45 CET 2014


Author: rossbennett34
Date: 2014-02-08 18:05:45 +0100 (Sat, 08 Feb 2014)
New Revision: 70

Added:
   pkg/GARPFRM/man/EWMA.Rd
   pkg/GARPFRM/man/chartSML.Rd
   pkg/GARPFRM/man/endingPrices.Rd
   pkg/GARPFRM/man/fcstGarch11.Rd
   pkg/GARPFRM/man/getCov.Rd
   pkg/GARPFRM/vignettes/EDA.R
   pkg/GARPFRM/vignettes/RB.tex
Modified:
   pkg/GARPFRM/DESCRIPTION
   pkg/GARPFRM/man/CAPM.Rd
   pkg/GARPFRM/man/getAlphas.Rd
   pkg/GARPFRM/man/getBetas.Rd
   pkg/GARPFRM/man/getStatistics.Rd
   pkg/GARPFRM/man/hypTest.Rd
   pkg/GARPFRM/man/monteCarlo.Rd
Log:
Adding documentation files

Modified: pkg/GARPFRM/DESCRIPTION
===================================================================
--- pkg/GARPFRM/DESCRIPTION	2014-02-08 06:29:39 UTC (rev 69)
+++ pkg/GARPFRM/DESCRIPTION	2014-02-08 17:05:45 UTC (rev 70)
@@ -13,3 +13,8 @@
 Suggests:
     quadprog
 License: GPL
+Collate:
+    'EWMA.R'
+    'capm.R'
+    'garch11.R'
+    'monte_carlo.R'

Modified: pkg/GARPFRM/man/CAPM.Rd
===================================================================
--- pkg/GARPFRM/man/CAPM.Rd	2014-02-08 06:29:39 UTC (rev 69)
+++ pkg/GARPFRM/man/CAPM.Rd	2014-02-08 17:05:45 UTC (rev 70)
@@ -2,7 +2,7 @@
 \alias{CAPM}
 \title{Capital Asset Pricing Model}
 \usage{
-CAPM(R, Rmkt)
+  CAPM(R, Rmkt)
 }
 \arguments{
   \item{R}{asset returns}
@@ -10,8 +10,8 @@
   \item{Rmkt}{market returns}
 }
 \description{
-Description of CAPM Retrieves alphas, betas, as well as
-pvalue and tstats
+  Description of CAPM Retrieves alphas, betas, as well as
+  pvalue and tstats
 }
 \examples{
 data(crsp.short)

Added: pkg/GARPFRM/man/EWMA.Rd
===================================================================
--- pkg/GARPFRM/man/EWMA.Rd	                        (rev 0)
+++ pkg/GARPFRM/man/EWMA.Rd	2014-02-08 17:05:45 UTC (rev 70)
@@ -0,0 +1,20 @@
+\name{EWMA}
+\alias{EWMA}
+\title{Exponential Weighted Moving Average (EWMA)}
+\usage{
+  EWMA(R, lambda = 0.94, inWnd = 10, cor = FALSE)
+}
+\arguments{
+  \item{R}{}
+
+  \item{lambda}{}
+
+  \item{inWnd}{}
+
+  \item{cor}{option (default = FALSE)}
+}
+\description{
+  Description of EWMA. The function handles UV and MLM
+  objects and returns either cov/cor.
+}
+

Added: pkg/GARPFRM/man/chartSML.Rd
===================================================================
--- pkg/GARPFRM/man/chartSML.Rd	                        (rev 0)
+++ pkg/GARPFRM/man/chartSML.Rd	2014-02-08 17:05:45 UTC (rev 70)
@@ -0,0 +1,14 @@
+\name{chartSML}
+\alias{chartSML}
+\title{CAPM SML}
+\usage{
+  chartSML(object)
+}
+\arguments{
+  \item{object}{a capm object created by
+  \code{\link{CAPM}}}
+}
+\description{
+  Description of CAPM Security Market Line (SML)
+}
+

Added: pkg/GARPFRM/man/endingPrices.Rd
===================================================================
--- pkg/GARPFRM/man/endingPrices.Rd	                        (rev 0)
+++ pkg/GARPFRM/man/endingPrices.Rd	2014-02-08 17:05:45 UTC (rev 70)
@@ -0,0 +1,10 @@
+\name{endingPrices}
+\alias{endingPrices}
+\title{Ending Prices of Monte Carlo Simulation}
+\usage{
+  endingPrices(mc)
+}
+\description{
+  Ending Prices of Monte Carlo Simulation
+}
+

Added: pkg/GARPFRM/man/fcstGarch11.Rd
===================================================================
--- pkg/GARPFRM/man/fcstGarch11.Rd	                        (rev 0)
+++ pkg/GARPFRM/man/fcstGarch11.Rd	2014-02-08 17:05:45 UTC (rev 70)
@@ -0,0 +1,14 @@
+\name{fcstGarch11}
+\alias{fcstGarch11}
+\title{Forecast GARCH(1,1)}
+\usage{
+  fcstGarch11(object, window)
+}
+\arguments{
+  \item{object}{a garch11 object created by
+  \code{\link{GARCH(1,1)}}}
+}
+\description{
+  Description of forecast GARCH(1,1)
+}
+

Modified: pkg/GARPFRM/man/getAlphas.Rd
===================================================================
--- pkg/GARPFRM/man/getAlphas.Rd	2014-02-08 06:29:39 UTC (rev 69)
+++ pkg/GARPFRM/man/getAlphas.Rd	2014-02-08 17:05:45 UTC (rev 70)
@@ -2,13 +2,13 @@
 \alias{getAlphas}
 \title{CAPM alphas}
 \usage{
-getAlphas(object)
+  getAlphas(object)
 }
 \arguments{
   \item{object}{a capm object created by
   \code{\link{CAPM}}}
 }
 \description{
-Description of CAPM alphas
+  Description of CAPM alphas
 }
 

Modified: pkg/GARPFRM/man/getBetas.Rd
===================================================================
--- pkg/GARPFRM/man/getBetas.Rd	2014-02-08 06:29:39 UTC (rev 69)
+++ pkg/GARPFRM/man/getBetas.Rd	2014-02-08 17:05:45 UTC (rev 70)
@@ -2,13 +2,13 @@
 \alias{getBetas}
 \title{CAPM betas}
 \usage{
-getBetas(object)
+  getBetas(object)
 }
 \arguments{
   \item{object}{a capm object created by
   \code{\link{CAPM}}}
 }
 \description{
-Description of CAPM betas
+  Description of CAPM betas
 }
 

Added: pkg/GARPFRM/man/getCov.Rd
===================================================================
--- pkg/GARPFRM/man/getCov.Rd	                        (rev 0)
+++ pkg/GARPFRM/man/getCov.Rd	2014-02-08 17:05:45 UTC (rev 70)
@@ -0,0 +1,14 @@
+\name{getCov}
+\alias{getCov}
+\title{EWMA volatility/cross-volatility}
+\usage{
+  getCov(object, asset1, asset2)
+}
+\arguments{
+  \item{object}{a EWMA object created by
+  \code{\link{EWMA}}}
+}
+\description{
+  Description of EWMA Vola
+}
+

Modified: pkg/GARPFRM/man/getStatistics.Rd
===================================================================
--- pkg/GARPFRM/man/getStatistics.Rd	2014-02-08 06:29:39 UTC (rev 69)
+++ pkg/GARPFRM/man/getStatistics.Rd	2014-02-08 17:05:45 UTC (rev 70)
@@ -2,14 +2,14 @@
 \alias{getStatistics}
 \title{CAPM statistics}
 \usage{
-getStatistics(object)
+  getStatistics(object)
 }
 \arguments{
   \item{object}{a capm object created by
   \code{\link{CAPM}}}
 }
 \description{
-Description of CAPM statistics (standard error, t-values,
-and p-values)
+  Description of CAPM statistics (standard error, t-values,
+  and p-values)
 }
 

Modified: pkg/GARPFRM/man/hypTest.Rd
===================================================================
--- pkg/GARPFRM/man/hypTest.Rd	2014-02-08 06:29:39 UTC (rev 69)
+++ pkg/GARPFRM/man/hypTest.Rd	2014-02-08 17:05:45 UTC (rev 70)
@@ -2,13 +2,13 @@
 \alias{hypTest}
 \title{CAPM hypTest}
 \usage{
-hypTest(object, CI)
+  hypTest(object, CI)
 }
 \arguments{
   \item{object}{a capm object created by
   \code{\link{CAPM}}}
 }
 \description{
-Description of CAPM beta/alpha hypothesis test
+  Description of CAPM beta/alpha hypothesis test
 }
 

Modified: pkg/GARPFRM/man/monteCarlo.Rd
===================================================================
--- pkg/GARPFRM/man/monteCarlo.Rd	2014-02-08 06:29:39 UTC (rev 69)
+++ pkg/GARPFRM/man/monteCarlo.Rd	2014-02-08 17:05:45 UTC (rev 70)
@@ -2,8 +2,8 @@
 \alias{monteCarlo}
 \title{Monte Carlo Price Path Simulation}
 \usage{
-monteCarlo(mu, sigma, N = 100, Time = 1, steps = 52,
-  starting_value = 100, log = TRUE)
+  monteCarlo(mu, sigma, N = 100, Time = 1, steps = 52,
+    starting_value = 100, log = TRUE)
 }
 \arguments{
   \item{mu}{annualized expected return}
@@ -22,10 +22,10 @@
   rather than S; where S is the price of the asset.}
 }
 \value{
-matrix of Monte Carlo simulated price paths
+  matrix of Monte Carlo simulated price paths
 }
 \description{
-Description for Monte Carlo. Geometric brownian motion. mu
-and sigma are assumed constant
+  Description for Monte Carlo. Geometric brownian motion.
+  mu and sigma are assumed constant
 }
 

Added: pkg/GARPFRM/vignettes/EDA.R
===================================================================
--- pkg/GARPFRM/vignettes/EDA.R	                        (rev 0)
+++ pkg/GARPFRM/vignettes/EDA.R	2014-02-08 17:05:45 UTC (rev 70)
@@ -0,0 +1,124 @@
+\documentclass[a4paper]{article}
+\usepackage[OT1]{fontenc}
+\usepackage{Sweave}
+\usepackage{Rd}
+\usepackage{amsmath}
+\usepackage{hyperref}
+\usepackage{url}
+\usepackage[round]{natbib}
+\usepackage{bm}
+\usepackage{verbatim}
+\usepackage[latin1]{inputenc}
+\bibliographystyle{abbrvnat}
+
+\let\proglang=\textsf
+%\newcommand{\pkg}[1]{{\fontseries{b}\selectfont #1}}
+%\newcommand{\R}[1]{{\fontseries{b}\selectfont #1}}
+%\newcommand{\email}[1]{\href{mailto:#1}{\normalfont\texttt{#1}}}
+%\newcommand{\E}{\mathsf{E}}
+%\newcommand{\VAR}{\mathsf{VAR}}
+%\newcommand{\COV}{\mathsf{COV}}
+%\newcommand{\Prob}{\mathsf{P}}
+
+\renewcommand{\topfraction}{0.85}
+\renewcommand{\textfraction}{0.1}
+\renewcommand{\baselinestretch}{1.5}
+\setlength{\textwidth}{15cm} \setlength{\textheight}{22cm} \topmargin-1cm \evensidemargin0.5cm \oddsidemargin0.5cm
+
+\usepackage[latin1]{inputenc}
+% or whatever
+
+\usepackage{lmodern}
+\usepackage[T1]{fontenc}
+% Or whatever. Note that the encoding and the font should match. If T1
+% does not look nice, try deleting the line with the fontenc.
+
+\begin{document}
+
+\title{Exploratory Data Analysis, basic probability and statistics}
+\author{Ross Bennett}
+
+\maketitle
+
+\begin{abstract}
+The goal of this vignette is to demonstrate key concepts in Financial Risk Manager (FRM (R)) Part 1: Quantitative Analysis using R and the GARPFRM package. This vignette will cover exploratory data analysis, basic probability and statistics, and linear regression.
+\end{abstract}
+
+\tableofcontents
+
+\section{Exploratory Data Analysis}
+
+Load the GARPFRM package and the \verb"returns" dataset. The \verb"returns" dataset includes weekly returns for SPY, AAPL, XOM, GOOG, MSFT, and GE from 2005-01-14 to 2013-11-22.
+<<>>=
+library(GARPFRM)
+data(returns)
+@
+
+The exploratory data analysis, basic probability and statistics will use the SPY weekly returns.
+<<>>=
+SPY.ret <- returns[, "SPY"]
+@
+
+Plot of the SPY weekly returns. 
+<<>>=
+plot(SPY.ret, main="SPY Weekly Returns")
+@
+
+The density of the SPY weekly returns is plotted to better understand its distribution. A normal density is overlayed on the plot with standard estimates of the sample mean and standard deviation. Another normal density is overlayed using robust estimates. It is clear from the chart that the robust estimates provide a better fit than the standard estimates of the sample mean and sample standard deviation, but it is not clear if the SPY returns are normally distributed.
+<<>>=
+# Plot the density of SPY Weekly Returns
+plot(density(SPY.ret), main="Density of SPY Weekly Returns")
+rug(SPY.ret)
+# sample estimates
+curve(dnorm(x, mean=mean(SPY.ret), sd=sd(SPY.ret)), 
+      add=TRUE, col="red", lty=2, lwd=2)
+# robust estimates
+curve(dnorm(x, mean=median(SPY.ret), sd=mad(SPY.ret)), 
+      add=TRUE, col="blue", lty=2, lwd=2)
+legend("topleft", legend=c("estimated density", "normal density", "robust normal density"), 
+       col=c("black", "red", "blue"), lty=c(1, 2, 2), bty="n", cex=0.8)
+@
+
+Quantile-Quantile plot of SPY weekly returns. It can be seen from the Normal Q-Q plot that the SPY returns have "fat tails".
+<<>>=
+qqnorm(SPY.ret)
+qqline(SPY.ret)
+@
+
+We can test if the SPY weekly returns came from a normal distribution using the Shapiro-Wilk test of normality. The null hypothesis is that the data came from a normal distribution. The p-value is very small and we can reject the null hypothesis.
+<<>>=
+shapiro.test(coredata(SPY.ret))
+@
+
+\subsection{Basic Statistics}
+Here calculate some basic statisitics on the SPY weekly returns.
+<<>>=
+# Sample mean of SPY return
+mean(SPY.ret)
+
+# Sample Variance of SPY returns
+var(SPY.ret)
+
+# Sample standard deviation of SPY returns
+sd(SPY.ret)
+
+# Standard error of SPY returns
+sd(SPY.ret) / sqrt(nrow(SPY.ret))
+
+# Sample skewness of SPY returns
+# See ?skewness for additional methods for calculating skewness
+skewness(SPY.ret, method="sample")
+
+# Sample kurtosis of SPY returns
+# See ?kurtosis for additional methods for calculating kurtosis
+kurtosis(SPY.ret, method="sample")
+
+# Summary statistics of SPY returns
+summary(SPY.ret)
+
+# Sample quantiles of SPY returns
+quantile(SPY.ret, probs=c(0, 0.25, 0.5, 0.75, 1))
+@
+
+
+\end{document}
\ No newline at end of file

Added: pkg/GARPFRM/vignettes/RB.tex
===================================================================
--- pkg/GARPFRM/vignettes/RB.tex	                        (rev 0)
+++ pkg/GARPFRM/vignettes/RB.tex	2014-02-08 17:05:45 UTC (rev 70)
@@ -0,0 +1,1221 @@
+\documentclass[a4paper]{article}\usepackage[]{graphicx}\usepackage[]{color}
+%% maxwidth is the original width if it is less than linewidth
+%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
+\makeatletter
+\def\maxwidth{ %
+  \ifdim\Gin at nat@width>\linewidth
+    \linewidth
+  \else
+    \Gin at nat@width
+  \fi
+}
+\makeatother
+
+\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
+\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
+\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
+\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
+\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
+\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
+\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
+\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
+\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
+\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
+
+\usepackage{framed}
+\makeatletter
+\newenvironment{kframe}{%
+ \def\at at end@of at kframe{}%
+ \ifinner\ifhmode%
+  \def\at at end@of at kframe{\end{minipage}}%
+  \begin{minipage}{\columnwidth}%
+ \fi\fi%
+ \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
+ \colorbox{shadecolor}{##1}\hskip-\fboxsep
+     % There is no \\@totalrightmargin, so:
+     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
+ \MakeFramed {\advance\hsize-\width
+   \@totalleftmargin\z@ \linewidth\hsize
+   \@setminipage}}%
+ {\par\unskip\endMakeFramed%
+ \at at end@of at kframe}
+\makeatother
+
+\definecolor{shadecolor}{rgb}{.97, .97, .97}
+\definecolor{messagecolor}{rgb}{0, 0, 0}
+\definecolor{warningcolor}{rgb}{1, 0, 1}
+\definecolor{errorcolor}{rgb}{1, 0, 0}
+\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX
+
+\usepackage{alltt}
+\usepackage[OT1]{fontenc}
+\usepackage{Sweave}
+\usepackage{Rd}
+\usepackage{amsmath}
+\usepackage{hyperref}
+\usepackage{url}
+\usepackage[round]{natbib}
+\usepackage{bm}
+\usepackage{verbatim}
+\usepackage[latin1]{inputenc}
+\bibliographystyle{abbrvnat}
+
+\let\proglang=\textsf
+%\newcommand{\pkg}[1]{{\fontseries{b}\selectfont #1}}
+%\newcommand{\R}[1]{{\fontseries{b}\selectfont #1}}
+%\newcommand{\email}[1]{\href{mailto:#1}{\normalfont\texttt{#1}}}
+%\newcommand{\E}{\mathsf{E}}
+%\newcommand{\VAR}{\mathsf{VAR}}
+%\newcommand{\COV}{\mathsf{COV}}
+%\newcommand{\Prob}{\mathsf{P}}
+
+\renewcommand{\topfraction}{0.85}
+\renewcommand{\textfraction}{0.1}
+\renewcommand{\baselinestretch}{1.5}
+\setlength{\textwidth}{15cm} \setlength{\textheight}{22cm} \topmargin-1cm \evensidemargin0.5cm \oddsidemargin0.5cm
+
+\usepackage[latin1]{inputenc}
+% or whatever
+
+\usepackage{lmodern}
+\usepackage[T1]{fontenc}
+% Or whatever. Note that the encoding and the font should match. If T1
+% does not look nice, try deleting the line with the fontenc.
+\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
+
+\begin{document}
+
+\title{Quantitative Analysis}
+\author{Ross Bennett}
+
+\maketitle
+
+\begin{abstract}
+The goal of this vignette is to demonstrate key concepts in Financial Risk Manager (FRM \textsuperscript{\textregistered}) Part 1: Quantitative Analysis using R and the GARPFRM package. This vignette will cover exploratory data analysis, basic probability and statistics, and linear regression.
+\end{abstract}
+
+\tableofcontents
+
+\section{Exploratory Data Analysis}
+
+Load the GARPFRM package and the \verb"crsp.short" dataset. Other packages are also loaded for plotting functions. The \verb"crsp.short" dataset contains monthly returns from 1997-01-31 to 2001-12-31 of stocks in micro, small, mid, and large market capitalizations as well as market returns and the risk free rate. The market is defined as the value weighted NYSE and NYSE Amex, the latter formerly being the American Stock Exchange and the NASDAQ composite. The risk free rate comes from the 90 day Treasury Bill.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{suppressMessages}(\hlkwd{library}(GARPFRM))
+\hlkwd{suppressMessages}(\hlkwd{library}(lattice))
+\hlkwd{suppressMessages}(\hlkwd{library}(pcaPP))
+\hlkwd{suppressMessages}(\hlkwd{library}(hexbin))
+\hlkwd{data}(crsp.short)
+
+\hlcom{# Use the first 6 stocks in largecap.ts}
+crsp.returns <- largecap.ts[, 1:6]
+
+\hlcom{# Get the names of the stocks and view the first 5 rows of crsp.returns}
+\hlkwd{colnames}(crsp.returns)
+\end{alltt}
+\begin{verbatim}
+## [1] "AMAT" "AMGN" "CAT"  "DD"   "G"    "GENZ"
+\end{verbatim}
+\begin{alltt}
+\hlkwd{head}(crsp.returns, 5)
+\end{alltt}
+\begin{verbatim}
+##                AMAT     AMGN      CAT        DD        G     GENZ
+## 1997-01-31  0.37391  0.03678 0.036877  0.164675  0.06341  0.28736
+## 1997-02-28  0.02532  0.08426 0.008052 -0.016465 -0.04091 -0.08036
+## 1997-03-31 -0.08395 -0.08589 0.025559 -0.011655 -0.08215 -0.12621
+## 1997-04-30  0.18329  0.05369 0.114019  0.001179  0.17336  0.02778
+## 1997-05-30  0.18907  0.13588 0.096910  0.029494  0.04559  0.03243
+\end{verbatim}
+\end{kframe}
+\end{knitrout}
+
+
+
+Plot of the returns of each asset.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{xyplot}(crsp.returns, scale = \hlkwd{list}(y = \hlstr{"same"}), main = \hlstr{"Monthly Returns"})
+\end{alltt}
+\end{kframe}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-2} 
+
+\end{knitrout}
+
+
+Another way to compare returns of several assets is with a boxplot.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{boxplot}(\hlkwd{coredata}(crsp.returns), pch = 20, main = \hlstr{"Monthly Returns"})
+\end{alltt}
+\end{kframe}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-3} 
+
+\end{knitrout}
+
+
+The exploratory data analysis, basic probability and statistics will use the market returns.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlcom{# Extract the column labeled 'market' to get the market returns}
+MKT.ret <- largecap.ts[, \hlstr{"market"}]
+\end{alltt}
+\end{kframe}
+\end{knitrout}
+
+
+
+Plot of the MKT weekly returns. 
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{plot}(MKT.ret, main = \hlstr{"Market Monthly Returns"})
+\end{alltt}
+\end{kframe}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-5} 
+
+\end{knitrout}
+
+
+A histogram and kernel density estimate of the market returns is plotted to better understand its distribution. 
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{hist}(MKT.ret, probability=TRUE, main=\hlstr{"Histogram of Market Returns"}, 
+     col=\hlstr{"lightblue"}, ylim=\hlkwd{c}(0, 10))
+\hlkwd{lines}(\hlkwd{density}(MKT.ret), lty=2)
+\hlkwd{rug}(MKT.ret)
+\hlkwd{legend}(\hlstr{"topleft"}, legend=\hlstr{"kernel density estimate"}, lty=2,
+       cex=0.8, bty=\hlstr{"n"})
+\end{alltt}
+\end{kframe}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-6} 
+
+\end{knitrout}
+
+
+
+A normal density is overlayed on the plot of the kernel density estimate with standard estimates of the sample mean and standard deviation. Another normal density is overlayed using robust estimates. It is clear from the chart that neither the robust estimates nor the standard estimates of the sample mean and sample standard deviation provide a visually goot fit. It appears that the kernel density estimate of the market returns is bimodal.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlcom{# Plot the kernel density estimate of market returns}
+\hlkwd{plot}(\hlkwd{density}(MKT.ret), main = \hlstr{"Density of Market Returns"})
+\hlkwd{rug}(MKT.ret)
+\hlcom{# sample estimates}
+\hlkwd{curve}(\hlkwd{dnorm}(x, mean = \hlkwd{mean}(MKT.ret), sd = \hlkwd{sd}(MKT.ret)), add = TRUE, col = \hlstr{"red"}, 
+    lty = 2, lwd = 2)
+\hlcom{# robust estimates}
+\hlkwd{curve}(\hlkwd{dnorm}(x, mean = \hlkwd{median}(MKT.ret), sd = \hlkwd{mad}(MKT.ret)), add = TRUE, col = \hlstr{"blue"}, 
+    lty = 2, lwd = 2)
+\hlkwd{legend}(\hlstr{"topleft"}, legend = \hlkwd{c}(\hlstr{"kernel density estimate"}, \hlstr{"normal density estimate"}, 
+    \hlstr{"robust normal density estimate"}), col = \hlkwd{c}(\hlstr{"black"}, \hlstr{"red"}, \hlstr{"blue"}), lty = \hlkwd{c}(1, 
+    2, 2), bty = \hlstr{"n"}, cex = 0.8)
+\end{alltt}
+\end{kframe}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-7} 
+
+\end{knitrout}
+
+
+Quantile-Quantile plot of market returns with a 95\% confidence envelope. It can be seen from the Normal Q-Q plot that the tails of the market returns are well outside of the 95\% confidence envelope.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{chart.QQPlot}(MKT.ret, envelope=0.95, pch=18, main=\hlstr{"Market Returns QQ Plot"},
+             xlab=\hlstr{"Theoretical Normal Quantiles"})
+\hlkwd{legend}(\hlstr{"topleft"}, legend=\hlkwd{c}(\hlstr{"Quartile-Pairs Line"}, \hlstr{"95% Confidence Envelope"}), 
+       col=\hlkwd{c}(\hlstr{"blue"}, \hlstr{"blue"}), lty=\hlkwd{c}(1, 2), cex=0.8, bty=\hlstr{"n"})
+\end{alltt}
+\end{kframe}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-8} 
+
+\end{knitrout}
+
+
+We can test if the market returns came from a normal distribution using the Shapiro-Wilk test of normality. The null hypothesis is that the data came from a normal distribution. The p-value is less than 0.05 and the null hypothesis can be rejected at a 95\% confidence level.
+
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{shapiro.test}(\hlkwd{coredata}(MKT.ret))
+\end{alltt}
+\begin{verbatim}
+## 
+## 	Shapiro-Wilk normality test
+## 
+## data:  coredata(MKT.ret)
+## W = 0.941, p-value = 0.005999
+\end{verbatim}
+\end{kframe}
+\end{knitrout}
+
+
+\subsection{Basic Statistics}
+Here we calculate some basic statisitics on the market returns.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlcom{# Sample mean of SPY return}
+\hlkwd{mean}(MKT.ret)
+\end{alltt}
+\begin{verbatim}
+## [1] 0.009145
+\end{verbatim}
+\begin{alltt}
+
+\hlcom{# Sample Variance of SPY returns}
+\hlkwd{var}(MKT.ret)
+\end{alltt}
+\begin{verbatim}
+##          market
+## market 0.002928
+\end{verbatim}
+\begin{alltt}
+
+\hlcom{# Sample standard deviation of SPY returns}
+\hlkwd{sd}(MKT.ret)
+\end{alltt}
+\begin{verbatim}
+## [1] 0.05411
+\end{verbatim}
+\begin{alltt}
+
+\hlcom{# Standard error of SPY returns}
+\hlkwd{sd}(MKT.ret)/\hlkwd{sqrt}(\hlkwd{nrow}(MKT.ret))
+\end{alltt}
+\begin{verbatim}
+## [1] 0.006985
+\end{verbatim}
+\begin{alltt}
+
+\hlcom{# Sample skewness of SPY returns.  See ?skewness for additional methods}
+\hlcom{# for calculating skewness}
+\hlkwd{skewness}(MKT.ret, method = \hlstr{"sample"})
+\end{alltt}
+\begin{verbatim}
+## [1] -0.7359
+\end{verbatim}
+\begin{alltt}
+
+\hlcom{# Sample kurtosis of SPY returns.  See ?kurtosis for additional methods}
+\hlcom{# for calculating kurtosis}
+\hlkwd{kurtosis}(MKT.ret, method = \hlstr{"sample"})
+\end{alltt}
+\begin{verbatim}
+## [1] 3.309
+\end{verbatim}
+\begin{alltt}
+
+\hlcom{# Summary statistics of SPY returns}
+\hlkwd{summary}(MKT.ret)
+\end{alltt}
+\begin{verbatim}
+##      Index                market        
+##  Min.   :1997-01-31   Min.   :-0.15685  
+##  1st Qu.:1998-04-22   1st Qu.:-0.02675  
+##  Median :1999-07-15   Median : 0.01877  
+##  Mean   :1999-07-15   Mean   : 0.00914  
+##  3rd Qu.:2000-10-07   3rd Qu.: 0.05178  
+##  Max.   :2001-12-31   Max.   : 0.08335
+\end{verbatim}
+\begin{alltt}
+
+\hlcom{# Sample quantiles of SPY returns}
+\hlkwd{quantile}(MKT.ret, probs = \hlkwd{c}(0, 0.25, 0.5, 0.75, 1))
+\end{alltt}
+\begin{verbatim}
+##       0%      25%      50%      75%     100% 
+## -0.15685 -0.02675  0.01877  0.05178  0.08335
+\end{verbatim}
+\end{kframe}
+\end{knitrout}
+
+
+Scatter plot of each pair of assets. This can be usefule to visually look for relationships among the assets.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{pairs}(\hlkwd{coredata}(crsp.returns), pch=20, col=\hlkwd{rgb}(0,0,100,50,maxColorValue=255))
+\end{alltt}
+\end{kframe}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-111} 
+\begin{kframe}\begin{alltt}
+\hlkwd{hexplom}(\hlkwd{coredata}(crsp.returns), varname.cex=0.75)
+\end{alltt}
+\end{kframe}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-112} 
+
+\end{knitrout}
+
+
+Correlation and covariance matrices of the assets.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlcom{# Sample correlation of returns}
+\hlkwd{cor}(crsp.returns)
+\end{alltt}
+\begin{verbatim}
+##        AMAT     AMGN       CAT       DD        G     GENZ
+## AMAT 1.0000  0.32350  0.045105  0.12941 0.218250  0.41977
+## AMGN 0.3235  1.00000 -0.223329 -0.06707 0.303333  0.44068
+## CAT  0.0451 -0.22333  1.000000  0.59569 0.004176 -0.02505
+## DD   0.1294 -0.06707  0.595690  1.00000 0.231427  0.10274
+## G    0.2182  0.30333  0.004176  0.23143 1.000000  0.15096
+## GENZ 0.4198  0.44068 -0.025055  0.10274 0.150958  1.00000
+\end{verbatim}
+\end{kframe}
+\end{knitrout}
+
+
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-13} 
+
+\end{knitrout}
+
+
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlcom{# Sample covariance of returns}
+\hlkwd{cov}(crsp.returns)
+\end{alltt}
+\begin{verbatim}
+##           AMAT       AMGN        CAT         DD         G      GENZ
+## AMAT 0.0322460  0.0071537  7.927e-04  0.0019791 3.761e-03  0.009867
+## AMGN 0.0071537  0.0151647 -2.692e-03 -0.0007034 3.584e-03  0.007103
+## CAT  0.0007927 -0.0026916  9.578e-03  0.0049652 3.921e-05 -0.000321
+## DD   0.0019791 -0.0007034  4.965e-03  0.0072534 1.891e-03  0.001145
+## G    0.0037607  0.0035843  3.921e-05  0.0018913 9.207e-03  0.001896
+## GENZ 0.0098667  0.0071035 -3.210e-04  0.0011454 1.896e-03  0.017134
+\end{verbatim}
+\end{kframe}
+\end{knitrout}
+
+
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-15} 
+
+\end{knitrout}
+
+
+
+\subsection{Distributions}
+R has functions to compute the density, distribution function, quantile, and random number generation for several distributions. The continuous distributions covered in chapter 1 are listed here.
+\begin{itemize}
+\item Normal Distribution: \verb"dnorm", \verb"pnorm", \verb"qnorm", \verb"rnorm"
+
+\item Chi-Squared Distribution: \verb"dchisq", \verb"pchisq", \verb"qchisq", \verb"rchisq"
+
+\item Student t Distribution: \verb"dt", \verb"pt", \verb"qt", \verb"rt"
+
+\item F Distribution: \verb"df", \verb"pf", \verb"qf", \verb"rf"
+\end{itemize}
+
+In general, the functions are as follows:
+\begin{itemize}
+\item d*: density
+\item p*: distribution function (probability)
+\item q*: quantile function
+\item r*: random generation
+\end{itemize}
+where * is the appropriate distribution.
+
+Here we demonstrate these functions for the normal distribution.
+
+Use dnorm to plot the pdf of a standard normal distribution
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{curve}(\hlkwd{dnorm}(x), from = -4, to = 4, main = \hlstr{"Standard Normal pdf"})
+\end{alltt}
+\end{kframe}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-16} 
+
+\end{knitrout}
+
+
+Calculate the probability that $Y \leq 2$ when $Y$ is distributed $N(1, 4)$ with mean of 1 and variance of 4.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{pnorm}(q = 2, mean = 1, sd = 2)
+\end{alltt}
+\begin{verbatim}
+## [1] 0.6915
+\end{verbatim}
+\begin{alltt}
+\hlcom{# Normalize as is done in the book}
+\hlkwd{pnorm}(q = 0.5)
+\end{alltt}
+\begin{verbatim}
+## [1] 0.6915
+\end{verbatim}
+\end{kframe}
+\end{knitrout}
+
+
+Quantile function of the standard normal distribution at probability 0.975.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{qnorm}(p = 0.975)
+\end{alltt}
+\begin{verbatim}
+## [1] 1.96
+\end{verbatim}
+\end{kframe}
+\end{knitrout}
+
+
+Generate 10 random numbers from a normal distribution with mean 0.0015 and standard deviation 0.025.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlcom{# Set the seed for reproducible results}
+\hlkwd{set.seed}(123)
+\hlkwd{rnorm}(n = 10, mean = 0.0015, sd = 0.025)
+\end{alltt}
+\begin{verbatim}
+##  [1] -0.012512 -0.004254  0.040468  0.003263  0.004732  0.044377  0.013023
+##  [8] -0.030127 -0.015671 -0.009642
+\end{verbatim}
+\end{kframe}
+\end{knitrout}
+
+
+\subsection{Hypothesis Test}
+The null hypothesis is that the true mean return of the market is equal to 0.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{t.test}(x=MKT.ret, alternative=\hlstr{"two.sided"}, mu=0)
+\end{alltt}
+\begin{verbatim}
+## 
+## 	One Sample t-test
+## 
+## data:  MKT.ret
+## t = 1.309, df = 59, p-value = 0.1956
+## alternative hypothesis: true mean is not equal to 0
+## 95 percent confidence interval:
+##  -0.004833  0.023122
+## sample estimates:
+## mean of x 
+##  0.009145
+\end{verbatim}
+\end{kframe}
+\end{knitrout}
+
+The p-value is greater than 0.05 so the null hypothesis cannot be rejected at a 95\% confidence level.
+
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlcom{# Replicate the results of t.test using the method outlined in the book}
+t_stat <- (\hlkwd{mean}(MKT.ret) - 0)/(\hlkwd{sd}(MKT.ret)/\hlkwd{sqrt}(\hlkwd{nrow}(MKT.ret)))
+p_value <- 2 * \hlkwd{pt}(q = -\hlkwd{abs}(t_stat), df = \hlkwd{nrow}(MKT.ret))
+df <- \hlkwd{nrow}(MKT.ret) - 1
+ci <- \hlkwd{mean}(MKT.ret) + \hlkwd{c}(-1, 1) * 1.96 * \hlkwd{sd}(MKT.ret)/\hlkwd{sqrt}(\hlkwd{nrow}(MKT.ret))
+\hlkwd{paste}(\hlstr{"t = "}, \hlkwd{round}(t_stat, 4), \hlstr{", df = "}, df, \hlstr{", p-value = "}, \hlkwd{round}(p_value, 
+    4), sep = \hlstr{""})
+\end{alltt}
+\begin{verbatim}
+## [1] "t = 1.3091, df = 59, p-value = 0.1955"
+\end{verbatim}
+\begin{alltt}
+\hlkwd{print}(\hlstr{"95% Confidence Interval"})
+\end{alltt}
+\begin{verbatim}
+## [1] "95% Confidence Interval"
+\end{verbatim}
+\begin{alltt}
+\hlkwd{print}(ci)
+\end{alltt}
+\begin{verbatim}
+## [1] -0.004546  0.022836
+\end{verbatim}
+\end{kframe}
+\end{knitrout}
+
+Note that the confidence interval is different. This is because the \verb"t.test" function calculates the exact confidence interval. Using a value of 1.96 is an approximation.
+
+\section{Regression}
+\subsection{Regression with a single regressor}
+
+The general form of a linear model is:
+\begin{equation}
+Y_i = \beta_0 + \beta_i X_i + u_i
+\end{equation}
+
+where:
+\begin{itemize}
+\item[$Y_i$]{ is the dependent variable}
+\item[$X_i$]{ is the independent variable}
+\item[$\beta_0$]{ is the intercept of the population regression line}
+\item[$\beta_1$]{ is the slope of the population regression line}
+\item[$u_i$]{ is the error term}
+\end{itemize}
+
+In the following linear model, CAT excess returns is the dependent variable and market excess returns is the independent variable. This model will be used to demonstrate linear regression in R.
+
+Extract the weekly returns of CAT from the returns object.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+CAT.ret <- crsp.returns[, \hlstr{"CAT"}] - largecap.ts[, \hlstr{"t90"}]
+MKT.ret <- largecap.ts[, \hlstr{"market"}] - largecap.ts[, \hlstr{"t90"}]
+
+\hlcom{# Fitting linear models works with xts objects, but works better with}
+\hlcom{# data.frame objects. This is especially true with the predict method for}
+\hlcom{# linear models.}
+ret.data <- \hlkwd{as.data.frame}(\hlkwd{cbind}(CAT.ret, MKT.ret))
+\end{alltt}
+\end{kframe}
+\end{knitrout}
+
+
+Scatterplot of CAT and market excess returns.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlkwd{plot}(\hlkwd{coredata}(MKT.ret), \hlkwd{coredata}(CAT.ret), pch=19,
+     col=\hlkwd{rgb}(0,0,100,50,maxColorValue=255),
+     xlab=\hlstr{"Market returns"}, ylab=\hlstr{"CAT returns"},
+     main=\hlstr{"CAT vs. Market Excess Returns"})
+\end{alltt}
+\end{kframe}
+\includegraphics[width=\maxwidth]{figure/unnamed-chunk-23} 
+
+\end{knitrout}
+
+
+Fit the linear regression model. \verb"CAT.ret" is the response variable and \verb"MKT.ret" is the explanatory variable. That is, a linear model will be fit using market excesss returns to describe CAT excess returns.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+model.fit <- \hlkwd{lm}(CAT ~ market, data = ret.data)
+\end{alltt}
+\end{kframe}
+\end{knitrout}
+
+
+The \verb"print" and \verb"summary" methods for \verb"lm" objects are very useful and provide several of the statistics covered in the book. Note that \verb"summary(model.fit)" will print the summary statisitcs, but it is often useful to assign the summary object to a variable so that elements from the summary object can be extracted as shown below.
+\begin{knitrout}
+\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
+\begin{alltt}
+\hlcom{# The print method displays the call and the coefficients of the linear}
+\hlcom{# model}
+model.fit
+\end{alltt}
+\begin{verbatim}
+## 
+## Call:
+## lm(formula = CAT ~ market, data = ret.data)
+## 
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/uwgarp -r 70


More information about the Uwgarp-commits mailing list