comparison toolboxes/FullBNT-1.0.7/bnt/general/sample_dbn.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function seq = sample_dbn(bnet, varargin)
2 % SAMPLE_DBN Generate a random sequence from a DBN.
3 % seq = sample_dbn(bnet, ...)
4 %
5 % seq{i,t} contains the values of the i'th node in the t'th slice.
6 %
7 % Optional arguments:
8 %
9 % length - length of sequence to be generated (can also just use sample_dbn(bnet,T))
10 % stop_test - name of a function which is used to decide when to stop;
11 % This will be called as feval(stop_test, seq(:,t))
12 % i.e., stop_test is passed a cell array containing all the nodes in the current slice.
13 % evidence - initial evidence; if evidence{i,t} is non-empty, this node won't be sampled.
14
15 args = varargin;
16 nargs = length(args);
17
18 if (nargs == 1) & ~isstr(args{1})
19 % Old syntax: sample_dbn(bnet, T)
20 T = args{1};
21 else
22 % get length
23 T = 1;
24 for i=1:2:nargs
25 switch args{i},
26 case 'length', T = args{i+1};
27 case 'evidence', T = size(args{i+1}, 2);
28 end
29 end
30 end
31
32 ss = length(bnet.intra);
33 % set default arguments
34 seq = cell(ss, T);
35 stop_test = [];
36 for i=1:2:nargs
37 switch args{i},
38 case 'evidence', seq = args{i+1}; % initialise observed nodes
39 case 'stop_test', stop_test = args{i+1};
40 end
41 end
42
43 t = 1;
44 for i=1:ss
45 if ~isempty(stop_test) | isempty(seq{i,t})
46 ps = parents(bnet.dag, i);
47 e = bnet.equiv_class(i,1);
48 pvals = seq(ps);
49 seq{i,t} = sample_node(bnet.CPD{e}, pvals);
50 %fprintf('sample i=%d,t=%d,val=%d,ps\n', i, t, seq(i,t)); pvals(:)'
51 end
52 end
53 t = 2;
54 done = 0;
55 while ~done
56 for i=1:ss
57 if ~isempty(stop_test) | isempty(seq{i,t})
58 ps = parents(bnet.dag, i+ss) + (t-2)*ss;
59 e = bnet.equiv_class(i,2);
60 pvals = seq(ps);
61 seq{i,t} = sample_node(bnet.CPD{e}, pvals);
62 %fprintf('sample i=%d,t=%d,val=%d,ps\n', i, t, seq(i,t)); pvals(:)'
63 end
64 end
65 if ~isempty(stop_test)
66 done = feval(stop_test, seq(:,t));
67 else
68 if t==T
69 done = 1;
70 end
71 end
72 t = t + 1;
73 end