wolffd@0: function L = log_marg_prob_node(CPD, self_ev, pev, usecell) wolffd@0: % LOG_MARG_PROB_NODE Compute sum_m log P(x(i,m)| x(pi_i,m)) for node i (tabular) wolffd@0: % L = log_marg_prob_node(CPD, self_ev, pev) wolffd@0: % wolffd@0: % This differs from log_prob_node because we integrate out the parameters. wolffd@0: % self_ev(m) is the evidence on this node in case m. wolffd@0: % pev(i,m) is the evidence on the i'th parent in case m (if there are any parents). wolffd@0: % (These may also be cell arrays.) wolffd@0: wolffd@0: ncases = length(self_ev); wolffd@0: sz = CPD.sizes; wolffd@0: nparents = length(sz)-1; wolffd@0: assert(ncases == size(pev, 2)); wolffd@0: wolffd@0: if nargin < 4 wolffd@0: %usecell = 0; wolffd@0: if iscell(self_ev) wolffd@0: usecell = 1; wolffd@0: else wolffd@0: usecell = 0; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: wolffd@0: if ncases==0 wolffd@0: L = 0; wolffd@0: return; wolffd@0: elseif ncases==1 % speedup the sequential learning case wolffd@0: CPT = CPD.CPT; wolffd@0: % We assume the CPTs are already set to the mean of the posterior (due to bayes_update_params) wolffd@0: if usecell wolffd@0: x = cat(1, pev{:})'; wolffd@0: y = self_ev{1}; wolffd@0: else wolffd@0: %x = pev(:)'; wolffd@0: x = pev; wolffd@0: y = self_ev; wolffd@0: end wolffd@0: switch nparents wolffd@0: case 0, p = CPT(y); wolffd@0: case 1, p = CPT(x(1), y); wolffd@0: case 2, p = CPT(x(1), x(2), y); wolffd@0: case 3, p = CPT(x(1), x(2), x(3), y); wolffd@0: otherwise, wolffd@0: ind = subv2ind(sz, [x y]); wolffd@0: p = CPT(ind); wolffd@0: end wolffd@0: L = log(p); wolffd@0: else wolffd@0: % We ignore the CPTs here and assume the prior has not been changed wolffd@0: wolffd@0: % We arrange the data as in the following example. wolffd@0: % Let there be 2 parents and 3 cases. Let p(i,m) be parent i in case m, wolffd@0: % and y(m) be the child in case m. Then we create the data matrix wolffd@0: % wolffd@0: % p(1,1) p(1,2) p(1,3) wolffd@0: % p(2,1) p(2,2) p(2,3) wolffd@0: % y(1) y(2) y(3) wolffd@0: if usecell wolffd@0: data = [cell2num(pev); cell2num(self_ev)]; wolffd@0: else wolffd@0: data = [pev; self_ev]; wolffd@0: end wolffd@0: %S = struct(CPD); fprintf('log marg prob node %d, ps\n', S.self); disp(S.parents) wolffd@0: counts = compute_counts(data, sz); wolffd@0: L = dirichlet_score_family(counts, CPD.dirichlet); wolffd@0: end wolffd@0: wolffd@0: