-
Notifications
You must be signed in to change notification settings - Fork 12
/
plotConfidenceIntervals.m
314 lines (285 loc) · 11 KB
/
plotConfidenceIntervals.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
function fh = plotConfidenceIntervals(pStruct, varargin)
% plotConfidenceIntervals.m visualizes confidence itervals stored in either
% the parameters or properties struct .CI
%
% USAGE:
% fh = plotParameterUncertainty(pStruct)
% fh = plotParameterUncertainty(pStruct, methods)
% fh = plotParameterUncertainty(pStruct, methods, options)
%
% plotMultiStarts() uses the following PestoPlottingOptions members:
% * PestoPlottingOptions::P
% * PestoPlottingOptions::S
% * PestoPlottingOptions::MS
% * PestoPlottingOptions::boundary
% * PestoPlottingOptions::subplot_size_1D
% * PestoPlottingOptions::subplot_indexing_1D
% * PestoPlottingOptions::CL
% * PestoPlottingOptions::hold_on
% * PestoPlottingOptions::interval
% * PestoPlottingOptions::bounds
% * PestoPlottingOptions::A
% * PestoPlottingOptions::add_points
% * PestoPlottingOptions::labels
% * PestoPlottingOptions::legend
% * PestoPlottingOptions::op2D
% * PestoPlottingOptions::fontsize
%
% Parameters:
% pStruct: either the parameter or the property struct containing
% information about parameters and results of optimization (.MS)
% and uncertainty analysis (.P and .S). This structures is the output
% of plotMultiStarts.m, getProfiles.m or plotSamples.m.
% varargin:
% method: integer array, from which method confidence intervals
% should be plotted:
% options: options of plotting as instance of PestoPlottingOptions
%
% Return values:
% fh: figure handle
%
% History:
% * 2016/11/14 Paul Stapor
% * 2017/12/27 Jan Hasenauer
%% Check and assign input
% Check which methods should be used to plot Confidence Intervals
if (length(varargin) >= 1)
if(~isempty(varargin{1}))
methIn = varargin{1};
boolWarning = true;
else
methIn = {'local_PL', 'PL', 'local_B', 'S'};
boolWarning = false;
end
else
methIn = {'local_PL', 'PL', 'local_B', 'S'};
end
methods = checkMeth(methIn, pStruct, boolWarning);
numConf = methods.num;
% Assignment of user-provided options
if (length(varargin) >= 2)
if (~isa(varargin{2}, 'PestoOptions'))
error('Argument 2 is not of type PestoOptions.')
end
allOptions = varargin{2};
options = allOptions.plot_options.copy();
else
allOptions = PestoOptions();
options = PestoPlottingOptions();
end
%% Check what exactly should be plotted
if isfield(pStruct, 'function')
type = 'Properties';
if isempty(allOptions.property_index)
pIndexSet = 1 : pStruct.number;
else
pIndexSet = allOptions.property_index;
end
else
type = 'Parameters';
if isempty(allOptions.parameter_index)
pIndexSet = 1 : pStruct.number;
else
pIndexSet = allOptions.parameter_index;
end
end
% Check, if pStruct has all necessary fieds
pStruct = checkSanityOfStructs(pStruct, ['p' type(2:end)]);
numP = length(pIndexSet);
iMAP = allOptions.MAP_index;
if isempty(iMAP)
iMAP = 1;
end
switch options.group_CI_by
case 'parprop'
indexSet = pIndexSet;
indexLen = length(pIndexSet);
case 'methods'
indexSet = 1 : numConf;
indexLen = numConf;
case 'all'
indexLen = 1;
otherwise
error('Call to undefinded grouping method plot plot of confidence intervals.');
end
%% Initialize the figure generation
sqrtPlots = ceil(sqrt(indexLen));
if ((sqrtPlots-1)*sqrtPlots >= indexLen)
plots = [sqrtPlots-1, sqrtPlots];
else
plots = [sqrtPlots, sqrtPlots];
end
% Open figure
fh = figure('Name', ['plotConfidenceIntervals - ' type]);
pos = nan(indexLen, 4);
%% Generate the Plots
switch options.group_CI_by
case 'parprop'
for iP = 1 : indexLen %indexSet
subplot(plots(1), plots(2), iP);
pos(iP,:) = get(gca, 'Position');
del = 0.1 / plots(2);
offsetLabels = 0.1;
step = (1 - offsetLabels) / plots(2);
pos(iP,:) = [mod((iP-1), plots(2))*step + del/2 + offsetLabels, pos(iP, 2), step-del, pos(iP, 4)];
hold on;
set(gca, 'YLim', [0.5, numConf + 0.5]);
ylabel('');
xlabel(pStruct.name{indexSet(iP)});
if (mod(iP-1, plots(2)) == 0)
set(gca, 'ytick', 1 : numConf, 'yticklabel', methods.name);
else
set(gca, 'ytick', 1 : numConf, 'yticklabel', '');
end
box on;
for j = 1 : numConf
CI = pStruct.CI.(methods.type{j});
if not((j == 1) && isnan(CI(indexSet(iP),1,1)))
for k = methods.numLevels : -1 : 1
h = methods.bars(k);
if (CI(indexSet(iP),1,k) == -inf)
CI(indexSet(iP),1,k) = pStruct.min(indexSet(iP));
end
if ((CI(indexSet(iP),2,k)) == inf)
CI(indexSet(iP),2,k) = pStruct.max(indexSet(iP));
end
patch([CI(indexSet(iP),1,k), CI(indexSet(iP),2,k), CI(indexSet(iP),2,k), CI(indexSet(iP),1,k)], [j-h, j-h, j+h, j+h], 'k', 'FaceColor', methods.colors(j,:,k), 'EdgeColor', 'k');
end
end
if isfield(pStruct, 'MS')
if (strcmp(type, 'Parameters'))
plot([pStruct.MS.par(indexSet(iP),1), pStruct.MS.par(indexSet(iP),1)], [j-0.4, j+0.4], 'k-', 'linewidth', 2);
else
plot([pStruct.MS.prop(indexSet(iP),1), pStruct.MS.prop(indexSet(iP),1)], [j-0.4, j+0.4], 'k-', 'linewidth', 2);
end
end
end
if (options.draw_bounds)
xLimits = get(gca, 'XLim');
if (xLimits(1) <= pStruct.min(indexSet(iP)) && xLimits(2) >= pStruct.min(indexSet(iP)))
plot([pStruct.min(indexSet(iP)), pStruct.min(indexSet(iP))], [0.5, numConf+0.5], 'b--', 'linewidth', 2);
set(gca, 'XLim', [pStruct.min(indexSet(iP)), xLimits(2)]);
end
xLimits = get(gca, 'XLim');
if (xLimits(1) <= pStruct.max(indexSet(iP)) && xLimits(2) >= pStruct.max(indexSet(iP)))
plot([pStruct.max(indexSet(iP)), pStruct.max(indexSet(iP))], [0.5, numConf+0.5], 'b--', 'linewidth', 2);
set(gca, 'XLim', [xLimits(1), pStruct.max(indexSet(iP))]);
end
end
hold off;
end
% Relocate the images to where I want them to be. Don't try to go
% for a more intelligent solution. I did, and it made me alomost
% mad... The Matlab syntax for figure handles is, well... o.O'
fig = gcf;
fig.Children = flip(fig.Children);
for iP = 1 : indexLen %indexSet
set(fig.Children(iP), 'Position', pos(iP,:));
end
case 'methods'
for iM = indexSet
subplot(plots(1), plots(2), iM);
pos(iM,:) = get(gca, 'Position');
del = 0.1 / plots(2);
offsetLabels = 0.1;
step = (1 - offsetLabels) / plots(2);
pos(iM,:) = [mod((iM-1), plots(2))*step + del/2 + offsetLabels, pos(iM, 2), step-del, pos(iM, 4)];
hold on;
set(gca, 'YLim', [0.5, numP + 0.5]);
title(methods.name{iM});
ylabel('');
xlabel('');
if (iM == 1 || iM == 3)
set(gca, 'ytick', 1 : numP, 'yticklabel', pStruct.name(pIndexSet));
else
set(gca, 'ytick', 1 : numP, 'yticklabel', '');
end
box on;
XMin = min(pStruct.min);
XMax = max(pStruct.max);
ax = gca;
iP = 1;
for j = pIndexSet
CI = pStruct.CI.(methods.type{iM});
for k = methods.numLevels : -1 : 1;
h = methods.bars(k);
CI(iM,1,k) = max(CI(iM,1,k), XMin);
CI(iM,1,k) = min(CI(iM,1,k), XMax);
patch([CI(j,1,k), CI(j,2,k), CI(j,2,k), CI(j,1,k)], [iP-h, iP-h, iP+h, iP+h], 'k', 'FaceColor', methods.colors(iM,:,k), 'EdgeColor', 'k');
end
if isfield(pStruct, 'MS')
if (strcmp(type, 'Parameters'))
plot([pStruct.MS.par(j,iMAP), pStruct.MS.par(j,iMAP)], [j-0.4, j+0.4], 'k-', 'linewidth', 2);
else
plot([pStruct.MS.prop(j,iMAP), pStruct.MS.prop(j,iMAP)], [j-0.4, j+0.4], 'k-', 'linewidth', 2);
end
end
iP = iP + 1;
end
if (options.draw_bounds)
limits = [ax.XLim(1), ax.XLim(2)];
pIndex1 = [pStruct.min(pIndexSet(1)); pStruct.min(pIndexSet); pStruct.min(pIndexSet(end))];
pIndex2 = [pStruct.max(pIndexSet(1)); pStruct.max(pIndexSet); pStruct.max(pIndexSet(end))];
plot(pIndex1, 0 : numP+1, 'b--', 'linewidth', 2);
plot(pIndex2, 0 : numP+1, 'b--', 'linewidth', 2);
set(gca, 'XLim', limits);
end
hold off;
end
fig = gcf;
fig.Children = flip(fig.Children);
for iM = indexSet
set(fig.Children(iM), 'Position', pos(iM,:));
end
case 'all'
end
end
function methodsOut = checkMeth(methodsIn, pStruct, boolWarning)
% checkMeth
%
% Parameters:
% methodsIn:
% pStruct:
% boolWarning:
%
% Return values:
% methodsOut:
tempMethType = {'PL', 'local_PL', 'local_B', 'S'};
tempMethName = {'Profile L.', 'L.App, th.', 'L.App, mass', 'Bay., Sampl.'};
checkMeth = zeros(1,4);
for j = 1 : 4
for k = 1 : length(methodsIn)
if (strcmp(methodsIn{k}, tempMethType{j}))
if (isfield(pStruct.CI, methodsIn{k}))
checkMeth(j) = 1;
else
if boolWarning
warning(['You wanted to plot confidence intervals using the method ' methodsIn{k} ...
', but you did not pass the data to do so. This confidence intervals will not be plotted.']);
end
end
end
end
end
iMeth = 0;
for j = 1 : 4
if (checkMeth(j) == 1)
iMeth = iMeth + 1;
methodsOut.type{iMeth} = tempMethType{j};
methodsOut.name{iMeth} = tempMethName{j};
end
end
methodsOut.numLevels = length(pStruct.CI.confLevels);
methodsOut.levels = pStruct.CI.confLevels;
methodsOut.num = length(methodsOut.name);
methodsOut.bars = linspace(0.3, 0.15, methodsOut.numLevels);
colors = nan(methodsOut.num, 3, methodsOut.numLevels);
tempColors = [0.1, 0.1, 0.7; 0.6, 0, 0; 0, 0.45, 0; 0.4, 0.3, 0];
tempColorStep = [0.25, 0.25, 0.1; 0.1, 0.2, 0.2; 0.2, 0.15, 0.2; 0.15, 0.15, 0.2];
colors(:,:,1) = tempColors(1:methodsOut.num,:);
colorStep = tempColorStep(1:methodsOut.num,:);
for j = 2 : methodsOut.numLevels
colors(:,:,j) = min(colors(:,:,j-1) + colorStep, 1);
end
methodsOut.colors = colors;
end