February 17, 2015

Simulating Gaussian White Noise

n <- 1000; time <- 1:n
eps <- rnorm(n)
plot(time, eps, type="l", col=2)

Simulating an MA(1)

beta <- 0.1
x <- filter(eps, c(1, beta), method="convolution", sides=1)
plot(time, x, type="l", col=2)

Simulating an AR(1)

alpha <- 0.5
x <- filter(eps, alpha, method="recursive")
plot(time, x, type="l", col=2)

Random Walk (Non-Stationary)

alpha <- 1
x <- filter(eps, alpha, method="recursive")
plot(time, x, type="l", col=2)

Trading an MA(1)

eps <- rnorm(n); beta <- 0.2
ret.today <- filter(eps, c(1, beta), method="convolution", sides=1)
plot(time, ret.today, type="l", col=2)

Today vs. Yesterday

ret.yesterday <- c(NA, ret.today[-length(ret.today)])
plot(ret.yesterday, ret.today, col=2)
abline(lm(ret.today ~ ret.yesterday), col=1, lty=2)

Today vs. Two Days Ago

ret.lag2 <- c(NA, ret.yesterday[-length(ret.yesterday)])
plot(ret.lag2, ret.today, col=2)
abline(lm(ret.today ~ ret.lag2), col=1, lty=2)

Buy on Up Days

mean(ret.today[ret.yesterday > 0], na.rm=TRUE)
[1] 0.06567127