R: Companion to Tsay (2005) Analysis of Financial Time Series
FinTS-package
R Documentation
Companion to Tsay (2005) Analysis of Financial
Time Series
Description
R companion to Tsay (2005)
Analysis of Financial Time Series, 2nd ed. (Wiley).
Includes data sets, functions and script files
required to work some of the examples. Version 0.2-x
includes R objects for all data files used in the text
and script files to recreate most of the analyses in
chapters 1-3 and 9 plus parts of chapters 4 and 11.
ARIMA Arima with Ljung-Box
Acf Autocorrelation Function
ArchTest ARCH LM Test
AutocorTest Box-Ljung autocorrelation test
FinTS.stats Financial Time Series summary statistics
TsayFiles List of the names of files downloaded from the
"Analysis of Financial Data" web site.
Unitroot unit root tests
apca Asymptotic PCA
as.yearmon2 Conditionally convert x to yearmon if the
conversion is unqique, retaining x as names.
ch01data financial time series for Tsay (2005, chapter
1[text])
compoundInterest compute compound interest
findConjugates Find complex conjugate pairs
package.dir Directory of a package
plot.loadings Plot loadings
plotArmaTrueacf plot the theoretical ACF corresponding to an
ARMA model
read.yearmon Reading Monthly zoo Series
runscript Run a package script
url2data Create local copies of files read from urls.
See the scripts subdirectory of the FinTS installation
directory = system.file(package='FinTS').
Corrections to the script files provided and contributions to script
files for other chapters will be graciously accepted.
# Where is the 'FinTS' directory?
system.file(package='FinTS')
# View the script file 'ch01.R', which is in the 'scripts'
# subdirectory of the system.file(package='FinTS') directory:
runscript(1, 'view')
# SP statistics in Table 1.2 of Tsay
data(d.ibmvwewsp6203)
FinTS.stats(100*d.ibmvwewsp6203[, "SP"])
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(FinTS)
Loading required package: zoo
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Date.numeric
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/FinTS/FinTS-package.Rd_%03d_medium.png", width=480, height=480)
> ### Name: FinTS-package
> ### Title: Companion to Tsay (2005) Analysis of Financial Time Series
> ### Aliases: FinTS-package FinTS
> ### Keywords: package ts
>
> ### ** Examples
>
> # Where is the 'FinTS' directory?
> system.file(package='FinTS')
[1] "/home/ddbj/local/lib64/R/library/FinTS"
>
> # View the script file 'ch01.R', which is in the 'scripts'
> # subdirectory of the system.file(package='FinTS') directory:
> runscript(1, 'view')
### ch. 1. Financial Time Series and Their Characteristics
###
###
### Ruey S. Tsay (2005)
### Analysis of Financial Time Series, 2nd ed.
### (Wiley)
###
###
library(FinTS)
##
## sec. 1.1. Assett Returns
##
# p. 4
# Table 1.1. Illustration of the effects of compounding
freqs <- c(1, 2, 4, 12, 52, 365, Inf)
cI <- compoundInterest(0.1, frequency=freqs)
(Table1.1 <- data.frame(Type=c(
"Annual", "Semiannual", "Quarterly",
"Monthly", "Weekly", "Daily", "Continuously"),
Number.of.payments=freqs,
Interest.rate.per.period=0.1/freqs,
Net.Value=cI) )
# p. 6
# Example 1.1.
logRtn <- .0446
100*expm1(logRtn)
mo.logRtns <- c(.0446, -.0734, .1077)
(q.logRtns <- sum(mo.logRtns))
##
## sec. 1.2. Distributional Properties of Returns
##
# p. 7
# sec. 1.2.1. Review of Statistical Distributions and their Moments
# p. 10
# Example 1.2.
data(d.ibmvwewsp6203)
s.ibm <- sd(d.ibmvwewsp6203[, "IBM"])
(skew.ibm <- skewness(d.ibmvwewsp6203[, "IBM"]))
(n.ibm <- dim(d.ibmvwewsp6203)[1])
(skew.sd <- sqrt(6/n.ibm))
(t.sk <- (skew.ibm/skew.sd))
pnorm(-t.sk)
# A slightly more accurate version of this is
# agostino.test
library(moments)
(skew.test <- agostino.test(as.numeric(d.ibmvwewsp6203[, "IBM"])))
# p. 11
# Table 1.2. Descriptive Statistics for Daily and Monthly
# Simple and Log Returns for Selected Indexes and Stocks
data(d.ibmvwewsp6203)
data(d.intc7303)
data(d.3m6203)
data(d.msft8603)
data(d.c8603)
(Daily.Simple.Returns.pct <- rbind(
FinTS.stats(100*d.ibmvwewsp6203[, "SP"]),
FinTS.stats(100*d.ibmvwewsp6203[, "VW"]),
FinTS.stats(100*d.ibmvwewsp6203[, "EW"]),
FinTS.stats(100*d.ibmvwewsp6203[, "IBM"]),
FinTS.stats(100*d.intc7303[,"Intel"]),
FinTS.stats(100*d.3m6203[, "MMM"]),
FinTS.stats(100*d.msft8603[, 'MSFT']),
FinTS.stats(100*d.c8603[, "C"])
) )
(Daily.log.Returns.pct <- rbind(
FinTS.stats(100*log(1+d.ibmvwewsp6203[, "SP"])),
FinTS.stats(100*log(1+d.ibmvwewsp6203[, "VW"])),
FinTS.stats(100*log(1+d.ibmvwewsp6203[, "EW"])),
FinTS.stats(100*log(1+d.ibmvwewsp6203[, "IBM"])),
FinTS.stats(100*log(1+d.intc7303[,"Intel"])),
FinTS.stats(100*log(1+d.3m6203[, "MMM"])),
FinTS.stats(100*log(1+d.msft8603[, 'MSFT'])),
FinTS.stats(100*log(1+d.c8603[, "C"]))
) )
data(m.ibmvwewsp2603)
data(m.intc7303)
data(m.3m4603)
data(m.msft8603)
data(m.c8603)
(Monthly.Simple.Returns.pct <- rbind(
SP=FinTS.stats(100*m.ibmvwewsp2603[, "SP"]),
VW=FinTS.stats(100*m.ibmvwewsp2603[, "VW"]),
EW=FinTS.stats(100*m.ibmvwewsp2603[, "EW"]),
IBM=FinTS.stats(100*m.ibmvwewsp2603[, "IBM"]),
Intel=FinTS.stats(100*m.intc7303[,"Intel"]),
MMM=FinTS.stats(100*m.3m4603[, "MMM"]),
Microsoft=FinTS.stats(100*m.msft8603[, 'MSFT']),
CitiGroup=FinTS.stats(100*m.c8603[, "C"])
) )
(Monthly.log.Returns.pct <- rbind(
SP=FinTS.stats(100*log(1+m.ibmvwewsp2603[, "SP"])),
VW=FinTS.stats(100*log(1+m.ibmvwewsp2603[, "VW"])),
EW=FinTS.stats(100*log(1+m.ibmvwewsp2603[, "EW"])),
IBM=FinTS.stats(100*log(1+m.ibmvwewsp2603[, "IBM"])),
Intel=FinTS.stats(100*log(1+m.intc7303[,"Intel"])),
MMM=FinTS.stats(100*log(1+m.3m4603[, "MMM"])),
Microsoft=FinTS.stats(100*log(1+m.msft8603[, 'MSFT'])),
CitiGroup=FinTS.stats(100*log(1+m.c8603[, "C"]))
) )
dimnames(Daily.Simple.Returns.pct)[[1]] <-
c("SP", "VW", "EW", "IBM", "Intel", "3M", "MSFT", "Citi")
dimnames(Daily.log.Returns.pct)[[1]] <-
c("SP", "VW", "EW", "IBM", "Intel", "3M", "MSFT", "Citi")
dimnames(Monthly.Simple.Returns.pct)[[1]] <-
c("SP", "VW", "EW", "IBM", "Intel", "3M", "MSFT", "Citi")
dimnames(Monthly.log.Returns.pct)[[1]] <-
c("SP", "VW", "EW", "IBM", "Intel", "3M", "MSFT", "Citi")
#write.table(rbind(Daily.Simple.Returns.pct, Daily.log.Returns.pct,
# Monthly.Simple.Returns.pct, Monthly.log.Returns.pct),"Table1-2.csv", sep=",")
# p. 12
# Comparable to S-Plus Demonstration
# module(finmetrics)
# x = matrix(scan(file = 'd-ibmvwewsp6203.txt'), 5) % load the data
# ibm = x[2,]*100 % compute percentage returns
data(d.ibmvwewsp6203)
quantile(100*as.numeric(d.ibmvwewsp6203[, "IBM"]))
FinTS.stats(100*d.ibmvwewsp6203[, "IBM"])
# p. 15
# Sec. 1.2.2. Distributions of Returns
# p. 16
# Figure 1.1. Comparisons of Finite Mixture, Stable
# and Standard Normal Distributions
library(distrEx)
N01 <- Norm()
N04 <- Norm(0, 4)
contamNorm <- ConvexContamination(N01, N04, size = 0.05)
plot(dnorm, xlim=c(-4, 4))
text(.75, .39, "Normal")
x <- seq(-4, 4, len=201)
lines(x, d(contamNorm)(x), lty="dotted", col="red", lwd=2)
text(0, 0.351, "Mixture", col="red")
lines(x, dcauchy(x), lty="dashed", col="green", lwd=2)
text(0, 0.25, "Cauchy", col="green")
# p. 17
# Sec. 1.2.5. Empirical properties of returns
# p. 18
# Figure 1.2. Time plots of monthly returns of IBM stock
op <- par(mfrow=c(2,1))
plot(m.ibmvwewsp2603[, "IBM"], xlab="year", ylab="s-rtn")
plot(log(1+m.ibmvwewsp2603[, "IBM"]), xlab="year", ylab="log-rtn")
par(op)
# Figure 1.3. Time plots of monthly returens of the value-weighted index
op <- par(mfrow=c(2,1))
plot(m.ibmvwewsp2603[, "VW"], xlab="year", ylab="s-rtn")
plot(log(1+m.ibmvwewsp2603[, "VW"]), xlab="year", ylab="log-rtn")
par(op)
# p. 19
# Figure 1.4. Comparison of empirical and normal densities
# for the monthly simple and log returns of IBM stock
if(require(logspline)){
m.ibm <- m.ibmvwewsp2603[, "IBM"]
dens.ibm.rtns <- logspline(100*as.numeric(m.ibm))
m.log.ibm <- 100*log(1+m.ibm)
dens.ibm.logrtns <- logspline(as.numeric(m.log.ibm))
op <- par(mfrow=c(1,2))
plot(dens.ibm.rtns, xlim=c(-40, 40), xlab="simple returns", ylab="density")
x.ibm <- seq(-40, 40, len=201)
lines(x.ibm, dnorm(x.ibm, mean=100*mean(m.ibm), s=100*sd(m.ibm)),
lty="dotted", col="red", lwd=2)
plot(dens.ibm.logrtns, xlim=c(-40, 40), xlab="log returns", ylab="density")
lines(x.ibm, dnorm(x.ibm, mean=mean(m.log.ibm), s=sd(m.log.ibm)),
lty="dotted", col="red", lwd=2)
par(op)
}
qqnorm(100*m.ibm, datax=TRUE)
qqnorm(m.log.ibm, datax=TRUE)
# normal plots may provide a more sensitive evaluation
# of skewness and kurtosis than density plots
# The kurtosis here does not look extreme.
# Similar plots of the daily returns show
# much more extreme kurtosis.
# p. 20
##
## 1.3. Processes Considered
##
data(m.gs10)
data(m.gs1)
# Figure 1.5. Time plot of US monthly interest rates
op <- par(mfrow=c(2,1))
plot(m.gs10, xlab="year", ylab="rate", type="l")
title("Monthly 10-yr Treasury constant maturity rates")
plot(m.gs1, xlab="year", ylab="rate", type="l")
title("Monthly 1-yr Treasury constant maturity rates")
par(op)
# p. 21
# Figure 1.6. Time plot of daily exchange rate
# between US dollara and Japanese Yen
data(d.fxjp00)
op <- par(mfrow=c(2,1))
plot(d.fxjp00, xlab="year", ylab="Yens", type="l")
plot(diff(d.fxjp00), xlab="year", ylab="Change", type="l")
par(op)
# p. 22
# Table 1.3. Descriptive Statistics of Selected US Financial Time Series
data(m.fama.bond5203)
(m.bondRtns <- rbind(
"1-12 months"=FinTS.stats(100*m.fama.bond5203[, "m1.12"]),
"24-36 months"=FinTS.stats(100*m.fama.bond5203[, "m24.36"]),
"48-60 months"=FinTS.stats(100*m.fama.bond5203[, "m48.60"]),
"61-120 months"=FinTS.stats(100*m.fama.bond5203[, "m61.120"]) ))
data(m.gs1)
data(m.gs3)
data(m.gs5)
data(m.gs10)
(m.treasuryRtns <- rbind(
"1 year"=FinTS.stats(m.gs1),
"3 years"=FinTS.stats(m.gs3),
"5 years"=FinTS.stats(m.gs5),
"10 years"=FinTS.stats(m.gs10) ))
data(w.tb3ms)
data(w.tb6ms)
(w.treasuryRtns <- rbind(
"3 months"=FinTS.stats(w.tb3ms),
"6 months"=FinTS.stats(w.tb6ms) ) )
#write.table(rbind(m.bondRtns, m.treasuryRtns, w.treasuryRtns),
# "Table1-3.csv", sep=",")
>
> # SP statistics in Table 1.2 of Tsay
> data(d.ibmvwewsp6203)
> FinTS.stats(100*d.ibmvwewsp6203[, "SP"])
Loading required package: e1071
Start Size Mean Standard.Deviation Skewness Excess.Kurtosis
1 1962-07-03 10446 0.03312081 0.9453075 -0.9449986 25.76459
Minimum Maximum
1 -20.47 9.1
>
>
>
>
>
>
> dev.off()
null device
1
>