wolffd@0: function [converged, decrease] = em_converged(loglik, previous_loglik, threshold, check_increased) wolffd@0: % EM_CONVERGED Has EM converged? wolffd@0: % [converged, decrease] = em_converged(loglik, previous_loglik, threshold) wolffd@0: % wolffd@0: % We have converged if the slope of the log-likelihood function falls below 'threshold', wolffd@0: % i.e., |f(t) - f(t-1)| / avg < threshold, wolffd@0: % where avg = (|f(t)| + |f(t-1)|)/2 and f(t) is log lik at iteration t. wolffd@0: % 'threshold' defaults to 1e-4. wolffd@0: % wolffd@0: % This stopping criterion is from Numerical Recipes in C p423 wolffd@0: % wolffd@0: % If we are doing MAP estimation (using priors), the likelihood can decrase, wolffd@0: % even though the mode of the posterior is increasing. wolffd@0: wolffd@0: if nargin < 3, threshold = 1e-4; end wolffd@0: if nargin < 4, check_increased = 1; end wolffd@0: wolffd@0: converged = 0; wolffd@0: decrease = 0; wolffd@0: wolffd@0: if check_increased wolffd@0: if loglik - previous_loglik < -1e-3 % allow for a little imprecision wolffd@0: fprintf(1, '******likelihood decreased from %6.4f to %6.4f!\n', previous_loglik, loglik); wolffd@0: decrease = 1; wolffd@0: converged = 0; wolffd@0: return; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: delta_loglik = abs(loglik - previous_loglik); wolffd@0: avg_loglik = (abs(loglik) + abs(previous_loglik) + eps)/2; wolffd@0: if (delta_loglik / avg_loglik) < threshold, converged = 1; end