about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/examples/static/SCG
diff options
context:
space:
mode:
Diffstat (limited to 'sourcecodes/bnt-master/BNT/examples/static/SCG')
-rw-r--r--sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Entries6
-rw-r--r--sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Repository1
-rw-r--r--sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Root1
-rw-r--r--sourcecodes/bnt-master/BNT/examples/static/SCG/scg1.m77
-rw-r--r--sourcecodes/bnt-master/BNT/examples/static/SCG/scg2.m12
-rw-r--r--sourcecodes/bnt-master/BNT/examples/static/SCG/scg3.m42
-rw-r--r--sourcecodes/bnt-master/BNT/examples/static/SCG/scg_3node.m52
-rw-r--r--sourcecodes/bnt-master/BNT/examples/static/SCG/scg_unstable.m91
8 files changed, 282 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Entries
new file mode 100644
index 00000000..8a722650
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Entries
@@ -0,0 +1,6 @@
+/scg1.m/1.1.1.1/Wed May 29 15:59:54 2002//
+/scg2.m/1.1.1.1/Wed May 29 15:59:54 2002//
+/scg3.m/1.1.1.1/Wed May 29 15:59:54 2002//
+/scg_3node.m/1.1.1.1/Wed May 29 15:59:54 2002//
+/scg_unstable.m/1.1.1.1/Wed May 29 15:59:54 2002//
+D
diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Repository
new file mode 100644
index 00000000..1b551eef
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Repository
@@ -0,0 +1 @@
+FullBNT/BNT/examples/static/SCG
diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Root
new file mode 100644
index 00000000..f3bd14a6
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Root
@@ -0,0 +1 @@
+:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt
diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/scg1.m b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg1.m
new file mode 100644
index 00000000..f504c1fe
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg1.m
@@ -0,0 +1,77 @@
+% Same as cg1, except we call stab_cond_gauss_inf_engine
+
+bnet  = mk_incinerator_bnet;
+
+engines = {};
+engines{end+1} = stab_cond_gauss_inf_engine(bnet);
+engines{end+1} = jtree_inf_engine(bnet);
+engines{end+1} = cond_gauss_inf_engine(bnet);
+nengines = length(engines);
+
+F = 1; W = 2; E = 3; B = 4; C = 5; D = 6; Min = 7; Mout = 8; L = 9;
+n = 9;
+dnodes = [B F W];
+cnodes = mysetdiff(1:n, dnodes);
+
+evidence = cell(1,n); % no evidence
+ll = zeros(1, nengines);
+for e=1:nengines
+  [engines{e}, ll(e)] = enter_evidence(engines{e}, evidence);
+end
+%assert(approxeq(ll(1), ll)))
+ll
+
+% Compare to the results in table on p1107.
+% These results are printed to 3dp in Cowell p150
+
+mu = zeros(1,n);
+sigma = zeros(1,n);
+dprob = zeros(1,n);
+addev = 1;
+tol = 1e-2;
+for e=1:nengines
+  for i=cnodes(:)'
+    m = marginal_nodes(engines{e}, i, addev);
+    mu(i) = m.mu;
+    sigma(i) = sqrt(m.Sigma);
+  end
+  for i=dnodes(:)'
+    m = marginal_nodes(engines{e}, i, addev);
+    dprob(i) = m.T(1);
+  end
+  assert(approxeq(mu([E D C L Min Mout]), [-3.25 3.04 -1.85 1.48 -0.214 2.83], tol))
+  assert(approxeq(sigma([E D C L Min Mout]), [0.709 0.770 0.507 0.631 0.459 0.860], tol))
+  assert(approxeq(dprob([B F W]), [0.85 0.95 0.29], tol))
+  %m = marginal_nodes(engines{e}, bnet.names('E'), addev);
+  %assert(approxeq(m.mu, -3.25, tol))
+  %assert(approxeq(sqrt(m.Sigma), 0.709, tol))
+end
+
+% Add evidence (p 1105, top right)
+evidence = cell(1,n);
+evidence{W} = 1; % industrial
+evidence{L} = 1.1;
+evidence{C} = -0.9;
+
+ll = zeros(1, nengines);
+for e=1:nengines
+  [engines{e}, ll(e)] = enter_evidence(engines{e}, evidence);
+end
+%assert(all(approxeq(ll(1), ll)))
+ll
+
+for e=1:nengines
+  for i=cnodes(:)'
+    m = marginal_nodes(engines{e}, i, addev);
+    mu(i) = m.mu;
+    sigma(i) = sqrt(m.Sigma);
+  end
+  for i=dnodes(:)'
+    m = marginal_nodes(engines{e}, i, addev);
+    dprob(i) = m.T(1);
+  end
+  assert(approxeq(mu([E D C L Min Mout]), [-3.90 3.61 -0.9 1.1 0.5 4.11], tol))
+  assert(approxeq(sigma([E D C L Min Mout]), [0.076 0.326 0 0 0.1 0.344], tol))
+  assert(approxeq(dprob([B F W]), [0.0122 0.9995 1], tol))
+end
+
diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/scg2.m b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg2.m
new file mode 100644
index 00000000..a9779248
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg2.m
@@ -0,0 +1,12 @@
+% Same as cg2, except we call stab_cond_gauss_inf_engine
+
+ns = 2*ones(1,9);
+bnet  = mk_incinerator_bnet(ns);
+
+engines = {};
+engines{end+1} = stab_cond_gauss_inf_engine(bnet);
+engines{end+1} = jtree_inf_engine(bnet);
+engines{end+1} = cond_gauss_inf_engine(bnet);
+nengines = length(engines);
+
+[err, time] = cmp_inference_static(bnet, engines, 'singletons_only', 1);
diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/scg3.m b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg3.m
new file mode 100644
index 00000000..cc35b0a5
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg3.m
@@ -0,0 +1,42 @@
+% Compare various inference engines on the following network (from Jensen (1996) p84 fig 4.17)
+%    1
+%  / | \
+% 2  3  4
+% |  |  |
+% 5  6  7
+%  \/ \/
+%  8   9
+% where all arcs point downwards
+
+N = 9;
+dag = zeros(N,N);
+dag(1,2)=1; dag(1,3)=1; dag(1,4)=1;
+dag(2,5)=1; dag(3,6)=1; dag(4,7)=1;
+dag(5,8)=1; dag(6,8)=1; dag(6,9)=1; dag(7,9) = 1;
+
+gauss = 1;
+if gauss
+  ns = ones(1,N); % scalar nodes
+  ns(1) = 2;
+  ns(9) = 3;
+  dnodes = [];
+else
+  ns = 2*ones(1,N); % binary nodes
+  dnodes = 1:N;
+end
+
+bnet = mk_bnet(dag, ns, 'discrete', dnodes);
+% use random params
+for i=1:N
+  if gauss
+    bnet.CPD{i} = gaussian_CPD(bnet, i);
+  else
+    bnet.CPD{i} = tabular_CPD(bnet, i);
+  end
+end
+
+engines = {};
+engines{1} = jtree_inf_engine(bnet);
+engines{2} = stab_cond_gauss_inf_engine(bnet);
+
+[err, time] = cmp_inference_static(bnet, engines);
diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_3node.m b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_3node.m
new file mode 100644
index 00000000..5f75946a
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_3node.m
@@ -0,0 +1,52 @@
+% This example is from Page.143 of "Probabilistic Networks and Expert Systems",
+% Cowell, Dawid, Lauritzen and Spiegelhalter, 1999, Springer.
+
+X = 1; Y = 2; Z = 3;
+n = 3;
+
+dag = zeros(n);
+dag(X, Y)=1;
+dag(Y, Z)=1;
+
+ns = ones(1, n);
+dnodes = [];
+
+bnet = mk_bnet(dag, ns, dnodes);
+bnet.CPD{X} = gaussian_CPD(bnet, X, 'mean', 0, 'cov', 1);
+bnet.CPD{Y} = gaussian_CPD(bnet, Y, 'mean', 0, 'cov', 1, 'weights', 1);
+bnet.CPD{Z} = gaussian_CPD(bnet, Z, 'mean', 0, 'cov', 1, 'weights', 1);
+
+engines = {};
+engines{end+1} = jtree_inf_engine(bnet);
+engines{end+1} = stab_cond_gauss_inf_engine(bnet);
+nengines = length(engines);
+
+evidence = cell(1,n);
+evidence{Y} = 1.5; 
+
+for e=1:nengines
+  engines{e} = enter_evidence(engines{e}, evidence);
+  margX = marginal_nodes(engines{e}, X);
+  assert(approxeq(margX.mu, 0.75))
+  assert(approxeq(margX.Sigma, 0.5))
+  
+  margZ = marginal_nodes(engines{e}, Z);
+  assert(approxeq(margZ.mu, 1.5))
+  assert(approxeq(margZ.Sigma, 1))
+end
+
+
+evidence = cell(1,n);
+evidence{Z} = 1.5; 
+
+for e=1:nengines
+  engines{e} = enter_evidence(engines{e}, evidence);
+  margX = marginal_nodes(engines{e}, X);
+  assert(approxeq(margX.mu, 1/2))
+  assert(approxeq(margX.Sigma, 2/3))
+  
+  margY = marginal_nodes(engines{e}, Y);
+  assert(approxeq(margY.mu, 1))
+  assert(approxeq(margY.Sigma, 2/3))
+end
+
diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_unstable.m b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_unstable.m
new file mode 100644
index 00000000..6617bfa9
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_unstable.m
@@ -0,0 +1,91 @@
+function scg_unstable()
+
+% the objective of this script is to test if the stable conditonal gaussian
+% inference can handle the numerical instability problem described on
+% page.151 of 'Probabilistic networks and expert system' by Cowell, Dawid, Lauritzen and
+% Spiegelhalter, 1999.
+
+A = 1; Y = 2;
+n = 2;
+
+ns = ones(1, n);
+dnodes = [A];
+cnodes = Y;
+ns = [2 1];
+
+dag = zeros(n);
+dag(A, Y) = 1;
+
+bnet = mk_bnet(dag, ns, dnodes);
+
+bnet.CPD{A} = tabular_CPD(bnet, A, [0.5 0.5]'); 
+bnet.CPD{Y} = gaussian_CPD(bnet, Y, 'mean', [0 1], 'cov', [1e-5 1e-6]);
+
+evidence = cell(1, n);
+
+pot_type = 'cg';
+potYgivenA = convert_to_pot(bnet.CPD{Y}, pot_type, [A Y], evidence);
+potA = convert_to_pot(bnet.CPD{A}, pot_type, A, evidence);
+potYandA = multiply_by_pot(potYgivenA, potA);
+potA2 = marginalize_pot(potYandA, A);
+
+thresh = 1; % 0dp
+
+[g,h,K] = extract_can(potA);
+assert(approxeq(g(:)', [-0.693147 -0.693147], thresh))
+
+
+[g,h,K] = extract_can(potYgivenA);
+assert(approxeq(g(:)', [4.83752 -499994], thresh))
+assert(approxeq(h(:)', [0 1e6]))
+assert(approxeq(K(:)', [1e5 1e6]))
+
+[g,h,K] = extract_can(potYandA);
+assert(approxeq(g(:)', [4.14437 -499995], thresh))
+assert(approxeq(h(:)', [0 1e6]))
+assert(approxeq(K(:)', [1e5 1e6]))
+
+
+[g,h,K] = extract_can(potA2);
+%assert(approxeq(g(:)', [-0.69315 -1]))
+g
+assert(approxeq(g(:)', [-0.69315 -0.69315]))
+
+
+
+if 0
+pot_type = 'scg';
+spotYgivenA = convert_to_pot(bnet.CPD{Y}, pot_type, [A Y], evidence);
+spotA = convert_to_pot(bnet.CPD{A}, pot_type, A, evidence);
+spotYandA = direct_combine_pots(spotYgivenA, spotA); 
+spotA2 = marginalize_pot(spotYandA, A);
+
+spotA=struct(spotA);
+spotA2=struct(spotA2);
+for i=1:2
+  assert(approxeq(spotA2.scgpotc{i}.p, spotA.scgpotc{i}.p))
+  assert(approxeq(spotA2.scgpotc{i}.A, spotA.scgpotc{i}.A))
+  assert(approxeq(spotA2.scgpotc{i}.B, spotA.scgpotc{i}.B))
+  assert(approxeq(spotA2.scgpotc{i}.C, spotA.scgpotc{i}.C))
+end
+
+end
+
+
+%%%%%%%%%%%
+
+function [g,h,K] = extract_can(pot)
+
+pot = struct(pot);
+D = length(pot.can);
+g = zeros(1, D);
+h = zeros(1, D);
+K = zeros(1, D);
+for i=1:D
+  S = struct(pot.can{i});
+  g(i) = S.g;
+  if length(S.h) > 0
+    h(i) = S.h;
+    K(i) = S.K;
+  end
+end