You are on page 1of 7

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/292970981

MATLAB Code of Artificial Neural Networks Estimation

Article · February 2016

CITATIONS READS

0 12,644

1 author:

Aymen Ammari
Ecole de Management Strasbourg
17 PUBLICATIONS   10 CITATIONS   

SEE PROFILE

All content following this page was uploaded by Aymen Ammari on 04 February 2016.

The user has requested enhancement of the downloaded file.


Available online at https://www.researchgate.net/profile/A_Ammari

MATLAB Code of Artificial Neural Networks Estimation

Aymen AMMARI *
High Business School of Tunis, University of Manouba, Tunisia

ARTICLE INFO ABSTRACT

Article history: Neural networks are very appropriate at function fit problems. A neural
Received 00 December 00 network with enough features (called neurons) can fit any data with
Received in revised form 00 January 00 arbitrary accuracy. They are for the most part well-matched in focusing
Accepted 00 February 00 on non-linear questions. In view of the non-linear nature of real world
events, neural networks are an effective runner for resolving the
Keywords: Artificial Neural Network
problem. This article provides a MATLAB code for numerically
simulating Artificial Neural Networks Estimation. An example of
corporate governance data (as input) linked to their Accounting and
Market performance (as output) of 80 French publicly traded firms from
2000 to 2013 was introduced just to generate results. This article is
written for the developers of MATLAB programming language. The
code was written for flexibility so as to be easily modified to many other
applications for educational purposes.

This paper is based on my Ph.D dissertation Thus results section of the


relation between French corporate governance and firm performance,
statistical analysis, the status of my findings in the context of the
literature and the existing knowledge about the subject, the limitations
and the implications of my findings for policy and practice are also
included in my Ph.D dissertation.

for depositing empirical knowledge and


1. Upbringing formulating it. Artificial neural network (ANN)
is a sort of Artificial Intelligence practice that
A neural network is a vastly analogous simulates the behavior of the human brain
distributed processor made up of simple (Haykin, 2009).
processing units that have an expected tendency ANNs have the faculty to model linear and non-
linear systems without the requirement to make

* Corresponding author. Tel.: +216 25 78 98 63


E-mail address: ammariaymen@yahoo.com

xxxx-xxxx/$ – see front matter © 2015 xxxxxxxx. All rights reserved.


2

assumptions implicitly as in most usual statistical Neural networks are considered universal
methodologies. As an alternative for regression approximators (Kolmogorov, 1957). Based on
techniques, ANNs are used. ANNs are applied in this theorem, it was proven that regardless of the
many fields such as financial services, activation function and the dimension of the
biomedical applications, time-series prediction, input space, a neural network with one hidden
text mining, decision making and many others. layer can approximate any continuous function
Although the applications of ANNs are arbitrarily well (Hornik et al., 1989). The
numerous, they all share an important common different input (the possible contributing
aspect: the processes to be predicted are variables) are weighed and combined into a final
correlated with a large number of explanatory model. The network is trained to estimate the and
variables and there may exist high-level non- this training implies that the different
linear relationships between those variables. One interconnection weights will be adapted every
of the main goals of ANNs is to detect those time a new part is fed to the network.
high-level non-linear relationships to enable a Adaptation of the weights will be done based on
better modeling of the process. ANNs are in fact a punishment/reward principle: if the
computer systems that simulate the learning interconnection did well during estimation of the
effect of the human brain and are typically previous part, this variable will be rewarded by
composed of a number of neurons, grouped in increasing the weight of its interconnection to the
one or more hidden layers connected by means of output. If the interconnection performed badly,
synapse connections. The output of each neuron the interconnection weights will be decreased in
will be a weighted function of the different the next iteration step. By minimizing the
incoming signals. The weight of an squared error (MSE) between the estimate and
interconnection between different neurons will the desired output (in our case firm
depend on the contribution performance of that performance), the network is trained and as more
neuron to the final output. parts are fed, the learning effect increases.
Fig. 1 shows the combination of different However, one cannot keep on training the
neurons (perceptron) into an artificial neural network into infinity. When new parts, not
network (multi-layer perceptron). included in the training set, are fed to the
network, inaccurate estimates can be generated.
It can indeed happen that the network has
focused too much on the specific data of the
training set (i.e. overfitting), but fails to
generalize when unknown parts are fed to the
network (i.e. generalization ability of ANNs). To
ensure that ANNs also generate accurate
estimates for parts not included in the training
set, a separate testing and validation set are used.
The network will be trained until the error of the
testing and validation set increases. At that point,
training will be stopped to avoid overfitting.
Neural networks are typically characterised by
the number of hidden layers, the number of
hidden neurons and the training algorithm. Those
design parameters determine to a large extent the
performance of the ANN and will differ
depending on the field of application. If the
number of hidden neurons is too small, not all
necessary dependencies between the variables
might be captured, resulting in a poor modelling
of the observed process. On the other hand, if the
number of hidden neurons is too large, spurious
relations and noise are included in the model. By
a trial–error process the number of hidden
neurons that best fits the specific purpose is
determined. The training algorithm will be
chosen in function of the specific application.
The biggest advantage of neural networks is the
fact that they can approximate functions very
3

