about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/CPDs/@tabular_CPD/log_marg_prob_node.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/BNT/CPDs/@tabular_CPD/log_marg_prob_node.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/BNT/CPDs/@tabular_CPD/log_marg_prob_node.m')
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@tabular_CPD/log_marg_prob_node.m69
1 files changed, 69 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@tabular_CPD/log_marg_prob_node.m b/sourcecodes/bnt-master/BNT/CPDs/@tabular_CPD/log_marg_prob_node.m
new file mode 100644
index 00000000..8a819488
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@tabular_CPD/log_marg_prob_node.m
@@ -0,0 +1,69 @@
+function L = log_marg_prob_node(CPD, self_ev, pev, usecell)
+% LOG_MARG_PROB_NODE Compute sum_m log P(x(i,m)| x(pi_i,m)) for node i (tabular)
+% L = log_marg_prob_node(CPD, self_ev, pev)
+%
+% This differs from log_prob_node because we integrate out the parameters.
+% self_ev(m) is the evidence on this node in case m.
+% pev(i,m) is the evidence on the i'th parent in case m (if there are any parents).
+% (These may also be cell arrays.)
+
+ncases = length(self_ev);
+sz = CPD.sizes;
+nparents = length(sz)-1;
+assert(ncases == size(pev, 2)); 
+
+if nargin < 4
+  %usecell = 0;
+  if iscell(self_ev)
+    usecell = 1;
+  else
+    usecell = 0;
+  end
+end
+
+
+if ncases==0
+  L = 0;
+  return;
+elseif ncases==1  % speedup the sequential learning case
+  CPT = CPD.CPT;
+  % We assume the CPTs are already set to the mean of the posterior (due to bayes_update_params)
+  if usecell
+    x = cat(1, pev{:})';
+    y = self_ev{1};
+  else
+    %x = pev(:)';
+    x = pev;
+    y = self_ev;
+  end
+  switch nparents
+   case 0, p = CPT(y);
+   case 1, p = CPT(x(1), y);
+   case 2, p = CPT(x(1), x(2), y);
+   case 3, p = CPT(x(1), x(2), x(3), y);
+   otherwise,
+    ind = subv2ind(sz, [x y]);
+    p = CPT(ind);
+  end
+  L = log(p);
+else
+  % We ignore the CPTs here and assume the prior has not been changed
+  
+  % We arrange the data as in the following example.
+  % Let there be 2 parents and 3 cases. Let p(i,m) be parent i in case m,
+  % and y(m) be the child in case m. Then we create the data matrix
+  % 
+  % p(1,1) p(1,2) p(1,3)
+  % p(2,1) p(2,2) p(2,3)
+  % y(1)   y(2)   y(3)
+  if usecell
+    data = [cell2num(pev); cell2num(self_ev)]; 
+  else
+    data = [pev; self_ev];
+  end
+  %S = struct(CPD); fprintf('log marg prob node %d, ps\n', S.self); disp(S.parents)
+  counts = compute_counts(data, sz);
+  L = dirichlet_score_family(counts, CPD.dirichlet);
+end
+
+