about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine
diff options
context:
space:
mode:
Diffstat (limited to 'sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine')
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Entries4
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Repository1
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Root1
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/enter_evidence.m39
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/likelihood_weighting_inf_engine.m25
-rw-r--r--sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/marginal_nodes.m53
6 files changed, 123 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Entries b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Entries
new file mode 100644
index 00000000..c9482cbd
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Entries
@@ -0,0 +1,4 @@
+/enter_evidence.m/1.1.1.1/Wed May 29 15:59:56 2002//
+/likelihood_weighting_inf_engine.m/1.1.1.1/Wed May 29 15:59:56 2002//
+/marginal_nodes.m/1.1.1.1/Wed May 29 15:59:56 2002//
+D
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Repository b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Repository
new file mode 100644
index 00000000..e39429d7
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Repository
@@ -0,0 +1 @@
+FullBNT/BNT/inference/static/@likelihood_weighting_inf_engine
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Root b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Root
new file mode 100644
index 00000000..f3bd14a6
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/CVS/Root
@@ -0,0 +1 @@
+:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/enter_evidence.m b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/enter_evidence.m
new file mode 100644
index 00000000..62e252aa
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/enter_evidence.m
@@ -0,0 +1,39 @@
+function [engine, ll] = enter_evidence(engine, evidence, nsamples)
+% ENTER_EVIDENCE Add the specified evidence to the network (likelihood_weighting)
+% [engine, ll] = enter_evidence(engine, evidence, nsamples)
+% evidence{i} = [] if if X(i) is hidden, and otherwise contains its observed value (scalar or column vector)
+%
+% If nsamples is not specified, the value specified when the engine was created will be used.
+% ll (log-likelihood) is set to [].
+
+ll = [];
+if nargin < 3, nsamples = engine.nsamples; end
+
+bnet = bnet_from_engine(engine);
+N = length(bnet.dag);
+samples = cell(nsamples, N);
+weights = zeros(1, nsamples);
+
+ns = bnet.node_sizes;
+original_evidence = evidence;
+observed = ~isemptycell(original_evidence);
+for s=1:nsamples
+  evidence = original_evidence(:); % must be a column vector
+  w = 1;
+  for i=1:N
+    ps = parents(bnet.dag, i);
+    e = bnet.equiv_class(i);
+    if observed(i)
+      p = exp(log_prob_node(bnet.CPD{e}, evidence(i), evidence(ps)));
+      w = w * p;
+    else
+      x = sample_node(bnet.CPD{e}, evidence(ps));
+      evidence{i} = x;
+    end
+  end
+  samples(s,:) = evidence;
+  weights(s) = w;
+end                 
+
+engine.samples = samples;
+engine.weights = weights;
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/likelihood_weighting_inf_engine.m b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/likelihood_weighting_inf_engine.m
new file mode 100644
index 00000000..eb1794fa
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/likelihood_weighting_inf_engine.m
@@ -0,0 +1,25 @@
+function engine = likelihood_weighting_inf_engine(bnet, varargin)
+% LIKELIHOOD_WEIGHTING_INF_ENGINE 
+% engine = likelihood_weighting_inf_engine(bnet, ...)
+%
+% Optional arguments [defaults]
+% nsamples - [500]
+
+nsamples = 500;
+
+if nargin >= 2
+  args = varargin;
+  nargs = length(args);
+  for i=1:2:nargs
+    switch args{i},
+     case 'nsamples', nsamples= args{i+1};
+     otherwise,
+      error(['invalid argument name ' args{i}]);
+    end
+  end
+end   
+
+engine.nsamples = nsamples;
+engine.samples = [];
+engine.weights = [];
+engine = class(engine, 'likelihood_weighting_inf_engine', inf_engine(bnet));
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/marginal_nodes.m b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/marginal_nodes.m
new file mode 100644
index 00000000..d00ee606
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/marginal_nodes.m
@@ -0,0 +1,53 @@
+function marginal = marginal_nodes(engine, nodes)
+% MARGINAL_NODES Compute the marginal on the specified query nodes (likelihood_weighting)
+% marginal = marginal_nodes(engine, nodes)
+
+bnet = bnet_from_engine(engine);
+ddom = myintersect(nodes, bnet.dnodes);
+cdom = myintersect(nodes, bnet.cnodes);
+nsamples = size(engine.samples, 1);
+ns = bnet.node_sizes;
+
+%w = normalise(engine.weights);
+w = engine.weights;
+if mysubset(nodes, ddom)
+  T = 0*myones(ns(nodes));
+  P = prod(ns(nodes));
+  indices = ind2subv(ns(nodes), 1:P);
+  samples = reshape(cat(1, engine.samples{:,nodes}), nsamples, length(nodes));
+  for j = 1:P
+    rows = find_rows(samples, indices(j,:));
+    T(j) = sum(w(rows));
+  end
+  T = normalise(T);
+  marginal.T = T;
+elseif subset(nodes, cdom)
+  samples = reshape(cat(1, engine.samples{:,nodes}), nsamples*sum(ns(nodes)), length(nodes));
+  [marginal.mu, marginal.Sigma] =  wstats(samples', normalise(w));
+else
+  error('can''t handle mixed marginals yet');
+end
+
+marginal.domain = nodes;
+
+%%%%%%%%%
+
+function rows = find_rows(M, v)
+% FINDROWS Find rows which are equal to a specified vector
+% rows = findrows(M, v)
+% Each row of M is a sample
+
+temp = abs(M - repmat(v, size(M, 1), 1));
+rows = find(sum(temp,2) == 0);      
+
+%%%%%%%%
+
+function [mu, Sigma] = wstats(X, w)
+
+% Computes the weighted mean and weighted covariance matrix for a given
+% set of observations X(:,i), and a set of normalised weights w(i).
+% Each column of X is a sample.
+
+d = X - repmat(X * w', 1, size(X, 2));
+mu = sum(X .* repmat(w, size(X, 1), 1), 2);
+Sigma = d * diag(w) * d';