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 / CPDs / @tabular_CPD / Old / sample_node_single_case.m @ 8:b5b38998ef3b
History | View | Annotate | Download (852 Bytes)
| 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 |