SnowCron.com








Learn Touch Typing

Cortex Artificial Neural Networks




What is "Cortex"?

Cortex neural networks software is an integrated package, built around the scripting engine. It means, that you can write scripts to automate tasks.

Cortex is a general purpose neural network program, it can be used for different tasks. However, as most visitors are interested in neural network forex trading, almost every neural network example that comes with Cortex has something to do with using neural networks for trading.

To some extent, Neural network trading is what Cortex was created for. It includes:




This tutorial illustrates the use of UI (non-scripting) features of Cortex. For the complete information on scripting language, see scripting tutorial and scripting reference guide.
Without scripting, you will only have access to about 10% of Cortex functionality.




Using Neural Networks (Features)

Let's outline the steps that we need to take, to use the Neural Network as the data analysing tool (I assume that you are already familiar with an introduction to neural networks theory). The Cortex will help you to do these steps faster and with less frustration, something you may want to keep in mind while reading.

  1. First of all, we need data, anything from stock quotes to the sound patterns for a speech recognition software. The only criteria is - the data must be sequential (a table with numbers in it's cells is a good example).
  2. These data need to be fed to the artificial neural networks application one row in a time. Let's say, we want to do stock trading. One row of data is simply not enough! You need a HISTORY, not just a current OHLC information. Can you predict the tomorrow's stock price based on today's price? Not unless you have the "historical price" information for at least couple of prior days.
    Therefore, to use a "one row in a time" approach, we need to make sure that this row contains all the historical data we need, for example, it can contain the today's data in the column one, the yesterday's data in a column two, and so on. This kind of file is called (at least in this tutorial) a lag-file, it can be automatically generated by the Cortex neural network program. As a matter of fact, it can be either generated by the Cortex, OR by its built-in scripting language, if you need to have more flexibility (for example, having Close, you may want to calculate moving average of Close, as well as couple of indicators that are used in stock trading, and only then create lags of this advanced input.
  3. We need to choose a Neural Network configuration - number of neurons, activation type and so on. The Cortex neural network software presents a simple visual interface, that allows to do just that. (Again, you can use scripting language to do it in automated way. When you only need one neural network, it does not make a lot of sense, but sometimes, you need to try many different combinations of input data, with different networks - automating this task is a real time saver).
  4. What we do next is training neural network. To do it, we run it against part of the data in the "backpropagation" mode, using another part of the data to test the performance of the net. The first part of the data will be (in this tutorial) called a learning data set, the second part is called a "testing" data set. As we doing it, a neural networks optimization occures.
    As the "Cortex" is fine-tuning the Neural Network, it is displaying both values and charts for learning and testing errors.
  5. After the neural networks optimization (training) is completed, we can use it on the "real" data. For example, we may teach it on the stock qoutes for the last year, and then we expect it to predict the tomorrow's price, based on the price for today, and couple of days of history.

    To do it we need to generate a lag file, and to run the data rows we want to analyze through the Neural Network. The resulting file will have the following columns:

    Columns for the input. What we present to the Neural Network.
    Columns for the output - what we were trying to predict.
    And the prediction: the column(s), generated by the Neural Net.

    Here is an example:

    Inputs Output Predicted
    No Close-1 Close-2 Close-3 Close-4 Close NN: Close
    0 5.150000 5.180000 5.120000 5.100000 5.200000 5.070705
    1 5.180000 5.120000 5.100000 4.950000 5.150000 5.094328
    ...

    As was already mentioned, the Cortex Neural Network Application does just that - and much more (see Cortex Built-in Scripting Language).

  6. Finally, after the Neural Network is created, we need to somehow call it from the trading software of our choice: TradeStation, MetaStocks, MetaTrader... In the article on Neural Network FOREX Trading you will find a working neural network trading system that is created, step by step, and then moved from Cortex to the trading platform, capable to place real trades with the real brocker.

Setting Things Up

A very brief intro

The following chapters will walk you through a simple example of using the Cortex Neural Networks Software - we are going to build the stock price predicting net.

  • Run the Cortex.exe: From the main menu select File - New NN File.

    The program comes with three sample data file groups - genz.*, msft.* and eurusd.*. First two contain data for Genzyme and Microsoft stocks, the third one contains FOREX (Foreign exchange) quotes for conversion between US dollar and EURO.

    From the neural network forecasting point of view, there is no difference between stock market and FOREX (except, stocks sometimes split), whatever you learn for one area, usually applies to the other.

  • Click the "..." button to open the data/stocks/genz.txt file.

    Note that you can specify what are the start and end line patterns, and how many lines should we skip before and after the start line pattern. Use it if the data are in the middle of the text file (for example, the file has a header and a footer).
    For this example, leave the first field blank.

    If you look inside the GENZ.TXT file, that you have received with the Cortex archive, you will notice that the last line has nothing to do with the stocks - it is some kind of a commercial. This is exactly the way it looked when I got this file from the Yahoo quote server. To deal with the problem (we do not want to feed THESE data to the program!), copy this last line, or at least its beginning, and paste it to the "End line" field of the Input dialog. Now the program will stop extracting the data, when it finds the string you provided.

    Important note: the stock quote file that I have downloaded from the Internet contains newest data FIRST. The neural network code (as many other data analyzing packages) expects the newest data LAST. So we need to click the "Reverse" checkbox to make sure data arrays are reversed before we do the processing.

    Also note that no matter if the "Reverse" is checked or not, the resulting file that the program will produce, will have records in the same order as the original file had. This way if you have some other program that can work with stock price file having new data at the beginning, it will also be able to work with whatever output we produce with Cortex. Also it is much less confusing, especially when you work with the sequence of files.

    What does that mean? Lets say you have a GENZ.TXT file. You checked the "Reverse" box, and then (see below for details) you clicked "Generate LAG file". The resulting lag file will still be reversed. But if you select Date and Close, and click Chart, the program will load the data, and then reverse them.

    To put it differently, "reverse" checkbox only works for the current tab in a dialog box. To have it at the next tab, check it there.

    Still, if you want, you can use the scripting language instead, it will give you the full control over the data.

  • Click the "Select fields...".

    The first (after we found the start pattern and skipped extra lines) line will be broken into the column names and presented in the list box for inputs and outputs.

  • Select "Adj. Close*" both for the input and output.

    We are going to use past values of the stock closing price, to predict future values.

    By the way, "adj" means "adjusted, the file I have is adjusted for stock splits. Some data sources do not have this nice feature - what shall we do then?

    One of the possible solutions is to use the Built-in scripting language to pre-process the file.

    As we are trying to create a network to PREDICT the future values for the line (represented by data in genz.txt), we need to provide the input in the form of HISTORICAL PATTERNS - not only today's data, but yesterdays, and so on.

    The reason we need it is simple. Can we predict the future value of the stock price by the current value ONLY? No. We need to know what the price was yesterday, and the day before yesterday - we need to know what is going on with this price.

  • The "Adj. Close*" is now selected in the list boxes.

    We are about to generate what is called a lag file. The idea of the lag file is to represent today's data in the same table, side by side with the data for yesterday and so on. Press the "lag file" button. You will have the file with .lgg extention containing something like:

    No Close Close-1 Close-2 ...
    10 5.200000 5.150000 5.180000 ...
    11 5.150000 5.180000 5.120000 ...
    12 5.180000 5.120000 5.100000 ...

    As you can see, the first value in Close-1 column was removed, and the entire column moved up. For the Close-2, TWO first values were removed and so on. Therefore, each line of this new file contains data for the current day AND data for nine previous days.

    Let's select the new inputs.

  • Click on the "Select fields" again...

    Select "Adj. Close*-1", "Adj. Close*-2"... "Adj. Close*-9" as inputs and "Adj. Close*" as output. This way we will be using nine PREVIOUS days to predict the coming price.

  • Click on the "Network" tab.

    As you can see, you can specify the number of layers, number of neurons in hidden layers (see Introduction to Neural Networks for details on what the elements of a Neural Net are), one of two activation functions (standard for almost any NN package), and stop criteria, if you want the learning process to stop automatically.

  • Select 7 neurons in the hidden layer.

    This is just a random selection that we use for our example.

  • Click on the "Processing" tab.

    Here you can specify one of two ways of breaking the data to the "learning" and "testing" parts (see Introduction to Neural Networks for details). You can use first N records (patterns) as a "learning" material and the rest - as a "testing" data. Or you can randomly select N % of the data.

    The random selection does not work with the prediction - it is called cheating ;) But it works well when you are trying to do a line fitting.

  • Choose the "First N records" option.

    How do we know how many records we have in our .lgg file? We can open the file in a text editor and find out. Or we can click the button on the right from the data entry field. I got 1228 records, and I have decided to use 1000 of them for "learning", and the rest - for the "testing".

    The "adjust range" combo box is important if there is a chance for our "test" data to get out from the range where the "learning" data are. To compensate, we can extend the range.

  • Let's leave it at 1.0

    It is safe in our case, as we know that the last 228 records are not out of range of the first 1000. When you deal with the real life computations, it is always a good idea to estimate if, for example, your stock price might hit the historical maximum or minimum, and to extend the range to handle the case when it does.

  • Click the "Learning" tab

    Select all check boxes and press Run. The Neural Network will begin the learning process. The number of epochs (how many times the entire data set was presented to the network), and the best (smallest) learning and testing errors will be displayed.

    As the learning continues, the error (representing the difference between the actual output of the network and the desired output) is decreasing. When we decide that it is small enough (and we can always go back and continue the training) we stop.

  • Click "Stop" and go to the "Apply" tab.

    The "Apply" tab is an exact copy of a "Input" tab except for the "Chart" and "Apply" buttons. The functionality is different, however.

  • Click the "..." and open the .LGG file.

    Select same fields you used as the inputs and outputs.

    When you press the "Apply" button, the file with the .apl extention is generated. It contains all data that the input contained, plus extra fields for the output, generated by the Neural Network.

  • Finally, switch to the "Output" tab

    Click the "..." and open the .APL file.

  • Select columns to use on chart

    Select the No (record number) as the input and Close and NN:Close as outputs - we are going to plot the Close and Predicted Close together on the same chart, to be able to compare them visually.

  • Click the "Chart" button

    It will produce a chart, presenting the "desired output" (Close) vs. the output, produced by the Neural Net.

    The following image is created by the undertrained Neural Net. The approximation is not very poor (for a one-day prediction). If you continue training, you will get better results.


Databases

Cortex uses SQLite database engine, which means you can create, browse and edit tables, both using scripting language and with program's UI only. It is possible to run SQL statements (again, from menu or from scripting language), which makes storing your data easy.

For details, check the Databases tutorial on our site.

Scripting

Cortex comes with a powerful scripting language called SLANG. Using SLANG you can achieve much more then with the menus only, you can do an elaborated data preprocessing, you even may consider writing your own SLANG plugins.

For details, check the Scripting tutorial on our site, for a cmplete list of SLANG functions, read the SLANG Reference Guide.

Programmer's API

If you are a programmer, you might want to access Cortex directly, from your software (keep in mind, that you still can create a plugin that exports data, and it is much easier).

The Cortex program comes with the SP_NN.DLL that is the core of Cortex artificial neural networks data processor. However this DLL expors classes instead of functions, so to work with it you need to be a C++ programmer, and even then it is quite inconvenient.

The Cortex API DLL solves this problem, providing a wrapper that you can use the way most programmers do, through calls to functions exported by this DLL.

For details, check the Programmer's API tutorial.

Other features: images and so on. Sample scripts

There are many other features in Cortex application. It can load pages from the Internet, it can parse these pages using regular expression syntax, extracting data according to your requirements.

It has a built-in image processor, so you can process all images in a selected folder, making them sharper, for example.

It can access additional functions in third-party DLLs, so you can write extensions to the built-in scripting language.

And more. As new features are introduced, new samples are added to the ZIP archive, so that you can see it in action.







(C) snowcron.com, all rights reserved

Please read the disclaimer