Quantcast aiQUANT » 2007 » September

Archive for September, 2007

Developing high quality trading systems

Trading Systems No Comments »

Constructing financial models is a multi-stage process consisting of the determination of goals for the project followed by data collection, data preprocessing, model construction, data postprocessing, model validation and finally model implementation.  The success of any trading model/system depends on the rigor of all the steps in its development process, and not just the sophostication of the predictive algorithm embedded in it.  You may have a really good algorithm, but if for instance the input data supplied is not of high quality then predictions might end-up on a similar footing.  I now wish to develop a FTSE100 index financial model using MLPs so as to backtest its performance over historical data and compare its profiteering capabilities with other trading strategies.

Notes from Matlab “World Tour” Seminar 2007

Matlab No Comments »

I attended this seminar in Oxford in May 2007; the notes relate to Matlab 7.1 which was current at that point.

Did you know…?

  • You can automatically generate reports from matlab, containing code, figures etc, in HTML, Word,
  • PowerPoint or XML form.  They are formatted using cell mode; see “Publishing Results” under “Desktop Tools and Development Environment” in Matlab Help.
  • There are compiler tools to generate Excel plugins as well as .dlls or .exes
  • There is a Distributed Computing Toolbox which provides support for distributing processing across clusters of PCs (allows larger, distributed data arrays, as well as parallel processing)
  • Matlab should be able to make use of multi-core processors on a single machine, to support parallel operations
  • The Matlab import wizard can import from a range of file formats e.g. Excel, jpeg etc.  There is a tick-box to create an m-file which can be used to automate the import process.
  • There are in-built functions to support reading of XML files, e.g. xmlread, xmlwrite
  • You can do things to data by right-clicking on it in the data window
  • When plotting, you don’t need to remember all the commands to pretty-up the result.  Just click the “show plot tools” icon on the figure and make the changes you want graphically.  Then you can save the commands which have the same effect, using “File -> Generate M-File”.  You can also do more sophisticated things like this such as adding more data to your plot, or creating sub-plots.
  • Cell mode allows you to divide up files into sections which may be independently tested; also used for publishing (can apply formatting to comments in cells)
  • You can add your favourite functions to the GUI shortcut bar
  • 3D plots can be rotated around for viewing at different angles
  • The statistics toolbox has functions for viewing and manipulating multi-dimensional data, including an optimisation tool to iterate to find an optimal output given multiple input variables
  • There is a try - catch statement sequence that can be used to trap errors
  • You can call functions in C .dlls, using functions such as calllib, loadlibrary, libfunctions etc.  Note the use of functions such as libpointer and libstruct, to create C-style pointers and structures.  Can use this to avoid the pain of mex wrapping?  See “Matlab Interface to Generic DLLs” under “External Interfaces” in the help.
  • If you right-click on a breakpoint, you can set up conditions for breaking, which can include any valid matlab code; e.g. you can run a whole function to evaluate whether or not to break…
  • There is an m-lint tool which analyses your code for problems in a similar way to lint in C.  Run it on all files in a directory, from the drop-down box in the directory window.
  • Running your code from the Profiler tool gives you all kinds of profile info including code coverage, “m-lint” warnings, time spent in each function etc.
  • The depfun function will report all the dependencies of a .m file (the Profiler can also be used for this purpose).  The Dependency Report tool (run from the drop-down box in the directory window) can do this for all files in a directory
  • If you put TODO, FIXME or NOTE in comments, then these can be picked up and listed for all files in a directory, using the TODO/FIXME report tool, run from the drop-down box in the directory window.
  • Using the Contents Report tool, you can automatically create a Contents.m file for a directory, listing descriptions for each file in the directory.  The contents can then be viewed using help <dirname>
  • The Help Report tool will display any help that you have written for all files in a directory.  Note that if you use the strings Example, See also, or Copyright, these are picked out separately by the Help Report tool.
  • If you need to create a large array and have a problem with available memory, try the undocumented command system_dependent memstats.  This shows the available blocks of contiguous memory.  Arrays must be held in contiguous memory, so you may need to re-root to get the area you need.
  • You can set up a timer which “wakes up” and runs a function every so often.  This can allow you to implement a simple form of multi-tasking between different matlab functions, by scheduling functions to run from the timer (see notes on function handles, below)

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.

Neural network construction using Grammatical Evolution

Grammatical Evolution, Neural Networks No Comments »

I bumped into this paper a short while back:

Neural network construction using grammatical evolution
Tsoulos, I.G.; Gavrilis, D.; Glavas, E.;
Signal Processing and Information Technology, 2005. Proceedings of the Fifth IEEE International Symposium on
18-21 Dec. 2005 Page(s):827 - 831

NNsVisGramE.PNG

 The authors have highlighted ways of not only Infering the architecture of a neural network, but also estimating the optimum set of nodes.  I like this diea because I think it provides more flexibility and computational efficiency compared to other Genetic Programming approaches.  Maybe my next project should be a self-evolving trading system based on the ideas presented in the paper.  But I’m very tempted to look at option pricing first.

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.

Neurons and How They Work

Eye Candy No Comments »

Really liked this video.  Check it out…

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