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 / @discrete_CPD / prob_node.m @ 8:b5b38998ef3b
History | View | Annotate | Download (1.74 KB)
| 1 |
function [P, p] = prob_node(CPD, self_ev, pev) |
|---|---|
| 2 |
% PROB_NODE Compute prod_m P(x(i,m)| x(pi_i,m), theta_i) for node i (discrete) |
| 3 |
% [P, p] = prob_node(CPD, self_ev, pev) |
| 4 |
% |
| 5 |
% self_ev(m) is the evidence on this node in case m. |
| 6 |
% pev(i,m) is the evidence on the i'th parent in case m (if there are any parents). |
| 7 |
% (These may also be cell arrays.) |
| 8 |
% |
| 9 |
% p(m) = P(x(i,m)| x(pi_i,m), theta_i) |
| 10 |
% P = prod p(m) |
| 11 |
|
| 12 |
if iscell(self_ev), usecell = 1; else usecell = 0; end |
| 13 |
|
| 14 |
ncases = length(self_ev); |
| 15 |
sz = dom_sizes(CPD); |
| 16 |
|
| 17 |
nparents = length(sz)-1; |
| 18 |
if nparents == 0 |
| 19 |
assert(isempty(pev)); |
| 20 |
else |
| 21 |
assert(isequal(size(pev), [nparents ncases])); |
| 22 |
end |
| 23 |
|
| 24 |
n = length(sz); |
| 25 |
dom = 1:n; |
| 26 |
p = zeros(1, ncases); |
| 27 |
if isa(CPD, 'tabular_CPD') |
| 28 |
% speed up by looking up CPT using index Zhang Yimin 2001-12-31 |
| 29 |
if usecell |
| 30 |
if nparents == 0 |
| 31 |
data = [cell2num(self_ev)]; |
| 32 |
else |
| 33 |
data = [cell2num(pev); cell2num(self_ev)]; |
| 34 |
end |
| 35 |
else |
| 36 |
if nparents == 0 |
| 37 |
data = [self_ev]; |
| 38 |
else |
| 39 |
data = [pev; self_ev]; |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
indices = subv2ind(sz, data'); % each row of data' is a case |
| 44 |
|
| 45 |
CPT=CPD_to_CPT(CPD); |
| 46 |
p = CPT(indices); |
| 47 |
|
| 48 |
%get the prob list |
| 49 |
%cpt_size = prod(sz); |
| 50 |
%prob_list=reshape(CPT, cpt_size, 1); |
| 51 |
%for m=1:ncases %here we assume we get evidence for node and all its parents |
| 52 |
% idx=indices(m); |
| 53 |
% p(m)=prob_list(idx); |
| 54 |
%end |
| 55 |
|
| 56 |
else % eg. softmax |
| 57 |
|
| 58 |
for m=1:ncases |
| 59 |
if usecell |
| 60 |
if nparents == 0 |
| 61 |
evidence = {self_ev{m}};
|
| 62 |
else |
| 63 |
evidence = cell(1,n); |
| 64 |
evidence(1:n-1) = pev(:,m); |
| 65 |
evidence(n) = self_ev(m); |
| 66 |
end |
| 67 |
else |
| 68 |
if nparents == 0 |
| 69 |
evidence = num2cell(self_ev(m)); |
| 70 |
else |
| 71 |
evidence = num2cell([pev(:,m)', self_ev(m)]); |
| 72 |
end |
| 73 |
end |
| 74 |
T = convert_to_table(CPD, dom, evidence); |
| 75 |
p(m) = T; |
| 76 |
end |
| 77 |
end |
| 78 |
|
| 79 |
P = prod(p); |
| 80 |
|
| 81 |
|