---
title: "Time Series Concepts"
author: "Eric Zivot"
date: "Wednesday, March 18, 2015"
output: slidy_presentation
---

## Set Options and Load Packages

```{r}
options(digits = 3)
options(width = 75)
library(forecast)
library(PerformanceAnalytics)
```

## Gaussian White Noise Process

```{r}
# simulate Gaussian White Noise process
set.seed(123)
y = rnorm(250)
ts.plot(y, main="Gaussian White Noise Process: $\epsilon \sim GWN(0, \sigma^2)$", xlab="time", ylab="y(t)",
        col="blue", lwd=2)
abline(h=0)

```

## GWN Process for Monthly Returns

```{r}
set.seed(123)
y = rnorm(60, mean=0.01, sd=0.05)
ts.plot(y, main="GWN Process for Monthly Returns",
        xlab="time", ylab="r(t)", col="blue", lwd=2)
abline(h=c(0,0.01,-0.04,0.06), lwd=2, lty=c("solid", "solid", "dotted","dotted"), 
       col=c("black", "green", "red", "red"))

```

## Independent White Noise

```{r}
## simulate IWN using scaled student's t distribution to have variance 1
set.seed(123)
y = (1/sqrt(3))*rt(250, df=3)
ts.plot(y, main="Independent White Noise Process", xlab="time", ylab="y(t)",
        col="blue", lwd=2)
abline(h=0)
```


## Deterministic Trend + GWN

```{r}
set.seed(123)
e = rnorm(250)
y.dt = 0.1*seq(1,250) + e
ts.plot(y.dt, lwd=2, col="blue", main="Deterministic Trend + Noise")
abline(a=0, b=0.1)
```

## Random Walk Process

```{r}
set.seed(321)
e = rnorm(250)
y.rw = cumsum(e)
ts.plot(y.rw, lwd=2, col="blue", main="Random Walk")
abline(h=0)

```

## Simulate MA(1) Process: Loop calculation

First we set the model parameters and run the simulation.

```{r}
# set simulation parameters
n.obs = 250
mu = 1
theta = 0.9
sigma.e = 1
set.seed(123)
e = rnorm(n.obs, sd = sigma.e)
y = rep(0, n.obs)
# simulate MA(1) using simple loop
y[1] = mu + e[1]
for (i in 2:n.obs) {
  y[i] = mu + e[i] + theta*e[i-1]
}
```

Next we plot the simulated series.

```{r}
# plot simulation
ts.plot(y, main="MA(1) Process: mu=1, theta=0.9, sigma.e=1",
        xlab="time", ylab="y(t)", col="blue", lwd=2)
abline(h=c(0,1), col=c("black", "red"), lty = c("solid", "dashed"))
```

## Simulate MA(1) Process: Vectorized Calculations

```{r}
# set parameters
mu = 1
theta = 0.9
sigma.e = 1
n.obs = 250
# simulated MA(1) using vectorized calculations
set.seed(123)
e = rnorm(n.obs, sd = sigma.e)
em1 = c(0, e[1:(n.obs-1)])
y = mu + e + theta*em1
```


## Simulate MA(1) Process: arima.sim() 
```{r}
ma1.model = list(ma=0.9)
mu = 1
set.seed(123)
ma1.sim = mu + arima.sim(model=ma1.model, n=250)
ts.plot(ma1.sim, main="MA(1) Process: mu=1, theta=0.9",
        xlab="time", ylab="y(t)", col="blue", lwd=2)
abline(h=c(0,1), col=c("black", "red"))
```

## ACF for MA(1) Process: Theta > 0

```{r}
ma1.acf = ARMAacf(ar=0, ma=0.9, lag.max=10)
ma1.acf
plot(0:10, ma1.acf, type="h", col="blue", lwd=2,
     main="ACF for MA(1): theta=0.9", xlab="lag", ylab="rho(j)")
abline(h=0)
```

## MA(1) Process: Theta < 0

```{r}
ma1.model = list(ma=-0.75)
mu = 1
set.seed(123)
ma1.sim = mu + arima.sim(model=ma1.model, n=250)
ts.plot(ma1.sim,main="MA(1) Process: mu=1, theta=-0.75",
        xlab="time", ylab="y(t)", col="blue", lwd=2)
abline(h=c(0, 1), col=c("black", "red"))
```

## ACF for MA(1) Process: Theta < 0

```{r}
ma1.acf = ARMAacf(ar=0, ma=-0.75, lag.max=10)
ma1.acf
plot(0:10, ma1.acf,type="h", col="blue", lwd=2,
main="ACF for MA(1): theta=-0.75", xlab="lag", ylab="rho(j)")
abline(h=0)

```

## AR(1) Process: Phi > 0

```{r}
ar1.model = list(ar=0.9)
mu = 1
set.seed(123)
ar1.sim = mu + arima.sim(model=ar1.model, n=250)
ts.plot(ar1.sim,main="AR(1) Process: mu=1, phi=0.9",
        xlab="time", ylab="y(t)", col="blue", lwd=2)
abline(h=c(0,1), col=c("black", "red"))
```

## ACF for AR(1) Process: Phi > 0

```{r}
ar1.acf = ARMAacf(ar=0.9, ma=0, lag.max=10)
ar1.acf
plot(0:10, ar1.acf, type="h", col="blue", lwd=2,
     main="ACF for AR(1): phi=0.9", xlab="lag", ylab="rho(j)")
abline(h=0)

```

## AR(1) Process: Phi < 0

```{r}
ar1.model = list(ar=-0.75)
mu = 1
set.seed(123)
ar1.sim = mu + arima.sim(model=ar1.model, n=250)
ts.plot(ar1.sim,main="AR(1) Process: mu=1, phi=-0.75", col="blue", lwd=2,
        xlab="time",ylab="y(t)")
abline(h=c(0,1), col=c("black", "red"))

```

## ACF for AR(1) Process: Phi < 0

```{r}
ar1.acf = ARMAacf(ar=-0.75, ma=0, lag.max=10)
ar1.acf
plot(0:10, ar1.acf,type="h", col="blue", lwd=2,
     main="ACF for AR(1): phi=-0.75", xlab="lag", ylab="rho(j)")
abline(h=0)

```

## AR(1) Process: Phi = 1 (Random Walk)

```{r}
set.seed(111)
rw.sim = cumsum(rnorm(250))
ts.plot(rw.sim,main="AR(1) Process: phi=1",
        xlab="time", ylab="y(t)", lwd=2, col="blue")
abline(h=0)

```

## AR(1) Process: Phi > 1 (Explosive)

```{r}
set.seed(123)
phi = 1.01
e = rnorm(250)
y = rep(0,250)
for (i in 2:250) {
  y[i] = phi*y[i-1] + e[i]
}
ts.plot(y,main="AR(1) Process: phi=1.01",
        xlab="time", ylab="y(t)", lwd=2, col="blue")
abline(h=0)
```

