//
you're reading...

Finance

A thought on Linear Models on Stocks

Timely Portfolio has a nice post about linear models sytems for stock.

The idea follows from the steps below:

  • Get the weekly closing values of the S&P 500.
  • Choose a time window (i.e. 25 weeks) and for each window, linearly regress the subset of closing values
  • Choose an investment strategy based on the residuals, the running average of slope coefficients, or the running average of correlation with data points

The idea is quite simple, and so far, from Timely Portfolio’s post, it looks like the drawdown is behaving nicely.

It seems like the idea could be extended to a non-linear method. The residuals are getting larger and larger, and this indicates that linear methods are less reliable as time goes by.

# code from Timely Portfolio
# http://timelyportfolio.blogspot.ca/2011/08/unrequited-lm-love.html
require(PerformanceAnalytics)
require(quantmod)
 
getSymbols("^GSPC",from="1896-01-01",to=Sys.Date())
 
GSPC <- to.weekly(GSPC)[,4]
 
width = 25
for (i in (width+1):NROW(GSPC)) {
        linmod <- lm(GSPC[((i-width):i),1]~index(GSPC[((i-width):i)]))
        ifelse(i==width+1,signal <- coredata(linmod$residuals[length(linmod$residuals)]),
                signal <- rbind(signal,coredata(linmod$residuals[length(linmod$residuals)])))
}
signal <- as.xts(signal,order.by=index(GSPC[(width+1):NROW(GSPC)]))
plot(signal, main="Residuals through time")
plot(log(signal), main="Log of Residuals through time")

Discussion

4 Responses to “A thought on Linear Models on Stocks”

  1. thanks so much for sharing the code and offering your unique insight. let me know if you ever want to collaborate on some R.

    Posted by timely portfolio | April 16, 2012, 10:14 pm
  2. This is very interesting, but I think including data going back to 1950 might be a little misleading…if one redraws the above plots for, say, 1999-2012, one does not get a sense of constantly escalating volatility…more of a (relatively) quiet period in the mid-2000s, and a failure of the market to fully calm itself after the 2008 crash.

    Also, the numbers on the y-axis are related to the scale of the S&P index itself, so the numbers tend to undergo growth as the index value increases (remember that in this 60 year period, your starting value is 16.98, while your ending value is closer to two orders of magnitude larger).

    But thanks for a very thought-provoking blog post!

    Posted by Jeff | April 19, 2012, 3:50 pm

Trackbacks/Pingbacks

  1. [...] a previous post, we discussed ideas generated by a Timely Portfolio post about Linear Models on [...]

Post a Comment

About

Eric Nguyen is a data scientist with current focus on Finance, Economics, Big Data Analytics, and Social Mood analysis.

more

Topics