comparison testing/utp_1.1/utps/ao/utp_ao_mcmc.m @ 44:409a22968d5e default

Add unit tests
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Tue, 06 Dec 2011 18:42:11 +0100
parents
children
comparison
equal deleted inserted replaced
43:bc767aaa99a8 44:409a22968d5e
1 % UTP_AO_MCMC a set of UTPs for the ao/mcmc method
2 %
3 % M Nofrarias 06-07-09
4 %
5 % $Id: utp_ao_mcmc.m,v 1.8 2011/11/16 15:21:13 nikos Exp $
6 %
7
8 % <MethodDescription>
9 %
10 % MCMC samples a probability distribution for a using a
11 % Metropolis algorithm.
12 %
13 % </MethodDescription>
14
15 function results = utp_ao_mcmc(varargin)
16
17 % Check the inputs
18 if nargin == 0
19
20 % Some keywords
21 class = 'ao';
22 mthd = 'mcmc';
23
24 results = [];
25 disp('******************************************************');
26 disp(['**** Running UTPs for ' class '/' mthd]);
27 disp('******************************************************');
28
29 % Test AOs
30 n1 = ao(plist('waveform','noise','type','normal','sigma',0.1,'nsecs',100,'fs',1,'yunits','m'));
31 n2 = ao(plist('waveform','noise','type','normal','sigma',0.1,'nsecs',100,'fs',1,'yunits','m'));
32 noise = [n1 ; n2];
33
34 in(1) = ao(plist('fs', 1, 'nsecs', 100, 'tsfcn', 'sin(2*pi*1*t)'));
35 in(2) = ao(plist('fs', 1, 'nsecs', 100, 'tsfcn', 'sin(2*pi*0.1*t)'));
36
37 out(1) = ao(plist('fs', 1, 'nsecs', 100, 'tsfcn', '10 + sin(2*pi*1*t)'));
38 out(2) = ao(plist('fs', 1, 'nsecs', 100, 'tsfcn', '200 + sin(2*pi*0.1*t)'));
39
40 % Test smodels
41 st1 = smodel('a + t');
42 st1.setXvar('f');
43 st1.setParams('a',10);
44 st1.setXvar('t');
45 st1.setXvals(in(1).x);
46
47 st2 = smodel('b + t');
48 st2.setXvar('f');
49 st2.setParams('b',10);
50 st2.setXvar('t');
51 st2.setXvals(in(1).x);
52
53 model = matrix(st1,st2,st1,st2,plist('shape',[2 2]));
54
55 % define plist
56 pl = plist('Fitparams',{'a','b'},...
57 'cov',[1 0; 0 1],...
58 'J',1,...
59 'N',2,...
60 'range',{[5 15],[180 220]},...
61 'x0',[10 200], ...
62 'noise',noise, ...
63 'Tc',[10 20], ...
64 'model',model, ...
65 'search', false, ...
66 'frequencies',[1e-3 1],...
67 'input', [in(1) ; in(2)],...
68 'Navs',5,...
69 'simplex', false);
70
71 % Exception list for the UTPs:
72 [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
73
74 % Run the tests
75 results = [results utp_01]; % getInfo call
76 results = [results utp_02]; % Vector input
77 results = [results utp_03]; % Matrix input
78 results = [results utp_04]; % Test history is working
79 results = [results utp_05]; % Test output of the data
80
81 %
82 disp('Done.');
83 disp('******************************************************');
84
85 elseif nargin == 1 % Check for UTP functions
86 if strcmp(varargin{1}, 'isutp')
87 results = 1;
88 else
89 results = 0;
90 end
91 else
92 error('### Incorrect inputs')
93 end
94
95 %% UTP_01
96
97 % <TestDescription>
98 %
99 % Tests that the getInfo call works for this method.
100 %
101 % </TestDescription>
102 function result = utp_01
103
104 % <SyntaxDescription>
105 %
106 % Test that the getInfo call works for no sets, all sets, and each set
107 % individually.
108 %
109 % </SyntaxDescription>
110
111 % <SyntaxCode>
112 try
113 % Call for no sets
114 io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
115 % Call for all sets
116 io(2) = eval([class '.getInfo(''' mthd ''')']);
117 % Call for each set
118 for kk=1:numel(io(2).sets)
119 io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
120 end
121 stest = true;
122 catch err
123 disp(err.message)
124 stest = false;
125 end
126 % </SyntaxCode>
127
128 % <AlgoDescription>
129 %
130 % 1) Check that getInfo call returned an minfo object in all cases.
131 % 2) Check that all plists have the correct parameters.
132 %
133 % </AlgoDescription>
134
135 % <AlgoCode>
136 atest = true;
137 if stest
138 % check we have minfo objects
139 if isa(io, 'minfo')
140 % SET 'None'
141 if ~isempty(io(1).sets), atest = false; end
142 if ~isempty(io(1).plists), atest = false; end
143 % Check all Sets
144 if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
145 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
146 % SET 'Default'
147 if io(3).plists.nparams ~= 19, atest = false; end
148 % Check key
149 if ~io(3).plists.isparam('N'), atest = false; end
150 if ~io(3).plists.isparam('cov'), atest = false; end
151 if ~io(3).plists.isparam('Fitparams'), atest = false; end
152 if ~io(3).plists.isparam('noise'), atest = false; end
153 if ~io(3).plists.isparam('model'), atest = false; end
154 if ~io(3).plists.isparam('search'), atest = false; end
155 if ~io(3).plists.isparam('simplex'), atest = false; end
156 if ~io(3).plists.isparam('heat'), atest = false; end
157 if ~io(3).plists.isparam('Tc'), atest = false; end
158 if ~io(3).plists.isparam('x0'), atest = false; end
159 if ~io(3).plists.isparam('jumps'), atest = false; end
160 if ~io(3).plists.isparam('plot'), atest = false; end
161 if ~io(3).plists.isparam('debug'), atest = false; end
162 %if ~io(3).plists.isparam('fpars'), atest = false; end
163 % Check default value
164 if ~isequal(io(3).plists.find('N'), 1000), atest = false; end
165 if ~isequal(io(3).plists.find('cov'), 1e-4), atest = false; end
166 if ~isEmptyDouble(io(3).plists.find('Fitparams')), atest = false; end
167 if ~isequal(io(3).plists.find('noise'), ''), atest = false; end
168 if ~isequal(io(3).plists.find('model'), ''), atest = false; end
169 if ~isequal(io(3).plists.find('search'), true), atest = false; end
170 if ~isequal(io(3).plists.find('simplex'), true), atest = false; end
171 if ~isequal(io(3).plists.find('heat'), 1), atest = false; end
172 if ~isequal(io(3).plists.find('Tc'), []), atest = false; end
173 if ~isequal(io(3).plists.find('x0'), []), atest = false; end
174 if ~isequal(io(3).plists.find('jumps'), []), atest = false; end
175 if ~isequal(io(3).plists.find('plot'), []), atest = false; end
176 if ~isequal(io(3).plists.find('debug'), false), atest = false; end
177 %if ~isequal(io(3).plists.find('fpars'), []), atest = false; end
178 % Check options
179 if ~isequal(io(3).plists.getOptionsForParam('N'), {[1000]}), atest = false; end
180 if ~isequal(io(3).plists.getOptionsForParam('cov'), {[1e-4]}), atest = false; end
181 if ~isequal(io(3).plists.getOptionsForParam('Fitparams'), {[]}), atest = false; end
182 if ~isequal(io(3).plists.getOptionsForParam('noise'), {[]}), atest = false; end
183 if ~isequal(io(3).plists.getOptionsForParam('model'), {[]}), atest = false; end
184 if ~isequal(io(3).plists.getOptionsForParam('search'), {true, false}), atest = false; end
185 if ~isequal(io(3).plists.getOptionsForParam('simplex'), {true, false}), atest = false; end
186 if ~isequal(io(3).plists.getOptionsForParam('heat'), {[1]}), atest = false; end
187 if ~isequal(io(3).plists.getOptionsForParam('Tc'), {[]}), atest = false; end
188 if ~isequal(io(3).plists.getOptionsForParam('x0'), {[]}), atest = false; end
189 if ~isequal(io(3).plists.getOptionsForParam('jumps'), {[]}), atest = false; end
190 if ~isequal(io(3).plists.getOptionsForParam('plot'), {[]}), atest = false; end
191 if ~isequal(io(3).plists.getOptionsForParam('debug'), {false, true}), atest = false; end
192 %if ~isequal(io(3).plists.getOptionsForParam('fpars'), {[]}), atest = false; end
193 end
194 else
195 atest = false;
196 end
197 % </AlgoCode>
198
199 % Return a result structure
200 result = utp_prepare_result(atest, stest, dbstack, mfilename);
201 end % END UTP_01
202
203 %% UTP_02
204
205 % <TestDescription>
206 %
207 % Tests that the metropolis2D method works with a vector of AOs as input.
208 %
209 % </TestDescription>
210 function result = utp_02
211
212 % <SyntaxDescription>
213 %
214 % Test that the metropolis2D method works for a vector of AOs as input.
215 %
216 % </SyntaxDescription>
217
218 % <SyntaxCode>
219 try
220 avec = [out(1) ; out(2)];
221 m = mcmc(avec,pl);
222 stest = true;
223 catch err
224 disp(err.message)
225 stest = false;
226 end
227 % </SyntaxCode>
228
229 % <AlgoDescription>
230 %
231 % 1) Check that the number of elements in 'out' is the square of the
232 % number in the input.
233 % 2) Check that each output AO contains the correct data.
234 %
235 % </AlgoDescription>
236
237 % <AlgoCode>
238 atest = true;
239 if stest
240 % Check we have the correct number of outputs
241 if numel(m.names) ~= numel(in), atest = false; end
242 else
243 atest = false;
244 end
245 % </AlgoCode>
246
247 % Return a result structure
248 result = utp_prepare_result(atest, stest, dbstack, mfilename);
249 end % END UTP_02
250
251 %% UTP_03
252
253 % <TestDescription>
254 %
255 % Tests that the metropolis2D method works with a matrix of AOs as input.
256 %
257 % </TestDescription>
258 function result = utp_03
259
260 % <SyntaxDescription>
261 %
262 % Test that the metropolis2D method works for a matrix of AOs as input.
263 %
264 % </SyntaxDescription>
265
266 % <SyntaxCode>
267 try
268 amat = matrix(out(1),out(2),plist('shape',[2 1]));
269 m = mcmc(amat,pl);
270 stest = true;
271 catch err
272 disp(err.message)
273 stest = false;
274 end
275 % </SyntaxCode>
276
277 % <AlgoDescription>
278 %
279 % 1) Check that the number of elements in 'out' is the square of the
280 % number in the input.
281 % 2) Check that each output AO contains the correct data.
282 %
283 % </AlgoDescription>
284
285 % <AlgoCode>
286 atest = true;
287 if stest
288 % Check we have the correct number of outputs
289 if numel(m.names) ~= numel(in), atest = false; end
290 else
291 atest = false;
292 end
293 % </AlgoCode>
294
295 % Return a result structure
296 result = utp_prepare_result(atest, stest, dbstack, mfilename);
297 end % END UTP_03
298
299
300 %% UTP_04
301 % <TestDescription>
302 %
303 % Tests that the metropolis2D method properly applies history.
304 %
305 % </TestDescription>
306 function result = utp_04
307
308 % <SyntaxDescription>
309 %
310 % Test that the result of applying the metropolis2D method can be processed back
311 % to an m-file.
312 %
313 % </SyntaxDescription>
314
315 % <SyntaxCode>
316 try
317 m = mcmc([out(1) ; out(2)],pl);
318 mm = rebuild(m);
319 stest = true;
320 catch err
321 disp(err.message)
322 stest = false;
323 end
324 % </SyntaxCode>
325
326 % <AlgoDescription>
327 %
328 % 1) Check that the last entry in the history of 'm' corresponds to
329 % 'metropolis2D'.
330 % 2) Check that the re-built object is the same object as 'm'.
331 % THIS IS NOT TRUE FOR METROPOLIS BECAUSE THE OUTPUT CHAIN IS ONLY
332 % EQUAL STATISTICALLY
333 %
334 % </AlgoDescription>
335
336 % <AlgoCode>
337 atest = true;
338 if stest
339 % Check the last step in the history of 'out'
340 if ~strcmp(m(1).hist.methodInfo.mname, 'mcmc'), atest = true; end
341 % Check the re-built object
342 % if ~eq(mm(1), m(1), ple2), atest = false; end
343 else
344 atest = false;
345 end
346 % </AlgoCode>
347
348 % Return a result structure
349 result = utp_prepare_result(atest, stest, dbstack, mfilename);
350 end % END UTP_04
351
352 %% UTP_05
353
354 % <TestDescription>
355 %
356 % Tests that the metropolis2D method can not modify the input AO.
357 %
358 % </TestDescription>
359 function result = utp_05
360
361 % <SyntaxDescription>
362 %
363 % Test that the tfe method can not modify the input AO.
364 % The method must throw an error for the modifier call.
365 %
366 % </SyntaxDescription>
367
368 try
369 % <SyntaxCode>
370 % copy at1 to work with
371 ain = ao(in(1));
372 % modify ain
373 ain.mcmc(in,out,pl);
374 % </SyntaxCode>
375 stest = false;
376 catch err
377 stest = true;
378 end
379
380 % <AlgoDescription>
381 %
382 % 1) Nothing to check.
383 %
384 % </AlgoDescription>
385
386 atest = true;
387 if stest
388 % <AlgoCode>
389 % </AlgoCode>
390 else
391 atest = false;
392 end
393
394 % Return a result structure
395 result = utp_prepare_result(atest, stest, dbstack, mfilename);
396 end % END UTP_05
397
398
399
400 end
401