## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

:

Financial Time Series Analysis in R

Paul Cowpertwait

Preface

Textbook

The text book for this part of the course is ‘Introductory Time Series

with R’ by Cowpertwait and Metcalfe (Springer, 2009), and is available

locally in city bookstores.

Software

This course uses R, which is an open source and freely available software

environment, based on the S language that was originally developed at

the Bell (AT&T) Laboratories. The S language was designed for the

purpose of data handling and analysis, making it an attractive tool to

the applied statistician and econometrician. This, together with power-

ful graphics utilities, makes R ideal for a time series analysis. Available

libraries provide a range of functions especially suited to econometrics

and ﬁnance, making R a strong competitor to specialist ﬁnancial soft-

ware; R is also supported by a number of internationally recognised

economic and ﬁnance groups and university departments (see ‘mem-

bers’ at www.r-project.org).

R has a command line interface which oﬀers considerable advantages

over menu systems in terms of eﬃciency and speed, once the commands

are known and the language understood. However, the command line

system can be a little daunting so there is a need for practice and

exercises. This course is therefore a practical course where you are

encouraged to experiment with a range of algorithms to ﬁt models to

ﬁnancial series and simulate forecasts from ﬁtted models.

2

Installing R

You can download R free of charge via the web site:

http://cran.stat.auckland.ac.nz/

If you decide to download R for your personal use, then ﬁrst go to the

pre-compiled (binary) links at the above site and follow the appropriate

instructions for the operating system you are using (e.g. MS Windows,

Mac or Linux). If you want to install R in Linux or on a Mac follow

the online instructions (or contact me). Detailed instructions for R

installation and time series packages in MS Windows now follow.

1. Go to: http://cran.stat.auckland.ac.nz/

2. Click ‘Windows’

3. Click ‘base’

4. Click link to setup program (e.g. called R-2.11.1 for version

2.11.1 - pick the latest version)

5. Download to setup program to your machine (e.g. to Desktop)

6. Double-click the setup program that is now on your machine. This

will install R. Follow any instructions on the screen. When R is in-

stalled carry out the following instructions to install the time series

library. (You can also install any other libraries in this way; e.g.

‘fseries’ or ‘vars’ for more advanced ﬁnancial time series analysis

should you need them.)

7. Go back to http://cran.stat.auckland.ac.nz/

8. Click ‘Windows’

9. Click ‘contrib’

10. Click ‘2.11.1’ (replace this with the version you are using)

3

11. Download the following (e.g. ‘save to desktop’): zoo_1.0.zip,

tseries_0.1.zip, quadprog_1.zip (use the latest versions here

also)

12. Now start R (e.g. Double click R icon)

13. Click “Packages” on the menu bar in R followed by “install from

local zip ﬁle”

14. Browse for zoo_1.zip (i.e. go to where you saved the ﬁle, e.g.

Desktop)

15. Click OK

16. Now repeat for the other two zip ﬁles: tseries_0.1.zip and

quadprog_1.zip (again using latest versions of these packages)

4

1. Notation and basic statistical models

Expectation

• The expected value, or expectation E, of a variable is its mean

value in a population.

• E(x) is the mean of x, often denoted µ.

• E{(x−µ)

2

} is the mean of the squared deviations about µ, better

known as the variance σ

2

of x.

• Subscripts can be used to distinguish between variables, e.g. for

the means we may write µ

x

and µ

y

to distinguish between the

mean of x and the mean of y.

• The standard deviation, σ is the square root of the variance.

5

Covariance

If we have two variables (x, y), the variance may be generalised to the

covariance, γ(x, y) deﬁned by:

γ(x, y) = E{(x −µ

x

)(y −µ

y

)}

The covariance is a measure of linear association between two vari-

ables (x, y). As mentioned before a linear association between variables

does not imply causality.

6

Covariance explained

If data pairs are plotted, the lines x = x and y = y divide the plot into

quadrants.

Points in the lower left quadrant have both (x

i

− x) and (y

i

− y)

negative, so the product is positive.

Points in the upper right quadrant also make a positive contribution.

In contrast, points in the upper left and lower right quadrants make a

negative contribution to the covariance.

Thus, if y tends to increase when x increases most of the points will

be in the lower left and upper right quadrants and the covariance will

be positive.

Conversely, if y tends to decrease as x increases the covariance be

negative.

If there is no such linear association the covariance will be small.

7

Correlation

Correlation is a dimensionless measure of the linear association be-

tween a pair of variables (x, y), and is obtained by standardising the

covariance by dividing it by the standard deviations of the variables.

Correlation takes a value between −1 and +1, with a value of 0 indi-

cating no linear association.

The population correlation, ρ, between a pair of variables (x, y) is

deﬁned by:

ρ(x, y) =

E{(x −µ

x

)(y −µ

y

)}

σ

x

σ

y

=

γ(x, y)

σ

x

σ

y

The sample correlation, cor, is an estimate of ρ , and is calculated as:

cor(x, y) =

cov(x, y)

sd(x)sd(y)

8

Stationarity

The mean function of a time series model is

µ(t) = E(x

t

)

and, in general, is a function of t.

The expectation in this deﬁnition is an average taken across all the

possible time series that might have been produced by the time series

model.

Usually we only have a single time series so all we can do is estimate

the mean at each sample point by the corresponding observed value.

If the mean function is constant in time we say that the time series

is stationary in the mean and we can then use the whole series to

estimate the mean µ.

9

Autocorrelation

The mean and variance play an important role in the study of statistical

distributions, because they summarise two key distributional properties

– a central location and the spread.

In the study of time series models a key role is played by the second-

order properties, which includes the mean and variance, but also the

serial correlation.

Consider a time series model which is stationary in the mean and vari-

ance. The variables may be correlated, and the model is second-order

stationary if the correlation between variables depends only on the

number of time steps separating them, and the mean and variance do

not change with time.

The number of time steps between the variables is known as the lag.

A correlation of a variable with itself at diﬀerent times is known as

autocorrelation or serial correlation.

10

Autocovariance function

If a time series model is second-order stationary we can deﬁne an au-

tocovariance function (acvf ), γ

k

, as a function of the lag, k.

γ

k

= E{(x

t

−µ)(x

t+k

−µ)}

Since the model is stationary, the function γ

k

does not depend on t.

This deﬁnition follows naturally from the deﬁnition of covariance given

earlier, by replacing x with x

t

and y with x

t+k

, and noting that the

mean µ is the mean of both x

t

and x

t+k

.

11

Autocorrelation function

The lag k autocorrelation function (acf ), ρ

k

is deﬁned by:

ρ

k

=

γ

k

σ

2

It follows from the deﬁnition that ρ

0

is 1.

It is customary to drop the term ‘second-order’ and use ‘stationary’ on

its own for a time series model that is at least second-order stationary.

12

Wave height example

We will demonstrate the calculations in R using a time series of wave

heights (mm relative to still water level) measured at the centre of a

wave tank. The sampling interval is 0.1 second and the record length

is 39.7 seconds.

The waves were generated by a wavemaker driven by a random signal,

programmed to emulate a rough sea. There is no trend and no seasonal

