Descriptive Statistics for Financial Time Series

Eric Zivot

Monday, April 27, 2015

Set options and load packages

options(digits=4, width=70)
library(car)
library(corrplot)
library(dygraphs)
library(IntroCompFinR)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(PerformanceAnalytics)
## 
## Attaching package: 'PerformanceAnalytics'
## 
## The following object is masked from 'package:graphics':
## 
##     legend
library(sn)
## Loading required package: stats4
## 
## Attaching package: 'sn'
## 
## The following object is masked from 'package:stats':
## 
##     sd
library(tseries)
library(zoo)
Sys.setenv(TZ="UTC")

Get data from introCompfinR package

# get daily prices on Microsoft and S&P500 Index
data(msftDailyPrices, sp500DailyPrices)
str(msftDailyPrices)
## An 'xts' object on 1993-01-04/2014-12-31 containing:
##   Data: num [1:5541, 1] 1.89 1.92 1.98 1.94 1.94 1.98 2 2.03 2.03 2 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr "MSFT"
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
##  NULL
# extract end-of-month prices
msftPrices = to.monthly(msftDailyPrices, OHLC=FALSE)
sp500Prices = to.monthly(sp500DailyPrices, OHLC=FALSE)

Create xts objects with both series

msftSp500DailyPrices = merge(msftDailyPrices, sp500DailyPrices)
msftSp500Prices = merge(msftPrices, sp500Prices)
# show data
head(msftSp500DailyPrices, n=3)
##            MSFT SP500
## 1993-01-04 1.89 435.4
## 1993-01-05 1.92 434.3
## 1993-01-06 1.98 434.5
head(msftSp500Prices, n=3)
##          MSFT SP500
## Jan 1993 1.92 438.8
## Feb 1993 1.85 443.4
## Mar 1993 2.06 451.7

Calculate simple and cc returns

msftRetS = Return.calculate(msftPrices, method="simple")
msftDailyRetS = Return.calculate(msftDailyPrices, method="simple")
sp500RetS = Return.calculate(sp500Prices, method="simple")
sp500DailyRetS = Return.calculate(sp500DailyPrices, method="simple")
msftSp500RetS = Return.calculate(msftSp500Prices, method="simple")
msftSp500DailyRetS = Return.calculate(msftSp500DailyPrices, method="simple")
# show data
head(msftSp500DailyRetS, n=3)
##               MSFT      SP500
## 1993-01-04      NA         NA
## 1993-01-05 0.01587 -0.0023887
## 1993-01-06 0.03125  0.0004144
head(msftSp500RetS, n=3)
##              MSFT   SP500
## Jan 1993       NA      NA
## Feb 1993 -0.03646 0.01048
## Mar 1993  0.11351 0.01870

Remove first NA observations

# Note: can alternatively use na.omit() to eliminate NA values
msftRetS = msftRetS[-1]
msftDailyRetS = msftDailyRetS[-1]
sp500RetS = sp500RetS[-1]
sp500DailyRetS = sp500DailyRetS[-1]
msftSp500RetS = msftSp500RetS[-1]
msftSp500DailyRetS = msftSp500DailyRetS[-1]

Compute cc returns

msftRetC = log(1 + msftRetS)
sp500RetC = log(1 + sp500RetS)
msftSp500RetC = merge(msftRetC, sp500RetC)
# show data
head(msftSp500RetC, n=3)
##              MSFT    SP500
## Feb 1993 -0.03714  0.01043
## Mar 1993  0.10752  0.01852
## Apr 1993 -0.08085 -0.02575

Plot individual prices in two panel graph

plot.zoo(msftSp500Prices, main="", lwd=2, col="blue")