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