period, so it is reasonable to suppose the time series is a realisation of

a stationary process.

> www <- "http://www.elena.aut.ac.nz/~pcowpert/ts/wave.dat"

> wave.dat <- read.table (www, header=T) ; attach(wave.dat)

> plot(ts(waveht)) ; plot(ts(waveht[1:60]))

13

Time

w

a

v

e

h

e

i

g

h

t

(

m

m

)

0 100 200 300 400

−

5

0

0

5

0

0

(a) Wave height over 39.7 seconds

Time

w

a

v

e

h

e

i

g

h

t

(

m

m

)

0 10 20 30 40 50 60

−

6

0

0

0

6

0

0

(b) Wave height over 6 seconds

Figure 1: Wave height at centre of tank sampled at 0.1 second intervals.

14

Correlogram

By default, the acf function in R produces a plot of the autocorrelation

r

k

against lag k, which is called the correlogram.

> acf(ts(waveht))

Comments based on acf of wave data

• The x-axis gives the lag (k) and the y-axis gives the autocorrelation

(r

k

) at each lag. For the wave example, the unit of lag is the

sampling interval, 0.1 second. Correlation is dimensionless, so there

is no unit for the y-axis.

• The dotted lines on the correlogram represent the 5% signiﬁcance

levels. If r

k

falls outside these lines we have evidence against the

null hypothesis, that ρ

k

= 0, at the 5% level. But, we should

be careful about interpreting such multiple hypothesis tests, since

even if the null hypothesis is true we expect 5% of the estimates,

r

k

, to fall outside the lines.

• The lag 0 autocorrelation is always 1 and is shown on the plot.

Its inclusion, helps us compare values of the other autocorrelations

relative to the theoretical maximum of 1.

15

0 5 10 15 20 25

−

0

.

5

0

.

0

0

.

5

1

.

0

Lag

A

C

F

Figure 2: Correlogram of wave heights

16

Discussion based on air passenger data

• Usually a trend in the data will show in the correlogram as a slow

decay in the autocorrelations, which are large and positive, due to

similar values in the series occurring close together in time.

• If there is seasonal variation, seasonal peaks will be superimposed

on this pattern.

• The annual cycle appears in the correlogram as a cycle of the same

period, superimposed on the gradually decaying ordinates of the

acf. This gives a maximum at a lag of 1 year, reﬂecting a positive

linear relationship between pairs of variables (x

t

, x

t+12

), separated

by 12-month periods.

• Because the seasonal trend is approximately sinusoidal, values sep-

arated by a period of 6 months will tend to have a negative rela-

tionship. For example, higher values tend to occur in the summer

months followed by lower values in the winter months. A dip in

the acf therefore occurs at lag 6 months (or 0.5 years).

• Although this is typical for seasonal variation which is approxi-

mated by a sinusoidal curve, other patterns, such as high sales at

Christmas, may just contribute a single spike to the correlogram

at some lag.

17

0.0 0.5 1.0 1.5

−

0

.

2

0

.

2

0

.

6

1

.

0

lag (month)

A

C

F

Figure 3: Correlogram for the air passenger data over the period 1949–1960. The gradual decay

is typical of a time series containing a trend. The peak at 1 year indicates seasonal variation.

18

White noise

A residual error is the diﬀerence between the observed value and the

model predicted value at time t.

Suppose the model is deﬁned for the variable y

t

, and ˆ y

t

is the value

predicted by the model, the residual error x

t

is:

x

t

= y

t

− ˆ y

t

As the residual errors occur in time, they form a time series: x

1

, x

2

, . . . , x

n

.

If a model has accounted for all the serial correlation in the data, the

residual series would be serially uncorrelated, so that a correlogram of

the residual series would exhibit no obvious patterns.

19

White noise: Deﬁnition

A time series {w

t

: t = 1, 2, . . . , n} is discrete white noise (DWN)

if the variables w

1

, w

2

, . . . , w

n

are independent and identically dis-

tributed with a mean of zero.

This implies that the variables all have the same variance σ

2

and

cor(w

i

, w

j

) = 0 for all i = j.

If the variables also follow a Normal distribution, i.e. w

t

∼ N(0, σ

2

),

the series is called Gaussian white noise.

20

Simulation

A ﬁtted time series model can be used to simulate data.

Time series simulated using a model are sometimes called a synthetic

series as oppose to observed historical series.

Simulation can be used to generate plausible future scenarios and con-

struct conﬁdence intervals for model parameters (sometimes called boot-

strapping).

21

Simulation in R

Most statistical distributions are simulated using a function that has

an abbreviated name for the distribution preﬁxed with an ‘r’ (for ‘ran-

dom’).

For example, rnorm(100) simulates 100 independent standard Normal

variables, which is equivalent to simulating a Gaussian white noise

series of length 100:

> set.seed(1)

> w = rnorm(100)

> plot(w, type = "l")

The function set.seed is used to provide a starting point (or seed)

in the simulations.

22

0 20 40 60 80 100

−

2

−

1

0

1

2

time

w

Figure 4: Time plot of simulated Gaussian white noise series

23

Second-order properties and the correlogram

The second-order properties of a white noise series {w

t

} follow from

the deﬁnition.

µ

w

= 0

γ

k

= Cov(w

t

, w

t+k

) =

σ

2

if k = 0

0 if k = 0

**The autocorrelation function follows as:
**

ρ

k

=

1 if k = 0

0 if k = 0

24

Simulated white noise

Simulated white noise data will not have autocorrelations that are ex-

actly zero (when k = 0), because of sampling variation.

It is expected that 5% of the autocorrelations will be signiﬁcantly dif-

ferent from zero at the 5% signiﬁcance level, shown as dotted lines on

the correlogram.

> set.seed(2)

> acf(rnorm(100))

25

0 5 10 15 20

−

0

.

2

0

.

2

0

.

6

1

.

0

Lag

A

C

F

Figure 5: Correlogram of a simulated white noise series. The underlying autocorrelations are

all zero (except at lag 0); the statistically signiﬁcant value at lag 7 is due to sampling variation.

26

Random walks

Let {x

t

} be a time series. Then {x

t

} is a random walk if:

x

t

= x

t−1

+ w

t

where {w

t

} is a white noise series.

Substituting x

t−1

= x

t−2

+ w

t−1

and then for x

t−2

, followed by x

t−3

and so on (a process known as ‘back substitution’) gives:

x

t

= w

t

+ w

t−1

+ w

t−2

+ . . .

In practice, the series will not be inﬁnite but will start at some time

t = 1. Hence,

x

t

= w

1

+ w

2

+ . . . + w

t

27

The backward shift operator

Back substitution occurs so frequently in the study of time series models

that the following deﬁnition is needed.

The backward shift (or lag) operator B is deﬁned by:

Bx

t

= x

t−1

Repeated substitution gives:

B

n

x

t

= x

t−n

28

The diﬀerence operator

Diﬀerencing adjacent terms of a series can transform a non-stationary

series to a stationary series. For example, if the series {x

t

} is a random

walk it is non-stationary, but the ﬁrst-order diﬀerences of {x

t

} produce

