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