wolffd@0: function [engine, loglik] = enter_evidence(engine, evidence, varargin) wolffd@0: % ENTER_EVIDENCE Add the specified evidence to the network (jtree) wolffd@0: % [engine, loglik] = enter_evidence(engine, evidence, ...) wolffd@0: % wolffd@0: % evidence{i} = [] if X(i) is hidden, and otherwise contains its observed value (scalar or column vector). wolffd@0: % wolffd@0: % The following optional arguments can be specified in the form of name/value pairs: wolffd@0: % [default value in brackets] wolffd@0: % wolffd@0: % maximize - if 1, does max-product instead of sum-product [0] wolffd@0: % soft - a cell array of soft/virtual evidence; wolffd@0: % soft{i} is a prob. distrib. over i's values, or [] [ cell(1,N) ] wolffd@0: % wolffd@0: % e.g., engine = enter_evidence(engine, ev, 'soft', soft_ev) wolffd@0: % wolffd@0: % For backwards compatibility with BNT2, you can also specify the parameters in the following order wolffd@0: % engine = enter_evidence(engine, ev, soft_ev) wolffd@0: wolffd@0: bnet = bnet_from_engine(engine); wolffd@0: ns = bnet.node_sizes(:); wolffd@0: N = length(bnet.dag); wolffd@0: wolffd@0: % set default params wolffd@0: exclude = []; wolffd@0: soft_evidence = cell(1,N); wolffd@0: maximize = 0; wolffd@0: wolffd@0: % parse optional params wolffd@0: args = varargin; wolffd@0: nargs = length(args); wolffd@0: if nargs > 0 wolffd@0: if iscell(args{1}) wolffd@0: soft_evidence = args{1}; wolffd@0: else wolffd@0: for i=1:2:nargs wolffd@0: switch args{i}, wolffd@0: case 'soft', soft_evidence = args{i+1}; wolffd@0: case 'maximize', maximize = args{i+1}; wolffd@0: otherwise, wolffd@0: error(['invalid argument name ' args{i}]); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: engine.maximize = maximize; wolffd@0: wolffd@0: onodes = find(~isemptycell(evidence)); wolffd@0: hnodes = find(isemptycell(evidence)); wolffd@0: pot_type = determine_pot_type(bnet, onodes); wolffd@0: if strcmp(pot_type, 'cg') wolffd@0: check_for_cd_arcs(onodes, bnet.cnodes, bnet.dag); wolffd@0: end wolffd@0: wolffd@0: wolffd@0: hard_nodes = 1:N; wolffd@0: soft_nodes = find(~isemptycell(soft_evidence)); wolffd@0: S = length(soft_nodes); wolffd@0: if S > 0 wolffd@0: assert(pot_type == 'd'); wolffd@0: assert(mysubset(soft_nodes, bnet.dnodes)); wolffd@0: end wolffd@0: wolffd@0: % Evaluate CPDs with evidence, and convert to potentials wolffd@0: pot = cell(1, N+S); wolffd@0: for n=1:N wolffd@0: fam = family(bnet.dag, n); wolffd@0: e = bnet.equiv_class(n); wolffd@0: pot{n} = convert_to_pot(bnet.CPD{e}, pot_type, fam(:), evidence); wolffd@0: end wolffd@0: wolffd@0: for i=1:S wolffd@0: n = soft_nodes(i); wolffd@0: pot{N+i} = dpot(n, ns(n), soft_evidence{n}); wolffd@0: end wolffd@0: wolffd@0: %clqs = engine.clq_ass_to_node([hard_nodes soft_nodes]); wolffd@0: %[clpot, loglik] = enter_soft_evidence(engine, clqs, pot, onodes, pot_type); wolffd@0: %engine.clpot = clpot; % save the results for marginal_nodes wolffd@0: wolffd@0: wolffd@0: clique = engine.clq_ass_to_node([hard_nodes soft_nodes]); wolffd@0: potential = pot; wolffd@0: wolffd@0: wolffd@0: % Set the clique potentials to all 1s wolffd@0: C = length(engine.cliques); wolffd@0: for i=1:C wolffd@0: engine.clpot{i} = mk_initial_pot(pot_type, engine.cliques{i}, ns, bnet.cnodes, onodes); wolffd@0: end wolffd@0: wolffd@0: % Multiply on specified potentials wolffd@0: for i=1:length(clique) wolffd@0: c = clique(i); wolffd@0: engine.clpot{c} = multiply_by_pot(engine.clpot{c}, potential{i}); wolffd@0: end wolffd@0: wolffd@0: root = 1; % arbitrary wolffd@0: engine = collect_evidence(engine, root); wolffd@0: engine = distribute_evidence(engine, root); wolffd@0: wolffd@0: ll = zeros(1, C); wolffd@0: for i=1:C wolffd@0: [engine.clpot{i}, ll(i)] = normalize_pot(engine.clpot{i}); wolffd@0: end wolffd@0: loglik = ll(1); % we can extract the likelihood from any clique wolffd@0: