# Testing for Significant Alpha in a Few Ways

(1) Over the Full Sample Period (2) Whether Alpha in one Subinterval Predicts Alpha in the Next (3) and Plotting the Time Series of Alpha and Beta

library(tseries)

alpha <- function(stock) {

days <- 5040

st <- get.hist.quote(stock, start = (Sys.Date() - days), quote = "Close")
sp <- get.hist.quote("^gspc", start = (Sys.Date() - days), quote = "Close")
r <- diff(st)/st[1:(length(st) - 1)]
m <- diff(sp)/sp[1:(length(sp) - 1)]

if (length(r) != length(m))
cat("Not enough data for this stock!!! Pick a different one.\n") else {

n <- floor(length(r)/30)
a <- rep(0, n)
b <- rep(0, n)
for (i in 1:n) {
g <- 30 * (i - 1)
h <- 30 * i
a[i] <- lm(r[g:h] ~ m[g:h])$coef[1] b[i] <- lm(r[g:h] ~ m[g:h])$coef[2]
}
cat("#########################################################\n")
cat("Test for significant average Alpha over all intervals:\n")
cat("#########################################################\n")
cat("The average monthly alpha is", mean(a), "\n")
t <- mean(a)/(sd(a)/sqrt(length(a)))
cat("For a t-stat of", t, "\n")
p1 <- 2 * (1 - pt(abs(t), length(a) - 1))
cat("And a p-value of", p1, "\n")
cat("So we", ifelse(p1 < 0.05, "reject the null, and find evidence for significant alpha.",
"do not reject the null, which is evidence for no alpha."), "\n")
cat("\n")
par(mfrow = c(2, 1))
plot(a, type = "b", ylab = "Alpha", xlab = "30 Day Intervals")
plot(density(a), type = "b", main = "Alpha Empirical Density", xlab = "30 Day Intervals")
par(mfrow = c(2, 1))
plot(b, type = "b", ylab = "Beta", xlab = "30 Day Intervals")
plot(density(b), type = "b", main = "Beta Empirical Density", xlab = "30 Day Intervals")
cat("#########################################################\n")
cat("Test whether this month's Alpha can predict next month's (first order autoregressive model):\n")
cat("#########################################################\n")
summary(arma(a, order = c(1, 0)))
}
}


We can then use the funciton like:

alpha("f")

## #########################################################
## Test for significant average Alpha over all intervals:
## #########################################################
## The average monthly alpha is -0.0007302
## For a t-stat of -1.539
## And a p-value of 0.1265
## So we do not reject the null, which is evidence for no alpha.


## #########################################################
## Test whether this month's Alpha can predict next month's (first order autoregressive model):
## #########################################################

##
## Call:
## arma(x = a, order = c(1, 0))
##
## Model:
## ARMA(1,0)
##
## Residuals:
##       Min        1Q    Median        3Q       Max
## -0.022352 -0.002254 -0.000197  0.002322  0.024459
##
## Coefficient(s):
##            Estimate  Std. Error  t value Pr(>|t|)
## ar1       -0.020227    0.093224    -0.22     0.83
## intercept -0.000735    0.000479    -1.53     0.12
##
## Fit:
## sigma^2 estimated as 2.61e-05,  Conditional Sum-of-Squares = 0,  AIC = -883.4