well without explaining them. This means that an Data for function fitting problems are set up for a
output is generated based on different input neural network by organizing the data into two
signals and by training those networks, accurate
estimates can be generated. ANNs are considered matrices, the input matrix “in” and the target
black box approaches (Smith and Mason, 1999), matrix “out”.
(Kim et al., 2004).To overcome this problem of corrplot(data): was introduced to verify
neural networks being black boxes, research has the problems of multicollinearity which means
been conducted to try and explain their that there is a nearly perfect linear relationship
behaviour. Equivalence between ANNs and
fuzzy-rule-based systems can be found, allowing between explanatory variables, we introduced the
to interpret the neural network. Benitez et al. Cross correlation Matrix. The correlation plot Fig
(1997) offer such a fuzzy-based interpretation of 2 shows that our independent variables
neural networks to translate the knowledge (corporate governance data) are not correlated.
embedded in the ANN into more understandable
fuzzy rules. However, despite the efforts in trying
to explain the complex relationships found by the
network, ANNs are still mainly considered black
boxes. Even if one were to extract the CER by
examining the weights, architecture and nodal
transfer functions, the equation is difficult to
explain. Fitting the data to a particular parametric
form is possible with regression but not practical
with neural networks (Smith and Mason, 1999).

2. 2. MATLAB Code of Artificial Neural


Networks Estimation:
This section illustrates the MATLAB code used
and clarifies step by step the implanting of
Artificial Neural Networks Estimation of
corporate governance data linked to their
Accounting and Market performance.

%% Artificial Neural Networks for


Estimation

% Ammari Aymen, 2015;

%% --<<< Explanations are in


green >>>--
Fig 2 : Correlation Matrix

 Preparing the Data


 Data log transformation
Univariate normality is not needed for least
*************************************** squares estimates of the regression parameters to
%% Data Input and Preparation
be meaningful (see Gauss-Markov theorem).
clc; clear; close all; However confidence intervals and hypothesis
in=xlsread('input'); % Input File tests will have better statistical properties if the
out=xlsread('output'); % variables exhibit multivariate normality. This can
Output File be assessed empirically by plotting the fitted
data=[in out];
corrplot(data)
values against the residuals, and by inspecting
%% the normal quantile plot of the residuals. Note
*************************************
4

that it is not relevant whether the dependent


variable Y is marginally normally distributed. net1.divideParam.trainRatio=trainRatio1
;
The log transformation is used because
distribution of our output is lognormal in a goal net1.divideParam.valRatio=valRatio1;
to make highly skewed distributions less skewed.
net1.divideParam.testRatio=testRatio1;
***************************************
input=[1 2 3 4 ]; % Input Layer estval=sim(net1,p(:,tr.valInd));
p=data(:,input); eval=mse(estval-
output=[5 6]; % t(:,tr.valInd));
Output Layer if eval<evalopt(ii)
t=data(:,output); netopt{(ii)}=net1;
p=p'; t=t'; % tropt(ii)=tr; evalopt(ii)=eval
Transposing Matrices end
end
end
t=log(t+1);
% Defining Validation Dataset plot(nnn1:nnnj:nnnf,evalopt)
trainRatio1=.6;
valRatio1=.2; n is number of neurons in the mid-layer.
testRatio1=.2;
***************************************
nn=1 is 5 neurons
nn=2 is 10 neurons
 Network Definition nn=3 is 15 neurons
