Saturday, November 19, 2011

Phase Shift


Earlier this month, jim offered some technical knowledge regarding the use of moving averages:

In digital signal processing (DSP) the moving average is classified as a low pass filter (tends to attenuate high frequency components in the data). The moving average is sometimes called a "boxcar" filter.

In DSP the issue of where you reference the input and output time stamp is referred to as causality. If you only use the most recent past input data points to create the current output data point the filter is causal. If you use data points ahead of the current output it is not causal.

If the output current sample point is the middle of the input sample points the frequency response of the filter is said to be "zero phase".

So... If the moving average is plotted at the last year of the years averaged, the result is "causal". If the moving average is plotted at the middle year of the years averaged, the result is "zero phase". The difference is significant enough that people invented names for the different versions.


Graph #1 shows the Federal Funds rate -- monthly numbers in blue, and annual numbers in red -- from the St. Louis Fed:

Graph #1

For Graphs #2 and #3 below, I'm using annual numbers. Because it's easier.

The vertical blue bars on the graphs below show the Federal Funds rate. The thin red line shows the moving average.

You can make the moving average move by sliding your mouse back-and-forth across the number bar below the graph.

Graph #2 is a "causal" representation. Each point on the red line shows the average of the previous N years:


Graph #2: A "Causal" Moving Average

1 YEAR DATA2 YEAR AVG 3 YEAR AVG 4 YEAR AVG 5 YEAR AVG 6 YEAR AVG 7 YEAR AVG 8 YEAR AVG 9 YEAR AVG 10 YEAR AVG

You can see the peaks mellow and drift to the right as you move the mouse rightward over the number bar. They mellow because they get averaged down, blended in with lower numbers. They drift to the right because each point on the red line is plotted at the last year of the years averaged. You can see the same rightward drift in the start-point, the left end of the red line as you wave the mouse back and forth.

It is as if the mouse pulls the red line rightward, then allows it to spring back to its original shape.


Graph #3 is a "zero-phase" representation. Each point on the red line is plotted in the middle of the period averaged. The average value plotted at the average year, so to speak:


Graph #3: A "Zero Phase" Moving Average

1 YEAR DATA3 YEAR AVG 5 YEAR AVG 7 YEAR AVG 9 YEAR AVG 11 YEAR AVG 13 YEAR AVG 15 YEAR AVG

Move your mouse back-and-forth across the number-bar below Graph #3. Again you can see the peaks mellow as you move to the right. But this time the mellowing peaks remain centered on the rigid peaks of the blue bar graph.

The "zero phase" version of the moving average makes more sense to me, for the things that I look at. It puts high points where the high points are, rather than dragging them off to the side.

Both versions are useful, no doubt. But the "causal" version appears to be more common; it's the way Excel does moving averages, for example. And the dragging-off-to-the-side thing is an important thing to know, if you plan to work with moving averages.

The red line itself is the same in both versions. Compare the 7-year Causal to the 7-year Zero Phase, or the 5-year Causal to the 5-year Zero Phase, for example. The red lines on the two graphs are identical. Only the location of that line against the background graph is different.

1 comment:

Jazzbumpa said...

Wow - you put a LOT of work into this.

In digital signal processing, the zero phase version probably has some useful purpose. But, when looking at macro phenomena, like interest rates, it's hard for me to relate to the zero phase version. The main practical problem is that at any real time, you don't have a current value or any recent values for the average.

What can you do with it?

This is all in the context of trends, remember, and deciding when a trend has changed.

Hard enough to do in real time, without delaying your signal by half a data packet.

http://amateurelliott.blogspot.com/2011/11/slightly-longer-view.html

Cheers!
JzB