Jim Killingsworth

Least Squares Mov­ing Av­er­ages

Mov­ing av­er­ages are of­ten over­laid on stock price charts to give a smooth rep­re­sen­ta­tion of chop­py price move­ments. But a sim­ple mov­ing av­er­age can lag sig­nif­i­cant­ly in a trend­ing mar­ket. In this post, I ex­plore the use of least squares re­gres­sion meth­ods to gen­er­ate more ac­cu­rate mov­ing av­er­ages.

Sim­ple Mov­ing Av­er­age

Let’s con­sid­er the sim­ple mov­ing av­er­age first so that we can use it as a ba­sis for com­par­ison. For each day, the mov­ing av­er­age val­ue is com­put­ed by tak­ing the av­er­age of all past clos­ing prices with­in a giv­en look­back pe­ri­od. The look­back win­dow shifts for­ward by one af­ter each new day clos­es. The fol­low­ing equa­tion il­lus­trates how to com­pute the sim­ple mov­ing av­er­age val­ue at a giv­en point in time:

Figure 1

For the pur­pose of brevi­ty, the equa­tion above can be rep­re­sent­ed us­ing the fol­low­ing short­hand no­ta­tion:

Figure 2

You can think of this as a con­stant func­tion for which we es­ti­mate the co­ef­fi­cient based on the ob­served da­ta points with­in the look­back win­dow. The con­stant func­tion, if plot­ted with­in the bounds of the look­back win­dow, would ap­pear as a hor­i­zon­tal line fit­ted to the av­er­age of all clos­ing prices with­in the look­back win­dow.

The charts be­low il­lus­trate what the mov­ing av­er­age looks like for three dif­fer­ent stock price da­ta set­s. The mov­ing av­er­ages are com­put­ed us­ing a 200-day look­back win­dow. The fit­ted line is plot­ted for the fi­nal look­back pe­ri­od.

For tick­er sym­bol MS­FT:

Figure 3
Figure 4

For tick­er sym­bol WYN­N:

Figure 5
Figure 6

For tick­er sym­bol HEAR:

Figure 7
Figure 8

The mov­ing av­er­ages are nice and smooth in all three cas­es, but they fail to ac­cu­rate­ly track the mar­ket prices dur­ing up­trends and down­trend­s. The mov­ing av­er­age lags be­hind the ac­tu­al price move­men­t. If you look at the fit­ted lines, you can see that the fit is less than ide­al be­cause it’s con­strained to the form of a con­stant func­tion.

Lin­ear Re­gres­sion

In­stead of fit­ting a con­stant func­tion to the ob­served da­ta points, we can in­stead fit a lin­ear func­tion to the data. A lin­ear mod­el takes the fol­low­ing for­m:

Figure 9

While there are many tech­niques that can be used to ap­prox­i­mate the co­ef­fi­cients, we’ll use the method of least squares in this post. You can com­pute the co­ef­fi­cients us­ing the fol­low­ing for­mu­las:

Figure 10

Like be­fore, the charts be­low il­lus­trate what the mov­ing av­er­age looks like for the three dif­fer­ent stock price da­ta set­s. In this case, the mov­ing av­er­age val­ues are cal­cu­lat­ed us­ing least squares lin­ear re­gres­sion. The fit­ted line takes the form of a lin­ear func­tion.

For tick­er sym­bol MS­FT:

Figure 11
Figure 12

For tick­er sym­bol WYN­N:

Figure 13
Figure 14

For tick­er sym­bol HEAR:

Figure 15
Figure 16

The lin­ear least squares mov­ing av­er­age tracks the price much bet­ter than the sim­ple mov­ing av­er­age does, es­pe­cial­ly dur­ing pe­ri­ods when the price is trend­ing up or down. If you look at the fit­ted line for MS­FT, you can see how it is al­most a per­fect fit. The lin­ear mod­el still seems to lag a lit­tle bit dur­ing re­ver­sal­s, how­ev­er.

Poly­no­mi­al Re­gres­sion

We might be able to track re­ver­sals bet­ter by fit­ting a parabo­la to the ob­served price data. A par­a­bol­ic mod­el takes the form of a sec­ond or­der poly­no­mi­al:

Figure 17

