-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFourthStep.m
378 lines (368 loc) · 12.8 KB
/
FourthStep.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
clear; % Clear Memory
clc; %Clear Command Window
%Define the folder location of CSV files
folder='C:\Users\Maulin Amin\OneDrive - University of Waterloo\Waterloo\Winter 2018\Environment Canada\Wind&SnowData\CSV\Trenton';
i=1; %Set i=1 so that it does not give error for data{0} which is impossible
%use the Datastore function to read all the CSV files in the folder
ds = tabularTextDatastore(folder,'FileExtensions','.csv','SelectedVariableNames',{'Date_Time','SpdOfMaxGust_km_h_'});
%a while loop divides the Datastore variable data into years
wind_data = readall(ds);
%Make a second set of data. Here we just replace <31 with 30.5 in the data
t = cell2table(wind_data.SpdOfMaxGust_km_h_); %extract just the data column for modifications
y = table2array(t); %prepare to make modifications
y = strrep(y,'<31','30.5'); %make modifications
wind_data.SpdOfMaxGust_km_h_ = y;
%remove the rows with missing entries
wind_data = rmmissing(wind_data);
%Covert to DatTime
temp_Date_Time = datetime(wind_data.Date_Time,'InputFormat','yyyy-MM-dd'); %Array that converts 'string date' to datetime format
wind_data.Date_Time = temp_Date_Time;
%Convert Wind data from String to Number
Wind_Gust = cell2table(wind_data.SpdOfMaxGust_km_h_);
Wind_Gust = table2array(Wind_Gust);
Wind_Gust = str2double(Wind_Gust);
wind_data.SpdOfMaxGust_km_h_ = (Wind_Gust);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Delete all the data below 60kmph
A = wind_data;
A = table2timetable(A);
toDelete = A.SpdOfMaxGust_km_h_ < 61;
A(toDelete,:) = [];
% % % % % %plot histogram
% % % % % figure;
% % % % % histogram(A.SpdOfMaxGust_km_h_,30);
% % % % % title('Histogram Kmph >60');
% % % % % print('PPP60kmphHist','-dpng')
%Shift the kmph data
for k = 1:numel(A.SpdOfMaxGust_km_h_)
A.SpdOfMaxGust_km_h_(k) = A.SpdOfMaxGust_km_h_(k) - 60;
end
%Calculate the time interval between the data
A = timetable2table(A);
for k = 2:numel(A.Date_Time)
A.Time_Interval(k) = daysact(A.Date_Time(k-1), A.Date_Time(k));
end
toDelete = A.Time_Interval < 8;
A(toDelete,:) = [];
% % % % % %plot histogram
% % % % % figure;
% % % % % histogram(A.Time_Interval,30);
% % % % % title('Histogram 60 Kmph Inter-arrival');
% % % % % print('PPP60TIHist','-dpng')
%Shift the Inter-arrival data
for k = 1:numel(A.Time_Interval)
A.Time_Interval(k) = A.Time_Interval(k) - 7;
end
%Copy it to the output variable
WindData{1} = A;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Delete all the data below 70kmph
A = wind_data;
A = table2timetable(A);
toDelete = A.SpdOfMaxGust_km_h_ < 71;
A(toDelete,:) = [];
% % % % % %plot histogram
% % % % % figure;
% % % % % histogram(A.SpdOfMaxGust_km_h_,30);
% % % % % title('Histogram Kmph >70');
% % % % % print('PPP70kmphHist','-dpng')
%Shift the data
for k = 1:numel(A.SpdOfMaxGust_km_h_)
A.SpdOfMaxGust_km_h_(k) = A.SpdOfMaxGust_km_h_(k) - 70;
end
%Calculate the time interval between the data
A = timetable2table(A);
for k = 2:numel(A.Date_Time)
A.Time_Interval(k) = daysact(A.Date_Time(k-1), A.Date_Time(k));
end
toDelete = A.Time_Interval < 8;
A(toDelete,:) = [];
% % % % % figure;
% % % % % histogram(A.Time_Interval,30);
% % % % % title('Histogram 70 Kmph Inter-arrival');
% % % % % print('PPP70TIHist','-dpng')
%Shift the Inter-arrival data
for k = 1:numel(A.Time_Interval)
A.Time_Interval(k) = A.Time_Interval(k) - 7;
end
%Copy it to the output variable
WindData{2} = A;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%DATA GREATER THAN 60 KMPH
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
PPTH60 = WindData{1}(:,2);
PPTH60 = sortrows(PPTH60,1,'ascend');
PPTH60.Properties.VariableNames{'SpdOfMaxGust_km_h_'} = 'Kmph';
%calculations for PPP
z = numel(PPTH60);
for k = 1:z
PPTH60.Ln_Kmph(k) = log(PPTH60.Kmph(k));
end
for k = 1:z
PPTH60.Rank(k) = k;
end
for k = 1:z
PPTH60.Pi(k) = k/(z+1);
end
for k = 1:z
PPTH60.InvPi(k) = norminv(PPTH60.Pi(k));
end
for k = 1:z
PPTH60.ExpPi(k) = -log(1-PPTH60.Pi(k));
end
for k = 1:z
PPTH60.WeibPi(k) = log(-log(1-PPTH60.Pi(k)));
end
%PLOT THE NORMAL PPP
p = polyfit(PPTH60.InvPi,PPTH60.Kmph,1);
f = polyval(p,PPTH60.InvPi);
figure
plot(PPTH60.InvPi,PPTH60.Kmph,'.',PPTH60.InvPi,f,'-')
title('PPP Normal for >60 kmph');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTH60.InvPi,PPTH60.Kmph);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
% % % % % print('PPP60kmphNormal','-dpng')
%PLOT THE LOGNORMAL PPP
p = polyfit(PPTH60.InvPi,PPTH60.Ln_Kmph,1);
f = polyval(p,PPTH60.InvPi);
figure
plot(PPTH60.InvPi,PPTH60.Ln_Kmph,'.',PPTH60.InvPi,f,'-')
title('PPP LogNormal for >60 kmph');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTH60.InvPi,PPTH60.Ln_Kmph);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
% % % % % print('PPP60kmphLogNormal','-dpng')
%====================
%PLOT THE WEIBULL PPP
p = polyfit(PPTH60.WeibPi,PPTH60.Ln_Kmph,1);
f = polyval(p,PPTH60.WeibPi);
figure
plot(PPTH60.WeibPi,PPTH60.Ln_Kmph,'.',PPTH60.WeibPi,f,'-')
title('PPP Weibull for >60 kmph');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTH60.WeibPi,PPTH60.Ln_Kmph);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
% % % % % print('PPP60kmphWeibull','-dpng')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%DATA GREATER THAN 70 KMPH
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
PPTH70 = WindData{2}(:,2);
PPTH70 = sortrows(PPTH70,1,'ascend');
PPTH70.Properties.VariableNames{'SpdOfMaxGust_km_h_'} = 'Kmph';
%calculations for PPP
z = numel(PPTH70);
for k = 1:z
PPTH70.Ln_Kmph(k) = log(PPTH70.Kmph(k));
end
for k = 1:z
PPTH70.Rank(k) = k;
end
for k = 1:z
PPTH70.Pi(k) = k/(z+1);
end
for k = 1:z
PPTH70.InvPi(k) = norminv(PPTH70.Pi(k));
end
for k = 1:z
PPTH70.ExpPi(k) = -log(1-PPTH70.Pi(k));
end
for k = 1:z
PPTH70.WeibPi(k) = log(-log(1-PPTH70.Pi(k)));
end
%PLOT THE NORMAL PPP
p = polyfit(PPTH70.InvPi,PPTH70.Kmph,1);
f = polyval(p,PPTH70.InvPi);
figure
plot(PPTH70.InvPi,PPTH70.Kmph,'.',PPTH70.InvPi,f,'-')
title('PPP Normal for >70 kmph');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTH70.InvPi,PPTH70.Kmph);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
print('PPP70kmphNormal','-dpng')
%PLOT THE LOGNORMAL PPP
p = polyfit(PPTH70.InvPi,PPTH70.Ln_Kmph,1);
f = polyval(p,PPTH70.InvPi);
figure
plot(PPTH70.InvPi,PPTH70.Ln_Kmph,'.',PPTH70.InvPi,f,'-')
title('PPP LogNormal for >70 kmph');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTH70.InvPi,PPTH70.Ln_Kmph);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
print('PPP70kmphLogNormal','-dpng')
%PLOT THE WEIBULL PPP
p = polyfit(PPTH70.WeibPi,PPTH70.Ln_Kmph,1);
f = polyval(p,PPTH70.WeibPi);
figure
plot(PPTH70.WeibPi,PPTH70.Ln_Kmph,'.',PPTH70.WeibPi,f,'-')
title('PPP Weibull for >70 kmph');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTH70.WeibPi,PPTH70.Ln_Kmph);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
print('PPP70kmphWeibull','-dpng')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%DATA GREATER THAN 70 KMPH but analysing Inter-arrival time
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
PPTI70 = WindData{2}(:,3);
PPTI70 = sortrows(PPTI70,1,'ascend');
z = numel(PPTI70);
for k = 1:z
PPTI70.Ln_TI(k) = log(PPTI70.Time_Interval(k));
end
for k = 1:z
PPTI70.Rank(k) = k;
end
for k = 1:z
PPTI70.Pi(k) = k/(z+1);
end
for k = 1:z
PPTI70.InvPi(k) = norminv(PPTI70.Pi(k));
end
for k = 1:z
PPTI70.ExpPi(k) = -log(1-PPTI70.Pi(k));
end
for k = 1:z
PPTI70.WeibPi(k) = log(-log(1-PPTI70.Pi(k)));
end
%PLOT TIE NORMAL PPP
p = polyfit(PPTI70.InvPi,PPTI70.Time_Interval,1);
f = polyval(p,PPTI70.InvPi);
figure
plot(PPTI70.InvPi,PPTI70.Time_Interval,'.',PPTI70.InvPi,f,'-')
title('PPP Normal for >70 TI');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTI70.InvPi,PPTI70.Time_Interval);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
print('PPP70TINormal','-dpng')
%PLOT TIE LOGNORMAL PPP
p = polyfit(PPTI70.InvPi,PPTI70.Ln_TI,1);
f = polyval(p,PPTI70.InvPi);
figure
plot(PPTI70.InvPi,PPTI70.Ln_TI,'.',PPTI70.InvPi,f,'-')
title('PPP LogNormal for >70 TI');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTI70.InvPi,PPTI70.Ln_TI);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
print('PPP70TILogNormal','-dpng')
%PLOT TIE WEIBULL PPP
p = polyfit(PPTI70.WeibPi,PPTI70.Ln_TI,1);
f = polyval(p,PPTI70.WeibPi);
figure
plot(PPTI70.WeibPi,PPTI70.Ln_TI,'.',PPTI70.WeibPi,f,'-')
title('PPP Weibull for >70 TI');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTI70.WeibPi,PPTI70.Ln_TI);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
print('PPP70TIWeibull','-dpng')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%DATA GREATER THAN 60 KMPH but analysing Inter-arrival time
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
PPTI60 = WindData{1}(:,3);
PPTI60 = sortrows(PPTI60,1,'ascend');
%calculations for PPP
z = numel(PPTI60);
for k = 1:z
PPTI60.Ln_TI(k) = log(PPTI60.Time_Interval(k));
end
for k = 1:z
PPTI60.Rank(k) = k;
end
for k = 1:z
PPTI60.Pi(k) = k/(z+1);
end
for k = 1:z
PPTI60.InvPi(k) = norminv(PPTI60.Pi(k));
end
for k = 1:z
PPTI60.ExpPi(k) = -log(1-PPTI60.Pi(k));
end
for k = 1:z
PPTI60.WeibPi(k) = log(-log(1-PPTI60.Pi(k)));
end
%PLOT TIE NORMAL PPP
p = polyfit(PPTI60.InvPi,PPTI60.Time_Interval,1);
f = polyval(p,PPTI60.InvPi);
figure
plot(PPTI60.InvPi,PPTI60.Time_Interval,'.',PPTI60.InvPi,f,'-')
title('PPP Normal for >60 TI');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTI60.InvPi,PPTI60.Time_Interval);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
print('PPP60TINormal','-dpng')
%PLOT TIE LOGNORMAL PPP
p = polyfit(PPTI60.InvPi,PPTI60.Ln_TI,1);
f = polyval(p,PPTI60.InvPi);
figure
plot(PPTI60.InvPi,PPTI60.Ln_TI,'.',PPTI60.InvPi,f,'-')
title('PPP LogNormal for >60 TI');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTI60.InvPi,PPTI60.Ln_TI);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
print('PPP60TILogNormal','-dpng')
%PLOT TIE WEIBULL PPP
p = polyfit(PPTI60.WeibPi,PPTI60.Ln_TI,1);
f = polyval(p,PPTI60.WeibPi);
figure
plot(PPTI60.WeibPi,PPTI60.Ln_TI,'.',PPTI60.WeibPi,f,'-')
title('PPP Weibull for >60 TI');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTI60.WeibPi,PPTI60.Ln_TI);
X = sprintf('%f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
annotation('textbox',dim,'String',X,'FitBoxToText','on');
print('PPP60TIWeibull','-dpng')
%===================================
%USING A FUNCTION TO FIND A DISTRIBUTION
%===================================
[D1, PD1] = allfitdist(PPTH60.Kmph, 'PDF');
[D1, PD1] = allfitdist(PPTH60.Kmph, 'CDF');
[D2, PD2] = allfitdist(PPTH70.Kmph, 'PDF');
[D3, PD3] = allfitdist(PPTI70.Time_Interval, 'PDF');
[D4, PD4] = allfitdist(PPTI60.Time_Interval, 'PDF');
File = 'C:\Users\Maulin Amin\OneDrive - University of Waterloo\Waterloo\Winter 2018\Environment Canada\Wind&Snow\Step4.xlsx';
%===================================
%CLEAR THE EXCEL FILE BEFORE RUNNING THE PROGRAM
%===================================
for SheetNum=1:4
[N, T, Raw]=xlsread(File, SheetNum);
[Raw{:, :}]=deal(NaN);
xlswrite(File, Raw, SheetNum);
end
writetable(PPTH60,File,'Sheet','PPP > 60kmph');
writetable(PPTH70,File,'Sheet','PPP > 70kmph');
writetable(PPTI60,File,'Sheet','PPP 60 interarrival');
writetable(PPTI70,File,'Sheet','PPP 70 interarrival');