Backpropagation Neural Networkswith the User Interface. Design, Teach and Use Neural Networks. Tutorial with examples (stock market trading). |
|
|
|
Cortex ver.5.0 Tutorial.A Backpropagation Neural Networks Application.
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 the 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 the 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. The data need to be fed to the neural network one row in a time. Let's
say, we want to do a 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.
3. We need to choose a Neural Network configuration - number of neurons, activation type and so on. The "Cortex" 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. We need to teach the 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 (again - in this tutorial) called a learning data set, the second part is
called a "testing" data set.
5. After the Neural Network 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 (again) 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.
Here is an example:
As was already mentioned, the Cortex Neural Network Application does just that - and much more. 6. Finally, after the Neural Network is created, we need to somehow call it from the rtading software of our choice: TradeStation, MetaStocks, MetaTrader... In this example you will find a working 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.
Download the Cortex archive. Some programs that you can download from this site can work together. If you want (strongly recommended) this kind of functionality to be available, you should create a common folder, called (recommended) S_PROJECTS, and to unzip all software in this folder. The sub-folders will be created for you automatically. When specifying options in your Winzip software, make sure that all subdirectories (subfolders) are restored. Usually it is the default setting for the WinZip. Installation is complete. On 20-May-2003, the S.Projects changed the passwords for all the software. If you have purchased the software before this date, we strongly advice you to make a backup copy of the entire folder, prior to re-downloading.
Delete the folder containing the Cortex files.
If you choose to register the Cortex software, you will need to enter the password (provided in the e.mail that you will receive after the registration) into the registration prompt. Please keep in mind, that the Cortex comes with few independent parts, that need to be registered separately.
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 NN point of view, there is no difference between stock market and FOREX, 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 NN (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 mater 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 that means? 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. 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:
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, and 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. For this particular task let's select 7 neurons in the hidden layer. 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. So for this example we will 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 as in our case the last 228 records are not out of range of the first 1000. 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 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 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. Use the "Chart" button, to plot the desired output (Close) vs. the output, produced by the Neural Net. The following image is produced 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.
To get fully enabled version of the Cortex, you need to register. And do not forget about the navigation bar at the bottom of the page - it will take you to other pages, some of them using neural networks, and all of them facinating ;) |
|
| Links | Home |
|
|
|
|