nn=4 is 20 neurons
*************************************** Fig 3 show the error plot for different n: 5, 10, 15
%% Network Definition and 20. Note that for n = 4, the squared error
(MSE) is in his best minimum value
nnn1=5; %
First Number of Neurons in Hidden Layer
nnnj=5 %
Jump in Number of Neurons in Hidden
Layer
nnnf=20; %
Last Number of Neurons in Hidden Layer

% net1.trainparam.lr=0.1;
% net1.trainParam.epochs=500;

% Training Network

it=20;
% Max Number of Iteration
ii=0;
netopt{:}=1:nnnf;
for nnn=nnn1:nnnj:nnnf Fig 3: Error plot for different n: 5, 10, 15 and 20
ii=ii+1; nnn
net1=newff(p,t,[nnn nnn]); % For  Network Output
more functions see: 'Function
Reference' in 'Neural Network Toolbox'
of Matlab help ***************************************
evalopt(ii)=100; %% Output
for i=1:it clear; close all;
[net1,tr,y,et]=train(net1,p,t); load('run_log_2');
% Training
nn=4
5

ptrain=p(:,tropt(nn).trainInd);
ttrain=t(:,tropt(nn).trainInd);
esttrain=sim(netopt{nn},ptrain);
ptest=p(:,tropt(nn).testInd);
ttest=t(:,tropt(nn).testInd);
esttest=sim(netopt{nn},ptest);
pval=p(:,tropt(nn).valInd);
tval=t(:,tropt(nn).valInd);
estval=sim(netopt{nn},pval);
estwhole=sim(netopt{nn},p);

% ttrain=exp(ttrain); ttest=exp(ttest);
tval=exp(tval); t=exp(t);
% esttrain=exp(esttrain);
esttest=exp(esttest);
estval=exp(estval);
estwhole=exp(estwhole);

figure; plot(ttrain,esttrain,'.b'); %
train data: ttrain: real and esttrain:
estimated
figure; plot(tval,estval,'.g'); %
validation
figure; plot(ttest,esttest,'.r'); % Fig 4: Plot of training, validation, and testing
test data of accounting performance.
figure; plot(t,estwhole,'.k') %
whole

figure;
plotregression(ttrain,esttrain,'Train',
tval,estval,'Validation',ttest,esttest,
'Test',t,estwhole,'Whole Data');
***************************************

Fig 4 and Fig 5 represent the plots of the training,


validation, and testing of our data. The dashed
line in each plot represents the perfect result –
outputs = targets. The solid line represents the
best fit linear regression line between outputs and
targets. The R value is an indication of the
relationship between the outputs and targets. If R
= 1, this indicates that there is an exact linear
relationship between outputs and targets. If R is
close to zero, then there is no linear relationship
between outputs and targets.
For this example, the training data indicates a
good fit. The validation and test results also show
R values that greater than 0.9. The scatter plot is Fig 5: Plot of training, validation, and testing
helpful in showing that certain data points have data of stock performance.
poor fits.
Note that if we rerun the code with higher
iterations for example it=100; it will reduce the
squared error.
6

Conclusion:
Based on a chapter of my Ph.D dissertation
which analyses the French corporate governance
linked to their firm performance, this paper
present the MATLAB code used and clarifies
step by step the implanting of Artificial Neural
Networks Estimation. The code was written for
flexibility so as to be easily modified to many
other applications for educational purposes.
Acknowledgement:
I acknowledge Verlinden, B. et al 2007, “Cost
estimation for sheet metal parts using multiple
regression and artificial neural networks: A case
study” for getting his background support.

View publication stats

You might also like