annotate toolboxes/FullBNT-1.0.7/bnt/general/sample_bnet.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function sample = sample_bnet(bnet, varargin)
Daniel@0 2 % SAMPLE_BNET Generate a random sample from a Bayes net.
Daniel@0 3 % SAMPLE = SAMPLE_BNET(BNET, ...)
Daniel@0 4 %
Daniel@0 5 % sample{i} contains the value of the i'th node.
Daniel@0 6 % i.e., the result is an Nx1 cell array.
Daniel@0 7 % Nodes are sampled in the order given by bnet.order.
Daniel@0 8 %
Daniel@0 9 % Optional arguments:
Daniel@0 10 %
Daniel@0 11 % evidence - initial evidence; if evidence{i} is non-empty, node i won't be sampled.
Daniel@0 12
Daniel@0 13 % set defauly params
Daniel@0 14 n = length(bnet.dag);
Daniel@0 15 sample = cell(n,1);
Daniel@0 16
Daniel@0 17 % get optional params
Daniel@0 18 args = varargin;
Daniel@0 19 nargs = length(args);
Daniel@0 20 for i=1:2:nargs
Daniel@0 21 switch args{i},
Daniel@0 22 case 'evidence', sample = args{i+1}(:);
Daniel@0 23 otherwise, error(['unrecognized argument ' args{i}])
Daniel@0 24 end
Daniel@0 25 end
Daniel@0 26
Daniel@0 27 for j=bnet.order(:)'
Daniel@0 28 if isempty(sample{j})
Daniel@0 29 %ps = parents(bnet.dag, j);
Daniel@0 30 ps = bnet.parents{j};
Daniel@0 31 e = bnet.equiv_class(j);
Daniel@0 32 sample{j} = sample_node(bnet.CPD{e}, sample(ps));
Daniel@0 33 end
Daniel@0 34 end