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