annotate toolboxes/FullBNT-1.0.7/bnt/inference/static/@cond_gauss_inf_engine/enter_evidence.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [engine, loglik] = enter_evidence(engine, evidence, varargin)
wolffd@0 2 % ENTER_EVIDENCE Add the specified evidence to the network (cond_gauss)
wolffd@0 3 % [engine, loglik] = enter_evidence(engine, evidence, ...)
wolffd@0 4 %
wolffd@0 5 % evidence{i} = [] if if X(i) is hidden, and otherwise contains its observed value (scalar or column vector)
wolffd@0 6
wolffd@0 7 bnet = bnet_from_engine(engine);
wolffd@0 8 ns = bnet.node_sizes(:);
wolffd@0 9 observed = ~isemptycell(evidence);
wolffd@0 10 onodes = find(observed);
wolffd@0 11 hnodes = find(isemptycell(evidence));
wolffd@0 12 engine.evidence = evidence;
wolffd@0 13
wolffd@0 14 % check there are no C->D links where C is hidden
wolffd@0 15 pot_type = determine_pot_type(bnet, onodes);
wolffd@0 16
wolffd@0 17 dhid = myintersect(hnodes, bnet.dnodes);
wolffd@0 18 S = prod(ns(dhid));
wolffd@0 19 T = zeros(S,1);
wolffd@0 20
wolffd@0 21 N = length(bnet.dag);
wolffd@0 22 mu = cell(1,N);
wolffd@0 23 Sigma = cell(1,N);
wolffd@0 24 cobs = myintersect(bnet.cnodes, onodes);
wolffd@0 25 chid = myintersect(bnet.cnodes, hnodes);
wolffd@0 26 ens = ns;
wolffd@0 27 ens(cobs) = 0;
wolffd@0 28 for j=chid(:)'
wolffd@0 29 mu{j} = zeros(ens(j), S);
wolffd@0 30 Sigma{j} = zeros(ens(j), ens(j), S);
wolffd@0 31 end
wolffd@0 32
wolffd@0 33 for i=1:S
wolffd@0 34 dvals = ind2subv(ns(dhid), i);
wolffd@0 35 evidence(dhid) = num2cell(dvals);
wolffd@0 36 [sub_engine, loglik] = enter_evidence(engine.sub_engine, evidence);
wolffd@0 37 for j=chid(:)'
wolffd@0 38 m = marginal_nodes(sub_engine, j);
wolffd@0 39 mu{j}(:,i) = m.mu;
wolffd@0 40 Sigma{j}(:,:,i) = m.Sigma;
wolffd@0 41 end
wolffd@0 42 T(i) = exp(loglik);
wolffd@0 43 end
wolffd@0 44
wolffd@0 45 [T, lik] = normalise(T);
wolffd@0 46 loglik = log(lik);
wolffd@0 47
wolffd@0 48 engine.T = T;
wolffd@0 49 engine.mu = mu;
wolffd@0 50 engine.Sigma = Sigma;
wolffd@0 51
wolffd@0 52 dnodes = bnet.dnodes;
wolffd@0 53 dobs = myintersect(dnodes, onodes);
wolffd@0 54 ens(dobs) = 1;
wolffd@0 55 engine.joint_dmarginal = dpot(dnodes, ens(dnodes), myreshape(engine.T, ens(dnodes)));
wolffd@0 56
wolffd@0 57 engine.onodes = onodes;