the stationary white noise series {w

t

} given by: x

t

−x

t−1

= w

t

.

The diﬀerence operator ∇ is deﬁned by:

∇x

t

= x

t

−x

t−1

Note that ∇x

t

= (1 − B)x

t

, so that ∇ can be expressed in terms of

the backward shift operator B.

In general, higher-order diﬀerencing can be expressed as:

∇

n

= (1 −B)

n

29

Simulation

Simulation enables the main features of the model to be observed in

plots, so that when historical data exhibit similar features the model

may be selected as a potential candidate.

The following commands can be used to simulate random walk data

for x.

> x = w = rnorm(1000)

> for (t in 2:1000) x[t] = x[t - 1] + w[t]

> plot(x, type = "l")

The correspondence between the R code above and the equation for a

random walk should be noted.

30

0 200 400 600 800 1000

0

2

0

4

0

6

0

8

0

Index

x

Figure 6: Time plot of a simulated random walk. The series exhibits an increasing trend.

However, this is purely stochastic and due to the high serial correlation.

0 5 10 15 20 25 30

0

.

0

0

.

2

0

.

4

0

.

6

0

.

8

1

.

0

Lag

A

C

F

Figure 7: The correlogram for the simulated random walk. A gradual decay from a high serial

correlation is a notable feature of a random walk series.

31

Diagnostics: Simulated random walk series

The ﬁrst order diﬀerences of a random walk are a white noise series,

so the correlogram of the series of diﬀerences can be used to assess

whether a given series is reasonably modelled as a random walk.

> acf(diff(x))

As expected, there is good evidence that the simulated series in x

follows a random walk.

32

0 5 10 15 20 25 30

0

.

0

0

.

2

0

.

4

0

.

6

0

.

8

1

.

0

Lag

A

C

F

Figure 8: Correlogram of diﬀerenced series. If a series follows a random walk the diﬀerenced

series will be white noise.

33

Correlogram of diﬀerenced exchange rates

Financial series often behave like random walks. Recall the exchange

rate series (NZ dollars per UK pounds). The data are close to a random

walk (although there is a signiﬁcant value in the correlogram at lag 1):

> www = "http://www.elena.aut.ac.nz/~pcowpert/ts/pounds_nz.dat"

> Z = read.table(www, header = T)

> Z.ts = ts(Z, st = 1991, fr = 4)

> acf(diff(Z.ts), main = "")

34

0 1 2 3

−

0

.

2

0

.

2

0

.

6

1

.

0

Lag

A

C

F

Figure 9: Correlogram of ﬁrst-order diﬀerences of the exchange rate series (UK pounds to NZ

dollars: 1991-2000). The signiﬁcant value at lag 1 indicates that an extension of the random

walk model is needed for this series.

35

Random walk with drift

Company stock holders expect their investment to increase in value

despite the volatility of ﬁnancial markets. The random walk model can

be adapted to allow for this by including a drift parameter δ.

x

t

= x

t−1

+ δ + w

t

The drift parameter can be estimated by the mean diﬀerence. The daily

closing prices of Hewlett-Packard stock can be modelled by a random

walk with drift, since the correlogram of the diﬀerence is near white

noise and the mean diﬀerence is signiﬁcantly diﬀerent from zero:

> www = "http://elena.aut.ac.nz/~pcowpert/ts/HP.txt"

> HP.dat = read.table(www, header = T)

> attach(HP.dat)

> DP = diff(Price)

> mean(DP)

[1] 0.03987

> t.test(DP)

One Sample t-test

data: DP

t = 2.2467, df = 670, p-value = 0.02498

alternative hypothesis: true mean is not equal to 0

95 percent confidence interval:

0.005025729 0.074706015

sample estimates:

mean of x

0.03986587

36

Day

C

l

o

s

i

n

g

P

r

i

c

e

0 100 200 300 400 500 600

2

0

2

5

3

0

3

5

4

0

4

5

Figure 10: Daily closing prices of Hewlett-Packard stock.

Day

D

i

f

f

e

r

e

n

c

e

o

f

c

l

o

s

i

n

g

p

r

i

c

e

0 100 200 300 400 500 600

−

2

−

1

0

1

2

3

Figure 11: Lag 1 diﬀerences of daily closing prices of Hewlett-Packard stock.

37

0 5 10 15 20 25

0

.

0

0

.

2

0

.

4

0

.

6

0

.

8

1

.

0

Lag

A

C

F

Series DP

Figure 12: Acf of Lag 1 diﬀerences of daily closing prices of Hewlett-Packard stock.

38

2. Models for stationary series

Stationary series

Recall that a time series is stationary if the mean, variance, and auto-

correlation do not change with time.

So a stationary series would contain no trend or seasonal variation.

So when a model (e.g. Holt-Winters, or regression) has accounted for

the trends in the data, the residuals are expected to be stationary but

may be autocorrelated (since they form a time series).

The models in this chapter may be used for stationary residual series

or other stationary series.

39

Autoregressive models

The series {x

t

} is an autoregressive process of order p, abbreviated to

AR(p), if:

x

t

= α

1

x

t−1

+ α

2

x

t−2

+ . . . + α

p

x

t−p

+ w

t

where {w

t

} is white noise, and the α

i

are the model parameters with

α

p

= 0 for an order p process.

This can be expressed in terms of the backward shift operator:

θ

p

(B)x

t

= (1 −α

1

B−α

2

B

2

−. . . −α

p

B

p

)x

t

= w

t

40

Notes

(a) The random walk is the special case AR(1) with α

1

= 1.

(b) The model is a regression of x

t

on past terms from the same series;

hence, the use of the term ‘autoregressive’.

(c) A prediction at time t is given by:

ˆ x

t

= α

1

x

t−1

+ α

2

x

t−2

+ . . . + α

p

x

t−p

(d) The model parameters can be estimated by minimising the sum

of squared errors.

41

Stationary and non-stationary AR processes

The roots of the polynomial θ

p

(B), where B is treated as a number

must all exceed unity in absolute value for the process to be station-

ary.

Notice that the random walk has θ = 1 − B with root B = 1 and is

non-stationary.

42

Examples

1. The AR(1) model x

t

=

1

2

x

t−1

+ w

t

is stationary, because the root

of 1 −

1

2

B = 0 is B = 2, which is greater than one.

2. The AR(2) model x

t

=

1

2

x

t−1

+

1

2

x

t−2

+ w

t

is non-stationary, be-

cause one of the roots is unity.

43

Correlogram of an AR(1) process

The autocorrelation function is given by:

ρ

k

= α

k

(k ≥ 0)

where |α| < 1. Thus, the correlogram decays to zero, more rapidly for

small α, and will oscillate when α < 0.

44

Partial autocorrelation

The partial autocorrelation at lag k is the kth coeﬃcient of a ﬁtted

AR(k) model – if the underlying process is AR(p), then the coeﬃcients

α

k

will be zero for all k > p.

So the partial autocorrelation of an AR(1) process will be zero for all

lags greater than 1.

An AR(p) process has a correlogram of partial autocorrelations that is

zero after lag p, so a plot of the partial autocorrelations can be useful

