Coursera Computational Finance and Financial Econometrics Assignment 5 Week 5

Lab

The following questions require R. On the class web page are the script files lab5.r and descriptiveStatistics.r. The former
contains hints for completing the assignment, and the latter contains the code for the doing the examples from class.

As in previous labs do NOT submit any output directly. There is an associated homework, “Assignment 5: R”, that has questions
related to the output of this lab.

In this lab, you will analyze continuously compounded monthly return data on the Vanguard long term bond index fund (VBLTX),
Fidelity Magellan stock mutual fund (FMAGX), and Starbucks stock (SBUX). I encourage you to go to finance.yahoo.com and research
these assets. The script file lab5.r walks you through all of the computations for the lab.

You will use the get.hist.quote() function from the tseries package to automatically load this data into R. You will also use
SEVERAL functions from the PerformanceAnalytics package. Remember to install packages before you load them into R.

Exercise

(I) Univariate Graphical Analysis

(1) Make time plots of the return data using the R command plot() as illustrated in the script file lab5.r. Think about any
relationships between the returns suggested by the plots. Pay particular attention to the behavior of returns toward the END of
2008 at the BEGINNING of the financial crisis.

(2) Make a cumulative return plot (future of $1 invested in each asset). Which assets gave the best and worst future values over
the investment horizon?

(3) For EACH return series, make a FOUR(4)-panel plot containing a histogram, density plot, boxplot and normal QQ-plot. Do the
return series look normally distributed?

(II) Univariate Numerical Summary Statistics

(1) Compute numerical descriptive statistics for ALL assets using the R functions summary(), mean(), var(), stdev(), skewness(),
and kurtosis() (in package PerformanceAnalytics). Which asset appears to be the riskiest asset?

(2) Using the mean monthly return for EACH asset, compute an estimate of the annual continuously compounded return (i.e. recall
the relationship between the expected monthly cc return and the expected annual cc return). Convert this annual cc return into a
simple annual return. Are there any surprises?

(3) Using the estimate of the monthly return standard deviation for EACH asset, compute an estimate of the annual return standard
deviation.

(III) Bivariate Graphical Analysis

Use the R pairs() function to create all pair-wise scatterplots of returns.

(IV) Bivariate Numerical Summary Statistics

Use the R functions var() and cor() to compute the sample covariance matrix and sample correlation matrix of the returns.

(V) Time Series Summary Statistics

Use the R function acf() to compute and plot the sample autocorrelation functions of EACH return. Do the returns appear to be
uncorrelated over time?

R Code

library(PerformanceAnalytics)
## Loading required package: zoo
## Attaching package: 'zoo'
## The following object(s) are masked from 'package:base':
## 
## as.Date, as.Date.numeric
## Loading required package: xts
## Attaching package: 'PerformanceAnalytics'
## The following object(s) are masked from 'package:graphics':
## 
## legend
library(zoo)
library(tseries)
## Loading required package: quadprog

#
# |------------------------------------------------------------------------------------------|
# | I N I T I A L I Z A T I O N |
# |------------------------------------------------------------------------------------------|
Init <- function(..., workDirStr = "C:/Users/denbrige/100 FxOption/103 FxOptionVerBack/080 Fx Git/R-source") {
    setwd(workDirStr)

    # --- Assert create an empty zoo
    mergeZoo <- zoo(0)

    for (tickerChr in c(...)) {
        tickerZoo = suppressWarnings(get.hist.quote(instrument = tickerChr, 
            start = "1998-01-01", end = "2009-12-31", quote = "AdjClose", provider = "yahoo", 
            origin = "1970-01-01", compression = "m", retclass = "zoo"))
        # --- Change class of time index to yearmon which is appropriate for
        # monthly data index() and as.yearmon() are functions in the zoo package
        index(tickerZoo) = as.yearmon(index(tickerZoo))

        # --- Create merged price data
        if (is.null(nrow(mergeZoo))) 
            mergeZoo <- tickerZoo else mergeZoo <- merge(mergeZoo, tickerZoo)
    }

    # --- Rename columns
    colnames(mergeZoo) = c(...)

    # --- Return value is a zoo
    return(mergeZoo)
}

