-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_analysis_AMI.m
More file actions
executable file
·192 lines (152 loc) · 3.75 KB
/
linear_analysis_AMI.m
File metadata and controls
executable file
·192 lines (152 loc) · 3.75 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
% Project Timeseries 2017-2018
% Team 29
% Diamanti Maria 8133
% Ntzioni Dimitra 8209
%% Timeseries
[matrix, filtered] = extremes(VarName1, 2);
AMA = [];
for i = 1:length(matrix)
if matrix(i,3) == 1;
AMA = [AMA,matrix(i,2)];
end
end
figure(1)
plot(AMA)
title('AMA')
AMI = [];
for i = 1:length(matrix)
if matrix(i,3) == -1;
AMI = [AMI,matrix(i,2)];
end
end
figure(2)
plot(AMI)
title('AMI')
AMD = [];
for i = 1:length(AMI)
AMD(i) = abs(AMI(i) - AMA(i+1));
end
figure(3)
plot(AMD)
title('AMD')
TMA = [];
for i = 1:(length(matrix)/2)
TMA(i) = abs(matrix(2*i,1) - matrix(2*i+1,1));
end
figure(4)
plot(TMA)
title('TMA')
TMI = [];
TMI(1) = abs(matrix(1,1) - matrix(2,1));
for i = 2:(length(matrix)/2)
TMI(i) = abs(matrix(2*i-1,1) - matrix(2*i,1));
end
figure(5)
plot(TMI)
title('TMI')
TBP = [];
for i = 1:(length(matrix)/2)
TBP(i) = abs(matrix(2*i-1,1) - matrix(2*i+1,1));
end
figure(6)
plot(TBP)
title('TBP')
%% plot autocorrelation AMI
n = length(AMI);
sdnoise = 10;
mux2= 20;
perseason = 12;
maxtau = 100;
alpha = 0.05;
zalpha = norminv(1-alpha/2);
acxM = autocorrelation(AMI, maxtau);
autlim = zalpha/sqrt(n);
figure(9)
clf
hold on
for ii=1:maxtau
plot(acxM(ii+1,1)*[1 1],[0 acxM(ii+1,2)],'b','linewidth',1.5)
end
plot([0 maxtau+1],[0 0],'k','linewidth',1.5)
plot([0 maxtau+1],autlim*[1 1],'--c','linewidth',1.5)
plot([0 maxtau+1],-autlim*[1 1],'--c','linewidth',1.5)
xlabel('\tau')
ylabel('r(\tau)')
title(sprintf('autocorrelation AMI'))
%% Remove trend using first differences
x1 = ones(length(AMI),1);
for i=2:1:length(AMI) % first differences
x1(i) = AMI(i) - AMI(i-1);
end
x1(1) = AMI(1);
%% Autocorrelation of the detrended time series by first differences
rXt1 = autocorrelation(x1,maxtau);
autlim = zalpha/sqrt(n);
figure(10)
clf
hold on
for ii=1:maxtau
plot(rXt1(ii+1,1)*[1 1],[0 rXt1(ii+1,2)],'b','linewidth',1.5)
end
plot([0 maxtau+1],[0 0],'k','linewidth',1.5)
plot([0 maxtau+1],autlim*[1 1],'--c','linewidth',1.5)
plot([0 maxtau+1],-autlim*[1 1],'--c','linewidth',1.5)
xlabel('\tau')
ylabel('r(\tau)')
title(sprintf('detrended time series by first differences, autocorrelation'))
%% Partial autocorrelation second differences
parRX2 = parautocor(x1,maxtau);
figure(12)
clf
hold on
for i=1:(maxtau-1)
plot(i*[1 1],[0 parRX2(i)],'b','linewidth',2)
end
plot([0 maxtau+1],[0 0],'k')
plot([0 maxtau+1],autlim*[1 1],'--c','linewidth',1.5)
plot([0 maxtau+1],-autlim*[1 1],'--c','linewidth',1.5)
xlabel('\tau')
ylabel('\phi_{\tau\tau}')
title('Partial autocorrelation, first differences')
%% Define the optimal model from AIC index
figure(13)
AIC = []; %check AR order
for j = 1:10
for i = 1:10
[nrmseV,phiV,thetaV,SDz,aicS,fpeS,armamodel] = fitARMA(x1,i,j,5); %%fit with ARMA(p,q) AIC is minimum
AIC(i,j) = aicS;
end
hold on
plot(AIC(:,j))
end
[p,q] = find(AIC == min(min(AIC)))
% chosen: ARMA(8,7) for data29v1
% chosen: ARMA(2,5) for data29v2
%% Prediction for 2 steps ahead
[nrmseV, preM, phiV, thetaV] = predictARMAnrmse(x1,p,q,2);
nrmseV
%% NRMSE
premAMI = ones(length(preM),2);
for i=2:1:length(preM) % first differences
premAMI(i,:) = preM(i,:) + preM(i-1,:);
end
premAMI(1,:) = preM(1,:);
premAMI1 = premAMI(:,1)';
premAMI2 = premAMI(:,2)';
start = floor(length(AMI)/2)+1;
nrmseAMI1 = nrmse(AMI(start:end),premAMI1);
nrmseAMI1
nrmseAMI2 = nrmse(AMI(start:end),premAMI2);
nrmseAMI2
figure(30)
hold on
plot(AMI(start:end))
plot(premAMI1)
hold off
title('AMI - Prediction one step ahead')
figure(31)
hold on
plot(AMI(start:end))
plot(premAMI2)
hold off
title('AMI - Prediction two steps ahead')