-
Notifications
You must be signed in to change notification settings - Fork 5
/
tsEVstatistics.m
164 lines (121 loc) · 4.24 KB
/
tsEVstatistics.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
function [EVmeta,EVdata,isValid] = tsEVstatistics(pointData, varargin)
% Evangelos Voukouvalas, Michalis Vousdoukas 2015
% gevMaxima can be annual or monthly. annual by default
% in a gaussian approximation alphaCI~68% corresponds to 1 sigma confidence
% interval
defvals={[],[],.68};
minvars=1;
EVdata = [];
minGEVSample = 10;
args.alphaCI = .95;
args.gevMaxima = 'annual';
args.gevType = 'GEV'; % can be 'GEV' or 'Gumbel'
args.evdType = {'GEV', 'GPD'};
args = tsEasyParseNamedArgs(varargin, args);
gevMaxima = args.gevMaxima;
gevType = args.gevType;
alphaCI = args.alphaCI;
evdType = args.evdType;
isValid = true;
%% Basic data
Tr=[5,10,20,50,100,200,500,1000];
EVmeta.Tr=Tr;
[nyears]=size(pointData.annualMax);
%% stationary GEV
imethod=1;
methodname='GEVstat';
paramEsts=nan(1,3);
paramCIs=nan*ones(2,3);
rlvls=nan(1,length(Tr));
if ~isempty(find(strcmpi(evdType, 'GEV'), 1)) && ~isempty(pointData.annualMax)
if strcmpi(gevMaxima, 'annual')
tmpmat=pointData.annualMax(:);
elseif strcmpi(gevMaxima, 'monthly')
tmpmat=pointData.monthlyMax(:);
else
error(['tsEVstatistics: invalid gevMaxima type: ' gevMaxima]);
end
iIN=~isnan(tmpmat);
if sum(iIN)>=minGEVSample
tmp=tmpmat(iIN);
if strcmpi(gevType, 'GEV')
[paramEsts,paramCIs]=gevfit(tmp, alphaCI);
elseif strcmpi(gevType, 'Gumbel')
paramEsts(1) = 0;
paramCIs(:,1) = 0;
[paramEsts(:,[3,2]),paramCIs(:,[3,2])]=evfit(-tmp, alphaCI);
paramEsts(3) = -paramEsts(3);
paramCIs([1,2], 3) = -paramCIs([2,1], 3);
else
error(['tsEVstatistics: invalid gevType: ' gevType '. Can be only GEV or Gumbel']);
end
% paramEsts(jj,1): shape param
% paramEsts(jj,2): scale param
% paramEsts(jj,3): location param
% the second parameter returned by gevfit is the 95% confidence
% interval
rlvls(1,:) = gevinv(1-1./Tr,paramEsts(1,1),paramEsts(1,2),paramEsts(1,3));
else
disp('Skipping...')
isValid = false;
end
EVdata(imethod).method=methodname;
EVdata(imethod).values=rlvls;
EVdata(imethod).parameters=paramEsts;
EVdata(imethod).paramCIs = paramCIs;
else
EVdata(imethod).method = methodname;
EVdata(imethod).values = [];
EVdata(imethod).parameters = [];
EVdata(imethod).paramCIs = [];
end
%% stationary GPD
imethod=2;
methodname='GPDstat';
paramEstsall=nan(1,6);
paramCIs=nan*ones(2,2);
rlvls=nan(1,length(Tr));
try
if ~isempty(find(strcmpi(evdType, 'GPD'), 1))
ik = 1;
d1=pointData.POT(ik).peaks-pointData.POT(ik).threshold;
[paramEsts,paramCIs]=gpfit(d1, alphaCI);
% shape parameter
ksi=paramEsts(1);
% scale parameter
sgm=paramEsts(2);
if ksi < -.5
% computing anyway the confidence interval (in a rough way)
probs = [alphaCI/2; 1-alphaCI/2];
[~, acov] = gplike([ksi sgm], d1);
se = sqrt(diag(acov))';
% Compute the CI for k using a normal distribution for khat.
kci = norminv(probs, ksi, se(1));
% VERY ROUGHT: minimizing the lower boundary of kci to -1
kci(kci < -1) = -1;
% Compute the CI for sigma using a normal approximation for
% log(sigmahat), and transform back to the original scale.
% se(log(sigmahat)) is se(sigmahat) / sigmahat.
lnsigci = norminv(probs, log(sgm), se(2)./sgm);
paramCIs = [kci exp(lnsigci)];
end
% paramCIs: 95% confidence interval
paramEstsall(ik,:)=[sgm ksi pointData.POT(ik).threshold length(d1) length(pointData.POT(ik).peaks) pointData.POT(ik).percentile];
rlvls(ik,:) = pointData.POT(ik).threshold+(sgm/ksi).*((((length(d1)/length(pointData.POT(ik).peaks))*(1./Tr)).^(-ksi))-1);
else
EVdata(imethod).method = methodname;
EVdata(imethod).values = [];
EVdata(imethod).parameters = [];
EVdata(imethod).paramCIs = [];
end
catch err
disp(getReport(err));
paramEstsall(ik,:)=[0 0 0 0 0 0];
rlvls(ik,:) = zeros(1, length(Tr));
isValid = false;
end
EVdata(imethod).method=methodname;
EVdata(imethod).values=rlvls;
EVdata(imethod).parameters=paramEstsall;
EVdata(imethod).paramCIs = flipdim(paramCIs, 2);
end