about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/KPMtools/em_converged.m
diff options
context:
space:
mode:
authorziejd22017-09-28 15:04:40 -0500
committerziejd22017-09-28 15:04:40 -0500
commit8070dc963753142bb86c4ed698d91fd623ed28e7 (patch)
treed0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/KPMtools/em_converged.m
parent7cc31810d53176e805532b2789955f4eedbce6bb (diff)
downloadBNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning.

I am calling this BNW_1.02. It can be accessed at:
compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/KPMtools/em_converged.m')
-rw-r--r--sourcecodes/bnt-master/KPMtools/em_converged.m32
1 files changed, 32 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/KPMtools/em_converged.m b/sourcecodes/bnt-master/KPMtools/em_converged.m
new file mode 100644
index 00000000..2b2919e2
--- /dev/null
+++ b/sourcecodes/bnt-master/KPMtools/em_converged.m
@@ -0,0 +1,32 @@
+function [converged, decrease] = em_converged(loglik, previous_loglik, threshold, check_increased)
+% EM_CONVERGED Has EM converged?
+% [converged, decrease] = em_converged(loglik, previous_loglik, threshold)
+%
+% We have converged if the slope of the log-likelihood function falls below 'threshold', 
+% i.e., |f(t) - f(t-1)| / avg < threshold,
+% where avg = (|f(t)| + |f(t-1)|)/2 and f(t) is log lik at iteration t.
+% 'threshold' defaults to 1e-4.
+%
+% This stopping criterion is from Numerical Recipes in C p423
+%
+% If we are doing MAP estimation (using priors), the likelihood can decrase,
+% even though the mode of the posterior is increasing.
+
+if nargin < 3, threshold = 1e-4; end
+if nargin < 4, check_increased = 1; end
+
+converged = 0;
+decrease = 0;
+
+if check_increased
+  if loglik - previous_loglik < -1e-3 % allow for a little imprecision
+    fprintf(1, '******likelihood decreased from %6.4f to %6.4f!\n', previous_loglik, loglik);
+    decrease = 1;
+converged = 0;
+return;
+  end
+end
+
+delta_loglik = abs(loglik - previous_loglik);
+avg_loglik = (abs(loglik) + abs(previous_loglik) + eps)/2;
+if (delta_loglik / avg_loglik) < threshold, converged = 1; end