java neural network
Forum  |  Blog  |  Wiki  
Get Java Neural Network Framework Neuroph at Fast, secure and Free Open Source software downloads


An example for time-series prediction

by Dr. Valentin Steinhauer

Short description

Time series prediction plays a big role in economics. The prices courses, as well as the consumption for example of chicken, can be predicted to be able to make decisions. This tutorial shows one possible approach how neural networks can be used for this kind of prediction. It extends the Neuroph tutorial called "Time Series Prediction", that gives a good theoretical base for prediction. To show how it works, we trained the network with the chicken prices data – for the two years (from 01.2008 to 10.2009) - to predict of increase or decrease of the chicken price at 11.2009. As a strategy we take the sequences from 4 months to predict each 5th month. In the training set 5th month is the supervised value. The data can be downloaded from the following url (one of the possibilities):

US cents per pound of chicken - 2008,2009:
double[] data = {77.77D, 76.85D, 77.25D, 79.15D, 81.23D, 82.04D, 83.46D, 85.71D, 88.25D, 88.42D, 88.40D, 87.54D, //2008
87.02D, 87.25D, 86.7D, 85.73D, 85.38D, 86.96D, 88.17D, 88.56D, 86.77D, 82.85D, 82.13D};//2009

As next step is the preparation of the training data in area (0-1). For this goal is needed to find of the max/min value of prices:  datamax, datamin.  These values will be multiplied with 1.2 and 0.8 accordently, to avoid small (0.0...) and big values (0.99...).

Further the topology of network is defined: what type of network, how many layers and how many neurons per layer are used. Actually, there is no rule for this, and usually it is determined experimentaly. However the common type of network used for prediction is a multi layer perceptron. A recommendation is to have 2n+1 nodes for hidden-layer, where n is the number of the input nodes. The output layer has only one node in this case.

TrainingSet<SupervisedTrainingElement> trainingSet = new TrainingSet<SupervisedTrainingElement>(4, 1);
for (int i = 0; i < data.length - 5; i++) {

trainingSet.addElement(new SupervisedTrainingElement(new double[]{(data[i] - datamin) / datamax, (data[i + 1] - datamin) / datamax, (data[i + 2] - datamin) / datamax, (data[i + 3] - datamin) / datamax}, new double[]{(data[i + 4] - datamin) / datamax}));


77.77 76.85 77.25 79.15->81.23
76.85 77.25 79.15 81.23->82.04
77.25 79.15 81.23 82.04->83.46
79.15 81.23 82.04 83.46->85.71
81.23 82.04 83.46 85.71->88.25
82.04 83.46 85.71 88.25->88.42
83.46 85.71 88.25 88.42->88.4
85.71 88.25 88.42 88.4->87.54
88.25 88.42 88.4 87.54->87.02
88.42 88.4 87.54 87.02->87.25
88.4 87.54 87.02 87.25->86.7
87.54 87.02 87.25 86.7->85.73
87.02 87.25 86.7 85.73->85.38
87.25 86.7 85.73 85.38->86.96
86.7 85.73 85.38 86.96->88.17
85.73 85.38 86.96 88.17->88.56
85.38 86.96 88.17 88.56->86.77
86.96 88.17 88.56 86.77->82.85

At this point, we are ready to train and test the net. As a test it is prepared data set in which the price data are given from the 07-08-09-10 months 2009 to predict the value at 11.2009.

TrainingSet<TrainingElement> testSet = new TrainingSet<TrainingElement>();
testSet.addElement(new TrainingElement(new double[]{(88.17D - datamin) / datamax, (88.56D - datamin) / datamax, (86.77D - datamin) / datamax, (82.85D - datamin) / datamax}));
for (TrainingElement testElement : testSet.trainingElements())
  double[] networkOutput = neuralNet.getOutput();

Because the start state of the network with random values of weights is generated, the test results differ from a calculation to calculation. After five tests it came out with the following prediction - results for 11.2009: 83,91; 83.75; 83.98; 83,75; 84.12. The value which was official announced on that day is 82.13. The middle value the last 4 month is 86.59. You can be take your decision, for example, that you can buy the chichen next month.

Using Clojure

Here is the demo implementation of Chicken sample using Neuroph in Clojure created by Bojan Jovicic
Basically the idea was to follow the original idea of the sample by Dr. V. Steinhauer as closelly as possible while leveraging some advantages of Clojure.

First point of interest is ease of getting maximum and minimum using Clojure:

(defn get-data-max [data] (* 1.2 (reduce max data)))
(defn get-data-min [data] (* 0.8 (reduce min data)))

Full implementation is available for download here


Note: These download packages are for versions of Neuroph older than 2.6, however differencies are minor

1. Neuroph framework with easyNeurons application
2. NetBeans project for Chicken Price Prediction example
3. Clojure implementation of Chicken sample

See also:
Time Series Prediction Tutorial
Stock Market Prediction Tutorial
Multi Layer Perceptron Tutorial

      Java Get Powered      Java Get Powered                           Get Java Neural Network Framework Neuroph at Fast, secure and Free Open Source software downloads