annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@tabular_CPD/maximize_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 = maximize_params(CPD, temp)
wolffd@0 2 % MAXIMIZE_PARAMS Set the params of a tabular node to their ML/MAP values.
wolffd@0 3 % CPD = maximize_params(CPD, temp)
wolffd@0 4
wolffd@0 5 if ~adjustable_CPD(CPD), return; end
wolffd@0 6
wolffd@0 7 %assert(approxeq(sum(CPD.counts(:)), CPD.nsamples)); % false!
wolffd@0 8 switch CPD.prior_type
wolffd@0 9 case 'none',
wolffd@0 10 counts = reshape(CPD.counts, size(CPD.CPT));
wolffd@0 11 CPD.CPT = mk_stochastic(counts);
wolffd@0 12 case 'dirichlet',
wolffd@0 13 counts = reshape(CPD.counts, size(CPD.CPT));
wolffd@0 14 CPD.CPT = mk_stochastic(counts + CPD.dirichlet);
wolffd@0 15
wolffd@0 16 % case 'entropic',
wolffd@0 17 % % For an HMM,
wolffd@0 18 % % CPT(i,j) = pr(X(t)=j | X(t-1)=i) = transprob(i,j)
wolffd@0 19 % % counts(i,j) = E #(X(t-1)=i, X(t)=j) = exp_num_trans(i,j)
wolffd@0 20 % Z = 1-temp;
wolffd@0 21 % fam_sz = CPD.sizes;
wolffd@0 22 % psz = prod(fam_sz(1:end-1));
wolffd@0 23 % ssz = fam_sz(end);
wolffd@0 24 % counts = reshape(CPD.counts, psz, ssz);
wolffd@0 25 % CPT = zeros(psz, ssz);
wolffd@0 26 % for i=CPD.entropic_pcases(:)'
wolffd@0 27 % [CPT(i,:), logpost] = entropic_map_estimate(counts(i,:), Z);
wolffd@0 28 % end
wolffd@0 29 % non_entropic_pcases = mysetdiff(1:psz, CPD.entropic_pcases);
wolffd@0 30 % for i=non_entropic_pcases(:)'
wolffd@0 31 % CPT(i,:) = mk_stochastic(counts(i,:));
wolffd@0 32 % end
wolffd@0 33 % %for i=1:psz
wolffd@0 34 % % [CPT(i,:), logpost] = entropic_map(counts(i,:), Z);
wolffd@0 35 % %end
wolffd@0 36 % if CPD.trim & (temp < 2) % at high temps, we would trim everything!
wolffd@0 37 % % grad(j) = d log lik / d theta(i ->j)
wolffd@0 38 % % CPT(i,j) = 0 => counts(i,j) = 0
wolffd@0 39 % % so we can safely replace 0s by 1s in the denominator
wolffd@0 40 % denom = CPT(i,:) + (CPT(i,:)==0);
wolffd@0 41 % grad = counts(i,:) ./ denom;
wolffd@0 42 % trim = find(CPT(i,:) <= exp(-(1/Z)*grad)); % eqn 32
wolffd@0 43 % if ~isempty(trim)
wolffd@0 44 % CPT(i,trim) = 0;
wolffd@0 45 % if all(CPD.trimmed_trans(i,trim)==0) % trimming for 1st time
wolffd@0 46 % disp(['trimming CPT(' num2str(i) ',' num2str(trim) ')'])
wolffd@0 47 % end
wolffd@0 48 % CPD.trimmed_trans(i,trim) = 1;
wolffd@0 49 % end
wolffd@0 50 % end
wolffd@0 51 % CPD.CPT = myreshape(CPT, CPD.sizes);
wolffd@0 52 end