## Saturday, April 23, 2011

### The many facets of linear regression

Many years ago, a portfolio manager asked me in a phone interview: "Do you believe that linear or nonlinear models are more powerful in building trading models?" Being a babe-in-the-woods, I did not hesitate in answering "Nonlinear!" Little did I know that this is the question that separate the men from the boys in the realm of quantitative trading. Subsequent experiences showed me that nonlinear models have mostly been unmitigated disasters in terms of trading profits. As Max Dama said in a recent excellent article on linear regression: "...when the signal to noise ratio is .05:1, ... there’s not much point in worrying about [higher order effects]". One is almost certain to overfit a nonlinear model to non-recurring noise.

Until recently, I have used linear regression mainly in finding hedge ratios between two instruments in pair trading, or more generally in finding the weightings (in number of shares) of individual stocks in a basket in some form of index arbitrage. Of course, others have found linear algebra useful in principal component analysis and more generally factor analysis as well. But thanks to a number of commenters on this blog as well as various private correspondents, I have begun to apply linear regression more directly in trading models.

One way to directly apply linear regression to trading is to use it in place of moving averages. Using moving average implicitly assumes that there is no trend in a price series, that the mean of the prices will remain the same. This of course may not be true. So using linear regression to project the current equilibrium price is sometimes more accurate than just setting it equal to a moving average. I have found that in some cases, this equilibrium price results in better mean-reverting models: e.g. short an instrument when its current price is way above the equilibrium price. Of course, one can also use linear regression in a similar way in momentum models: e.g. if the current price is way above the equilibrium price, consider this a "breakout" and buy the instrument.

Max in his article referenced above also pointed out a more sophisticated version of linear regression, commonly called "weighted least squares regression" (WLS). WLS is to linear regression what exponential moving average (EMA) is to simple moving average (SMA): it gives more weights to recent data points. Indeed I have found that EMA often gives better results than SMA in trading. However, so far I have not found WLS to be better than simple least squares. Max also referenced an article which establishes the equivalence between weighted least squares and Kalman filter. Now Kalman filter is a linear model that is very popular among quantitative traders. The nice feature about Kalman filter is that there is very few free parameters: the model will adapt itself to the means and covariances of the input time series gradually. And furthermore, it can do so one-step at a time (or in technical jargon, using an "online" algorithm) : i.e., there is no need to separate the data into "training" and "test" sets, and no need to define a "lookback" period unlike moving averages. It makes use of "hidden states" much like Hidden Markov Models (HHM), but unlike HHM, Kalman filter is faithfully linear.

I haven't used Kalman filter much myself, but I would welcome any comments from our readers on its usage. Also, if you know of other ways to use linear regression in trading, do share with us here!

#### 42 comments:

Damian said...

I've often heard people refer to the Kalman filter as a T3 moving average - but I've not seen one coded up that didn't include a lookback period.

Here's an implementation in Amibroker - curious what you think.

http://www.wisestocktrader.com/indicators/240-t3-function-include-afl

Ernie Chan said...

Hi Damian,
Thanks for the link. I think the implementation of the T3 Kalman filter is too complicated and ad-hoc. The "nonlinear" in "... T3 is a six-pole non-linear Kalman filter ..." is precisely what most of us want to avoid.

