Skip to contents

R companion to Tsay (2005) Analysis of Financial Time Series, second edition (Wiley). Includes data sets, functions and script files required to work some of the examples. Version 0.3-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.

Details

PackageFinTS
Version0.4-7
Date2022-11-14
AuthorSpencer Graves [aut], Georgi N. Boshnakov [cre, ctb]
MaintainerGeorgi N. Boshnakov <georgi.boshnakov@manchester.ac.uk>
LicenseGPL (>= 2)
URLhttps://geobosh.github.io/FinTSDoc/ (doc), https://r-forge.r-project.org/projects/fints/ (devel)
Dependszoo, graphics
Suggestsmoments, distrEx, tseries, urca, lmtest, sandwich, psych, GPArotation, chron, polynom, e1071
BuiltR 4.2.2; ; 2022-11-14 17:40:38 UTC; unix

Index:

ARIMA                   Arima with Ljung-Box
Acf                     Autocorrelation Function
ArchTest                ARCH LM Test
AutocorTest             Box-Ljung autocorrelation test
FinTS-package           Companion to Tsay (2005) Analysis of Financial
                        Time Series
FinTS.stats             Financial Time Series summary statistics
TsayFiles               List of the names of files downloaded from the
                        "Analysis of Financial Data" web site.
apca                    Asymptotic PCA
as.yearmon2             Conditionally convert x to yearmon if the
                        conversion is unique, retaining x as names.
ch01data                Financial time series for Tsay (2005, ch. 1)
ch02data                Financial time series for Tsay (2005, ch. 2)
ch03data                Financial time series for Tsay (2005, ch. 3)
ch04data                Financial time series for Tsay (2005, ch. 4)
ch05data                Financial time series for Tsay (2005, ch. 5)
ch06data                Financial time series for Tsay (2005, ch. 6)
ch07data                Financial time series for Tsay (2005, ch. 7)
ch08data                Financial time series for Tsay (2005, ch. 8)
ch09data                Financial time series for Tsay (2005, ch. 9)
ch10data                Financial time series for Tsay (2005, ch. 10)
ch11data                Financial time series for Tsay (2005, ch. 11)
ch12data                Financial time series for Tsay (2005, ch. 12)
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.

Author

Spencer Graves

Maintainer: Georgi N. Boshnakov <georgi.boshnakov@manchester.ac.uk>

References

Ruey Tsay (2005) Analysis of Financial Time Series, 2nd ed. (Wiley)

Examples

# Where is the 'FinTS' directory?
system.file(package='FinTS')
#> [1] "/tmp/RtmpX14IGL/temp_libpathe16b71875ebc/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 <- e1071::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"])
#>        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