Quantcast aiQUANT » Filter Design

Archive for the 'Filter Design' Category

Negative lag filter - new algorithm

Filter Design, Signal Processing, Technical Indicators 2 Comments »

In an earlier post I mentioned a filtering algorithm which demonstrated negative lag for low frequencies (trending market) and high magnitude gain for higher frequencies (cyclic market).  I modified the algorithm to deal with the high frequency overshoot problem by removing the dependency on the second order power of the price.  Although the filter copes relatively well at higher frequencies, we no longer have negative lag at the low frequency.  Perhaps it would be nice to compare lag and magnitude gain of both filters across all frequencies to understand the actual differences.

negLagNew.png

A filter with negative lag

Filter Design, Technical Indicators 6 Comments »

The problem with technical indicators is that they suffer lag, resulting in signals appearing after events have occurred in time series context. Recently I’ve been investigating ways of reducing lag in these filters, building upon the results from a previous post which shows the workings of a zero-lag indicator derived from first principles. It seems something can be done, although the technique as it stands needs further refining. The graph below shows how it compares with the Exponential Moving Average (EMA) and the Moving Average (MA):

predictFil.png

The thing with this filter is that it has negative lag when the market is trending but has high gain when the market is in cycle mode. The reason for this is that in the filtering algorithm there is a part where I multiply the actual price by itself which results in noise components being amplified. To understand this let us consider the following:

Price data can be seen as comprising of two components: a signal part which is useful and a noise part which is not useful.

\small \text{Price=Signal+Noise}

The effect of multiplying Price by itself is a quadratic equation with signal and noise terms:

