To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / _FullBNT / BNT / CPDs / @discrete_CPD / sample_node.m @ 8:b5b38998ef3b

History | View | Annotate | Download (839 Bytes)

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);