To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / _FullBNT / BNT / general / sample_bnet.m @ 8:b5b38998ef3b
History | View | Annotate | Download (863 Bytes)
| 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 |