In the Kalman implementation referenced by Max (http://www2.econ.iastate.edu/tesfatsi/FLSTemporalDataMining.GMontana2009.pdf), there is a parameter that controls how fast the regression parameter is allowed to change. This can be viewed as some kind of lookback parameter, since the faster it is allowed to change, the shorter the effective lookback period is.

Ernie

Damian said...

Yes a very different thing....pretty interesting. Thanks for the link.

Matthew said...

The Kalman filtering approach is a really important concept. Two things to keep in mind is that the filter uses a model to predict the system's next state. So you still have to choose between linear and non-linear modeling even after deciding to apply such a Kalman filter.

Also be aware that the mathematical underpinnings of the Kalman filter assume continuous, normally distributed variables. Items which are strikingly hard to come by in trading.

Mr.LoL said...

Hi Ernest:

I am pretty new in this quant world, I have been reading your posts and I have a question if you don't mind...

What is the point of focusing on HFT when using pair trading and holding them for some days seems to be so much easier?

I mean that you could get a 10%? 20% maybe? using that approach and you just have to worry about the mathematical model itself, not the implementation,slippage, etc.

I also assume that the shorter the timeframe, the greater the randomness, is this right? Does it really pay off in terms of reward?Could you give us some approximation here comparing those two ways of trading??

Ernie Chan said...

Hi LoL,
The advantage of HFT is that the Sharpe ratio is typically much higher than overnight trading, so that allows you to use more leverage, and that in turns allow you to have much higher returns.

I do not believe that randomness increases with trading frequency. In fact, I find the opposite to be true, as short time scales prevents extraneous events from disrupting the model.

Ernie

Anonymous said...

Hi Ernie,

Dumb me, I just noticed I missed the seasonal trade on RBOB (+\$3381 per RBOB contract this year)...

Did you remember to trade it?

Ernie Chan said...

Hi Anon,
Good to hear RBOB is still working!
No, I didn't trade either: I am focusing on higher frequency trading these days.
Ernie

Anonymous said...

Can you elaborate on how you use linear regression in place of moving averages? What's the dependent variable and what's the regressor? Thanks.

Shaun Overton said...

Hi Ernie,

This is a decidedly non-mathematical approach, but I've taken to resetting my moving averages whenever a bar > 2 standard deviations forms. The MA period grows linearly until the next volatility event.

Click on the "Resetting Moving Average" to get the indicator code for MT4 or NinjaTrader.

Ernie Chan said...

Anon,
In using LR instead of MA, the time variable t=1,2,3... is the independent variable, and the price is the dependent one.
Ernie

Ernie Chan said...

Hi Shaun,
That's an interesting approach and it does make sense. Thanks for sharing!
Ernie

Wei said...

Hi Ernie,

Great article and thanks for sharing your thoughts on linear regression and other technical methods. There are a couple of important aspects worth pointing out:

1. OLS and WLS require specifying hyperparameters like the length of lookback window, with expanding or rolling window being popular choices. The coefficients however are sensitive to the size of window: too slow to adapt if too long a window, high sample error if too short. I have encountered situations where the hedge ratio changes its sign as data samples rolling forward, completely nonsense and purely an artifact of LR properties and sampling errors. It led to a breakdown of the regression-based trading model, but I consider this a fortunate revelation as I had long worried about the arbitrariness of selecting a hyperparameter without clear economic justification. One could "optimize" the window size to get the best backtest results but the problem is that tomorrow is a different day. That leads to the second and more serious problem with linear regression.

2. OLS, and even WLS, disregard the intertemporal structure of the time series data. Max claims that WLS solves this problem, but my experience has been that it makes no significant difference, and you seem to agree. Again, one faces the problem of deciding what kind of weights and decay rate to be applied. Yet another hyperparameter, to be decided not on economic grounds.

3. Kalman filter solves these problems to a large extend, and it works well with discrete data (unlike one commentator claimed). It's also simple and efficient to implement, but it's not a free lunch. To use it, you need a model specification, and there is no off-the-shelf way of doing that. It's completely up to your creativity and understanding the trading problem. Of course, it has its own set of issues too, but at least you can frame it in economic terms, because hopeful you have created a model specification based on sensible economics.

4. Last but not least, on whether high frequency microstructure is more or less noisy. It depends on the market and assets you are looking at. For an asset with high intraday volatility, you might be better off using low frequency data. Almost by definition, high volatility is an indication of high degree of noise around the "true" fundamental value.

Ernie Chan said...

Hi Wei,
Thanks for your thoughtful comments on OLS, WLS, KF, and noise.

In my experience, hedge ratios do not vary too much based on the lookback period. Perhaps that's because I focus only on ETF pairs and they are pretty stable. I am surprised though to hear that even WLS has such a sensitive dependence, as the weights are supposed to smooth this out.

With regard to choosing the right model for KF, I stick to Occam's razor as usual! But yes, if you know a bit about the economics of the trade, it would be a big help, though usually I am clueless until after-the-fact.

With regard to noise -- for a mean-reversion trader, more noise means more profit opportunities! (We assume that the noise is mean-reverting.) So if intraday trading is noisy in that sense, intraday trading is therefore very profitable. The noise we don't like is the type that does not mean-revert: for e.g. those created by exogenous corporate/economic/political events.

Ernie

K said...

Hi Ernest,

I just started my own blog for my personal usage, can I have your permission to add your link to my blog?

Thank you.

Regards
Kenneth

Ernie Chan said...

Hi Kenneth,
Sure, please feel free to link to my blog.
Ernie

Anonymous said...

Ernie,

This may sound like a strange question to ask on a blog called, "Quantitative Trading", but have you ever evaluated the quant-oriented daytrading methods discussed on this blog in comparison to plain old value investing and security analysis? That method can be summarized as follows: gain a detailed understanding of a stock or bond security through research, buy when the price is much lower than what it's really worth (i.e., intrinsic value), and sell as it approaches intrinsic value.

The reason I ask such a fundamental question is that my background is very technical and mathematical, much like yours. I have a Ph.D. in Electrical Engineering and have a background in things like linear regression and kalman filters. But after evaluating all investment methods I was aware of, the simple non-quant idea of buying securities at a deep discount to fair value still makes the most sense to me.

I do enjoy your blog - don't get me wrong. I was just wondering if you ever thought about this more fundamental question.

Thanks,
aagold

Ernie Chan said...

Anon,
Both approaches are equally valid.

The value investing approach usually implies a long holding period. However, value investing is not antithetical to a quantitative approach. In fact, many people call Ben Graham the first "quant". For e.g. factor models utilize many fundamental and economic indicators in order to determine the fair value of a stock.

What people usually have in mind as algorithmic or quantitative trading typically occur at a higher frequency. At such frequency, fundamental information becomes less important.

Value investing typically has low Sharpe ratio and large drawdown, but it has very high capacity. High frequency algorithmic trading has the opposite characteristics.

An ideal hedge fund should encompass both approaches, but few managers have equally excellent skills in both, not even Jim Simons.

Ernie

Ronnie said...

Hi Ernie!

Sorry for the off topic..

Could you explain which way is the better for a new trader to start?

Imagine you have not a lot of capital, pair trading often involves buying-selling contracts that are quite big for the small money, so the way to do this would be over-leveraging which is dangerous.

My question is: Where should a trader with 20k for example look at? forex? commodities? stocks?

Would pair trading be ok for this? O should the trader go for other options like volatility trading or such?

Thanks in advance.

Ernie Chan said...

Hi Ronnie,
FX and futures are the best areas for a trader with small capital base to start, due to the small margin requirement. Of course, that assumes that you have good strategies in those areas!

Pair trading ETF's are pretty easy and safe, but as you said, requires a good bit of capital to make a living.

Ernie

Anonymous said...

Hi Erine

Roughly, how much you have to spend on setting up the infrastructure (co-location) of HFT business, those hardware seems pretty expensive. The setup cost seems too much for retail trader

Kat

Ernie Chan said...

Hi Kat,
Hardware is not expensive. Any server of about \$5K will do. What's expensive is what your broker will charge for the ongoing colocation expense: at least \$2K / month.

None of these matter if your HFT strategy actually works!
Ernie

Anonymous said...

Hi Erine

HFT seems a quite profitable strategy for small fund capital, I heard that some of the banks embbeded their trading strategy in a microchip to gain extra speed. Sounds likes everyone keeps on investing on hardware to front run the other taders. What software language you use to implement the HFT, matlab?

Dave

Ernie Chan said...

Hi Dave,
I hesitate to call my strategies HFT: I can certainly tolerate latency of a few seconds.

While the high turnover of HFT does allow a small fund to use its small capital base very efficiently, the infrastructure cost for a true HFT strategy is beyond most small funds.

Yes, I implement all my strategies in Matlab.

Ernie

Issy said...

hi Ernie, what's your thought on measuring divergence between the price and an oscillator such as RSI?

Given that the divergence is done on the swings; and not on the raw data points; is Linear Regression a good candidate?

Issy

Ernie Chan said...

Hi Issy,
I am not exactly sure what you mean by "divergence is done on the swing, and not the raw data points". Could you please elaborate?
Ernie

GTji said...

I suspect that Linearregression Avg may cause your system to be overly curve fitted, whats you opinion on that?

Suny said...

I have a question on OLS function by Spatial Econometrics. I used that function as suggested by Ernie in this book. Somehow the hedge ratio (or beta) of the regression comes out different from when I run it with glmfit function in Econometric toolbox. The result from OLS in Spatial econometrics and REGRESS in matlab comes out the same but different from glmfit. I have tested with simple Excel regression and SAS function. Those numbers agree with glmfit. I am just wondering what makes the difference here. Am I missing something here? Thanks

Ernie Chan said...

Suny,
Have you made sure that no offset was used in the regression fit in all cases?
Ernie

Anonymous said...

Hi Ernie - quick question if you don't mind the time. Appreciate your time as always. I'm wondering how do you set up the regression in place of the MA:

>In using LR instead of MA, the time >variable t=1,2,3... is the >independent variable, and the price >is the dependent one.

Price = a + b * t

1) Are you using intercept or is it better to leave it out?

