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