when trying to determine the order of an AR process for data.

45

Simulation

An AR(1) process can be simulated in R as follows:

> set.seed(1)

> x = w = rnorm(100)

> for (t in 2:100) x[t] = 0.7 * x[t - 1] + w[t]

> plot(x)

> acf(x)

> pacf(x)

46

0 20 40 60 80 100

0

2

index

x

(a) Time plot.

0 5 10 15 20

0

.

0

0

.

5

1

.

0

lag

A

C

F

(b) Correlogram.

5 10 15 20

0

.

0

0

.

5

lag

P

a

r

t

i

a

l

A

C

F

(c) Partial Correlogram.

Figure 13: A simulated AR(1) process: x

t

= 0.7x

t−1

+w

t

. Note that in the partial-correlogram

(c) only the ﬁrst lag is signiﬁcant, which is usually the case when the underlying process is

AR(1).

47

Fitted models

Model ﬁtted to simulated series

An AR(p) model can be ﬁtted to data in R using the ar function.

> x.ar = ar(x, method = "mle")

> x.ar$order

[1] 1

> x.ar$ar

[1] 0.601

> x.ar$ar + c(-2, 2) * sqrt(x.ar$asy.var)

[1] 0.440 0.761

48

AIC

The method “mle” used in the ﬁtting procedure above is based on

maximising the likelihood function (the probability of obtaining the

data given the model) with respect to the unknown parameters.

The order p of the process is chosen using the Akaike Information

Criteria (AIC; Akaike (1974)), which penalises models with too many

parameters:

AIC = −2 ×log-likelihood + 2 ×number of parameters

In the function ar, the model with the smallest AIC is selected as the

best ﬁtting AR model.

49

Exchange rate series: Fitted AR model

An AR(1) model is ﬁtted to the exchange rate series and the upper

bound of the conﬁdence interval for the parameter includes 1, consis-

tent with the earlier conclusion that a random walk provides a good

approximation for this series.

> Z.ar = ar(Z.ts)

> mean(Z.ts)

[1] 2.82

> Z.ar$order

[1] 1

> Z.ar$ar

[1] 0.89

> Z.ar$ar + c(-2, 2) * sqrt(Z.ar$asy.var)

[1] 0.74 1.04

> acf(Z.ar$res[-1])

By default, the mean is subtracted before the parameters are estimated,

so a predicted value ˆ z

t

at time t based on the above output is given by:

ˆ z

t

= 2.8 + 0.89(z

t−1

−2.8)

50

0 5 10 15

−

0

.

2

0

.

2

0

.

6

1

.

0

Lag

A

C

F

Figure 14: The correlogram of residual series for the AR(1) model ﬁtted to the exchange rate

data.

51

Moving average models

