annotate toolboxes/FullBNT-1.0.7/KPMtools/em_converged.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 [converged, decrease] = em_converged(loglik, previous_loglik, threshold, check_increased)
wolffd@0 2 % EM_CONVERGED Has EM converged?
wolffd@0 3 % [converged, decrease] = em_converged(loglik, previous_loglik, threshold)
wolffd@0 4 %
wolffd@0 5 % We have converged if the slope of the log-likelihood function falls below 'threshold',
wolffd@0 6 % i.e., |f(t) - f(t-1)| / avg < threshold,
wolffd@0 7 % where avg = (|f(t)| + |f(t-1)|)/2 and f(t) is log lik at iteration t.
wolffd@0 8 % 'threshold' defaults to 1e-4.
wolffd@0 9 %
wolffd@0 10 % This stopping criterion is from Numerical Recipes in C p423
wolffd@0 11 %
wolffd@0 12 % If we are doing MAP estimation (using priors), the likelihood can decrase,
wolffd@0 13 % even though the mode of the posterior is increasing.
wolffd@0 14
wolffd@0 15 if nargin < 3, threshold = 1e-4; end
wolffd@0 16 if nargin < 4, check_increased = 1; end
wolffd@0 17
wolffd@0 18 converged = 0;
wolffd@0 19 decrease = 0;
wolffd@0 20
wolffd@0 21 if check_increased
wolffd@0 22 if loglik - previous_loglik < -1e-3 % allow for a little imprecision
wolffd@0 23 fprintf(1, '******likelihood decreased from %6.4f to %6.4f!\n', previous_loglik, loglik);
wolffd@0 24 decrease = 1;
wolffd@0 25 converged = 0;
wolffd@0 26 return;
wolffd@0 27 end
wolffd@0 28 end
wolffd@0 29
wolffd@0 30 delta_loglik = abs(loglik - previous_loglik);
wolffd@0 31 avg_loglik = (abs(loglik) + abs(previous_loglik) + eps)/2;
wolffd@0 32 if (delta_loglik / avg_loglik) < threshold, converged = 1; end