-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbiips_diagnosis.m
More file actions
163 lines (143 loc) · 5.43 KB
/
biips_diagnosis.m
File metadata and controls
163 lines (143 loc) · 5.43 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
function diagn = biips_diagnosis(samples, varargin)
% BIIPS_DIAGNOSIS Diagnosis of the SMC algorithm.
% diagn = biips_diagnosis(samples, 'Propertyname', propertyvalue, ...)
%
% INPUT
% - samples: structure containing either the output of a SMC algorithm
% as returned by BIIPS_SMC_SAMPLES or the output of a MCMC algorithm
% as returned by BIIPS_PIMH_SAMPLES or BIIPS_PMMH_SAMPLES
% Optional Inputs:
% - type: string containing the characters 'f' (fitering),
% 's' (smoothing) and/or 'b' (backward smoothing).
% Select the corresponding fields of the input to be diagnosed.
% (default = 'fsb').
% - ess_thres : integer. Threshold on the Effective Sample Size (ESS).
% If all the ESS components are over the
% threshold, the diagnostic is 'GOOD', otherwise it is
% 'BAD'. (default = 30).
% - quiet: logical. Disable message display. (default = false)
%
% OUTPUT
% - diagn: structure with the same nested fields as the input
% 'samples' structure. Contains the minimum ESS value.
%
% See also BIIPS_SMC_SAMPLES
%--------------------------------------------------------------------------
% EXAMPLE:
% modelfile = 'hmm.bug';
% type(modelfile);
%
% data = struct('tmax', 10, 'p', [.5; .5], 'logtau_true', log(1), 'logtau', log(1));
% model = biips_model(modelfile, data, 'sample_data', true);
%
% n_part = 100;
%
% [out_smc, lml] = biips_smc_samples(model, {'x', 'c[2:10]'}, n_part, 'type', 'fs', 'rs_thres', .5, 'rs_type', 'stratified');
%
% biips_diagnosis(out_smc);
%
% biips_diagnosis(out_smc.x);
%
% out_smc_c = getfield(out_smc, 'c[2:10]')
% biips_diagnosis(out_smc_c);
%
% biips_diagnosis(out_smc.x.f);
% biips_diagnosis(out_smc.x.s);
%--------------------------------------------------------------------------
% Biips Project - Bayesian Inference with interacting Particle Systems
% Matbiips interface
% Authors: Adrien Todeschini, Marc Fuentes, Fran�ois Caron
% Copyright (C) Inria
% License: GPL-3
% Jan 2014; Last revision: 21-10-2014
%--------------------------------------------------------------------------
%
%% PROCESS AND CHECK INPUTS
%%% Process and check optional arguments
optarg_names = {'type', 'ess_thres', 'quiet'};
optarg_default = {'fsb', 30, false};
optarg_valid = {{'f', 's', 'b', 'fs', 'fb', 'sb', 'fsb'}, [0, intmax],...
{true, false}};
optarg_type = {'char', 'numeric', 'logical'};
[type, ess_thres, quiet] = parsevar(varargin, optarg_names, optarg_type,...
optarg_valid, optarg_default);
if ~isstruct(samples)
error('samples must be a struct')
end
if is_smc_array(samples)
%% S contains only one variable
ess_min = min(samples.ess(:));
valid = (ess_min > ess_thres);
diagn.ess_min = ess_min;
diagn.valid = valid;
if ~quiet
varname = deparse_varname(samples.name, samples.lower, samples.upper);
disp(['* Diagnosis of variable: ' , varname]);
end
if ~quiet
switch (samples.type)
case 'filtering'
name = ' Filtering: ';
case 'smoothing'
name = ' Smoothing: ';
case 'backward_smoothing'
name = ' Backward smoothing: ';
end
if valid
disp([name, 'GOOD']);
else
disp([name, 'POOR'])
disp([' The minimum effective sample size is too low: ', num2str(ess_min)])
disp(' Estimates may be poor for some variables.')
disp(' You should increase the number of particles.')
end
end
elseif has_fsb_fields(samples)
%% S contains only one variable with f,s,b fields
names = fieldnames(samples);
s = getfield(samples, names{1});
if ~quiet
varname = deparse_varname(s.name, s.lower, s.upper);
disp(['* Diagnosis of variable: ' , varname]);
end
diagn = struct();
for i=1:numel(names)
fsb = names{i};
if isempty(strfind(type, fsb))
continue
end
s = getfield(samples, fsb);
ess_min = min(s.ess(:));
valid = (ess_min > ess_thres);
diagn_s.ess_min = ess_min;
diagn_s.valid = valid;
diagn = setfield(diagn, fsb, diagn_s);
if ~quiet
switch (fsb)
case 'f'
name = ' Filtering: ';
case 's'
name = ' Smoothing: ';
case 'b'
name = ' Backward smoothing: ';
end
if valid
disp([name, 'GOOD']);
else
disp([name, 'POOR'])
disp([' The minimum effective sample size is too low: ', num2str(ess_min)])
disp(' Estimates may be poor for some variables.')
disp(' You should increase the number of particles.')
end
end
end
else
%% S contains several variables
varnames = fieldnames(samples);
diagn = cell(size(varnames));
for i=1:numel(varnames)
s = getfield(samples, varnames{i});
diagn{i} = biips_diagnosis(s, varargin{:});
end
diagn = cell2struct_weaknames(diagn, varnames);
end