Skip to content

Instantly share code, notes, and snippets.

@josephdunn
Created May 29, 2012 04:09
Show Gist options
  • Save josephdunn/2822448 to your computer and use it in GitHub Desktop.
Save josephdunn/2822448 to your computer and use it in GitHub Desktop.
running.acf.max <- function(returns, window=100, nlags=5, plot=TRUE, main='')
{
clevel <- qnorm (0.5 + 0.95/2) / sqrt(window)
series <- rollapply(returns, window, align='right', by=1, function(w)
{
acfs <- pacf(coredata(w), lag.max=nlags, plot=FALSE)$acf
cum <- cumsum(acfs)
low <- min(cum)
high <- max(cum)
if (abs(low) > abs(high))
low
else
high
})
if (plot)
{
plot(series, main=main, cex.axis=0.65, xlab='', ylab='acf (cum max)')
abline(h=0, col='blue')
abline(h=-clevel, col='red', lty=2)
abline(h=clevel, col='red', lty=2)
}
invisible(series)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment