comparison toolboxes/FullBNT-1.0.7/bnt/CPDs/@discrete_CPD/sample_node.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, pvals)
2 % SAMPLE_NODE Draw a random sample from P(Xi | x(pi_i), theta_i) (discrete)
3 % y = sample_node(CPD, parent_evidence)
4 %
5 % parent_evidence{i} is the value of the i'th parent
6
7 if 0
8 n = length(pvals)+1;
9 dom = 1:n;
10 evidence = cell(1,n);
11 evidence(1:n-1) = pvals;
12 T = convert_to_table(CPD, dom, evidence);
13 y = sample_discrete(T);
14 end
15
16
17 CPT = CPD_to_CPT(CPD);
18 sz = mysize(CPT);
19 nparents = length(sz)-1;
20 switch nparents
21 case 0, T = CPT;
22 case 1, T = CPT(pvals{1}, :);
23 case 2, T = CPT(pvals{1}, pvals{2}, :);
24 case 3, T = CPT(pvals{1}, pvals{2}, pvals{3}, :);
25 case 4, T = CPT(pvals{1}, pvals{2}, pvals{3}, pvals{4}, :);
26 otherwise,
27 pvals = cat(1, pvals{:});
28 psz = sz(1:end-1);
29 ssz = sz(end);
30 i = subv2ind(psz, pvals(:)');
31 T = reshape(CPT, [prod(psz) ssz]);
32 T = T(i,:);
33 end
34 y = sample_discrete(T);