wolffd@0: function CPD = bayes_update_params(CPD, self_ev, pev) wolffd@0: % UPDATE_PARAMS_COMPLETE Bayesian parameter updating given completely observed data (tabular) wolffd@0: % CPD = update_params_complete(CPD, self_ev, pev) wolffd@0: % 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 can be arrays or cell arrays. wolffd@0: % wolffd@0: % We update the Dirichlet pseudo counts and set the CPT to the mean of the posterior. wolffd@0: wolffd@0: if iscell(self_ev), usecell = 1; else usecell = 0; end wolffd@0: wolffd@0: ncases = length(self_ev); wolffd@0: sz = CPD.sizes; wolffd@0: nparents = length(sz)-1; wolffd@0: assert(nparents == size(pev,1)); wolffd@0: wolffd@0: if ncases == 0 | ~adjustable_CPD(CPD) wolffd@0: return; wolffd@0: elseif ncases == 1 % speedup the sequential learning case by avoiding normalization of the whole array 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: y = self_ev; wolffd@0: end wolffd@0: switch nparents wolffd@0: case 0, wolffd@0: CPD.dirichlet(y) = CPD.dirichlet(y)+1; wolffd@0: CPD.CPT = CPD.dirichlet / sum(CPD.dirichlet); wolffd@0: case 1, wolffd@0: CPD.dirichlet(x(1), y) = CPD.dirichlet(x(1), y)+1; wolffd@0: CPD.CPT(x(1), :) = CPD.dirichlet(x(1), :) ./ sum(CPD.dirichlet(x(1), :)); wolffd@0: case 2, wolffd@0: CPD.dirichlet(x(1), x(2), y) = CPD.dirichlet(x(1), x(2), y)+1; wolffd@0: CPD.CPT(x(1), x(2), :) = CPD.dirichlet(x(1), x(2), :) ./ sum(CPD.dirichlet(x(1), x(2), :)); wolffd@0: case 3, wolffd@0: CPD.dirichlet(x(1), x(2), x(3), y) = CPD.dirichlet(x(1), x(2), x(3), y)+1; wolffd@0: 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: otherwise, wolffd@0: ind = subv2ind(sz, [x y]); wolffd@0: CPD.dirichlet(ind) = CPD.dirichlet(ind) + 1; wolffd@0: CPD.CPT = mk_stochastic(CPD.dirichlet); wolffd@0: end wolffd@0: else 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: counts = compute_counts(data, sz); wolffd@0: CPD.dirichlet = CPD.dirichlet + counts; wolffd@0: CPD.CPT = mk_stochastic(CPD.dirichlet); wolffd@0: end