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