The least squares co­ef­fi­cients can be found by solv­ing the fol­low­ing ma­trix equa­tion:

Figure 18

While the ma­trix equa­tion above is arranged specif­i­cal­ly for find­ing the co­ef­fi­cients of a sec­ond or­der poly­no­mi­al, this tech­nique can be gen­er­al­ized to find the co­ef­fi­cients for high­er or­der poly­no­mi­als as well. In fac­t, the gen­er­al­ized form can even be used to find the co­ef­fi­cients for the lin­ear and con­stant func­tions de­scribed in the pre­vi­ous sec­tion­s.

Us­ing the same three stock price da­ta sets as be­fore, the charts be­low il­lus­trate what the mov­ing av­er­age looks like when ap­ply­ing least squares re­gres­sion to a qua­drat­ic mod­el. The fit­ted line takes the shape of a par­a­bol­ic curve.

For tick­er sym­bol MS­FT:

Figure 19
Figure 20

For tick­er sym­bol WYN­N:

Figure 21
Figure 22

For tick­er sym­bol HEAR:

Figure 23
Figure 24

No­tice that the fit­ted line for WYNN tracks the re­ver­sal quite nice­ly. The mov­ing av­er­age com­put­ed us­ing sec­ond or­der poly­no­mi­al re­gres­sion ap­pears to track the mar­ket prices much more tight­ly than the oth­er meth­od­s. The draw­back, how­ev­er, is that the mov­ing av­er­age is not quite as smooth.

Ex­po­nen­tial Re­gres­sion

One of my ob­jec­tives in this study is to quan­ti­ta­tive­ly recog­nize and mea­sure sus­tained trend­s. If prices are ris­ing or falling by a rel­a­tive­ly con­stant per­cent­age each day, the growth or de­cay in val­ue is not lin­ear; it is ex­po­nen­tial. In this case, it would be ap­pro­pri­ate to per­form the re­gres­sion us­ing an ex­po­nen­tial mod­el func­tion:

Figure 25

We can es­ti­mate the co­ef­fi­cients of the mod­el func­tion us­ing the lin­ear re­gres­sion tech­nique de­scribed pre­vi­ous­ly. To do this, we first need to trans­form the ex­po­nen­tial mod­el above in­to the form of a lin­ear mod­el:

Figure 26

The ex­po­nen­tial mod­el can be trans­formed in­to the lin­ear form by tak­ing the log­a­rithm of both sides:

Figure 27

The fol­low­ing equa­tions il­lus­trate how the parts of the trans­formed ex­po­nen­tial mod­el map to the lin­ear mod­el:

Figure 28

The fol­low­ing for­mu­las can then be used to cal­cu­late the co­ef­fi­cients for the lin­ear mod­el. These for­mu­las are sim­i­lar to those de­fined ear­li­er in the sec­tion on lin­ear re­gres­sion:

Figure 29

Once the co­ef­fi­cients for the lin­ear mod­el are known, we can use the val­ues to com­pute the co­ef­fi­cients for the ex­po­nen­tial mod­el:

Figure 30

Once again, the charts be­low il­lus­trate what the mov­ing av­er­age looks like for all three stock price da­ta set­s, this time us­ing least squares re­gres­sion with an ex­po­nen­tial mod­el. Note that the fit­ted line now takes the form of a ris­ing or falling ex­po­nen­tial curve.

For tick­er sym­bol MS­FT:

Figure 31
Figure 32

For tick­er sym­bol WYN­N:

Figure 33
Figure 34

For tick­er sym­bol HEAR:

Figure 35
Figure 36

When per­form­ing the re­gres­sion us­ing the ex­po­nen­tial mod­el, the re­sults look very sim­i­lar to those found us­ing the lin­ear mod­el. While ex­po­nen­tial re­gres­sion might pro­vide a slight­ly bet­ter fit in cas­es where there is ex­po­nen­tial growth or de­cay, the price track­ing can still lag be­hind dur­ing re­ver­sal­s. As the charts for HEAR show, the mov­ing av­er­age can even over­shoot quite a bit in cas­es where there is a sharp re­ver­sal.

Ac­com­pa­ny­ing source code is avail­able on GitHub.

Com­ments

Show comments