2) The time variable t - are you using just an integer for t that increments by 1 as you move forward in time?

3) I imagine you are doing a rolling-window regression, similar to how a moving average rolls forward based on the window period selected.

Greatly appreciate the insights.

Shal

Ernie Chan said...

Anon,
1) Intercept is needed here because prices do not go to zero at an arbitrary t=0.

2) t can increment by 1 at every bar.

3) For ordinary OLS, a rolling window is needed. For WLS or Kalman Filter, we don't need rolling window.
Ernie

Anonymous said...

Thanks Ernie for the feedback on the regression setup. Here is a simple way to do this in R if anyone wants to fiddle around.

library('quantmod')
getSymbols(c('AAPL'),from='01-01-2003')

lm(Cl(AAPL) ~ index(AAPL)) -> results

summary(results)
plot(index(AAPL), Cl(AAPL),type='line')

abline(coef = coef(test))

Shal

Boris said...

Hi Ernie,
you mentioned you used LR also in basket arbitrage trading. I am using it in forex basket arbitrage trading with R where the regressand is EURUSD. Can you tell me how you are deriving the lot sizes from the calculated coefficients ?

Ernie Chan said...

Hi Boris,
I assume you are regressing A.USD, B.USD, ..., against EUR.USD, so that all independent variables are denominated in USD? If so, then the regression coefficients are the lot sizes.
Ernie