#
# |------------------------------------------------------------------------------------------|
# | I N T E R N A L F U N C T I O N S |
# |------------------------------------------------------------------------------------------|

#
# |------------------------------------------------------------------------------------------|
# | M A I N P R O C E D U R E |
# |------------------------------------------------------------------------------------------|
# --- Init loading data
rawDataZoo <- Init("VBLTX", "FMAGX", "SBUX")
## time series starts 1998-01-02
## time series ends   2009-12-01
## time series starts 1998-01-02
## time series ends   2009-12-01
## time series starts 1998-01-02
## time series ends   2009-12-01
# --- Count of cols of data
ncol(rawDataZoo)
## [1] 3
# --- Count of rows of data
nrow(rawDataZoo)
## [1] 144
# --- Names of header
names(rawDataZoo)
## [1] "VBLTX" "FMAGX" "SBUX"
# --- Peek at data
head(rawDataZoo)
##          VBLTX FMAGX SBUX
## Jan 1998  4.70 48.24 4.39
## Feb 1998  4.68 51.90 4.76
## Mar 1998  4.69 54.50 5.45
## Apr 1998  4.71 55.14 5.79
## May 1998  4.80 54.06 5.77
## Jun 1998  4.90 56.36 6.42
# --- Calculate cc returns as difference in log prices
retDataZoo <- diff(log(rawDataZoo))

#
# |------------------------------------------------------------------------------------------|
# | P A R T O N E P R O C E D U R E |
# |------------------------------------------------------------------------------------------|
# --- Assert FOUR (4) plots on the same chart
layout(matrix(1:3, 3, 1, byrow = TRUE))

# --- THREE(3)-panel plot (each y axis has different scale) Note: The
# generic plot() function invokes the plot method for objects of class
# zoo.  See the help on plot.zoo
plot(retDataZoo, col = "blue", lwd = 2, main = "Monthly cc returns on 3 assets")

plot of chunk unnamed-chunk-1


# --- Assert ONE (1) plot on the same chart
layout(matrix(1:1, 1, 1, byrow = TRUE))

plot(retDataZoo, plot.type = "single", col = c("yellow", "blue", "red"), lwd = 2, 
    main = "Monthly cc returns on 3 assets (base)", ylab = "Return", ylim = c(-0.5, 
        0.5))
legend(x = "bottom", legend = colnames(retDataZoo), col = c("yellow", "blue", 
    "red"), lwd = 2)
abline(h = 0)

plot of chunk unnamed-chunk-1


# --- Plot returns using the PerformanceAnalytics function
# chart.TimeSeries() This creates a slightly nicer looking plot that
# plot.zoo().
chart.TimeSeries(retDataZoo, legend.loc = "bottom", main = "Monthly cc returns on 3 assets (PerformanceAnalytics)")

plot of chunk unnamed-chunk-1


# --- Cumulative return plot - must use simple returns and not cc returns
# for this Use PerformanceAnalytics function chart.CumReturns()
chart.CumReturns(diff(rawDataZoo)/lag(rawDataZoo, k = -1), legend.loc = "topleft", 
    wealth.index = TRUE, main = "Future Value of $1 invested")

plot of chunk unnamed-chunk-1


# --- Create matrix of return data.  Some core R functions don't work
# correctly with zoo objects
retDataMat = coredata(retDataZoo)
class(retDataMat)
## [1] "matrix"
colnames(retDataMat)
## [1] "VBLTX" "FMAGX" "SBUX"
head(retDataMat)
##          VBLTX    FMAGX     SBUX
## [1,] -0.004264  0.07313  0.08092
## [2,]  0.002134  0.04888  0.13537
## [3,]  0.004255  0.01167  0.06052
## [4,]  0.018928 -0.01978 -0.00346
## [5,]  0.020619  0.04167  0.10675
## [6,] -0.006141 -0.00748 -0.24400