A moving average process of order q (MA(q) is a linear combination

of the current white noise term and the q most recent past white noise

terms and is deﬁned by:

x

t

= w

t

+ β

1

w

t−1

+ . . . + β

q

w

t−q

where {w

t

} is white noise with zero mean and variance σ

2

w

. The Equa-

tion can be rewritten in terms of the backward shift operator B:

x

t

= (1 + β

1

B+ β

2

B

2

+ · · · + β

q

B

q

)w

t

= φ

q

(B)w

t

where φ

q

is a polynomial of order q.

Because MA processes consist of a ﬁnite sum of stationary white noise

terms they are stationary.

52

Fitted MA models

An MA(q) model can be ﬁtted to data in R using the arima function

with the order function parameter set to c(0,0,q).

Below a moving average process is simulated and then a model ﬁtted

to the simulated series.

> set.seed(1)

> b <- c(0.8, 0.6, 0.4)

> x <- w <- rnorm(1000)

> for (t in 4:1000) for (j in 1:3) x[t] <- x[t] + b[j] * w[t -

j]

> plot(x, type = "l")

> acf(x)

> x.ma <- arima(x, order = c(0, 0, 3))

> x.ma

Call:

arima(x = x, order = c(0, 0, 3))

Coefficients:

ma1 ma2 ma3 intercept

0.790 0.566 0.396 -0.032

s.e. 0.031 0.035 0.032 0.090

sigma^2 estimated as 1.07: log likelihood = -1452, aic = 2915

53

Exchange rate series: Fitted MA model

In the code below, an MA(1) model is ﬁtted to the exchange rate series.

> www <- "http://www.elena.aut.ac.nz/~pcowpert/ts/pounds_nz.dat"

> x <- read.table(www, header = T)

> x.ts <- ts(x, st = 1991, fr = 4)

> x.ma <- arima(x.ts, order = c(0, 0, 1))

> x.ma

Call:

arima(x = x.ts, order = c(0, 0, 1))

Coefficients:

ma1 intercept

1.000 2.833

s.e. 0.072 0.065

sigma^2 estimated as 0.0417: log likelihood = 4.76, aic = -3.53

> acf(x.ma$res[-1])

54

0 5 10 15

−

0

.

5

0

.

0

0

.

5

1

.

0

Lag

A

C

F

Figure 15: The correlogram of residual series for the MA(1) model ﬁtted to the exchange rate

data.

55

Mixed models: The ARMA process

Recall that a series {x

t

} is an autoregressive process of order p, an

AR(p) process, if:

x

t

= α

1

x

t−1

+ α

2

x

t−2

+ . . . + α

p

x

t−p

+ w

t

where {w

t

} is white noise, and the α

i

are the model parameters.

A useful class of models are obtained when AR and MA terms are

added together in a single expression.

A time series {x

t

} follows an autoregressive-moving-average (ARMA)

process of order (p, q), denoted ARMA(p, q), when:

x

t

= α

1

x

t−1

+α

2

x

t−2

+. . .+α

p

x

t−p

+w

t

+β

1

w

t−1

+β

2

w

t−2

+. . .+β

q

w

t−q

where {w

t

} is white noise.

And in terms of the backward shift operator B we have:

θ

p

(B)x

t

= φ

q

(B)w

t

56

The following points should be noted about an ARMA(p, q) process:

(a) The process is stationary when the roots of θ all exceed unity in

absolute value.

(b) The AR(p) model is the special case ARMA(p, 0).

(c) The MA(q) model is the special case ARMA(0, q).

(d) Parameter parsimony. When ﬁtting to data, an ARMA model

will often be more parameter eﬃcient (i.e. require fewer parame-

ters) than a single MA or AR model.

(e) Parameter redundany. When θ and φ share a common factor,

the model can be simpliﬁed. For example, the model (1−

1

2

B)(1−

1

3

B)x

t

= (1 −

1

2

B)a

t

can be written (1 −

1

3

B)x

t

= a

t

.

57

Simulation and ﬁtting

The ARMA process, and the more general ARIMA processes discussed

in the next section, can be simulated using the R function arima.sim.

An ARMA(p, q) model can be ﬁtted using the arima function with

the order function parameter set to c(p, 0, q).

Below, data from an ARMA(1,1) process are simulated for α = −0.6

and β = 0.5, and an ARMA(1,1) model ﬁtted to the simulated series.

> set.seed(1)

> x <- arima.sim(n = 10000, list(ar = -0.6, ma = 0.5))

> coef(arima(x, order = c(1, 0, 1)))

ar1 ma1 intercept

-0.59697 0.50270 -0.00657

58

Exchange rate series

A simple MA(1) model failed to provide an adequate ﬁt to the exchange

rate series. In the code below ﬁtted MA(1), AR(1) and ARMA(1,1)

models are compared using AIC.

> x.ma <- arima(x.ts, order = c(0, 0, 1))

> x.ar <- arima(x.ts, order = c(1, 0, 0))

> x.arma <- arima(x.ts, order = c(1, 0, 1))

> AIC(x.ma)

[1] -3.53

> AIC(x.ar)

[1] -37.4

> AIC(x.arma)

[1] -42.3

> x.arma

Call:

arima(x = x.ts, order = c(1, 0, 1))

Coefficients:

ar1 ma1 intercept

0.892 0.532 2.960

s.e. 0.076 0.202 0.244

sigma^2 estimated as 0.0151: log likelihood = 25.1, aic = -42.3

59

0 1 2 3

−

0

.

2

0

.

2

0

.

6

1

.

0

Lag

A

C

F

Figure 16: The correlogram of residual series for the ARMA(1,1) model ﬁtted to the exchange

rate data.

60

3. Non-stationary models

Diﬀerencing and the electricity series

Diﬀerencing a series {x

t

} can remove trends, whether these trends are

stochastic, as in a random walk, or deterministic, as in the case of a

linear trend.

In the case of a random walk, x

t

= x

t−1

+w

t

, the ﬁrst order diﬀerenced

series is white noise {w

t

}, i.e. ∇x

t

= x

t

−x

t−1

= w

t

, and so stationary.

In contrast, if x

t

= a + bt + w

t

, a linear trend with white noise errors

then ∇x

t

= x

t

−x

t−1

= b + w

t

−w

t−1

, which is a stationary moving

average process, rather than white noise.

We can take ﬁrst order diﬀerences in R using the diﬀerence function

diff:

> www <- "http://www.elena.aut.ac.nz/~pcowpert/ts/cbe.dat"

> cbe <- read.table(www, he = T)

> elec.ts <- ts(cbe[, 3], start = 1958, freq = 12)

> layout(c(1, 1, 2, 3))

> plot(elec.ts)

> plot(diff(elec.ts))

> plot(diff(log(elec.ts)))

61

(a)

Time

e

l

e

c

.

t

s

1960 1965 1970 1975 1980 1985 1990

2

0

0

0

4

0

0

0

6

0

0

0

8

0

0

0

1

0

0

0

0

1

4

0

0

0

(b)

Time

d

i

f

f

(

e

l

e

c

.

t

s

)

1960 1965 1970 1975 1980 1985 1990

−

1

5

0

0

0

1

0

0

0

(c)

Time

d

i

f

f

(

l

o

g

(

e

l

e

c

.

t

s

)

)

1960 1965 1970 1975 1980 1985 1990

−

0

.

1

5

0

.

0

5

0

.

2

0

Figure 17: (a) Plot of Australian electricity production series; (b) Plot of the ﬁrst order diﬀer-

enced series; (c) plot of the ﬁrst order diﬀerenced log-transformed series

62

Integrated model

A series {x

t

} is integrated of order d, denoted as I(d), if the d-th

diﬀerence of {x

t

} is white noise {w

t

}, i.e. ∇

d

x

t

= w

t

. Since, ∇

d

≡

(1 − B)

d

, where B is the backward shift operator, a series {x

t

} is

integrated of order d if:

(1 −B)

d

x

t

= w

t

The random walk is the special case I(1).

63

ARIMA models

A time series {x

t

} follows an ARIMA(p,d,q) process if the d-th dif-

ferences of {x

t

} series are an ARMA(p,q) process. If we introduce

y

t

= (1 −B)

d

x

t

then θ

p

(B)y

t

= φ

q

(B)w

t

. We can now substitute for

y

t

to obtain:

θ

p

(B)(1 −B)

d

x

t

= φ

q

(B)w

t

where θ

p

and φ

q

are polynomials of order p and q respectively.

64

Examples

(a) x

t

= x

t−1

+ w

t

+ βw

t−1

, where β is a model parameter. To see

which model this represents express in terms of the backward shift

operator: (1 − B)x

t

= (1 + βB)w

t

. We can see that {x

t

} is

ARIMA(0,1,1), which is sometimes called an integrated moving

average model, denoted as IMA(1,1). In general, ARIMA(0,d,q)

≡ IMA(d,q).

(b) x

t

= αx

t−1

+x

t−1

−αx

t−2

+w

t

, where α is a model parameter. Re-

arranging and factorising gives: (1 −αB)(1 −B)x

t

= w

t

, which is

ARIMA(1,1,0), also known as an integrated autoregressive process

and denoted as ARI(1,1). In general, ARI(p,d) ≡ ARIMA(p,d,0).

65

Simulation and ﬁtting

An ARIMA(p,d,q) process can be ﬁtted to data using the R function

arima with the parameter order set to order = c(p, d, q).

In the code below data for the ARIMA(1,1,1) model x

t

= 0.5x

t−1

+

x

t−1

− 0.5x

t−2

+ w

t

+ 0.3w

t−1

are simulated, and the model ﬁtted to

the simulated series to recover the parameter estimates.

> x <- w <- rnorm(1000)

> for (i in 3:1000) x[i] <- 0.5 * x[i - 1] + x[i - 1] - 0.5 * x[i -

2] + w[i] + 0.3 * w[i - 1]

> arima(x, order = c(1, 1, 1))

Call:

arima(x = x, order = c(1, 1, 1))

Coefficients:

ar1 ma1

0.526 0.290

s.e. 0.038 0.042

sigma^2 estimated as 0.887: log likelihood = -1358, aic = 2721

66

IMA(1,1) model ﬁtted to the Australian beer production

series

An IMA(1,1) model is often appropriate because it represents a linear

trend with white noise added.

> beer.ts <- ts(cbe[, 2], start = 1958, freq = 12)

> beer.ima <- arima(beer.ts, order = c(0, 1, 1))

> beer.ima

Call:

arima(x = beer.ts, order = c(0, 1, 1))

Coefficients:

ma1

-0.333

s.e. 0.056

sigma^2 estimated as 360: log likelihood = -1723, aic = 3451

> acf(resid(beer.ima))

From the above, the ﬁtted model is: x

t

= x

t−1

+ w

t

−0.33w

t−1

.

67

Forecasts can be obtained using the predict function in R with the

parameter n.ahead set to the number of values in the future:

> beer.1991 <- predict(beer.ima, n.ahead = 12)

> sum(beer.1991$pred)

[1] 2365

68

0.0 0.5 1.0 1.5 2.0

−

0

.

4

0

.

0

0

.

4

0

.

8

Lag

A

C

F

Figure 18: Australian beer series: Correlogram of the residuals of the ﬁtted IMA(1,1) model

69

Seasonal ARIMA models

The seasonal ARIMA model includes autoregressive and moving aver-

age terms at lag s. The SARIMA(p, d, q)(P, D, Q)

s

model can be

most succinctly expressed using the backward shift operator:

Θ

P

(B

s

)θ

p

(B)(1 −B

s

)

D

(1 −B)

d

x

t

= Φ

Q

(B

S

)φ

q

(B)w

t

where Θ

P

, θ

p

, Φ

Q

, and φ

q

are polynomials of orders P, p, Q and q

respectively.

In general, the model is non-stationary (unless D = d = 0 and the

roots of the AR polynomial all exceed unity).

70

Examples

Some examples of seasonal ARIMA models are:

(a) A simple AR model with a seasonal period of 12 units, denoted

as ARIMA(0,0,0)(1,0,0)

12

, is: x

t

= αx

t−12

+ w

t

. Such a model

would be appropriate for monthly data when only the value in the

month of the previous year inﬂuences the current monthly value.

The model is stationary when |α

−1/12

| > 1.

(b) It is common to ﬁnd series with stochastic trends which neverthe-

less have seasonal inﬂuences. The model in (a) above could be ex-

tended to: x

t

= x

t−1

+αx

t−12

−αx

t−13

+w

t

. Rearranging and fac-

torizing gives (1−αB

12

)(1−B)x

t

= w

t

or Θ

1

(B

12

)(1−B)x

t

= w

t

,

which is ARIMA(0,1,0)(1,0,0)

12

. Note this model could also be

written ∇x

t

= α∇x

t−12

+ w

t

which emphasises that it is the

change at time t which is dependent on the change at the same

time in the previous year. The model is non-stationary, since the

polynomial on the left-hand-side contains the term (1 −B), which

implies there exists a unit root B = 1.

71

Fitting procedure

Seasonal ARIMA models can have a large number of parameters. There-

fore, it is appropriate to try out a wide range of models and to choose

a best ﬁtting model using smallest AIC.

Once a best ﬁtting model has been found the correlogram of the resid-

uals should be veriﬁed as white noise.

72

ARCH models: Example based on SP500 Series

Standard and Poors (of the McGraw-Hill companies) publish a range

of ﬁnancial indices and credit ratings. Consider the following time

plot and correlogram of the SP500 index (for trading days since 1990)

available in the MASS library within R.

> library(MASS)

> data(SP500)

> plot(SP500, type='l')

> acf(SP500)

73

0 500 1000 1500 2000 2500

−

6

−

2

2

(a)

Index

S

P

5

0

0

0 5 10 15 20 25 30 35

0

.

0

0

.

4

0

.

8

(b)

Lag

A

C

F

Figure 19: Standard and Poors SP500 Index: (a) Time plot; (b) Correlogram

74

Volatility

The series has periods of changing variance, sometimes called volatil-

ity although this is not in a regular way (as noted for the Electricity

production series and Airline series).

When a variance is not constant in time (e.g. increasing as in the

Airline and Electricity data), the series is called heteroskedastic.

If a series exhibits periods of non-constant variance (so the variance

is correlated in time), such as in the SP500 data, the series exhibits

volatility and is called conditional heteroskedastic.

75

Correlogram of squared values

If a correlogram appears to be white noise, then volatility can be de-

tected by looking at the correlogram of the squared values.

Since the mean should be approximately zero, the squared values repre-

sent the variance and the correlogram should pick up seriel correlation

in the variance process.

The correlogram of the squared values of the SP500 index is given by:

> acf(SP500^2)

From this we can see there is evidence of serial correlation in the squared

values, so there is evidence of volatility.

76

0 5 10 15 20 25 30 35

0

.

0

0

.

2

0

.

4

0

.

6

0

.

8

1

.

0

Lag

A

C

F

Figure 20: Standard and Poors SP500 Index: Correlogram of the squared values

77

Modelling volatility: ARCH model

In order to account for volatility we require a model that allows for

changes in the variance. One approach to this is to use an autoregressive

model essentially on the variance process.

A series {

t

} is ﬁrst-order autoregressive conditional heteroskedastic,

denoted ARCH(1), if:

t

= w

t

α

0

+ α

1

2

t−1

where {w

t

} is white noise with zero mean and unit variance, and α

0

and α

1

are model parameters.

To see how this introduces volatility, square to obtain the variance:

Var(

t

) = E(

2

t

)

= E(w

2

t

)E(α

0

+ α

1

2

t−1

)

= E(α

0

+ α

1

2

t−1

)

= α

0

+ α

1

Var(

t−1

)

since {w

t

} has unit variance and {

t

} has zero mean.

If we compare this with the AR(1) process x

t

= α

0

+ α

1

x

t−1

+ w

t

it

is clear that the variance of an ARCH(1) process behaves just like an

AR(1) model.

78

Extensions and GARCH models

The ﬁrst-order ARCH model can be extended to a p-th order process

by the inclusion of higher lags. An ARCH(p) process is given by:

t

= w

t

α

0

+

p

¸

i=1

α

p

2

t−i

where {w

t

} is again white noise with zero mean and unit variance.

A further extension, widely used in ﬁnancial applications, is the gener-

alised ARCH model, denoted GARCH(q, p), which has the ARCH(p)

model as the special case GARCH(0, p). A series {

t

} is GARCH(q,

p) if:

t

= w

t

h

t

where

h

t

= α

0

+

p

¸

i=1

α

i

2

t−i

+

q

¸

j=1

β

j

h

t−j

and α

i

and β

j

(i = 0, 1, . . . , p; j = 1, . . . , q) are model parameters.

79

GARCH model for SP500 series

Below the GARCH model is ﬁtted to the SP500 return series. We check

the correlogram of the residuals and squared residuals, which should

be near white noise if we have obtained a good ﬁtting GARCH model.

> sp.garch <- garch(SP500, trace=F)

> sp.res <- sp.garch$res[-1]

> acf(sp.res)

> acf(sp.res^2)

80

0 5 10 15 20 25 30 35

0

.

0

0

.

4

0

.

8

(a)

Lag

A

C

F

0 5 10 15 20 25 30 35

0

.

0

0

.

4

0

.

8

(b)

Lag

A

C

F

Figure 21: GARCH model ﬁtted to SP500 series: (a) Correlogram of the residuals; (b) Correl-

ogram of the squared residuals

81

4. Multivariate models

Spurious regression

It is common practice to use regression to explore the relationship

between two or more variables.

For time series variables we have to be careful before ascribing any

causal relationship, since an apparent relationship could exist due to

underlying trends or because both series exhibit seasonal variation.

For example, the Australian electricity and chocolate production series

share an increasing trend due to an increasing Australian population,

but this does not imply that changes in one variable cause changes in

the other, and any regression of the two variables would be spurious.

> www <- "http://www.elena.aut.ac.nz/~pcowpert/ts/cbe.dat"

> cbe <- read.table(www, header = T)

> elec.ts <- ts(cbe[, 3], start = 1958, freq = 12)

> choc.ts <- ts(cbe[, 1], start = 1958, freq = 12)

> plot(as.vector(aggregate(choc.ts)), as.vector(aggregate(elec.ts)))

> cor(aggregate(choc.ts), aggregate(elec.ts))

[1] 0.958

82

30000 40000 50000 60000 70000 80000 90000

2

0

0

0

0

6

0

0

0

0

1

0

0

0

0

0

1

4

0

0

0

0

Chocolate production

E

l

e

c

t

r

i

c

i

t

y

p

r

o

d

u

c

t

i

o

n

Figure 22: Annual electricity and chocolate production plotted against each other

83

Spurious stochastic trends

The term spurious regression is also used when underlying stochastic

trends in both series happen to coincide.

Stochastic trends are a feature of an ARIMA process with a unit root

(i.e. B = 1 is a solution of the ARI polynomial equation). We illustrate

this by simulating two independent random walks:

> set.seed(10); x <- rnorm(100); y <- rnorm(100)

> for(i in 2:100) {

> x[i] <- x[i-1]+rnorm(1)

> y[i] <- y[i-1]+rnorm(1) }

> plot(x,y)

> cor(x,y)

[1] 0.904

The above can be repeated for diﬀerent random number seeds though

you will only sometimes notice spurious correlation.

84

0 2 4 6 8 10 12

−

5

0

5

1

0

x

y

Figure 23: The values of two independent simulated random walks plotted against each other.

(See the code in the text.)

85

Stochastic trends are common in economic series, and so multiple eco-

nomic series require considerable care when trying to determine any

relationships between the variables.

It may be that an underlying relationship can be justiﬁed even when

the series exhibit stochastic trends, because two series may be related

by a common shared stochastic trend (see cointegration later).

86

Example: Exchange rates

The daily exchange rate series for UK pounds, the Euro, and New

Zealand dollars, given for the period January 2004 to December 2007,

are all per US dollar. The correlogram plots of the diﬀerenced UK and

EU series indicate that both exchange rates can be well approximated

by random walks, whilst the scatter plot of the rates show a strong

linear relationship which is supported by a high correlation of 0.95.

Since the UK is part of the European Economic Community (EEC),

any change in the Euro exchange rate is likely to be apparent in the

UK pound exchange rate.

> www <- "http://www.elena.aut.ac.nz/~pcowpert/ts/us_rates.dat"

> xrates <- read.table(www, header = T)

> xrates[1:3, ]

UK NZ EU

1 0.558 1.52 0.794

2 0.553 1.49 0.789

3 0.548 1.49 0.783

> acf(diff(xrates$UK))

> acf(diff(xrates$EU))

> plot(xrates$UK, xrates$EU, pch=4

> cor(xrates$UK, xrates$EU)

[1] 0.946

87

0 5 10 15 20 25 30

0

.

0

0

.

4

0

.

8

(a)

Lag

A

C

F

0 5 10 15 20 25 30

0

.

0

0

.

4

0

.

8

(b)

Lag

A

C

F

Figure 24: Correlograms of the diﬀerenced exchange rate series: (a) UK rate; (b) EU rate

88

0.70 0.75 0.80 0.85

0

.

4

8

0

.

5

2

0

.

5

6

EU rate

U

K

r

a

t

e

Figure 25: Scatter plot of the UK and EU exchange rates (both rates are per US dollars)

89

Tests for unit roots

When analysing more than one ﬁnancial series, the ﬁrst step is usually

to see whether the series contain stochastic trends, and whether these

trends are common to more than one series.

Looking at the correlogram of the diﬀerenced series may help to de-

termine whether a random walk is appropriate for any of the series.

Whilst this may work for a simple random walk, we have seen that

stochastic trends are a feature of any time series model with unit roots

(B = 1), including more complex ARIMA processes.

Dickey and Fuller developed a test of the null hypothesis that α = 1,

against an alternative hypothesis that α < 1, for the model x

t

=

αx

t−1

+ w

t

in which w

t

is white noise.

The method is implemented in R by the function adf.test within the

tseries library. The null hypothesis of a unit root can not be rejected

for our simulated random walk x:

> library(tseries)

> adf.test(x)

Augmented Dickey-Fuller Test

data: x

Dickey-Fuller = -2.23, Lag order = 4, p-value = 0.4796

alternative hypothesis: stationary

90

Phillips-Perron test

An alternative to the Dickey-Fuller test known as the Phillips-Perron

test (Perron (1988)) is implemented in the R function pp.test. The

Phillips-Perron procedure estimates autocorrelations in the error pro-

cess, rather than assuming white noise errors, and for this reason the

Phillips-Perron test is more generally applicable.

> pp.test(xrates$UK)

Phillips-Perron Unit Root Test

data: xrates$UK

Dickey-Fuller Z(alpha) = -10.6, Truncation lag parameter = 7, p-value =

0.521

alternative hypothesis: stationary

> pp.test(xrates$EU)

Phillips-Perron Unit Root Test

data: xrates$EU

Dickey-Fuller Z(alpha) = -6.81, Truncation lag parameter = 7, p-value =

0.7297

alternative hypothesis: stationary

91

Cointegration

The UK and Euro exchange rates are very highly correlated, which

is explained by the similarity of the two economies relative to the US

economy. So although the rates are approximately random walks, they

share stochastic trends due to the eﬀect of the US economy.

Thus it is quite possible for two series to contain unit roots and be

related, so the relationship between them would not be spurious. Such

series are said to be cointegrated.

92

Cointegration: Deﬁnition

Two non-stationary time series {x

t

} and {y

t

} are cointegrated

if some linear combination ax

t

+ by

t

, with a and b constant,

is a stationary series.

As an example consider a random walk {µ

t

} given by µ

t

= µ

t−1

+ w

t

,

where {w

t

} is white noise with zero mean, and two series {x

t

} and

{y

t

} given by x

t

= µ

t

+w

xt

and y

t

= µ

t

+w

yt

, where {w

xt

} and {w

yt

}

are white noise with zero mean.

Both series are non-stationary but their diﬀerence {x

t

−y

t

} is stationary

since it is a ﬁnite linear combination of independent white noise terms.

Thus the linear combination of {x

t

} and {y

t

}, with a = 1 and b = −1,

produced a stationary series. Hence {x

t

} and {y

t

} are cointegrated,

and share the underlying stochastic trend {µ

t

}.

93

Cointegration test in R

Two series can be tested for cointegration using the Phillips-Ouliaris

test implemented in the function po.test within the tseries library.

> x <- y <- mu <- rep(0, 1000)

> for (i in 2:1000) mu[i] <- mu[i - 1] + rnorm(1)

> x <- mu + rnorm(1000)

> y <- mu + rnorm(1000)

> adf.test(x)$p.value

[1] 0.502

> adf.test(y)$p.value

[1] 0.544

> po.test(cbind(x, y))

Phillips-Ouliaris Cointegration Test

data: cbind(x, y)

Phillips-Ouliaris demeaned = -1020, Truncation lag parameter = 9,

p-value = 0.01

In the above example, the conclusion of the adf.test is to retain the

null hypothesis that the series have unit roots.

The po.test provides evidence that the series are cointegrated since

the null hypothesis is rejected at the 1% level.

94

Exchange rate series

Below, there is evidence that the UK and Euro series are cointegrated,

so a regression model is justiﬁed in this case, and ﬁtted using the lm

function.

The AR models provide a better ﬁt to the residual series than the

ARIMA(1,1,0) model, so the residual series is stationary, supporting

the result of the Phillips-Ouliaris test, since a linear combination ob-

tained from the regression model has produced a stationary residual

series.

> po.test(cbind(xrates$UK, xrates$EU))

Phillips-Ouliaris Cointegration Test

data: cbind(xrates$UK, xrates$EU)

Phillips-Ouliaris demeaned = -21.7, Truncation lag parameter = 10,

p-value = 0.04118

> ukeu.lm <- lm(xrates$UK ~ xrates$EU)

> ukeu.res <- resid(ukeu.lm)

> AIC(arima(ukeu.res, order = c(1, 0, 0)))

[1] -9880

> AIC(arima(ukeu.res, order = c(1, 1, 0)))

[1] -9876

95

Bivariate white noise

Two series {w

x,t

} and {w

y,t

} are bivariate white noise when they are

white noise when considered individually but when considered as a

pair may be be cross-correlated at lag 0 (so cor({w

x,t

}, {w

y,t

}) may be

non-zero).

For more than two white noise series, the deﬁnition of bivariate white

noise readily extends to multivariate white noise.

96

Simulation of bivariate white noise

Multivariate Gaussian white noise can be simulated with the rmvnorm

function in the mvtnorm library:

> library(mvtnorm)

> cov.mat <- matrix(c(1, 0.8, 0.8, 1), nr = 2)

> w <- rmvnorm(1000, sigma = cov.mat)

> cov(w)

[,1] [,2]

[1,] 1.073 0.862

[2,] 0.862 1.057

> wx <- w[, 1]

> wy <- w[, 2]

> ccf(wx, wy, main = "")

The ccf function veriﬁes that the cross-correlations are approximately

zero for all non-zero lags.

97

−20 −10 0 10 20

0

.

0

0

.

2

0

.

4

0

.

6

0

.

8

Lag

A

C

F

Figure 26: Cross-correlation of simulated bivariate Guassian white noise

98

Vector Autoregressive Models

Two time series, {x

t

} and {y

t

}, follow a vector autoregressive process

of order 1 (denoted VAR(1)) if:

x

t

= θ

11

x

t−1

+ θ

12

y

t−1

+ w

xt

y

t

= θ

21

x

t−1

+ θ

22

y

t−1

+ w

yt

where {w

xt

} and {w

yt

} are bivariate white noise and θ

ij

are model

parameters. The name ‘vector autoregressive’ comes from the matrix

representation of the model:

Z

t

= ΘZ

t−1

+ w

t

where:

Z

t

=

x

t

y

t

Θ =

θ

11

θ

12

θ

21

θ

22

w

t

=

w

xt

w

yt

99

VAR estimation in R

The parameters of a VAR(p) model can be estimated using the ar

function in R, which selects a best ﬁtting order p based on the smallest

AIC.

> x <- y <- rep(0, 1000)

> x[1] <- wx[1]

> y[1] <- wy[1]

> for (i in 2:1000) {

x[i] <- 0.4 * x[i - 1] + 0.3 * y[i - 1] + wx[i]

y[i] <- 0.2 * x[i - 1] + 0.1 * y[i - 1] + wy[i]

}

> xy.ar <- ar(cbind(x, y))

> xy.ar$ar[, , ]

x y

x 0.399 0.321

y 0.208 0.104

As expected, the parameter estimates are close to the underlying model

values.

100

VAR model ﬁtted to US economic series

Below a best ﬁtting VAR model is ﬁtted to the Gross National Product

(GNP) and Real Money (M1).

> library(tseries)

> data(USeconomic)

> US.ar <- ar(cbind(GNP, M1), method = "ols")

> US.ar$ar

, , GNP

GNP M1

1 1.11674 -0.0434

2 -0.00797 0.0633

3 -0.10774 -0.0188

, , M1

GNP M1

1 0.995 1.577

2 -0.194 -0.453

3 -0.802 -0.147

> acf(US.ar$res[-c(1:3), 1])

> acf(US.ar$res[-c(1:3), 2])

From the above, we see that the best ﬁtting VAR model is of order 3.

The correlogram of the residual series indicates that the residuals are

approximately bivariate white noise, thus validating the assumptions

for a VAR model.

101

0 5 10 15 20

−

0

.

2

0

.

2

0

.

6

1

.

0

(a)

Lag

A

C

F

0 5 10 15 20

−

0

.

2

0

.

2

0

.

6

1

.

0

(b)

Lag

A

C

F

Figure 27: Residual correlograms for VAR(3) model ﬁtted to US Economic Series: (a) Residuals

for GNP; (b) Residuals for M1

102

Prediction for VAR models

Below we give the predicted values for the next year of the series, which

are then added to a time series plot for each variable:

> US.pred <- predict(US.ar, n.ahead = 4)

> US.pred$pred

GNP M1

1988 Q1 3944 628

1988 Q2 3964 626

1988 Q3 3978 623

1989 Q4 3991 620

> GNP.pred <- US.pred$pred[, 1]

> M1.pred <- US.pred$pred[, 2]

> ts.plot(cbind(window(GNP, start = 1981), GNP.pred), lty = 1:2)

> ts.plot(cbind(window(M1, start = 1981), M1.pred), lty = 1:2)

103

(a)

Time

1982 1984 1986 1988

3

2

0

0

3

6

0

0

4

0

0

0

(b)

Time

1982 1984 1986 1988

4

5

0

5

5

0

Figure 28: US Economic Series: (a) Time plot for GNP (from 1981) with added predicted

values (dotted) for the next year; (b) Time plot for M1 (from 1981) with added predicted

values (dotted) for the next year

104

References

Akaike, H. (1974). A new look at statistical model indentiﬁcation.

IEEE Transactions on Automatic Control, 19:716–722.

Perron, P. (1988). Trends and random walks in macroeconomic time

series. Journal of Economic Dynamics and Control, 12:297–332.

105

- stochint
- blatt11
- Column 24 Understanding the Kelly Criterion 2
- Cointegration R Workshop
- Financial Mathematics (Lectures)
- Forex Frontiers
- Rachev S.T. Financial Eco No Metrics (LN, Karlsruhe 2006)(146s)_FL
- C++ In Action
- Fnancial Mathematics
- Complete Newbie s Guide to Online Forex Trading
- OR
- 3642125972
- STATICTICS
- Logic gates
- Column Understanding the Kelly Criterion
- Jaksa Cvitanic Fernando Zapatero
- Fuzzy logic
- Quantitative Methods in Mgmt Ref1
- Understanding Equity Options[1]
- Number Play - Gyles Brandreth
- Advances_In_Quantitative_Analysis_Of_Finance_And_Accounting_Vol._6
- Dominate the Forex
- TilsonBehavioralFinance
- Stochastic Calculus, Filtering, And Stochastic Control
- 5 Minute Forex
- Trading Using Chart
- Advanced Financial Models
- Putting Volatility to Work
- Optimization Methods in Finance
- Mathmatical logic

Sign up to vote on this title

UsefulNot usefulRead Free for 30 Days

Cancel anytime.

Close Dialog## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Loading