about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/inference/static/@likelihood_weighting_inf_engine/enter_evidence.m
blob: 62e252aa0dbf1dc57e0b6f74c7ae21c65116a6d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;