# --- Assert FOUR (4) plots on the same chart
par(mfrow = c(2, 2))

# --- Here are the FOUR(4)-panel plots
hist(retDataMat[, "VBLTX"], main = "VBLTX monthly returns", xlab = "VBLTX", 
    probability = T, col = "slateblue1")
boxplot(retDataMat[, "VBLTX"], outchar = T, main = "Boxplot", col = "slateblue1")
plot(density(retDataMat[, "VBLTX"]), type = "l", main = "Smoothed density", 
    xlab = "monthly return", ylab = "density estimate", col = "slateblue1")
qqnorm(retDataMat[, "VBLTX"], col = "slateblue1")
qqline(retDataMat[, "VBLTX"])

plot of chunk unnamed-chunk-1


par(mfrow = c(2, 2))

hist(retDataMat[, "FMAGX"], main = "FMAGX monthly returns", xlab = "FMAGX", 
    probability = T, col = "slateblue1")
boxplot(retDataMat[, "FMAGX"], outchar = T, main = "Boxplot", col = "slateblue1")
plot(density(retDataMat[, "FMAGX"]), type = "l", main = "Smoothed density", 
    xlab = "monthly return", ylab = "density estimate", col = "slateblue1")
qqnorm(retDataMat[, "FMAGX"], col = "slateblue1")
qqline(retDataMat[, "FMAGX"])

plot of chunk unnamed-chunk-1


par(mfrow = c(2, 2))

hist(retDataMat[, "SBUX"], main = "SBUX monthly returns", xlab = "SBUX", probability = T, 
    col = "slateblue1")
boxplot(retDataMat[, "SBUX"], outchar = T, main = "Boxplot", col = "slateblue1")
plot(density(retDataMat[, "SBUX"]), type = "l", main = "Smoothed density", xlab = "monthly return", 
    ylab = "density estimate", col = "slateblue1")
qqnorm(retDataMat[, "SBUX"], col = "slateblue1")
qqline(retDataMat[, "SBUX"])

plot of chunk unnamed-chunk-1


# --- Show boxplot of three series on one plot
par(mfrow = c(1, 1))

boxplot(retDataMat[, "VBLTX"], retDataMat[, "FMAGX"], retDataMat[, "SBUX"], 
    names = colnames(retDataMat), col = "slateblue1", main = "Return Distribution Comparison (base)")

plot of chunk unnamed-chunk-1


# --- Do the same thing using the PerformanceAnalytics function
# chart.Boxplot()
par(mfrow = c(1, 1))

chart.Boxplot(retDataZoo, main = "Return Distribution Comparison (PerformanceAnalytics)")

plot of chunk unnamed-chunk-1


#
# |------------------------------------------------------------------------------------------|
# | P A R T T W O P R O C E D U R E |
# |------------------------------------------------------------------------------------------|
# --- Compute univariate descriptive statistics
summary(retDataMat)
##      VBLTX              FMAGX               SBUX        
##  Min.   :-0.09168   Min.   :-0.24350   Min.   :-0.4796  
##  1st Qu.:-0.00984   1st Qu.:-0.02045   1st Qu.:-0.0488  
##  Median : 0.00914   Median : 0.00989   Median : 0.0181  
##  Mean   : 0.00530   Mean   : 0.00189   Mean   : 0.0113  
##  3rd Qu.: 0.01960   3rd Qu.: 0.03970   3rd Qu.: 0.0865  
##  Max.   : 0.10807   Max.   : 0.13023   Max.   : 0.2769

# --- Compute descriptive statistics by column using the base R function
# apply() Note: skewness() and kurtosis() are in the package
# PerformanceAnalytics Note: kurtosis() returns excess kurtosis
apply(retDataMat, 2, mean)
##    VBLTX    FMAGX     SBUX 
## 0.005301 0.001888 0.011328
apply(retDataMat, 2, var)
##     VBLTX     FMAGX      SBUX 
## 0.0006837 0.0032063 0.0142496
apply(retDataMat, 2, sd)
##   VBLTX   FMAGX    SBUX 
## 0.02615 0.05662 0.11937
apply(retDataMat, 2, skewness)
##   VBLTX   FMAGX    SBUX 
## -0.1451 -1.0330 -0.8983
apply(retDataMat, 2, kurtosis)
## VBLTX FMAGX  SBUX 
## 2.875 2.628 2.612

