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 / log_marg_prob_node.m @ 8:b5b38998ef3b
History | View | Annotate | Download (1.78 KB)
| 1 |
function L = log_marg_prob_node(CPD, self_ev, pev, usecell) |
|---|---|
| 2 |
% LOG_MARG_PROB_NODE Compute sum_m log P(x(i,m)| x(pi_i,m)) for node i (tabular) |
| 3 |
% L = log_marg_prob_node(CPD, self_ev, pev) |
| 4 |
% |
| 5 |
% This differs from log_prob_node because we integrate out the parameters. |
| 6 |
% self_ev(m) is the evidence on this node in case m. |
| 7 |
% pev(i,m) is the evidence on the i'th parent in case m (if there are any parents). |
| 8 |
% (These may also be cell arrays.) |
| 9 |
|
| 10 |
ncases = length(self_ev); |
| 11 |
sz = CPD.sizes; |
| 12 |
nparents = length(sz)-1; |
| 13 |
assert(ncases == size(pev, 2)); |
| 14 |
|
| 15 |
if nargin < 4 |
| 16 |
%usecell = 0; |
| 17 |
if iscell(self_ev) |
| 18 |
usecell = 1; |
| 19 |
else |
| 20 |
usecell = 0; |
| 21 |
end |
| 22 |
end |
| 23 |
|
| 24 |
|
| 25 |
if ncases==0 |
| 26 |
L = 0; |
| 27 |
return; |
| 28 |
elseif ncases==1 % speedup the sequential learning case |
| 29 |
CPT = CPD.CPT; |
| 30 |
% We assume the CPTs are already set to the mean of the posterior (due to bayes_update_params) |
| 31 |
if usecell |
| 32 |
x = cat(1, pev{:})';
|
| 33 |
y = self_ev{1};
|
| 34 |
else |
| 35 |
%x = pev(:)'; |
| 36 |
x = pev; |
| 37 |
y = self_ev; |
| 38 |
end |
| 39 |
switch nparents |
| 40 |
case 0, p = CPT(y); |
| 41 |
case 1, p = CPT(x(1), y); |
| 42 |
case 2, p = CPT(x(1), x(2), y); |
| 43 |
case 3, p = CPT(x(1), x(2), x(3), y); |
| 44 |
otherwise, |
| 45 |
ind = subv2ind(sz, [x y]); |
| 46 |
p = CPT(ind); |
| 47 |
end |
| 48 |
L = log(p); |
| 49 |
else |
| 50 |
% We ignore the CPTs here and assume the prior has not been changed |
| 51 |
|
| 52 |
% We arrange the data as in the following example. |
| 53 |
% Let there be 2 parents and 3 cases. Let p(i,m) be parent i in case m, |
| 54 |
% and y(m) be the child in case m. Then we create the data matrix |
| 55 |
% |
| 56 |
% p(1,1) p(1,2) p(1,3) |
| 57 |
% p(2,1) p(2,2) p(2,3) |
| 58 |
% y(1) y(2) y(3) |
| 59 |
if usecell |
| 60 |
data = [cell2num(pev); cell2num(self_ev)]; |
| 61 |
else |
| 62 |
data = [pev; self_ev]; |
| 63 |
end |
| 64 |
%S = struct(CPD); fprintf('log marg prob node %d, ps\n', S.self); disp(S.parents)
|
| 65 |
counts = compute_counts(data, sz); |
| 66 |
L = dirichlet_score_family(counts, CPD.dirichlet); |
| 67 |
end |
| 68 |
|
| 69 |
|