about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old
diff options
context:
space:
mode:
Diffstat (limited to 'sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old')
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Entries5
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Repository1
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Root1
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/collect_evidence.m29
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/distribute_evidence.m26
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/enter_evidence.m107
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/enter_soft_evidence.m19
7 files changed, 188 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Entries
new file mode 100644
index 00000000..5d0e75e3
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Entries
@@ -0,0 +1,5 @@
+/collect_evidence.m/1.1.1.1/Wed May 29 15:59:56 2002//
+/distribute_evidence.m/1.1.1.1/Wed May 29 15:59:56 2002//
+/enter_evidence.m/1.1.1.1/Wed May 29 15:59:56 2002//
+/enter_soft_evidence.m/1.1.1.1/Wed May 29 15:59:56 2002//
+D
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Repository
new file mode 100644
index 00000000..cf59323d
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Repository
@@ -0,0 +1 @@
+FullBNT/BNT/inference/static/@jtree_inf_engine/Old
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Root b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Root
new file mode 100644
index 00000000..f3bd14a6
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/CVS/Root
@@ -0,0 +1 @@
+:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/collect_evidence.m b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/collect_evidence.m
new file mode 100644
index 00000000..2f7757f1
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/collect_evidence.m
@@ -0,0 +1,29 @@
+function engine = collect_evidence(engine, root)
+
+if isempty(engine.postorder{root})
+  % this is the first time we have collected to this root
+  % memoize the order
+  [jtree, preorder, postorder] = mk_rooted_tree(engine.jtree, root);
+  postorder_parents = cell(1,length(postorder));
+  for n=postorder(1:end-1)
+    postorder_parents{n} = parents(jtree, n);
+  end
+  engine.postorder{root} = postorder;
+  engine.postorder_parents{root} = postorder_parents;
+else
+  postorder = engine.postorder{root};
+  postorder_parents = engine.postorder_parents{root};
+end
+
+C = length(engine.clpot);
+seppot = cell(C, C);
+% separators are implicitely initialized to 1s
+
+% collect to root (node to parents)
+for n=postorder(1:end-1)
+  for p=postorder_parents{n}
+    %clpot{p} = divide_by_pot(clpot{n}, seppot{p,n}); % dividing by 1 is redundant
+    engine.seppot{p,n} = marginalize_pot(engine.clpot{n}, engine.separator{p,n}, engine.maximize);
+    engine.clpot{p} = multiply_by_pot(engine.clpot{p}, engine.seppot{p,n});
+  end
+end
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/distribute_evidence.m b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/distribute_evidence.m
new file mode 100644
index 00000000..f8d78be4
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/distribute_evidence.m
@@ -0,0 +1,26 @@
+function engine = distribute_evidence(engine, root)
+
+if isempty(engine.preorder{root})
+  % this is the first time we have distributed from this root
+  % memoize the order
+  [jtree, preorder, postorder] = mk_rooted_tree(engine.jtree, root);
+  preorder_children = cell(1,length(preorder));
+  for n=preorder
+    preorder_children{n} = children(jtree, n);
+  end
+  engine.preorder{root} = preorder;
+  engine.preorder_children{root} = preorder_children;
+else
+  preorder = engine.preorder{root};
+  preorder_children = engine.preorder_children{root};
+end
+
+
+% distribute from root (node to children)
+for n=preorder(:)'
+  for c=preorder_children{n}(:)'
+    engine.clpot{c} = divide_by_pot(engine.clpot{c}, engine.seppot{n,c}); 
+    engine.seppot{n,c} = marginalize_pot(engine.clpot{n}, engine.separator{n,c}, engine.maximize);
+    engine.clpot{c} = multiply_by_pot(engine.clpot{c}, engine.seppot{n,c});
+  end
+end
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/enter_evidence.m b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/enter_evidence.m
new file mode 100644
index 00000000..aafeeecb
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/enter_evidence.m
@@ -0,0 +1,107 @@
+function [engine, loglik] = enter_evidence(engine, evidence, varargin)
+% ENTER_EVIDENCE Add the specified evidence to the network (jtree)
+% [engine, loglik] = enter_evidence(engine, evidence, ...)
+%
+% evidence{i} = [] if X(i) is hidden, and otherwise contains its observed value (scalar or column vector).
+%
+% The following optional arguments can be specified in the form of name/value pairs:
+% [default value in brackets]
+%
+% maximize - if 1, does max-product instead of sum-product [0]
+% soft    - a cell array of soft/virtual evidence;
+%           soft{i} is a prob. distrib. over i's values, or [] [ cell(1,N) ]
+%
+% e.g., engine = enter_evidence(engine, ev, 'soft', soft_ev)
+%
+% For backwards compatibility with BNT2, you can also specify the parameters in the following order
+%  engine = enter_evidence(engine, ev, soft_ev)
+
+bnet = bnet_from_engine(engine);
+ns = bnet.node_sizes(:);
+N = length(bnet.dag);
+
+% set default params
+exclude = [];
+soft_evidence = cell(1,N);
+maximize = 0;
+
+% parse optional params
+args = varargin;
+nargs = length(args);
+if nargs > 0
+  if iscell(args{1})
+    soft_evidence = args{1};
+  else
+    for i=1:2:nargs
+      switch args{i},
+       case 'soft',    soft_evidence = args{i+1}; 
+       case 'maximize', maximize = args{i+1}; 
+       otherwise,  
+	error(['invalid argument name ' args{i}]);       
+      end
+    end
+  end
+end
+
+engine.maximize = maximize;
+
+onodes = find(~isemptycell(evidence));
+hnodes = find(isemptycell(evidence));
+pot_type = determine_pot_type(bnet, onodes);
+ if strcmp(pot_type, 'cg')
+  check_for_cd_arcs(onodes, bnet.cnodes, bnet.dag);
+end
+
+
+hard_nodes = 1:N;
+soft_nodes = find(~isemptycell(soft_evidence));
+S = length(soft_nodes);
+if S > 0
+  assert(pot_type == 'd');
+  assert(mysubset(soft_nodes, bnet.dnodes));
+end
+ 
+% Evaluate CPDs with evidence, and convert to potentials  
+pot = cell(1, N+S);
+for n=1:N
+  fam = family(bnet.dag, n);
+  e = bnet.equiv_class(n);
+  pot{n} = convert_to_pot(bnet.CPD{e}, pot_type, fam(:), evidence);
+end
+
+for i=1:S
+  n = soft_nodes(i);
+  pot{N+i} = dpot(n, ns(n), soft_evidence{n});
+end
+
+%clqs = engine.clq_ass_to_node([hard_nodes soft_nodes]); 
+%[clpot, loglik] = enter_soft_evidence(engine, clqs, pot, onodes, pot_type);
+%engine.clpot = clpot; % save the results for marginal_nodes
+
+
+clique = engine.clq_ass_to_node([hard_nodes soft_nodes]); 
+potential = pot;
+
+
+% Set the clique potentials to all 1s
+C = length(engine.cliques);
+for i=1:C
+  engine.clpot{i} = mk_initial_pot(pot_type, engine.cliques{i}, ns, bnet.cnodes, onodes);
+end
+
+% Multiply on specified potentials
+for i=1:length(clique)
+  c = clique(i);
+  engine.clpot{c} = multiply_by_pot(engine.clpot{c}, potential{i});
+end
+
+root = 1; % arbitrary
+engine = collect_evidence(engine, root);
+engine = distribute_evidence(engine, root);
+
+ll = zeros(1, C);
+for i=1:C
+  [engine.clpot{i}, ll(i)] = normalize_pot(engine.clpot{i});
+end
+loglik = ll(1); % we can extract the likelihood from any clique
+    
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/enter_soft_evidence.m b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/enter_soft_evidence.m
new file mode 100644
index 00000000..59671415
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@jtree_inf_engine/Old/enter_soft_evidence.m
@@ -0,0 +1,19 @@
+function [clpot, loglik] = enter_soft_evidence(engine, clique, potential, onodes, pot_type)
+% ENTER_SOFT_EVIDENCE Add the specified potentials to the network (jtree)
+% [clpot, loglik] = enter_soft_evidence(engine, clique, potential, onodes, pot_type, maximize)
+%
+% We multiply potential{i} onto clique(i) before propagating.
+% We return all the modified clique potentials.
+
+[clpot, seppot] = init_pot(engine, clique, potential, pot_type, onodes);
+[clpot, seppot] = collect_evidence(engine, clpot, seppot);
+[clpot, seppot] = distribute_evidence(engine, clpot, seppot);
+
+C = length(clpot);
+ll = zeros(1, C);
+for i=1:C
+  [clpot{i}, ll(i)] = normalize_pot(clpot{i});
+end
+loglik = ll(1); % we can extract the likelihood from any clique
+
+