comparison toolboxes/FullBNT-1.0.7/bnt/general/sample_bnet.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 sample = sample_bnet(bnet, varargin)
2 % SAMPLE_BNET Generate a random sample from a Bayes net.
3 % SAMPLE = SAMPLE_BNET(BNET, ...)
4 %
5 % sample{i} contains the value of the i'th node.
6 % i.e., the result is an Nx1 cell array.
7 % Nodes are sampled in the order given by bnet.order.
8 %
9 % Optional arguments:
10 %
11 % evidence - initial evidence; if evidence{i} is non-empty, node i won't be sampled.
12
13 % set defauly params
14 n = length(bnet.dag);
15 sample = cell(n,1);
16
17 % get optional params
18 args = varargin;
19 nargs = length(args);
20 for i=1:2:nargs
21 switch args{i},
22 case 'evidence', sample = args{i+1}(:);
23 otherwise, error(['unrecognized argument ' args{i}])
24 end
25 end
26
27 for j=bnet.order(:)'
28 if isempty(sample{j})
29 %ps = parents(bnet.dag, j);
30 ps = bnet.parents{j};
31 e = bnet.equiv_class(j);
32 sample{j} = sample_node(bnet.CPD{e}, sample(ps));
33 end
34 end