Boris said...

Hi Ernie, thanks for the feedback. I am using mixed pairs xxxUSD and USDxxx like USDJPY and USDCHF. Deposit currency is USD.

Ernie Chan said...

Hi Boris,
In that case, you have to first convert all the pairs to X.USD first, do the LR, obtain the lot size, and then convert the lot size back to USD.X.
Ernie

Boris said...

Thanks for the feedback. It sounds good. I am thinking about another approach: normalization of the currency pairs should be also achieved by deviding quotes through its related pip value per lot - calculated with ticksize and tickvalue correct ?

Ernie Chan said...

Boris,
Yes, as long as each point move represents the same dollar amount, you can run your LR on any price series.
Ernie

Unknown said...

Hi Ernie,

I have one question regarding LR. I am using them as Moving Average for example 21 day LR & 63 day LR. I will be looking out for cross over and also price cross them either from up or down. My question is what can be better option to filter for trend identification and also how to avoid whipsaw as we often see in MA crossover.

Ernie Chan said...

Unknown,
Different lookback is optimal for different time series. If you are looking for trend, you should check the correlation coefficient of various lookback periods with a holding period and see which one is optimal for your time series.
Ernie

sg said...

By the way, what does it mean by "independent variable" and "dependent variable" in this context?

Ernie Chan said...

Hi sg,
For pair trading, you can arbitrarily pick any one price series as independent variable, and the other as dependent. However, it is a good idea to try both permutations.
Ernie