annotate toolboxes/FullBNT-1.0.7/KPMtools/em_converged.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [converged, decrease] = em_converged(loglik, previous_loglik, threshold, check_increased)
Daniel@0 2 % EM_CONVERGED Has EM converged?
Daniel@0 3 % [converged, decrease] = em_converged(loglik, previous_loglik, threshold)
Daniel@0 4 %
Daniel@0 5 % We have converged if the slope of the log-likelihood function falls below 'threshold',
Daniel@0 6 % i.e., |f(t) - f(t-1)| / avg < threshold,
Daniel@0 7 % where avg = (|f(t)| + |f(t-1)|)/2 and f(t) is log lik at iteration t.
Daniel@0 8 % 'threshold' defaults to 1e-4.
Daniel@0 9 %
Daniel@0 10 % This stopping criterion is from Numerical Recipes in C p423
Daniel@0 11 %
Daniel@0 12 % If we are doing MAP estimation (using priors), the likelihood can decrase,
Daniel@0 13 % even though the mode of the posterior is increasing.
Daniel@0 14
Daniel@0 15 if nargin < 3, threshold = 1e-4; end
Daniel@0 16 if nargin < 4, check_increased = 1; end
Daniel@0 17
Daniel@0 18 converged = 0;
Daniel@0 19 decrease = 0;
Daniel@0 20
Daniel@0 21 if check_increased
Daniel@0 22 if loglik - previous_loglik < -1e-3 % allow for a little imprecision
Daniel@0 23 fprintf(1, '******likelihood decreased from %6.4f to %6.4f!\n', previous_loglik, loglik);
Daniel@0 24 decrease = 1;
Daniel@0 25 converged = 0;
Daniel@0 26 return;
Daniel@0 27 end
Daniel@0 28 end
Daniel@0 29
Daniel@0 30 delta_loglik = abs(loglik - previous_loglik);
Daniel@0 31 avg_loglik = (abs(loglik) + abs(previous_loglik) + eps)/2;
Daniel@0 32 if (delta_loglik / avg_loglik) < threshold, converged = 1; end