annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@tabular_CPD/log_marg_prob_node.m @ 0:cc4b1211e677 tip

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