Вы находитесь на странице: 1из 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

READS

668

1 author:
Aymen Ammari
Universit de Jendouba
11 PUBLICATIONS 2 CITATIONS
SEE PROFILE

Available from: Aymen Ammari


Retrieved on: 04 August 2016

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


network with enough features (called neurons) can fit any data with
arbitrary accuracy. They are for the most part well-matched in focusing
on non-linear questions. In view of the non-linear nature of real world
events, neural networks are an effective runner for resolving the
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.

Received 00 December 00
Received in revised form 00 January 00
Accepted 00 February 00
Keywords: Artificial Neural Network

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.

1. Upbringing
A neural network is a vastly analogous
distributed processor made up of simple
processing units that have an expected tendency

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


E-mail address: ammariaymen@yahoo.com

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

for depositing empirical knowledge and


formulating it. Artificial neural network (ANN)
is a sort of Artificial Intelligence practice that
simulates the behavior of the human brain
(Haykin, 2009).
ANNs have the faculty to model linear and nonlinear systems without the requirement to make

assumptions implicitly as in most usual statistical


methodologies. As an alternative for regression
techniques, ANNs are used. ANNs are applied in
many fields such as financial services,
biomedical applications, time-series prediction,
text mining, decision making and many others.
Although the applications of ANNs are
numerous, they all share an important common
aspect: the processes to be predicted are
correlated with a large number of explanatory
variables and there may exist high-level nonlinear relationships between those variables. One
of the main goals of ANNs is to detect those
high-level non-linear relationships to enable a
better modeling of the process. ANNs are in fact
computer systems that simulate the learning
effect of the human brain and are typically
composed of a number of neurons, grouped in
one or more hidden layers connected by means of
synapse connections. The output of each neuron
will be a weighted function of the different
incoming signals. The weight of an
interconnection between different neurons will
depend on the contribution performance of that
neuron to the final output.
Fig. 1 shows the combination of different
neurons (perceptron) into an artificial neural
network (multi-layer perceptron).

Neural networks are considered universal


approximators (Kolmogorov, 1957). Based on
this theorem, it was proven that regardless of the
activation function and the dimension of the
input space, a neural network with one hidden
layer can approximate any continuous function
arbitrarily well (Hornik et al., 1989). The
different input (the possible contributing
variables) are weighed and combined into a final
model. The network is trained to estimate the and
this training implies that the different
interconnection weights will be adapted every
time a new part is fed to the network.
Adaptation of the weights will be done based on
a
punishment/reward
principle:
if
the
interconnection did well during estimation of the
previous part, this variable will be rewarded by
increasing the weight of its interconnection to the
output. If the interconnection performed badly,
the interconnection weights will be decreased in
the next iteration step. By minimizing the
squared error (MSE) between the estimate and
the desired output (in our case firm
performance), the network is trained and as more
parts are fed, the learning effect increases.
However, one cannot keep on training the
network into infinity. When new parts, not
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 trialerror 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

well without explaining them. This means that an


output is generated based on different input
signals and by training those networks, accurate
estimates can be generated. ANNs are considered
black box approaches (Smith and Mason, 1999),
(Kim et al., 2004).To overcome this problem of
neural networks being black boxes, research has
been conducted to try and explain their
behaviour. Equivalence between ANNs and
fuzzy-rule-based systems can be found, allowing
to interpret the neural network. Benitez et al.
(1997) offer such a fuzzy-based interpretation of
neural networks to translate the knowledge
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).

Data for function fitting problems are set up for a


neural network by organizing the data into two
matrices, the input matrix in and the target
matrix out.
corrplot(data): was introduced to verify
the problems of multicollinearity which means
that there is a nearly perfect linear relationship
between explanatory variables, we introduced the
Cross correlation Matrix. The correlation plot Fig
2 shows that our independent variables
(corporate governance data) are not correlated.

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 >>>-

Preparing the Data

***************************************
%% Data Input and Preparation
clc; clear; close all;
in=xlsread('input'); % Input File
out=xlsread('output');
%
Output File
data=[in out];
corrplot(data)
%%
*************************************

Fig 2 : Correlation Matrix

Data log transformation

Univariate normality is not needed for least


squares estimates of the regression parameters to
be meaningful (see Gauss-Markov theorem).
However confidence intervals and hypothesis
tests will have better statistical properties if the
variables exhibit multivariate normality. This can
be assessed empirically by plotting the fitted
values against the residuals, and by inspecting
the normal quantile plot of the residuals. Note

that it is not relevant whether the dependent


variable Y is marginally normally distributed.
The log transformation is used because
distribution of our output is lognormal in a goal
to make highly skewed distributions less skewed.
***************************************
input=[1 2 3 4 ]; % Input Layer
p=data(:,input);
output=[5 6];
%
Output Layer
t=data(:,output);
p=p'; t=t';
%
Transposing Matrices

t=log(t+1);
% Defining Validation Dataset
trainRatio1=.6;
valRatio1=.2;
testRatio1=.2;
***************************************

Network Definition

***************************************
%% Network Definition
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.divideParam.trainRatio=trainRatio1
;
net1.divideParam.valRatio=valRatio1;
net1.divideParam.testRatio=testRatio1;
estval=sim(net1,p(:,tr.valInd));
eval=mse(estvalt(:,tr.valInd));
if eval<evalopt(ii)
netopt{(ii)}=net1;
tropt(ii)=tr; evalopt(ii)=eval
end
end
end
plot(nnn1:nnnj:nnnf,evalopt)

n is number of neurons in the mid-layer.


nn=1 is 5 neurons
nn=2 is 10 neurons
nn=3 is 15 neurons
nn=4 is 20 neurons
Fig 3 show the error plot for different n: 5, 10, 15
and 20. Note that for n = 4, the squared error
(MSE) is in his best minimum value

% 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
ii=ii+1; nnn
net1=newff(p,t,[nnn nnn]); % For
more functions see: 'Function
Reference' in 'Neural Network Toolbox'
of Matlab help
evalopt(ii)=100;
for i=1:it
[net1,tr,y,et]=train(net1,p,t);
% Training

Fig 3: Error plot for different n: 5, 10, 15 and 20

Network Output

***************************************
%% Output
clear; close all;
load('run_log_2');
nn=4

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');
%
test
figure; plot(t,estwhole,'.k')
%
whole

Fig 4: Plot of training, validation, and testing


data of accounting performance.

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
helpful in showing that certain data points have
poor fits.

Fig 5: Plot of training, validation, and testing


data of stock performance.
Note that if we rerun the code with higher
iterations for example it=100; it will reduce the
squared error.

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.

Вам также может понравиться