# --- A nice PerformanceAnalytics function that computes all of the
# relevant descriptive statistics is table.Stats()
table.Stats(retDataZoo)
##                    VBLTX    FMAGX     SBUX
## Observations    143.0000 143.0000 143.0000
## NAs               0.0000   0.0000   0.0000
## Minimum          -0.0917  -0.2435  -0.4796
## Quartile 1       -0.0098  -0.0204  -0.0488
## Median            0.0091   0.0099   0.0181
## Arithmetic Mean   0.0053   0.0019   0.0113
## Geometric Mean    0.0050   0.0002   0.0035
## Quartile 3        0.0196   0.0397   0.0865
## Maximum           0.1081   0.1302   0.2769
## SE Mean           0.0022   0.0047   0.0100
## LCL Mean (0.95)   0.0010  -0.0075  -0.0084
## UCL Mean (0.95)   0.0096   0.0112   0.0311
## Variance          0.0007   0.0032   0.0142
## Stdev             0.0261   0.0566   0.1194
## Skewness         -0.1451  -1.0330  -0.8983
## Kurtosis          2.8748   2.6284   2.6116

# --- Annualize monthly estimates Annualized cc mean
12 * apply(retDataMat, 2, mean)
##   VBLTX   FMAGX    SBUX 
## 0.06361 0.02265 0.13593
# Annualized simple mean
exp(12 * apply(retDataMat, 2, mean)) - 1
##   VBLTX   FMAGX    SBUX 
## 0.06568 0.02291 0.14560
# Annualized sd values
sqrt(12) * apply(retDataMat, 2, sd)
##   VBLTX   FMAGX    SBUX 
## 0.09058 0.19615 0.41352

#
# |------------------------------------------------------------------------------------------|
# | P A R T T H R E E P R O C E D U R E |
# |------------------------------------------------------------------------------------------|
# --- Compute bivariate descriptive statistics
par(mfrow = c(1, 1))

pairs(retDataMat, col = "slateblue1", pch = 16)

plot of chunk unnamed-chunk-1


#
# |------------------------------------------------------------------------------------------|
# | P A R T F O U R P R O C E D U R E |
# |------------------------------------------------------------------------------------------|
# --- Compute 3 x 3 covariance and correlation matrices
var(retDataMat)
##            VBLTX     FMAGX       SBUX
## VBLTX  6.837e-04 7.867e-05 -0.0001875
## FMAGX  7.867e-05 3.206e-03  0.0030325
## SBUX  -1.875e-04 3.033e-03  0.0142496
cor(retDataMat)
##          VBLTX   FMAGX     SBUX
## VBLTX  1.00000 0.05314 -0.06007
## FMAGX  0.05314 1.00000  0.44864
## SBUX  -0.06007 0.44864  1.00000

#
# |------------------------------------------------------------------------------------------|
# | P A R T F I V E P R O C E D U R E |
# |------------------------------------------------------------------------------------------|
# --- Compute time series diagnostics Autocorrelations
par(mfrow = c(1, 1))

acf(retDataMat[, "VBLTX"], main = "Autocorrelation VBLTX")

plot of chunk unnamed-chunk-1


par(mfrow = c(1, 1))

acf(retDataMat[, "FMAGX"], main = "Autocorrelation FMAGX")

plot of chunk unnamed-chunk-1


par(mfrow = c(1, 1))

acf(retDataMat[, "SBUX"], main = "Autocorrelation SBUX")

plot of chunk unnamed-chunk-1


#
# |------------------------------------------------------------------------------------------|
# | E N D O F S C R I P T |
# |------------------------------------------------------------------------------------------|