Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/CPDs/@tabular_CPD/Old/sample_node_single_case.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 y = sample_node(CPD, pev) | |
2 % SAMPLE_NODE Draw a random sample from P(Xi | x(pi_i), theta_i) (tabular) | |
3 % y = sample_node(CPD, pev) | |
4 % | |
5 % pev{i} is the value of the i'th parent (if any) | |
6 | |
7 %assert(~any(isemptycell(pev))); | |
8 | |
9 %CPT = CPD_to_CPT(CPD); | |
10 %sz = mysize(CPT); | |
11 sz = CPD.sizes; | |
12 nparents = length(sz)-1; | |
13 if nparents > 0 | |
14 pvals = cat(1, pev{:}); | |
15 end | |
16 switch nparents | |
17 case 0, T = CPD.CPT; | |
18 case 1, T = CPD.CPT(pvals(1), :); | |
19 case 2, T = CPD.CPT(pvals(1), pvals(2), :); | |
20 case 3, T = CPD.CPT(pvals(1), pvals(2), pvals(3), :); | |
21 case 4, T = CPD.CPT(pvals(1), pvals(2), pvals(3), pvals(4), :); | |
22 otherwise, | |
23 psz = sz(1:end-1); | |
24 ssz = sz(end); | |
25 i = subv2ind(psz, pvals(:)'); | |
26 T = reshape(CPD.CPT, [prod(psz) ssz]); | |
27 T = T(i,:); | |
28 end | |
29 | |
30 if sz(end)==2 | |
31 r = rand(1,1); | |
32 if r > T(1) | |
33 y = 2; | |
34 else | |
35 y = 1; | |
36 end | |
37 else | |
38 y = sample_discrete(T); | |
39 end |