wolffd@0: function CPD = maximize_params(CPD, temp) wolffd@0: % MAXIMIZE_PARAMS Set the params of a tabular node to their ML/MAP values. wolffd@0: % CPD = maximize_params(CPD, temp) wolffd@0: wolffd@0: if ~adjustable_CPD(CPD), return; end wolffd@0: wolffd@0: %assert(approxeq(sum(CPD.counts(:)), CPD.nsamples)); % false! wolffd@0: switch CPD.prior_type wolffd@0: case 'none', wolffd@0: counts = reshape(CPD.counts, size(CPD.CPT)); wolffd@0: CPD.CPT = mk_stochastic(counts); wolffd@0: case 'dirichlet', wolffd@0: counts = reshape(CPD.counts, size(CPD.CPT)); wolffd@0: CPD.CPT = mk_stochastic(counts + CPD.dirichlet); wolffd@0: wolffd@0: % case 'entropic', wolffd@0: % % For an HMM, wolffd@0: % % CPT(i,j) = pr(X(t)=j | X(t-1)=i) = transprob(i,j) wolffd@0: % % counts(i,j) = E #(X(t-1)=i, X(t)=j) = exp_num_trans(i,j) wolffd@0: % Z = 1-temp; wolffd@0: % fam_sz = CPD.sizes; wolffd@0: % psz = prod(fam_sz(1:end-1)); wolffd@0: % ssz = fam_sz(end); wolffd@0: % counts = reshape(CPD.counts, psz, ssz); wolffd@0: % CPT = zeros(psz, ssz); wolffd@0: % for i=CPD.entropic_pcases(:)' wolffd@0: % [CPT(i,:), logpost] = entropic_map_estimate(counts(i,:), Z); wolffd@0: % end wolffd@0: % non_entropic_pcases = mysetdiff(1:psz, CPD.entropic_pcases); wolffd@0: % for i=non_entropic_pcases(:)' wolffd@0: % CPT(i,:) = mk_stochastic(counts(i,:)); wolffd@0: % end wolffd@0: % %for i=1:psz wolffd@0: % % [CPT(i,:), logpost] = entropic_map(counts(i,:), Z); wolffd@0: % %end wolffd@0: % if CPD.trim & (temp < 2) % at high temps, we would trim everything! wolffd@0: % % grad(j) = d log lik / d theta(i ->j) wolffd@0: % % CPT(i,j) = 0 => counts(i,j) = 0 wolffd@0: % % so we can safely replace 0s by 1s in the denominator wolffd@0: % denom = CPT(i,:) + (CPT(i,:)==0); wolffd@0: % grad = counts(i,:) ./ denom; wolffd@0: % trim = find(CPT(i,:) <= exp(-(1/Z)*grad)); % eqn 32 wolffd@0: % if ~isempty(trim) wolffd@0: % CPT(i,trim) = 0; wolffd@0: % if all(CPD.trimmed_trans(i,trim)==0) % trimming for 1st time wolffd@0: % disp(['trimming CPT(' num2str(i) ',' num2str(trim) ')']) wolffd@0: % end wolffd@0: % CPD.trimmed_trans(i,trim) = 1; wolffd@0: % end wolffd@0: % end wolffd@0: % CPD.CPT = myreshape(CPT, CPD.sizes); wolffd@0: end