-
Notifications
You must be signed in to change notification settings - Fork 12
/
getPropertyProfiles.m
726 lines (667 loc) · 28.8 KB
/
getPropertyProfiles.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
function [properties,fh] = getPropertyProfiles(properties, parameters, objective_function, varargin)
% getPropertyProfiles.m calculates the profiles of user-supplied property
% functions, starting from the maximum a posteriori estimate. This
% calculation is done by varying the value of each property function
% respectively, starting from the value of this function at the global
% optimum and by reoptimizing the likelihood/posterior estimate in each
% variational step of the property. The initial guess for the next
% reoptimization point is computed by extrapolation from the previous
% points to ensure a quick optimization.
%
% Note: This function can exploit up to (n_theta + 1) workers when running
% in 'parallel' mode.
%
% USAGE:
% [...] = getPropertyProfiles(properties, parameters, objective_function)
% [...] = getPropertyProfiles(properties, parameters, objective_function, options)
% [parameters, fh] = getPropertyProfiles(...)
%
% % getPropertyProfiles() uses the following PestoOptions members:
% * PestoOptions::boundary
% * PestoOptions::calc_profiles
% * PestoOptions::comp_type
% * PestoOptions::dJ
% * PestoOptions::dR_max
% * PestoOptions::fh
% * PestoOptions::fmincon
% * PestoOptions::foldername
% * PestoOptions::MAP_index
% * PestoOptions::mode
% * PestoOptions::obj_type
% * PestoOptions::options_getNextPoint .guess .min .max .update .mode
% * PestoOptions::plot_options
% * PestoOptions::property_index
% * PestoOptions::R_min
% * PestoOptions::save
%
% Parameters:
% properties: property struct
% parameters: parameter struct
% objective_function: objective function to be optimized.
% This function should accept one input, the parameter vector.
% varargin:
% options: A PestoOptions object holding various options for the
% algorithm.
%
% Required fields of properties:
% number: Number of properties
% min: Lower bound for each properties
% max: upper bound for each properties
% name = {'name1', ...}: names of the properties
% function = {'function1', ...}: functions to evaluate property
% values. These functions provide the values of the respective
% properties and the corresponding 1st and 2nd order
% derivatives.
%
% Required fields of parameters:
% number: Number of parameters
% min: Lower bound for each parameter
% max: upper bound for each parameter
% name = {'name1', ...}: names of the parameters
% MS: results of global optimization, obtained using for instance
% the routine 'getMultiStarts.m'. MS has to contain at least
% * par: sorted list n_theta x n_starts of parameter estimates.
% The first entry is assumed to be the best one.
% * logPost: sorted list n_starts x 1 of of log-posterior values
% corresponding to the parameters listed in .par.
% * hessian: Hessian matrix (or approximation) at the optimal point
%
%
% Return values:
% properties: updated property struct
% fh: figure handle
%
% Generated fields of properties:
% P(i): profile for i-th parameter
% * prop: MAPs along profile
% * par: MAPs along profile
% * logPost: maximum log-posterior along profile
% * R: ratio
%
% History:
% * 2012/03/02 Jan Hasenauer
% * 2016/04/10 Daniel Weindl
% * 2016/10/12 Paul Stapor
%% Check and assign inputs
if length(varargin) >= 1
options = handleOptionArgument(varargin{1});
else
options = PestoOptions();
end
% Check and assign options
%TODO
options.plot_options.mark_constraint = false;
options.P.min = parameters.min;
options.P.max = parameters.max;
options.MAP_index = 1;
% Warning if objective function gradient is not available
if isempty(options.profileOptimizationOptions)
options.profileOptimizationOptions = options.localOptimizerOptions;
end
if ~strcmp(options.profileOptimizationOptions.GradObj, 'on')
warning('For efficient and reliable optimization, getPropertyProfiles.m requires gradient information.')
end
%% Initialization and figure generation
fh = [];
switch options.mode
case 'visual'
if (isempty(options.fh) || ~isvalid(options.fh))
fh = figure('Name','getPropertyProfiles');
else
fh = figure(options.fh);
end
case 'text'
fprintf(' \nProfile likelihood caculation:\n===============================\n');
case 'silent' % no output
% Force fmincon to be silent.
options.profileOptimizationOptions.Display = 'off';
end
% Check, if MultiStart was launched before
if(~isfield(parameters, 'MS'))
error('No information from multi-start local optimization available. Please run getMultiStarts() before getParameterProfiles.');
end
% Check and assign options
options.P.min = properties.min;
options.P.max = properties.max;
if isempty(options.property_index)
options.property_index = 1:properties.number;
end
if (isempty(options.MAP_index))
options.MAP_index = 1;
end
options.profileOptimizationOptions.algorithm = 'interior-point';
options.profileOptimizationOptions.MaxIter = 400;
options.profileOptimizationOptions.TolCon = 1e-4;
options.profileOptimizationOptions.MaxFunEvals = 200*parameters.number;
%% Initialization of property struct
for i = options.property_index
properties.P(i).prop = properties.MS.prop(i,options.MAP_index);
properties.P(i).par = properties.MS.par(:,options.MAP_index);
properties.P(i).logPost = properties.MS.logPost(options.MAP_index);
properties.P(i).R = 1;
end
logPost_max = properties.MS.logPost(1);
%% Preperation of folder
if options.save
[~,~,~] = mkdir(options.foldername);
save([options.foldername '/init'],'properties');
end
%% Profile calculation -- SEQUENTIAL
if strcmp(options.comp_type,'sequential') && options.calc_profiles
% Profile calculation
for i = options.property_index
% Initialization
P_prop = properties.MS.prop(i,options.MAP_index);
P_par = parameters.MS.par(:,options.MAP_index);
P_logPost = parameters.MS.logPost(options.MAP_index);
P_R = exp(parameters.MS.logPost(options.MAP_index)-parameters.MS.logPost(1));
if isfield(parameters.MS,'exitflag')
P_exitflag = parameters.MS.exitflag(options.MAP_index);
else
P_exitflag = NaN;
end
if ((P_prop <= properties.min(i)) || (properties.max(i) <= P_prop)) && ~strcmp(options.mode,'silent')
warning(['MAP of ' num2str(i) ordstr(i) ' property not between respective minimum and maximum.']);
end
% Compute profile for in- and decreasing property
for s = [-1,1]
% Starting point
prop = properties.MS.prop(i,options.MAP_index);
theta = parameters.MS.par(:,options.MAP_index);
logPost = parameters.MS.logPost(options.MAP_index);
computeProfile = (logPost >= (log(options.R_min) + parameters.MS.logPost(1))) && ...
(prop > (properties.min(i)+options.boundary_tol)) && ...
((properties.max(i)-options.boundary_tol) > prop);
% Sequential update
while computeProfile
% Proposal of next profile point
J_exp = -(log(1-options.dR_max)+options.dJ*(logPost-logPost_max)+logPost);
% Optimization
[theta,prop,exitflag] = ...
fmincon(@(theta) prop_fun(theta,properties.function{i},properties.min(i),properties.max(i),s),...
theta,...
parameters.constraints.A ,parameters.constraints.b ,... % linear inequality constraints
parameters.constraints.Aeq,parameters.constraints.beq,... % linear equality constraints
parameters.min,... % lower bound
parameters.max,... % upper bound
@(theta) obj_con(theta,objective_function,-J_exp,options.obj_type),...
options.profileOptimizationOptions); % options
% Adaptation of signs
if s == +1
prop = -prop;
end
% Reoptimization at boundary
if (prop <= properties.min(i)) || (properties.max(i) <= prop)
[theta,J_opt] = ...
fmincon(@(theta) obj(theta,objective_function,options.obj_type),...
theta,...
parameters.constraints.A ,parameters.constraints.b ,... % linear inequality constraints
parameters.constraints.Aeq,parameters.constraints.beq,... % linear equality constraints
parameters.min,... % lower bound
parameters.max,... % upper bound
@(theta) prop_con_fun(theta,properties.function{i},properties.min(i),properties.max(i),s),...
options.profileOptimizationOptions); % options
else
J_opt = obj(theta,objective_function,options.obj_type);
end
% Assignment of log-posterior
logPost = -J_opt;
% Sorting
switch s
case -1
P_prop = [prop,P_prop];
P_par = [theta,P_par];
P_logPost = [logPost,P_logPost];
P_R = [exp(logPost - parameters.MS.logPost(1)),P_R];
P_exitflag = [exitflag,P_exitflag];
case +1
P_prop = [P_prop,prop];
P_par = [P_par,theta];
P_logPost = [P_logPost,logPost];
P_R = [P_R,exp(logPost - parameters.MS.logPost(1))];
P_exitflag = [P_exitflag,exitflag];
end
% Assignment
properties.P(i).prop = P_prop;
properties.P(i).par = P_par;
properties.P(i).logPost = P_logPost;
properties.P(i).R = P_R;
properties.P(i).exitflag = P_exitflag;
% Save
if options.save
dlmwrite([options.foldername '/properties_P' num2str(i,'%d') '__prop.csv'],P_prop,'delimiter',',','precision',12);
dlmwrite([options.foldername '/properties_P' num2str(i,'%d') '__par.csv'],P_par,'delimiter',',','precision',12);
dlmwrite([options.foldername '/properties_P' num2str(i,'%d') '__logPost.csv'],P_logPost,'delimiter',',','precision',12);
dlmwrite([options.foldername '/properties_P' num2str(i,'%d') '__R.csv'],P_R,'delimiter',',','precision',12);
dlmwrite([options.foldername '/properties_P' num2str(i,'%d') '__exitflag.csv'],P_exitflag,'delimiter',',','precision',12);
end
% Output
str = [num2str(i,'%d') ordstr(i) ' P: point ' num2str(length(properties.P(i).R)-1,'%d') ', R = ' ...
num2str(exp(- J_opt - properties.MS.logPost(1)),'%.3e')];
switch options.mode
case 'visual', fh = plotPropertyProfiles(properties,'1D',fh,options.property_index,options.plot_options);
case 'text', disp(str);
case 'silent' % no output
end
% Condition for the while-loop
computeProfile = (logPost >= (log(options.R_min) + parameters.MS.logPost(1))) && ...
(prop > (properties.min(i)+options.boundary_tol)) && ...
((properties.max(i)-options.boundary_tol) > prop);
end
end
end
elseif strcmp(options.comp_type,'parallel') && options.calc_profiles
%% Profile calculation -- PARALLEL
% Assignement of profile
P = properties.P;
% Profile calculation
parfor i = options.property_index
% Initialization
P_prop = properties.MS.prop(i,options.MAP_index);
P_par = parameters.MS.par(:,options.MAP_index);
P_logPost = parameters.MS.logPost(options.MAP_index);
P_R = exp(parameters.MS.logPost(options.MAP_index)-parameters.MS.logPost(1));
if isfield(parameters.MS,'exitflag')
P_exitflag = parameters.MS.exitflag(options.MAP_index);
else
P_exitflag = NaN;
end
if ((P_prop <= properties.min(i)) || (properties.max(i) <= P_prop)) && ~strcmp(options.mode,'silent')
warning(['MAP of ' num2str(i) ordstr(i) ' property not between respective minimum and maximum.']);
end
% Compute profile for in- and decreasing property
for s = [-1,1]
% Starting point
prop = properties.MS.prop(i,options.MAP_index);
theta = parameters.MS.par(:,options.MAP_index);
logPost = parameters.MS.logPost(options.MAP_index);
computeProfile = (logPost >= (log(options.R_min) + parameters.MS.logPost(1))) && ...
(prop > (properties.min(i)+options.boundary_tol)) && ...
((properties.max(i)-options.boundary_tol) > prop);
% Sequential update
while computeProfile
% Proposal of next profile point
J_exp = -(log(1-options.dR_max)+options.dJ*(logPost-logPost_max)+logPost);
% Optimization
[theta,prop,exitflag] = ...
fmincon(@(theta) prop_fun(theta,properties.function{i},properties.min(i),properties.max(i),s),...
theta,...
parameters.constraints.A ,parameters.constraints.b ,... % linear inequality constraints
parameters.constraints.Aeq,parameters.constraints.beq,... % linear equality constraints
parameters.min,... % lower bound
parameters.max,... % upper bound
@(theta) obj_con(theta,objective_function,-J_exp,options.obj_type),...
options.profileOptimizationOptions); % options
% Adaptation of signs
if s == +1
prop = -prop;
end
% Reoptimization at boundary
if (prop <= properties.min(i)) || (properties.max(i) <= prop)
[theta,J_opt] = ...
fmincon(@(theta) obj(theta,objective_function,options.obj_type),...
theta,...
parameters.constraints.A ,parameters.constraints.b ,... % linear inequality constraints
parameters.constraints.Aeq,parameters.constraints.beq,... % linear equality constraints
parameters.min,... % lower bound
parameters.max,... % upper bound
@(theta) prop_con_fun(theta,properties.function{i},properties.min(i),properties.max(i),s),...
options.profileOptimizationOptions); % options
else
J_opt = obj(theta,objective_function,options.obj_type);
end
% Assignment of log-posterior
logPost = -J_opt;
% Sorting
switch s
case -1
P_prop = [prop,P_prop];
P_par = [theta,P_par];
P_logPost = [logPost,P_logPost];
P_R = [exp(logPost - parameters.MS.logPost(1)),P_R];
P_exitflag = [exitflag,P_exitflag];
case +1
P_prop = [P_prop,prop];
P_par = [P_par,theta];
P_logPost = [P_logPost,logPost];
P_R = [P_R,exp(logPost - parameters.MS.logPost(1))];
P_exitflag = [P_exitflag,exitflag];
end
% Assignment
P(i).prop = P_prop;
P(i).par = P_par;
P(i).logPost = P_logPost;
P(i).R = P_R;
P(i).exitflag = P_exitflag;
% Save
if options.save
dlmwrite([options.foldername '/property_P' num2str(i,'%d') '__prop.csv'],P_prop,'delimiter',',','precision',12);
dlmwrite([options.foldername '/property_P' num2str(i,'%d') '__par.csv'],P_par,'delimiter',',','precision',12);
dlmwrite([options.foldername '/property_P' num2str(i,'%d') '__logPost.csv'],P_logPost,'delimiter',',','precision',12);
dlmwrite([options.foldername '/property_P' num2str(i,'%d') '__R.csv'],P_R,'delimiter',',','precision',12);
dlmwrite([options.foldername '/property_P' num2str(i,'%d') '__exitflag.csv'],P_exitflag,'delimiter',',','precision',12);
end
% Condition for the while-loop
computeProfile = (logPost >= (log(options.R_min) + parameters.MS.logPost(1))) && ...
(prop > (properties.min(i)+options.boundary_tol)) && ...
((properties.max(i)-options.boundary_tol) > prop);
end
end
end
% Assignment
properties.P = P;
% Output
switch options.mode
case 'visual', fh = plotPropertyProfiles(properties,'1D',fh,options.property_index,options.plot_options);
case 'text' % no output
case 'silent' % no output
end
end
%% Output
switch options.mode
case {'visual','text'}, disp('-> Profile calculation for properties FINISHED.');
case 'silent' % no output
end
end
%% Objetive function interface
% This function is used as interface to the user-provided objective
% function. It adapts the sign and supplies the correct number of outputs.
% Furthermore, it catches errors in the user-supplied objective function.
% theta ... parameter vector
% fun ... user-supplied objective function
% type ... type of user-supplied objective function
function varargout = obj(theta,fun,type)
try
switch nargout
case {0,1}
J = fun(theta);
if isnan(J)
error('J is NaN.')
end
switch type
case 'log-posterior' , varargout = {-J};
case 'negative log-posterior' , varargout = { J};
end
case 2
[J,G] = fun(theta);
if max(isnan([J;G(:)]))
error('J and/or G contain a NaN.')
end
switch type
case 'log-posterior' , varargout = {-J,-G};
case 'negative log-posterior' , varargout = { J, G};
end
case 3
[J,G,H] = fun(theta);
if max(isnan([J;G(:);H(:)]))
error('J, G and/or H contain a NaN.')
end
switch type
case 'log-posterior' , varargout = {-J,-G,-H};
case 'negative log-posterior' , varargout = { J, G, H};
end
end
catch
switch nargout
case {0,1}
varargout = {inf};
case 2
varargout = {inf,zeros(length(theta),1)};
case 3
varargout = {inf,zeros(length(theta),1),zeros(length(theta))};
end
end
end
%% Constrained objetive function interface
% This function is used as interface to the user-provided objective
% function. It adapts the sign and supplies the correct number of outputs.
% Furthermore, it catches errors in the user-supplied objective function.
% theta ... parameter vector
% fun ... user-supplied objective function
% fun_min ... minimum objective function
% type ... type of user-supplied objective function
function varargout = obj_con(theta,fun,fun_min,type)
try
switch nargout
case {0,1}
J = fun(theta);
if isnan(J)
error('J is NaN.')
end
switch type
case 'log-posterior' , varargout = {fun_min-J};
case 'negative log-posterior' , varargout = {fun_min+J};
end
case 2
J = fun(theta);
if isnan(J)
error('J is NaN.')
end
switch type
case 'log-posterior' , varargout = {fun_min-J,[]};
case 'negative log-posterior' , varargout = {fun_min+J,[]};
end
case 3
[J,G] = fun(theta);
if max(isnan([J;G(:)]))
error('J and/or G contain a NaN.')
end
switch type
case 'log-posterior' , varargout = {fun_min-J,[],-G};
case 'negative log-posterior' , varargout = {fun_min+J,[], G};
end
case 4
[J,G] = fun(theta);
if max(isnan([J;G(:)]))
error('J and/or G contain a NaN.')
end
switch type
case 'log-posterior' , varargout = {fun_min-J,[],-G,[]};
case 'negative log-posterior' , varargout = {fun_min+J,[], G,[]};
end
end
catch
switch nargout
case {0,1}
varargout = {inf};
case 2
varargout = {inf,[]};
case 3
varargout = {inf,[],zeros(length(theta),1)};
case 4
varargout = {inf,[],zeros(length(theta),1),[]};
end
end
end
%% Property function interface
% This function is used as interface to the user-provided property
% function. It adapts the sign and supplies the correct number of outputs.
% Furthermore, it catches errors in the user-supplied objective function.
% theta ... parameter vector
% fun ... user-supplied property function
% prop_min ... minumum property value of interest (= profile boundary)
% prop_max ... maximum property value of interest (= profile boundary)
% s ... compute profile for increasing (s = +1) and decreasing (s = -1) property
function varargout = prop_fun(theta,fun,prop_min,prop_max,s)
if s == -1
try
switch nargout
case {0,1}
prop = fun(theta);
if prop < prop_min
prop = prop_min;
end
if isnan(prop)
error('prop is NaN.')
end
varargout = {prop};
case 2
[prop,propG] = fun(theta);
if prop < prop_min
prop = prop_min;
propG = zeros(size(propG));
end
if max(isnan([prop;propG(:)]))
error('prop and/or propG contain a NaN.')
end
varargout = {prop,propG};
case 3
[prop,propG,propH] = fun(theta);
if prop < prop_min
prop = prop_min;
propG = zeros(size(propG));
propH = zeros(size(propH));
end
if max(isnan([prop;propG(:);propH(:)]))
error('prop, propG and/or propH contain a NaN.')
end
varargout = {prop,propG,propH};
end
catch
switch nargout
case {0,1}
varargout = {inf};
case 2
varargout = {inf,zeros(length(theta),1)};
case 3
varargout = {inf,zeros(length(theta),1),zeros(length(theta))};
end
end
elseif s == +1
try
switch nargout
case {0,1}
prop = fun(theta);
if prop > prop_max
prop = prop_max;
end
if isnan(prop)
error('prop is NaN.')
end
varargout = {-prop};
case 2
[prop,propG] = fun(theta);
if prop > prop_max
prop = prop_max;
propG = zeros(size(propG));
end
if max(isnan([prop;propG(:)]))
error('prop and/or propG contain a NaN.')
end
varargout = {-prop,-propG};
case 3
[prop,propG,propH] = fun(theta);
if prop > prop_max
prop = prop_max;
propG = zeros(size(propG));
propH = zeros(size(propH));
end
if max(isnan([prop;propG(:);propH(:)]))
error('prop, propG and/or propH contain a NaN.')
end
varargout = {-prop,-propG,-propH};
end
catch
switch nargout
case {0,1}
varargout = {inf};
case 2
varargout = {inf,zeros(length(theta),1)};
case 3
varargout = {inf,zeros(length(theta),1),zeros(length(theta))};
end
end
end
end
%% Property constraint function interface
% This function is used as interface to the user-provided property
% function. It adapts the sign and supplies the correct number of outputs.
% Furthermore, it catches errors in the user-supplied objective function.
% theta ... parameter vector
% fun ... user-supplied property function
% prop_min ... minumum property value of interest (= profile boundary)
% prop_max ... maximum property value of interest (= profile boundary)
% s ... compute profile for increasing (s = +1) and decreasing (s = -1) property
function varargout = prop_con_fun(theta,fun,prop_min,prop_max,s)
if s == -1
try
switch nargout
case {0,1}
prop = fun(theta);
if isnan(prop)
error('prop is NaN.')
end
varargout = {prop-prop_min};
case 2
prop = fun(theta);
if isnan(prop)
error('prop is NaN.')
end
varargout = {prop-prop_min,[]};
case 3
[prop,propG] = fun(theta);
if max(isnan([prop;propG(:)]))
error('prop and/or propG contain a NaN.')
end
varargout = {prop-prop_min,[],propG};
case 4
[prop,propG] = fun(theta);
if max(isnan([prop;propG(:)]))
error('prop and/or propG contain a NaN.')
end
varargout = {prop-prop_min,[],propG,[]};
end
catch
switch nargout
case {0,1}
varargout = {inf};
case 2
varargout = {inf,[]};
case 3
varargout = {inf,[],zeros(length(theta),1)};
case 4
varargout = {inf,[],zeros(length(theta),1),[]};
end
end
elseif s == +1
try
switch nargout
case {0,1}
prop = fun(theta);
if isnan(prop)
error('prop is NaN.')
end
varargout = {prop_max-prop};
case 2
prop = fun(theta);
if isnan(prop)
error('prop is NaN.')
end
varargout = {prop_max-prop,[]};
case 3
[prop,propG] = fun(theta);
if max(isnan([prop;propG(:)]))
error('prop and/or propG contain a NaN.')
end
varargout = {prop_max-prop,[],-propG};
case 4
[prop,propG] = fun(theta);
if max(isnan([prop;propG(:)]))
error('prop and/or propG contain a NaN.')
end
varargout = {prop_max-prop,[],-propG,[]};
end
catch
switch nargout
case {0,1}
varargout = {inf};
case 2
varargout = {inf,[]};
case 3
varargout = {inf,[],zeros(length(theta),1)};
case 4
varargout = {inf,[],zeros(length(theta),1),[]};
end
end
end
end