Greg Stanley and Associates Performity LLC
 Home   About Us   Products   Services   Examples   Tech Resources   Contact Us 
HomeTech ResourcesFault DiagnosisFiltering > MACD Approach >

The “MACD Approach” to Derivative (Rate of Change) Estimation

Filtering topics:


This page describes the “MACD approach” for filtering to estimate derivatives (rate of change of variables over time), and second derivatives as well. This page is part of the section on
Filtering that is part of
A Guide to Fault Detection and Diagnosis..

An overview of the MACD (dual filter difference) approach

The central idea is to subtract a heavily filtered value from a lightly filtered value, as shown in the following block diagram. (A scaling factor must be applied, not shown here.)  In this diagram, the filters are exponential filters, with time constants  tau1tau2 :

MACD general block diagram

The extreme case with tau1 = 0 (no light filter at all) is also included, as discussed in a special section later. That is, just subtract a heavily filtered value from the current value. 

This is intuitively appealing: roughly speaking, the lightly filtered value approximates a recent value, and the heavily filtered value approximates an older value. Derivatives are the difference between a recent value and an old value, after dividing by a scale factor representing a time interval.

The original “MACD” acronym stands for “Moving Average Convergence Divergence”. This terminology describes a particular calculation used for trend analysis for investments. In that case, the heart of the calculation involves exponential filters with 12-week and 26-week time constants. That specific MACD calculation also throws in another 9-week exponential filter in series, to filter the derivative estimate even more, and also enable estimation of the second derivative. Here, we use the terminology “MACD approach” to mean the idea of taking the difference of two filter outputs to estimate a derivative.

This “moving average” part of the MACD acronym abuses the ARMA “moving average” terminology, since there is no input history that is used - just the current input. This naming continued the unfortunate practice (used in stock analysis and some other places) of calling an exponential filter an “exponentially weighted moving average” (EWMA or EMA), even though it is not a moving average using traditional time series terminology.

Effects of the time constants for exponential filters in an MACD approach

This approach with two exponential filters generates an estimate of the filtered value of the derivative. The calculation is the equivalent of those same two filters in series, in series with a differentiator, with an overall gain of the difference in the two time constants (tau2 - tau1). That is, to estimate the derivative, take the output and divide it by (tau2 - tau1).

When using exponential filters with a fixed sample time interval, the time scale is based on the sample time. To convert to the time derivative, divide the output by the sampling interval time.

One important special case is when tau1 = 0. That is, you take a variable with no filtering, and subtract a filtered value using a filter with time constant tau2 .  In this case, you get an estimate of the derivative filtered by a filter with time constant tau2 , and gain tau2 .  The full version with both filters is usually preferred to reduce the impact of noise.

As an approximation at low frequencies, the MACD approach can be seen to roughly approximate a differentiator in series with a single filter. The approximate filter time constant for that single equivalent filter is roughly the sum of the two time constants (tau1 + tau2). The output has the same gain, equal to (tau2 - tau1). 

Why does MACD estimate the time derivative?

You can skip the explanation of this approximation and just use the results above. The analysis that follows is for the continuous time (analog) equivalent of these digital filters. We do some “hand waving” that the filter outputs for the analog and digital first order lags are the same at the sampling times, when the digital “smoothing” constant (a number between 0 and 1) is set based on the time constant. This is explained in the section on the exponential filter. By looking at the continuous time equivalent, we can use Laplace transforms, which are probably more commonly known than the z transforms of discrete time systems. The equivalent to the MACD diagram above can then be represented by the following block diagram, where the exponential filters are replaced by the corresponding first-order lags:

MACD Laplace transform equivalent

We can then write the gain G(s) of this system as

MACD gain calculation

That is, the MACD calculation is the equivalent of the same two filters in series, in series with a differentiator. The gain term for the overall block is the difference of the time constants. In block diagram form, this is:


MACD equivalent block diagram


The special case of a single-filter

In the special case of tau1 = 0, the first block in the equivalent diagram above has no dynamics - just a gain of 1, which can be ignored. Then the derivative estimate is just filtered by the single filter with time constant tau2 and gain tau2 . This is the result for the simplest estimator - when you just subtract a filtered value from the input value. In block diagram form, the implementation is just:


An approximation for low frequencies

The previous formula for the MACD gain can be rewritten as: 


For lower frequencies, s approaches zero, so the s squared term can be neglected as an approximation. After discarding that term, the block diagram of the system is approximately


MACD approximate block diagram


That is, we have a first-order filter (lag) in series with a differentiator with a gain. The first order filter has a time constant equivalent to the sum of the original time constants. The gain term for the overall block is the difference of the time constants. 

MACD for estimating the second derivative

The full MACD calculation involves 3 exponential filters. The derivative estimate described above, when plotted on a graph, is called the “MACD line”. An additional filter called the “signal filter” then further filters the MACD output (with a 9-week time constant for the typical MACD calculation). The output of that signal filter is called the “signal”. A subtraction (MACD - signal) is called the “histogram”, not because it is an actual histogram in normal probability usage, but probably because it is usually plotted with bars.  The “histogram” is an estimate of the second derivative, with additional gain and time constant from the signal filter.

The “histogram” estimates the second derivative, because, as noted earlier, subtracting a filtered variable from the variable generates an estimate of its time derivative. The input to the signal filter is already the first derivative, so the “histogram” estimates the derivative of that, to get the second derivative. There is already enough filtering in place that it only takes the one additional “signal” filter to estimate the second derivative. 

The Wikipedia article on MACD provides a good visualization of the calculations for stock price analysis, with an example graph. In “technical stock analysis”, “velocity” means derivative, and “acceleration” means second derivative. 

Advantages and disadvantages of the “MACD approach”

The advantages are simplicity, minimal computation, and minimal data storage. The two exponential filters are easy to implement and widely available in existing systems. Exponential filters have a minimal memory requirement for data - just the previous output, and the fastest calculation (assuming fixed time sampling size).  The MACD approach takes much less computation than a full least squares filter, although the special case of the Savitzky-Golay filter is comparable for its simplicity and computational effort.

The results are very smoothly changing outputs, heavily lagged so that there is no overshoot in the derivative estimate.

One disadvantage is the extra lag compared to, say, a least squares filter. Also, some may be uncomfortable with the fact that this is an infinite impulse response (IIR) filter. As a result, following a step change, the sign of the derivative estimate will remain the same essentially forever as it decays towards zero. In the real world, the input will be changing constantly, so this is unlikely to be a problem. Exponential filters are IIR filters, but are heavily used in control systems.

Copyright 2010 - 2020, Greg Stanley

Return to Filtering   Next: Filter Strategy and Tuning

Return to A Guide to Fault Detection and Diagnosis


Share this page:  Share this page by e-mailing link...