-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtsCopulaPlotJointReturnPeriod.m
More file actions
393 lines (290 loc) · 14.4 KB
/
tsCopulaPlotJointReturnPeriod.m
File metadata and controls
393 lines (290 loc) · 14.4 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
function tsCopulaPlotJointReturnPeriod(copulaAnalysis,varargin)
% plots the multivariate return period according to AND/OR Scenarios
% see https://doi.org/10.1002/2015WR017225
% Parts of the code were reworked from MvCAT toolbox https://doi.org/10.1002/2016WR020242
%
%Bahmanpour, M.H., 2023
args.marginalDistributions = "gp";
args.plotType='AND';
args.PL=[2,5,10,25,50,100,200];
args = tsEasyParseNamedArgs(varargin, args);
margDist = args.marginalDistributions;
plotType= args.plotType;
PL=args.PL;
jMax=copulaAnalysis.jointExtremes; %extreme compound events sampled previously
Family=copulaAnalysis.copulaParam.family;
switch margDist
case 'gp'
eps =cellfun(@(x) x{1}(2).parameters.epsilon,copulaAnalysis.marginalAnalysis,'UniformOutput',1);
sig=cellfun(@(x) x{1}(2).parameters.sigma(1),copulaAnalysis.marginalAnalysis,'UniformOutput',1);
thr=cellfun(@(x) x{1}(2).parameters.threshold(1),copulaAnalysis.marginalAnalysis,'UniformOutput',1);
nYear =cellfun(@(x) (x{1}(2).parameters.timeHorizonEnd-x{1}(2).parameters.timeHorizonStart)/365,copulaAnalysis.marginalAnalysis,'UniformOutput',1);
nPeak=cellfun(@(x) length(x{1}(2).objs.peakIndexes),copulaAnalysis.marginalAnalysis,'UniformOutput',1);
Scl2=nPeak./nYear; %scaling to account for average number of peaks per year- needed when GPD return levels are to be calculated
case 'gev'
eps=cellfun(@(x) x{1}(1).parameters.epsilon,copulaAnalysis.marginalAnalysis,'UniformOutput',1);
sig=cellfun(@(x) x{1}(1).parameters.sigma(1),copulaAnalysis.marginalAnalysis,'UniformOutput',1);
thr=cellfun(@(x) x{1}(1).parameters.mu(1),copulaAnalysis.marginalAnalysis,'UniformOutput',1);
Scl2=1; %annual maxima
end
PAR=copulaAnalysis.copulaParam.rho;
x = [linspace(1e-5,.95,400) linspace(0.95+1e-5,1-1e-5,500)];
[xx,yy] = meshgrid(x,x);
S = [xx(:),yy(:)];
if ~iscell(PAR) %stationary copula is intended
switch Family
case 'Gaussian'
P = copulacdf('Gaussian',S,PAR);
case 't'
PAR2=copulaAnalysis.copulaParam.nu;
P = copulacdf('t',S,PAR,PAR2);
case 'Clayton'
P = copulacdf('Clayton',S,PAR);
case 'Frank'
P = copulacdf('Frank',S,PAR);
case 'Gumbel'
P = copulacdf('Gumbel',S,PAR);
end
elseif iscell(PAR) % time-varying copula intended
sCell=repmat({S},1,size(PAR,2));
switch Family
case 'Gaussian'
% P = copulacdf('Gaussian',S,PAR);
pCell=cellfun(@(x,y) copulacdf('Gaussian',x,y),sCell,PAR,'UniformOutput',0);
case 't'
% P = copulacdf('t',S,PAR(1),PAR(2));
PAR2=copulaAnalysis.copulaParam.nu;
pCell=cellfun(@(x,y,z) copulacdf('t',x,y,z),sCell,PAR,PAR2,'UniformOutput',0);
case 'Clayton'
pCell=cellfun(@(x,y) copulacdf('Clayton',x,y),sCell,PAR,'UniformOutput',0);
case 'Frank'
pCell=cellfun(@(x,y) copulacdf('Frank',x,y),sCell,PAR,'UniformOutput',0);
case 'Gumbel'
pCell=cellfun(@(x,y) copulacdf('Gumbel',x,y),sCell,PAR,'UniformOutput',0);
end
end
if ~iscell(PAR)
switch plotType
case 'OR'
cBar = 1 - P; % P is copulaCDF
RP=1./cBar; %OR scenario
figure
jointReturnPeriodPlot(S,RP,Scl2,PL,margDist,eps,sig,thr,Family,PAR,jMax,plotType,copulaAnalysis);
case 'AND'
EP1=S(:,1);
EP2=S(:,2);
cBar = 1 - EP1-EP2+ P; %survival copula
RP=1./cBar; %AND scenario
figure
jointReturnPeriodPlot(S,RP,Scl2,PL,margDist,eps,sig,thr,Family,PAR,jMax,plotType,copulaAnalysis);
end
elseif iscell(PAR)
timeStampsByTimeWindow=copulaAnalysis.copulaParam.timeStampsByTimeWindow;
switch plotType
case 'OR'
rPCell=cellfun(@(x) 1./(1-x),pCell,'UniformOutput',0);
jointReturnPeriodPlot(sCell,rPCell,Scl2,PL,margDist,eps,sig,thr,Family,PAR,jMax,plotType,copulaAnalysis,'timeStampWindowCell',timeStampsByTimeWindow);
case 'AND'
rPCell=cellfun(@(x,y) 1./(1-y(:,1)-y(:,2)+x),pCell,sCell,'UniformOutput',0);
jointReturnPeriodPlot(sCell,rPCell,Scl2,PL,margDist,eps,sig,thr,Family,PAR,jMax,plotType,copulaAnalysis,'timeStampWindowCell',timeStampsByTimeWindow);
end
end
end
function jointReturnPeriodPlot(EP,EBVP,Scl2,PL,margDist,eps,sig,thr,Family,PAR,jMax,plotType,copulaAnalysis,varargin)
args.timeStampWindowCell = {};
args = tsEasyParseNamedArgs(varargin, args);
timeStampsByTimeWindow = args.timeStampWindowCell;
if ~iscell(EBVP)
hold on;
% Sort data based on joint probability
[P_Sort, ID_Pr] = sort(EBVP);
% Associated uniform marginal probabilities
U1 = EP(ID_Pr, 1);
U2 = EP(ID_Pr, 2);
% Probability contours and their acceptable lower and upper bounds
P_LB = PL - 0.005*PL; P_UB = PL + 0.005*PL;
% Define size of text
SIZE = 12;
% Loop through probability (or return period) contours
for j = 1:length(PL)
% Find indices associated with each probability contour
ID_Contour = find( P_Sort(:) >= P_LB(j) & P_Sort(:) <= P_UB(j) );
% Sort first uniform marginal
[UU, ID_U] = sort( U1(ID_Contour) );
% Associated second uniform marginal
VV = U2(ID_Contour);
VVV = VV(ID_U);
IUU = icdf( margDist, 1-1./(Scl2(1)*(1./(1-UU))),eps(1),sig(1),thr(1));
IVVV = icdf( margDist, 1-1./(Scl2(2)*(1./(1-VVV))),eps(2),sig(2),thr(2));
% Compute inverse of cdf for each varaible (with appropriate
% scaling for GPD return levels to account for average number of peaks per year)
% Trick: remove infinity if there is any
IDNAN = find( isinf(IUU) | isinf(IVVV) | isnan(IUU) | isnan(IVVV) );
IUU(IDNAN) = []; IVVV(IDNAN) = [];
% Calculate densities along the probability isoline
switch Family
case 'Gaussian'
Dens = copulapdf('Gaussian',[UU,VV(ID_U)],PAR);
case 't'
PAR2=copulaAnalysis.copulaParam.nu;
Dens = copulapdf('t',[UU,VV(ID_U)],PAR,PAR2);
case 'Clayton'
Dens = copulapdf('Clayton',[UU,VV(ID_U)],PAR);
case 'Frank'
Dens = copulapdf('Frank',[UU,VV(ID_U)],PAR);
case 'Gumbel'
Dens = copulapdf('Gumbel',[UU,VV(ID_U)],PAR);
end
% Normalize Densities to the highest density value
Dens = Dens / max(Dens);
Dens(IDNAN) = [];
% Color code probability isoline with density level
ZZ = zeros(size(IUU));
col = Dens; % This is the color, vary with x in this case.
h(j)=surface([IUU';IUU'],[IVVV';IVVV'],[ZZ';ZZ'],[col';col'],...
'facecol','no',...
'edgecol','flat',...
'linew',2);
colormap(jet)
if j == 1
cb = colorbar('location','south');
set(cb, 'xlim', [0 1]);
set(cb,'position',[cb.Position(1) cb.Position(2) 0.6*cb.Position(3) 0.5*cb.Position(4)])
cb.Label.String='Copula pdf'
end
text(max(IUU),min(IVVV),num2str(PL(j)),'Color','red','FontSize',10,'fontweight','bold')
if j == 1 % Write the title once
t = title(strcat('\color{red}',{[upper(Family),' Copula'];['Joint Return Period - ', plotType,' Scenario']}));
set(t, 'units', 'normalized', 'horizontalAlignment', 'center','fontname','times','fontweight','bold','fontsize',SIZE);
end
end
box on; %axis square
hold on
h(end+1)=plot(jMax(:,1),jMax(:,2),'r*');
xlabel('Variable 1')
ylabel('Variable 2')
legend(h(end),'Joint peaks')
elseif iscell(EBVP) %time-varying joint return periods
axxcell={}; %cell array containing all axes
nSubPlot=size(EP,2);
if mod(nSubPlot,8)==0
nFig=nSubPlot/8;
else
nFig=fix(nSubPlot/8)+1;
end
axn=1;
for ij=1:nFig
rt=1;
b0=27;
l0=21;
spMan = tsLcSubplotManager(b0, l0, 'CellXSize', round(21*rt), 'CellYSize', round(27*rt), 'gap', [0.03 0.01]);
spMan.initFigure;
numberofverticalpanels=4;
numberofhorizontalpanels=2;
ratiovertical=8;
ratiohoriz=8;
bmargin=0.2;
leftpanels=[-numberofverticalpanels -numberofverticalpanels+1;1 -ratiovertical]\[-b0+bmargin;0];
nb=leftpanels(1);
uddelta=leftpanels(2);
lmargin=1;
horizpanels=[-numberofhorizontalpanels -numberofhorizontalpanels+1;1 -ratiohoriz]\[-l0+lmargin;0];
nl=horizpanels(1);
lrdelta=horizpanels(2);
for ik=1:numberofhorizontalpanels
for ij=1:numberofverticalpanels
if axn>nSubPlot
break
end
eval(['b',num2str(ij),'=','b0-(numberofverticalpanels-ij)*(nb)-(numberofverticalpanels-ij+1)*(uddelta);'])
eval(['l',num2str(ik),'=','l0-(numberofhorizontalpanels-ik+1)*(nl)-(numberofhorizontalpanels-ik)*(lrdelta)+lmargin;'])
axx=spMan.createAxes(num2str(axn),eval(['b',num2str(ij)]),eval(['l',num2str(ik)]),nb,nl);
axxcell=[axxcell,axx];
axn=axn+1;
end
end
end
% Sort data based on joint probability
[P_Sortcell, ID_Prcell]=cellfun(@(x) sort(x),EBVP,'UniformOutput',0);
% Associated uniform marginal probabilities
U1cell=cellfun(@(x,y) x(y,1),EP,ID_Prcell,'UniformOutput',0);
U2cell=cellfun(@(x,y) x(y,2),EP,ID_Prcell,'UniformOutput',0);
% Probability contours (or return periods) and their acceptable lower and upper bounds
P_LB = PL - 0.005*PL; P_UB = PL + 0.005*PL;
% Define size of text
SIZE = 12;
epscell=repmat({eps},1,size(PAR,2));
sigcell=repmat({sig},1,size(PAR,2));
thrcell=repmat({thr},1,size(PAR,2));
Scl2cell=repmat({Scl2},1,size(PAR,2));
margDistcell=repmat({margDist},1,size(PAR,2));
for jk=1:size(P_Sortcell,2)
for j = 1:length(PL)
% Find indices associated with each probability contour
P_LBcell=repmat({P_LB(j)},1,size(P_Sortcell,2));
P_UBcell=repmat({P_UB(j)},1,size(P_Sortcell,2));
ID_Contourcell=cellfun(@(x,y,z) find(x>=y & x<=z),P_Sortcell,P_LBcell,P_UBcell,'UniformOutput',0);
% Sort first uniform marginal
[UUcell, ID_Ucell]=cellfun(@(x,y) sort(x(y)),U1cell,ID_Contourcell,'UniformOutput',0);
% Associated second uniform marginal
VVcell=cellfun(@(x,y) x(y),U2cell,ID_Contourcell,'UniformOutput',0);
VVVcell=cellfun(@(x,y) x(y),VVcell,ID_Ucell,'UniformOutput',0);
% Compute inverse of cdf for each varaible (return levels)
IUUcell=cellfun(@(x,x1,x2,x3,x4,x5) icdf(x5,1-1./(x4(1).*(1./(1-x))),x1(1),x2(1),x3(1)),UUcell,epscell,sigcell,thrcell,Scl2cell,margDistcell,'UniformOutput',0);
IVVVcell=cellfun(@(x,x1,x2,x3,x4,x5) icdf(x5,1-1./(x4(2).*(1./(1-x))),x1(2),x2(2),x3(2)),VVVcell,epscell,sigcell,thrcell,Scl2cell,margDistcell,'UniformOutput',0);
% Trick: find non-nan and non-inf values
IDGoodcell=cellfun(@(x,y) find(~isinf(x)&~isinf(y)&~isnan(x)&~isnan(y)),IUUcell,IVVVcell,'UniformOutput',0);
IUUcell=cellfun(@(x,y) x(y),IUUcell,IDGoodcell,'UniformOutput',0);
IVVVcell=cellfun(@(x,y) x(y),IVVVcell,IDGoodcell,'UniformOutput',0);
% Calculate densities along the probability isoline
switch Family
case 'Gaussian'
% Dens = copulapdf('Gaussian',[UU,VV(ID_U)],PAR);
Denscell=cellfun(@(x,y,z) copulapdf('Gaussian',[x y],z),UUcell,VVVcell,PAR,'UniformOutput',0);
case 't'
PAR2=copulaAnalysis.copulaParam.nu;
Denscell=cellfun(@(x,y,z,z1) copulapdf('t',[x y],z,z1),UUcell,VVVcell,PAR,PAR2,'UniformOutput',0);
case 'Clayton'
Denscell=cellfun(@(x,y,z) copulapdf('Clayton',[x y],z),UUcell,VVVcell,PAR,'UniformOutput',0);
case 'Frank'
Denscell=cellfun(@(x,y,z) copulapdf('Frank',[x y],z),UUcell,VVVcell,PAR,'UniformOutput',0);
case 'Gumbel'
Denscell=cellfun(@(x,y,z) copulapdf('Gumbel',[x y],z),UUcell,VVVcell,PAR,'UniformOutput',0);
end
% Normalize Densities to the highest density value
Denscell=cellfun(@(x) x./max(x),Denscell,'UniformOutput',0);
Denscell=cellfun(@(x,y) x(y),Denscell,IDGoodcell,'UniformOutput',0);
ZZcell=cellfun(@(x) zeros(size(x)),IUUcell,'UniformOutput',0);
% color code probability isoline with density level
colcell= Denscell;
% construct x, y, z, and c, usable in surface function
X=cellfun(@(x1) [x1';x1'],IUUcell,'UniformOutput',0);
Y=cellfun(@(x1) [x1';x1'],IVVVcell,'UniformOutput',0);
Z=cellfun(@(x1) [x1';x1'],ZZcell,'UniformOutput',0);
C=cellfun(@(x1) [x1';x1'],colcell,'UniformOutput',0);
surface(axxcell(jk),X{jk},Y{jk},Z{jk},C{jk},...
'facecol','no',...
'edgecol','flat',...
'linew',2);
set(axxcell(jk),'NextPlot','add')
if mod(j,2)==0
text(axxcell(jk),max(IUUcell{jk}),min(IVVVcell{jk}),num2str(PL(j)),'Color','red','FontSize',10,'fontweight','bold','HorizontalAlignment','center')
end
if j==1
colormap(axxcell(jk),'jet')
cb = colorbar(axxcell(jk),'location','south');
if strcmp(copulaAnalysis.copulaParam.family,'Gaussian') ||strcmp(copulaAnalysis.copulaParam.family,'t')
nr=round(PAR{jk}(2)*1000)/1000;
elseif strcmp(copulaAnalysis.copulaParam.family,'Frank') || strcmp(copulaAnalysis.copulaParam.family,'Clayton') ||strcmp(copulaAnalysis.copulaParam.family,'Gumbel')
nr=round(PAR{jk}(1));
end
t1x=datestr(timeStampsByTimeWindow{jk}(1),'yyyy');
t2x=datestr(timeStampsByTimeWindow{jk}(end),'yyyy');
ht=title(axxcell(jk),{[t1x,' - ',t2x];['Rho = ',num2str(nr)]});
ht.VerticalAlignment='top';
end
end
scatter(axxcell(jk),jMax{jk}(:,1),jMax{jk}(:,2), 'markerfacecolor', 'r');
end
end
end