**A filter framework**

With this javascript code, you can generate your own trend filters, including Henderson filters. The framework and notation is mainly based on the following paper with some additional parameters:

Gray, A. and Thomson, P. (1996). Design of moving-average trend filters using fidelity, smoothness and minimum revisions criteria, Bureau of the Census, RR96/01 (paper)

Note: this javascript implementation makes use of the very useful sylvester.js and highcharts.js javascript libraries. Some of the matrix calculations are not currently optimal so some parameters have been restricted to keep it reasonably speedy - although it may still be slow (disclaimer). This has mainly been used as an exercise in javascript and to try out a few things so if someone finds it useful - then enjoy!

**Parameters**

The notation used below broadly matches that used in the Gray and Thomson (1996) research paper. Please read the paper for a full description of the framework.

There are ten parameters than can be changed. They are:

r | n = 2r+1 is the length of the moving average. Setting r=6 will give a 13 term moving average. Note that this input parameter is limited to positive whole numbers between 2 and 20 in this javascript implementation as unfortunately the matrix calculations get rather slow in javascript. |

p | p+1 is the smoothness of the filter. For example, set p=2 for the Henderson. Note that this input parameter is limited to positive whole numbers between 0 and 7. Typical parameters are 0,1,2 or 3. |

glam | The variance scale factor for the noise of the random walk. This is set at a default of 0. Note this input parameter is limited to positive whole numbers between 0 and 99 and up to two decimal points. |

phi | The scale factor for the variance of the white noise component. This is set at a default of 0. Note this input parameter is limited to positive whole numbers between 0 and 99 and up to two decimal points. |

evar | The variance of the noise on the original series (epsilon). This is set at a default of 1. Note this input parameter is limited to positive numbers between 0.01 and 99 and up to two decimal points, e.g. not zero. |

theta | This is a fidelity and smoothness compromise. For example, theta * F + (1 - theta)*S. Theta is set at a default of 0, eg. only considering smoothness such as for the Henderson filters. Note this input parameter is limited to positive numbers between -1 and 1 and up to two decimal points. |

endic | This is the end weight noise to trend (I/C) ratio. For example, this is set to 3.5 for the 13 term Henderson. Note this input parameter is limited to positive whole numbers between 0 and 99 and up to two decimal points. |

endp | This is the smoothness at the end of the series. This is set at a default of 1. Note that this input parameter is limited to positive whole numbers between 0 and 7. |

endlamb | This is the variance scale factor at the end of the series. This is set at a default of 0. Note this input parameter is limited to positive whole numbers between 0 and 99 and up to two decimal points. |

endevar | This is the variance of the noise at the end of the original series (epsilon). This is set at a default of 1. Note this input parameter is limited to positive whole numbers between 0 and 99 and up to two decimal points. |

**Calculate a filter**

For the standard Henderson filter framework set: p=2, glam=0, phi=0, evar=1, theta=0, endp=1, endlamb=0, endevar=1. Then change r and endic to what you would like. You can even calculate the endic parameter based on the actual series data.

For quarterly time series a 5 (r=2), 7 (r=3) or 9 (r=4) term Henderson filter is typically used.

For monthly time series the standard 13 term Henderson filter can be used which has settings: r=6, p=2, glam=0, phi=0, evar=1, theta=0, endic=3.5, endp=1, endlamb=0, endevar=1.

For more volatile monthly time series, the standard 23 term Henderson filter can be used which has settings: r=11, p=2, glam=0, phi=0, evar=1, theta=0, endic=4.5, endp=1, endlamb=0, endevar=1.

The weights will sum to one across each row of the table.

The graph of each filter weight shows how the asymmetric weights approximate the symmetric weight as more time points are added. Add and remove lines by clicking on the labels.

The gain function of each filter weight shows how the cycles within the series that the filter is applied to are impacted. A amplitude of 1 means that the cycle is unchanged. An amplitude greater than 1 means that cycle is amplified. An amplitude of 0.2 means that cycle has been reduced to 20% of its original magnitude. An amplitude of zero means that cycle is removed. Add and remove lines by clicking on the labels.

I've been struggling to get the code to work through this interface, so for the time being it is available on this page.

If you don't want to click on the buttons, or don't have javascript activated, then you can get the selected pre-prepared versions here: Henderson 5, Henderson 7, Henderson 9 and Henderson 13.