-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsortMultiStarts.m
88 lines (72 loc) · 2.45 KB
/
sortMultiStarts.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
function [parameters] = sortMultiStarts(parameters)
% sortMultiStarts.m sorts the multi-start results from highest to lowest logarithm of the posterior probability 'logPost'.
%
% USAGE:
% [parameters] = sortMultiStart(parameters)
%
% Parameters:
% parameters: parameters struct
%
% Return values:
% parameters: parameter struct with sorted MS
%
% History:
% * 2014/06/12 Jan Hasenauer
%% Sort
[~,ind] = sort(parameters.MS.logPost,1,'descend');
ind = ind([find(~isnan(parameters.MS.logPost(ind)));...
find( isnan(parameters.MS.logPost(ind)))]);
if(isfield(parameters.MS,'fval_trace'))
min_fval = transpose(min(parameters.MS.fval_trace(:,isnan(parameters.MS.logPost(ind)))));
[~,sort_unfinished] = sort(min_fval,1,'ascend');
ind_unfinished = ind(find( isnan(parameters.MS.logPost(ind))));
ind = [ind(~isnan(parameters.MS.logPost(ind)));...
ind_unfinished(sort_unfinished)];
end
%% Assignment of variables which are always contained in the struct
if isfield(parameters.MS,'par0')
parameters.MS.par0 = parameters.MS.par0(:,ind);
end
if isfield(parameters.MS,'par')
parameters.MS.par = parameters.MS.par(:,ind);
end
if isfield(parameters.MS,'logPost0')
parameters.MS.logPost0 = parameters.MS.logPost0(ind);
end
if isfield(parameters.MS,'logPost')
parameters.MS.logPost = parameters.MS.logPost(ind);
end
if isfield(parameters.MS,'gradient')
parameters.MS.gradient = parameters.MS.gradient(:,ind);
end
if isfield(parameters.MS,'hessian')
parameters.MS.hessian = parameters.MS.hessian(:,:,ind);
end
if isfield(parameters.MS,'n_objfun')
parameters.MS.n_objfun = parameters.MS.n_objfun(ind);
end
if isfield(parameters.MS,'n_iter')
parameters.MS.n_iter = parameters.MS.n_iter(ind);
end
if isfield(parameters.MS,'t_cpu')
parameters.MS.t_cpu = parameters.MS.t_cpu(ind);
end
if isfield(parameters.MS,'exitflag')
parameters.MS.exitflag = parameters.MS.exitflag(ind);
end
if isfield(parameters.MS,'AIC')
parameters.MS.AIC = parameters.MS.AIC(ind);
end
if isfield(parameters.MS,'BIC')
parameters.MS.BIC = parameters.MS.BIC(ind);
end
%% Assignment of variables which are not always contained in the struct
if isfield(parameters.MS,'par_trace')
parameters.MS.par_trace = parameters.MS.par_trace(:,:,ind);
end
if isfield(parameters.MS,'fval_trace')
parameters.MS.fval_trace = parameters.MS.fval_trace(:,ind);
end
if isfield(parameters.MS,'time_trace')
parameters.MS.time_trace = parameters.MS.time_trace(:,ind);
end