Approach to the project
This paper describes the results of an exercise in the application of an ANN to the short term forecasting of All Share Price Index (ASPI) by first training the network on published historical data and feeding the ANN with current data set (todays data) and predicting the next days (tomorrows) ASPI . For the exercise historical data were collected from 2 nd April 2001 to 23 rd May. Prediction of ANN is based on All Share Price Index, Milanka Price Index, 91 days Treasury Bills Yield, Commercial Bank Prime Lending Weekly Average and Call Money Weekly Average. The ANN could be easily converted to predict any of the above variables after tuning to a particular variable.
Algorithm
Forward propagation
The ANN developed in the exercise consists of input layer with 5 nodes having sigmoidal transfer function with y= 1/(1 + exp ^ -ls) where
y= transfer function
exp = exponential function
^ = raised to the power
l = parameter used in ANN fine tuning
s= weighted sum of the layer
and a hidden layer with 8 nodes with similar transfer function and an output layer with simple linear transfer function in the form of y=mx shown in the code.
Back propagation
ANN uses supervised training method and compares the ANN forward propagation output with next days ASPI using the data set provided in the table 1. The difference (error) is used in adjusting the weights of the output layer with delta rule in the back propagation of the training phase. Back propagation of the next layer raises problems since there are no available set of target values ( desired outputs) for hidden units. Following method was adopted for backpropagation of the hidden layer. The method adopted in Back propagation can be described as follows.
Error Backpropagation Leaning Rule
( Gradient Descent Rule / Delta Rule)
The slope of a function at any point is the gradient of the tangent to the curve at the point.
If x is small, then y is a almost the same as the change y in the function y, when the change x is made in x
That is <0 and we travelled down the curve towards the minimal point. If we keep repeating steps like * iteratively , then we should approach the value if x associated with the function minimum. This technique is called the Gradient Descent .
Gradient Descent on an error
The idea is to calculate an error each time the net is presented with a training vector ( given that we have supervised learning where there is a target ) and to perform a gradient descent on the error considered as function of weights. There will be gradient or slope for each weight. Thus we find the weights which give the minimal error. The situation is as follows.
Formally for each pattern p, we assign an error E which is a function of the weights ; that is E ( w ,w , w w ) Typically this is defined by the square difference between the output and the target. Thus ( for a single node )
Where we regard y as a function of the weights. The total error E is then just the sum of the pattern errors.
Using the above in our ANN for updating weights we can use the delta rule since the target for the output is defined as ASPI of the next day
Transfer function y = kk * ss
. f = 2
The learning rule for the hidden layer has a problem since we do not have a staight forward target values for each hidden units. This amounts to propagating the output errors back through the output layer toward the hidden units in an attempt to estimate dynamic targets for these units. Such a learning rule is termed error back propagation or backprop learning rule and may be viewed as an aextension of the delta rule, used for updating the output layer. To complete the derivation of backprop for the hidden layer weights, and similar to the proceeding derivation for the output layer weights, gradient descent is performed
When the partial derivative is to be estimated at the current weight values, using the chain rule for differentiation one may express the partial derivative in equation above as
By substituting and
The desired learning rule is obtained
All weights are adjusted accordingly.
This forward and back iterations will process until the difference of ANN output and next days ASPI is less than 1 which is a sufficient approximation for the forecast.
Code used for training
Dim x(5) As Double
' c and cc are counters used
Dim c As Integer
Dim cc As Integer
Dim w(5, 8) As Double
Dim s(8) As Double
Dim y(8) As Double
Dim ww(8) As Double
Dim ss As Double
Dim yy As Double
Dim t As Double
Dim k As Long
Dim kk As Long
Dim n As Integer
Dim sww As Double
Dim train As Integer
Dim count As Integer
Dim l As Integer
Dim sum As Double
t = 0
k = 1
kk = 1
sww = 0
' initialize weights
For c = 1 To 5
For cc = 1 To 8
w(c, cc) = 0.0000000001
Next cc
Next c
' initialize weights of the second layer
For cc = 1 To 8
ww(cc) = 0.0000000001
Next cc
'train until error equals to a preset
Do
'For train = 1 To 5 ' iteratios using whole set
Text1.Text = "Training started"
'use training sets until record before the last record
Data1.Recordset.MoveFirst 'record pointer to first record
For n = 1 To (Data1.Recordset.RecordCount - 2)
'MsgBox (Data1.Recordset.RecordCount)
'For count = 1 To 10 ' iterations using 1 data set
' go to the first record of the database
'read first record from data source '
For c = 1 To 5
x(c) = Data1.Recordset.Fields(1 + c)
Next c
'Data1.Recordset.MoveNext 'record pointer to next record
'forward propergation first hidden layer
For c = 1 To 8 ' 8 nodes HIDDEN layer
s(c) = w(1, c) * x(1) + w(2, c) * x(2) + w(3, c) * x(3) + w(4, c) * x(4) + w(5, c) * x(5)
y(c) = 1 / (1 + Exp(-s(c))) ' transfer function
'MsgBox y(c) ' first forward propergation will not give any valy to y ( w=0 )
Next c
ss = ww(1) * y(1) + ww(2) * y(2) + ww(3) * y(3) + ww(4) * y(4) + ww(5) * y(5) + ww(6) * y(6) + ww(7) * y(7) + ww(8) * y(8)
yy = 2 * ss 'linear transfer function output layer
'forward propergation finished
' compare the error
'consider the target as the next ASPI record
' read record
Data1.Recordset.MoveNext
t = Data1.Recordset.Fields(2)
' backpropergation HIDDEN layer
For c = 1 To 8
ww(c) = ww(c) + 0.000001 * (t - yy) ^ 2 * kk * ss * y(c)
Next c
For cc = 1 To 8
For c = 1 To 5
For l = 1 To 8
sum = 0.000001 * (t - yy) ^ 2 * ss * ww(cc)
Next l
w(c, cc) = w(c, cc) + sum * s(c) * x(c) * (1 / (1 + Exp(-s(c)))) * (1 - (1 / (1 + Exp(-s(c)))))
Next c
Next cc
' Next count
Next n
'Next train
MsgBox (t - yy)
Loop Until (t - yy) < 1
' write the weights in the table 2
Data2.Recordset.MoveFirst
For cc = 1 To 8
Data2.Recordset.Edit
Data2.Recordset.Fields(2) = ww(cc)
Data2.Recordset.Update
Data2.Recordset.MoveNext
Next cc
For cc = 1 To 8
For c = 1 To 5
Data2.Recordset.Edit
Data2.Recordset.Fields(2) = w(c, cc)
Data2.Recordset.Update
Data2.Recordset.MoveNext
Next c
Next cc
Text1.Text = yy
Code used for Prediction
Dim x(5) As Double
Dim c As Integer
Dim cc As Integer
Dim w(5, 8) As Double
Dim s(8) As Double
Dim y(8) As Double
Dim ww(8) As Double
Dim ss As Double
Dim yy As Double
Dim k As Long
Dim kk As Long
Dim count As Integer
Dim l As Integer
Dim sum As Double
k = 1
kk = 1
'Data1.Recordset.MoveFirst 'record pointer to first record
'read data from text boxes
x(1) = Text1.Text
x(2) = Text2.Text
x(3) = Text3.Text
x(4) = Text4.Text
x(5) = Text5.Text
For c = 1 To 8
ww(c) = Form1.Data2.Recordset.Fields(2)
Form1.Data2.Recordset.MoveNext
Next c
'forward propergation first hidden layer
For c = 1 To 5
For cc = 1 To 8
w(c, cc) = Form1.Data2.Recordset.Fields(2)
Form1.Data2.Recordset.MoveNext
Next cc
Next c
For c = 1 To 8 ' 8 nodes HIDDEN layer
s(c) = w(1, c) * x(1) + w(2, c) * x(2) + w(3, c) * x(3) + w(4, c) * x(4) + w(5, c) * x(5)
y(c) = 1 / (1 + Exp(-s(c))) ' transfer function
'MsgBox y(c) ' first forward propergation will not give any valy to y ( w=0 )
Next c
ss = ww(1) * y(1) + ww(2) * y(2) + ww(3) * y(3) + ww(4) * y(4) + ww(5) * y(5) + ww(6) * y(6) + ww(7) * y(7) + ww(8) * y(8)
yy = 2 * ss 'linear transfer function output layer
Text6.Text = yy
disclaimer - Author takes no responsility of non-working of the programs