about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/learning/bayes_update_params.m
diff options
context:
space:
mode:
Diffstat (limited to 'sourcecodes/bnt-master/BNT/learning/bayes_update_params.m')
-rw-r--r--sourcecodes/bnt-master/BNT/learning/bayes_update_params.m38
1 files changed, 38 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/learning/bayes_update_params.m b/sourcecodes/bnt-master/BNT/learning/bayes_update_params.m
new file mode 100644
index 00000000..4a0a28f4
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/learning/bayes_update_params.m
@@ -0,0 +1,38 @@
+function bnet = bayes_update_params(bnet, cases, clamped)
+% BAYES_UPDATE_PARAMS Bayesian parameter updating given completely observed data
+% bnet = bayes_update_params(bnet, cases, clamped)
+%
+% If there is a missing data, you must use EM.
+% cases(i,m) is the value assigned to node i in case m (this can also be a cell array).
+% clamped(i,m) = 1 if node i was set by intervention in case m (default: clamped = zeros).
+% Clamped nodes are not updated.
+% If there is a single case, clamped is a list of the clamped nodes, not a bit vector.
+
+
+%if iscell(cases), usecell = 1; else usecell = 0; end
+
+n = length(bnet.dag);
+ncases = size(cases, 2);
+if n ~= size(cases, 1)
+  error('data must be of size nnodes * ncases');
+end
+
+if ncases == 1 % clamped is a list of nodes
+  if nargin < 3, clamped = []; end
+  clamp_set = clamped;
+  clamped = zeros(n,1);
+  clamped(clamp_set) = 1;
+else % each row of clamped is a bit vector
+  if nargin < 3, clamped = zeros(n,ncases); end
+end
+
+for i=1:n
+  e = bnet.equiv_class(i);
+  if adjustable_CPD(bnet.CPD{e})
+    u = find(clamped(i,:)==0);
+    ps = parents(bnet.dag, i);
+    bnet.CPD{e} = bayes_update_params(bnet.CPD{e}, cases(i,u), cases(ps,u));
+  end
+end
+
+