annotate toolboxes/FullBNT-1.0.7/bnt/inference/static/@jtree_inf_engine/Old/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 (jtree)
wolffd@0 3 % [engine, loglik] = enter_evidence(engine, evidence, ...)
wolffd@0 4 %
wolffd@0 5 % evidence{i} = [] if X(i) is hidden, and otherwise contains its observed value (scalar or column vector).
wolffd@0 6 %
wolffd@0 7 % The following optional arguments can be specified in the form of name/value pairs:
wolffd@0 8 % [default value in brackets]
wolffd@0 9 %
wolffd@0 10 % maximize - if 1, does max-product instead of sum-product [0]
wolffd@0 11 % soft - a cell array of soft/virtual evidence;
wolffd@0 12 % soft{i} is a prob. distrib. over i's values, or [] [ cell(1,N) ]
wolffd@0 13 %
wolffd@0 14 % e.g., engine = enter_evidence(engine, ev, 'soft', soft_ev)
wolffd@0 15 %
wolffd@0 16 % For backwards compatibility with BNT2, you can also specify the parameters in the following order
wolffd@0 17 % engine = enter_evidence(engine, ev, soft_ev)
wolffd@0 18
wolffd@0 19 bnet = bnet_from_engine(engine);
wolffd@0 20 ns = bnet.node_sizes(:);
wolffd@0 21 N = length(bnet.dag);
wolffd@0 22
wolffd@0 23 % set default params
wolffd@0 24 exclude = [];
wolffd@0 25 soft_evidence = cell(1,N);
wolffd@0 26 maximize = 0;
wolffd@0 27
wolffd@0 28 % parse optional params
wolffd@0 29 args = varargin;
wolffd@0 30 nargs = length(args);
wolffd@0 31 if nargs > 0
wolffd@0 32 if iscell(args{1})
wolffd@0 33 soft_evidence = args{1};
wolffd@0 34 else
wolffd@0 35 for i=1:2:nargs
wolffd@0 36 switch args{i},
wolffd@0 37 case 'soft', soft_evidence = args{i+1};
wolffd@0 38 case 'maximize', maximize = args{i+1};
wolffd@0 39 otherwise,
wolffd@0 40 error(['invalid argument name ' args{i}]);
wolffd@0 41 end
wolffd@0 42 end
wolffd@0 43 end
wolffd@0 44 end
wolffd@0 45
wolffd@0 46 engine.maximize = maximize;
wolffd@0 47
wolffd@0 48 onodes = find(~isemptycell(evidence));
wolffd@0 49 hnodes = find(isemptycell(evidence));
wolffd@0 50 pot_type = determine_pot_type(bnet, onodes);
wolffd@0 51 if strcmp(pot_type, 'cg')
wolffd@0 52 check_for_cd_arcs(onodes, bnet.cnodes, bnet.dag);
wolffd@0 53 end
wolffd@0 54
wolffd@0 55
wolffd@0 56 hard_nodes = 1:N;
wolffd@0 57 soft_nodes = find(~isemptycell(soft_evidence));
wolffd@0 58 S = length(soft_nodes);
wolffd@0 59 if S > 0
wolffd@0 60 assert(pot_type == 'd');
wolffd@0 61 assert(mysubset(soft_nodes, bnet.dnodes));
wolffd@0 62 end
wolffd@0 63
wolffd@0 64 % Evaluate CPDs with evidence, and convert to potentials
wolffd@0 65 pot = cell(1, N+S);
wolffd@0 66 for n=1:N
wolffd@0 67 fam = family(bnet.dag, n);
wolffd@0 68 e = bnet.equiv_class(n);
wolffd@0 69 pot{n} = convert_to_pot(bnet.CPD{e}, pot_type, fam(:), evidence);
wolffd@0 70 end
wolffd@0 71
wolffd@0 72 for i=1:S
wolffd@0 73 n = soft_nodes(i);
wolffd@0 74 pot{N+i} = dpot(n, ns(n), soft_evidence{n});
wolffd@0 75 end
wolffd@0 76
wolffd@0 77 %clqs = engine.clq_ass_to_node([hard_nodes soft_nodes]);
wolffd@0 78 %[clpot, loglik] = enter_soft_evidence(engine, clqs, pot, onodes, pot_type);
wolffd@0 79 %engine.clpot = clpot; % save the results for marginal_nodes
wolffd@0 80
wolffd@0 81
wolffd@0 82 clique = engine.clq_ass_to_node([hard_nodes soft_nodes]);
wolffd@0 83 potential = pot;
wolffd@0 84
wolffd@0 85
wolffd@0 86 % Set the clique potentials to all 1s
wolffd@0 87 C = length(engine.cliques);
wolffd@0 88 for i=1:C
wolffd@0 89 engine.clpot{i} = mk_initial_pot(pot_type, engine.cliques{i}, ns, bnet.cnodes, onodes);
wolffd@0 90 end
wolffd@0 91
wolffd@0 92 % Multiply on specified potentials
wolffd@0 93 for i=1:length(clique)
wolffd@0 94 c = clique(i);
wolffd@0 95 engine.clpot{c} = multiply_by_pot(engine.clpot{c}, potential{i});
wolffd@0 96 end
wolffd@0 97
wolffd@0 98 root = 1; % arbitrary
wolffd@0 99 engine = collect_evidence(engine, root);
wolffd@0 100 engine = distribute_evidence(engine, root);
wolffd@0 101
wolffd@0 102 ll = zeros(1, C);
wolffd@0 103 for i=1:C
wolffd@0 104 [engine.clpot{i}, ll(i)] = normalize_pot(engine.clpot{i});
wolffd@0 105 end
wolffd@0 106 loglik = ll(1); % we can extract the likelihood from any clique
wolffd@0 107