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