pp.rollbetavol <- function (stock, market, window=250) { # placed in the public domain 2012 by Burns Statistics com <- intersect(names(stock), names(market)) nobs <- length(com) if(nobs < window) stop("only ", nobs, " names in common") stock <- stock[com] market <- market[com] beta <- stock beta[] <- NA vol <- beta dseq <- (-window + 1):0 xmat <- cbind(rep(1, window), 0) for(i in window:nobs) { this.ind <- dseq + i this.stock <- stock[this.ind] vol[i] <- sd(this.stock) xmat[,2] <- market[this.ind] beta[i] <- lm.fit(xmat, this.stock)$coef[2] } ans <- cbind(Volatility=sqrt(252) * 100 * vol, Beta=beta) ans[-1:(-window + 1), ] }