-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.m
144 lines (114 loc) · 4.98 KB
/
main.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
%%%****************************************************************************************
%%%* This is the source code for the paper "Multi-step-ahead Stock Price Prediction Using *
%%%* Recurrent Fuzzy Neural Network and Variational Mode Decomposition" (VMD-MFRFFN) *
%%%* Authors: Hamid Nasiri, Mohammad Mehdi Ebadzadeh *
%%%****************************************************************************************
rng(1); % For reproducibility of results
global nStates;
global nData;
global nRules_Output;
global rbarOutput;
global rbarState;
global currentIndividual;
global targetOutput;
%% Load HSI Benchmark
fprintf("Loading Hang Seng Index Dataset...\n");
maxNumberOfIMFS = 9;
TargetDimension = 1;
load('Benchmarks\\HSI_Index.mat');
% Test Data
testOutputTemp = data(end-375:end,TargetDimension);
% Train Data
data = data(1:end-377,TargetDimension);
finalPrediction = [];
fprintf("Decomposing Train Data Using VMD...\n");
[imf,residual] = vmd(data,'NumIMF',maxNumberOfIMFS);
fprintf("Decomposing Test Data Using VMD...\n");
[imf_test, residual_test] = vmd(testOutputTemp,'NumIMF',maxNumberOfIMFS);
numberOfRules = [6 6 6 5 5 5 4 4 4];
for numberOfIMFS = 1:maxNumberOfIMFS
data = imf(:,numberOfIMFS);
data_test = imf_test(:,numberOfIMFS);
tempdata = data;
tempdata_test = data_test;
data = (data - min(data)) ./ (max(data)-min(data)) ;
data_test = (data_test - min(data_test)) ./ (max(data_test)-min(data_test)) ;
trainInput = data(1:end-1,TargetDimension);
targetOutput = data(2:end,TargetDimension);
testInput = data_test(1:end-1,TargetDimension);
testOutput = data_test(2:end,TargetDimension);
% ****************************************************
% * Parameters *
% ****************************************************
coverageRules = false; % Generating Fuzzy Rules Using Coverage?
ruleSigma = 0.3;
coverageThreshold = 0.2;
nRules_Output = numberOfRules(numberOfIMFS);
nRules_State = 2;
nStates = 2;
mfType = "trimf"; % trimf gaussmf gauss2mf gbellmf
nFuzzySetsOutput = numberOfRules(numberOfIMFS);
nFuzzySetsState = 2;
PSO_SwarmSize = 25;
PSO_MaxIteration = 2;
nData = size(trainInput,1);
nDimensions = size(trainInput,2);
%% Create Fuzzy Rules
if ~coverageRules
if mfType == "gaussmf"
mfOutput = createGaussianMembershipFunction(nFuzzySetsOutput);
mfState = createGaussianMembershipFunction(nFuzzySetsState);
elseif mfType == "trimf"
mfOutput = createMembershipFunction(nFuzzySetsOutput);
mfState = createMembershipFunction(nFuzzySetsState);
elseif mfType == "gauss2mf"
mfOutput = createGaussian2MembershipFunction(nFuzzySetsOutput);
mfState = createGaussian2MembershipFunction(nFuzzySetsState);
elseif mfType == "gbellmf"
mfOutput = createGeneralizedBellshapeMembershipfunction(nFuzzySetsOutput);
mfState = createGeneralizedBellshapeMembershipfunction(nFuzzySetsState);
end
mfOutput = repmat(mfOutput,1,nDimensions);
mfState = repmat(mfState,1,nDimensions);
else
if ~exist('mf','var')
if mfType == "gaussmf"
generateGaussianFuzzyRulesUsingCoverage;
elseif mfType == "trimf"
generateTriangleFuzzyRulesUsingCoverage;
end
end
mfOutput = mf;
mfState = mf;
nFuzzySetsOutput = length(mf);
nFuzzySetsState = length(mf);
nRules_Output = nFuzzySetsOutput;
nRules_State = nFuzzySetsState;
end
fprintf("Calculating Membership Values...\n");
calculateMembershipValues;
%% Initialization
% W -> Weight of Output Network
% V -> Weight of State Network
currentIndividual.V = rand(nStates,nRules_State);
currentIndividual.W = rand(nStates,nRules_Output);
costCalculation;
%% Training Network
fprintf("Training Network Using PSO For IMF%d ...",numberOfIMFS);
fun = @(x) objectiveFunction(x);
lb = zeros(1,nRules_State*nStates);
ub = ones(1,nRules_State*nStates);
options = optimoptions('particleswarm','SwarmSize',PSO_SwarmSize,'Display','iter','MaxIterations',PSO_MaxIteration); %patternsearch fmincon
[x, fval] = particleswarm(fun,nRules_State*nStates,lb,ub,options);
currentIndividual.V = x;
costCalculation
calculatingTestError;
finalPrediction = [finalPrediction ; scaledPredicted'];
end
predictedOutput = sum(finalPrediction);
predictedOutput = predictedOutput';
testRMSE = sqrt(immse(predictedOutput,testOutputTemp(2:end)));
testMAPE = mean(abs((predictedOutput-testOutputTemp(2:end))./testOutputTemp(2:end))*100);
fprintf("One-step-ahead Prediction Results:\n");
fprintf("RMSE = %e\n",testRMSE);
fprintf("MAPE = %e\n",testMAPE);