\small \text{Price*Price}=\left\{\text{(S+N)(S+N)} \\ \text{S^{2}+SN+NS+N^{2}}\right.

We have three noise terms, two of which affect our signal. I think this is the cause of the laggish behavior when the market is cyclic because the high frequency noise components are amplified.

Now there are two ways I can think of improving the performance of this filter

  1. Find a means of modifying the value of alpha when the filter detects that the market is no longer trending. This will require an additional algorithm to estimate whether the market is in trend mode or cycle mode - something which the Hilbert Transform can take care of.
  2. Find an alternative to multiplying the price by itself so that additional noise terms are not introduced.

I am biased towards 1 so shall spend more time on that. Because I consider this work in progress, I am tempted to discuss the actual workings of the filtering algorithm at a future date when we have some acceptable performance.

Oscillator design update

Filter Design, Statistics 2 Comments »

I’ve been struggling to derive a suitable zero-lag high-pass filter.  It is not as easy as deriving a zero-lag low-pass indicator.  The design approach is the reverse of what we did in the previous case, and there comes a stage where you need to reduce the order of the filter whilst maintaining lag at high frequencies.  The algebra is overly complicated, and I’m not sure if I’m doing the right thing.  This is the best I could manage, but if far from perfect.

HPlagTryOne.png

Two things I could do here.  Either give-up on this task or consult someone specialised in filter design.  Maybe I shall leave it for now and focus on other things.

Revisiting the brownian forecasting task

Filter Design, Neural Networks No Comments »

I re-simulate the MLP neural network to forecast the brownian path, but this time I smooth my inputs using the new zero lag filter I designed.  Have a look at the results: 

 compareMAZeroLag.PNG 

Lesson Learnt:

  • If there is no lag in your inputs, there won’t be lag in your outputs.  Point noted.

Time for a zero lag oscillator

Filter Design No Comments »

I have shown so far how do develop a low-pass filter in order to eliminate lag in smoothed time-series.  It works very well in trending markets, but has slight lag in cyclic markets.  I now wish to create a filter that has zero lag in cyclic markets.  This will take the form of a high-pass filter because it will remove slow changing trend components and only display smoothed cyclic components with near zero lag.

 The reason I’m designing these filters is so that I can smooth my neural network input data whilst avioding lag which comes as a consequence of smoothing.  This would improve the learning capabilities of my neural networks, and forecasts would eventually become more accurate when I decide to use Genetic Algorithms to optimise a population of network structures.

The Zero-lag filter in action

Filter Design 3 Comments »

Have a look at these pictures showing the zero lag filter at work. 

 trendPerform1.png

trendPerform2.png

Smoothing nerual network input data with such a filter would result in your forecasted outputs appearing earlier.

Designing a zero lag indicator

Filter Design 7 Comments »

The problem with indicators and oscillators is that they suffer lag. For instance, a 19-bar moving average has a 9-bar lag i.e almost half the filter length. Exponential moving averages also suffer lag which varies over the sampling frequency range. The are many reasons why one should opt for a minimum lag indicator when filtering input data to a neural network. Generally speaking, you would not want your neural network to learn the lag present in the input data, or atleast you want to minimise this as much as possible. Here I describe a way of deriving an almost zero lag indicator in the Z-domain, as outlined in [Ehlers]

The time domain difference equation for an EMA is

equation1.png

where alpha is a number between 0 and 1. Taking the z-transform of both sides and re-arranging we get

equation2.png

I want to create a high-pass filter using the z-domain expression for the low-pass EMA filter. Hence I subtract the expression from 1. The reasoning behind this is that a filter with transfer response 1 represents all frequencies. Hence the residual of subtracting a low-pass filter from 1 should be a high-pass filter. But care must be taken. The transfer response of the EMA does not cover all frequencies right through to infinity because it is 0 at the nyquist frequency (i.e. when z = -1). Doing a direct subtraction would lead to a gain error in the frequency response. To eleminate this problem we take two sequential inputs and average them together, rather than using one input sample at a time. Thus the transfer response as a result of averaging becomes

equation3.png

We can now subtract this equation from 1 without fear of getting a gain error. Thus we have

equation5.png

equation6.png

equation7.png

Inorder to improve attenuation of the derived high-pass filter, we increase its order. But increasing the order of a filter is equivalent to introducing lag. However, the second-order Gaussain filter suffers the least amount of lag amongst all second order filters. Squaring the above equation would yield a second-order Gaussain filter. Thus we have

equation8.png

If we apply this filter to price data, we would essentially see cyclic components at the output. We are interested in trending components, or the low-pass components because we are designing a low-pass indicator filter. Hence we subtract the above equation from 1 again. After much simplification we get

equation9.png

By inspection we see that the number of delay operators in the numerator and denominator are equal. This implies the filter has zero lag. Hence we have derived an indicator with zero lag. The simulations below show different characteristics of the filter. I have also shown an equivalent MA filter for comparison purposes, since the lag relationship between an EMA and a MA is governed by the following equation

equation4.png

IRcomp3.png

The impulse respose of our new filter is finite, unlike an its equivalent EMA filter. This shows our new filter demonstrates short memory over finite duration, which might be a desired feature if too many past values are not required to be remembered.

MAGcomp3.png

The frequency response of our new filter is comparable to that of its EMA counterpart. Notice that the attenuation at nyquist frequency is an asymptote. That is the benefit of taking two sequential input samples in deriving the new filter.

And now the most important graph - showing the filter lags:

LAGcomp3.png

We have successfully designed a filter with virtually zero lag.

Filter design summary

  1. Get z-domain EMA filter equation for general alpha.
  2. Convert z-domain EMA equation to take two sequential inputs at a time so as to eliminate gain error.
  3. Subtract expression from 1 to get high-pass filter.
  4. Convert expression to 2nd order Gaussain inorder to improve filter attenuation.
  5. Finally subtract expression from 1 to get your zero lag low-pass filter.

In my next post I shall compare the performace of our new filter using real price time-series.

The problem with moving averages

Filter Design 2 Comments »

In the brownian path forcasting example I highlighted the danger of using a moving average vector as an input to a neural network.  The values are abviously time lagged, and patterns appear in delay.  The neural network inevitaby learns this lag and lag is a bad thing!  I wish to develop a filter that has almost zero lag, whilst providing the same smoothing capabilities of a SMA or an EMA regardless of the filter length chosen.  In the next couple of posts I shall describe this using Z-transforms.

Close
E-mail It