annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@tabular_CPD/bayes_update_params.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function CPD = bayes_update_params(CPD, self_ev, pev)
wolffd@0 2 % UPDATE_PARAMS_COMPLETE Bayesian parameter updating given completely observed data (tabular)
wolffd@0 3 % CPD = update_params_complete(CPD, self_ev, pev)
wolffd@0 4 %
wolffd@0 5 % self_ev(m) is the evidence on this node in case m.
wolffd@0 6 % pev(i,m) is the evidence on the i'th parent in case m (if there are any parents).
wolffd@0 7 % These can be arrays or cell arrays.
wolffd@0 8 %
wolffd@0 9 % We update the Dirichlet pseudo counts and set the CPT to the mean of the posterior.
wolffd@0 10
wolffd@0 11 if iscell(self_ev), usecell = 1; else usecell = 0; end
wolffd@0 12
wolffd@0 13 ncases = length(self_ev);
wolffd@0 14 sz = CPD.sizes;
wolffd@0 15 nparents = length(sz)-1;
wolffd@0 16 assert(nparents == size(pev,1));
wolffd@0 17
wolffd@0 18 if ncases == 0 | ~adjustable_CPD(CPD)
wolffd@0 19 return;
wolffd@0 20 elseif ncases == 1 % speedup the sequential learning case by avoiding normalization of the whole array
wolffd@0 21 if usecell
wolffd@0 22 x = cat(1, pev{:})';
wolffd@0 23 y = self_ev{1};
wolffd@0 24 else
wolffd@0 25 x = pev(:)';
wolffd@0 26 y = self_ev;
wolffd@0 27 end
wolffd@0 28 switch nparents
wolffd@0 29 case 0,
wolffd@0 30 CPD.dirichlet(y) = CPD.dirichlet(y)+1;
wolffd@0 31 CPD.CPT = CPD.dirichlet / sum(CPD.dirichlet);
wolffd@0 32 case 1,
wolffd@0 33 CPD.dirichlet(x(1), y) = CPD.dirichlet(x(1), y)+1;
wolffd@0 34 CPD.CPT(x(1), :) = CPD.dirichlet(x(1), :) ./ sum(CPD.dirichlet(x(1), :));
wolffd@0 35 case 2,
wolffd@0 36 CPD.dirichlet(x(1), x(2), y) = CPD.dirichlet(x(1), x(2), y)+1;
wolffd@0 37 CPD.CPT(x(1), x(2), :) = CPD.dirichlet(x(1), x(2), :) ./ sum(CPD.dirichlet(x(1), x(2), :));
wolffd@0 38 case 3,
wolffd@0 39 CPD.dirichlet(x(1), x(2), x(3), y) = CPD.dirichlet(x(1), x(2), x(3), y)+1;
wolffd@0 40 CPD.CPT(x(1), x(2), x(3), :) = CPD.dirichlet(x(1), x(2), x(3), :) ./ sum(CPD.dirichlet(x(1), x(2), x(3), :));
wolffd@0 41 otherwise,
wolffd@0 42 ind = subv2ind(sz, [x y]);
wolffd@0 43 CPD.dirichlet(ind) = CPD.dirichlet(ind) + 1;
wolffd@0 44 CPD.CPT = mk_stochastic(CPD.dirichlet);
wolffd@0 45 end
wolffd@0 46 else
wolffd@0 47 if usecell
wolffd@0 48 data = [cell2num(pev); cell2num(self_ev)];
wolffd@0 49 else
wolffd@0 50 data = [pev; self_ev];
wolffd@0 51 end
wolffd@0 52 counts = compute_counts(data, sz);
wolffd@0 53 CPD.dirichlet = CPD.dirichlet + counts;
wolffd@0 54 CPD.CPT = mk_stochastic(CPD.dirichlet);
wolffd@0 55 end