Least Squares Moving Averages
Moving averages are often overlaid on stock price charts to give a smooth representation of choppy price movements. But a simple moving average can lag significantly in a trending market. In this post, I explore the use of least squares regression methods to generate more accurate moving averages.
Simple Moving Average
Let’s consider the simple moving average first so that we can use it as a basis for comparison. For each day, the moving average value is computed by taking the average of all past closing prices within a given lookback period. The lookback window shifts forward by one after each new day closes. The following equation illustrates how to compute the simple moving average value at a given point in time:
For the purpose of brevity, the equation above can be represented using the following shorthand notation:
You can think of this as a constant function for which we estimate the coefficient based on the observed data points within the lookback window. The constant function, if plotted within the bounds of the lookback window, would appear as a horizontal line fitted to the average of all closing prices within the lookback window.
The charts below illustrate what the moving average looks like for three different stock price data sets. The moving averages are computed using a 200-day lookback window. The fitted line is plotted for the final lookback period.
For ticker symbol MSFT:
For ticker symbol WYNN:
For ticker symbol HEAR:
The moving averages are nice and smooth in all three cases, but they fail to accurately track the market prices during uptrends and downtrends. The moving average lags behind the actual price movement. If you look at the fitted lines, you can see that the fit is less than ideal because it’s constrained to the form of a constant function.
Linear Regression
Instead of fitting a constant function to the observed data points, we can instead fit a linear function to the data. A linear model takes the following form:
While there are many techniques that can be used to approximate the coefficients, we’ll use the method of least squares in this post. You can compute the coefficients using the following formulas:
Like before, the charts below illustrate what the moving average looks like for the three different stock price data sets. In this case, the moving average values are calculated using least squares linear regression. The fitted line takes the form of a linear function.
For ticker symbol MSFT:
For ticker symbol WYNN:
For ticker symbol HEAR:
The linear least squares moving average tracks the price much better than the simple moving average does, especially during periods when the price is trending up or down. If you look at the fitted line for MSFT, you can see how it is almost a perfect fit. The linear model still seems to lag a little bit during reversals, however.
Polynomial Regression
We might be able to track reversals better by fitting a parabola to the observed price data. A parabolic model takes the form of a second order polynomial:
The least squares coefficients can be found by solving the following matrix equation:
While the matrix equation above is arranged specifically for finding the coefficients of a second order polynomial, this technique can be generalized to find the coefficients for higher order polynomials as well. In fact, the generalized form can even be used to find the coefficients for the linear and constant functions described in the previous sections.
Using the same three stock price data sets as before, the charts below illustrate what the moving average looks like when applying least squares regression to a quadratic model. The fitted line takes the shape of a parabolic curve.
For ticker symbol MSFT:
For ticker symbol WYNN:
For ticker symbol HEAR:
Notice that the fitted line for WYNN tracks the reversal quite nicely. The moving average computed using second order polynomial regression appears to track the market prices much more tightly than the other methods. The drawback, however, is that the moving average is not quite as smooth.
Exponential Regression
One of my objectives in this study is to quantitatively recognize and measure sustained trends. If prices are rising or falling by a relatively constant percentage each day, the growth or decay in value is not linear; it is exponential. In this case, it would be appropriate to perform the regression using an exponential model function:
We can estimate the coefficients of the model function using the linear regression technique described previously. To do this, we first need to transform the exponential model above into the form of a linear model:
The exponential model can be transformed into the linear form by taking the logarithm of both sides:
The following equations illustrate how the parts of the transformed exponential model map to the linear model:
The following formulas can then be used to calculate the coefficients for the linear model. These formulas are similar to those defined earlier in the section on linear regression:
Once the coefficients for the linear model are known, we can use the values to compute the coefficients for the exponential model:
Once again, the charts below illustrate what the moving average looks like for all three stock price data sets, this time using least squares regression with an exponential model. Note that the fitted line now takes the form of a rising or falling exponential curve.
For ticker symbol MSFT:
For ticker symbol WYNN:
For ticker symbol HEAR:
When performing the regression using the exponential model, the results look very similar to those found using the linear model. While exponential regression might provide a slightly better fit in cases where there is exponential growth or decay, the price tracking can still lag behind during reversals. As the charts for HEAR show, the moving average can even overshoot quite a bit in cases where there is a sharp reversal.
Comments