wolffd@0: function [engine, loglik] = enter_evidence(engine, evidence, varargin) wolffd@0: % ENTER_EVIDENCE Add the specified evidence to the network (hmm) wolffd@0: % [engine, loglik] = enter_evidence(engine, evidence, ...) wolffd@0: % wolffd@0: % evidence{i,t} = [] if if X(i,t) 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 (not yet supported), else sum-product [0] wolffd@0: % filter - if 1, does filtering, else smoothing [0] wolffd@0: % oneslice - 1 means only compute marginals on nodes within a single slice [0] wolffd@0: % wolffd@0: % e.g., engine = enter_evidence(engine, ev, 'maximize', 1) wolffd@0: wolffd@0: maximize = 0; wolffd@0: filter = 0; wolffd@0: oneslice = 0; wolffd@0: wolffd@0: % parse optional params wolffd@0: args = varargin; wolffd@0: nargs = length(args); wolffd@0: if nargs > 0 wolffd@0: for i=1:2:nargs wolffd@0: switch args{i}, wolffd@0: case 'maximize', maximize = args{i+1}; wolffd@0: case 'filter', filter = args{i+1}; wolffd@0: case 'oneslice', oneslice = 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: wolffd@0: [ss T] = size(evidence); wolffd@0: engine.maximize = maximize; wolffd@0: engine.evidence = evidence; wolffd@0: bnet = bnet_from_engine(engine); wolffd@0: engine.node_sizes = repmat(bnet.node_sizes_slice(:), [1 T]); wolffd@0: wolffd@0: obs_bitv = ~isemptycell(evidence(:)); wolffd@0: bitv = reshape(obs_bitv, ss, T); wolffd@0: for t=1:T wolffd@0: onodes = find(bitv(:,t)); wolffd@0: if ~isequal(onodes, bnet.observed(:)) wolffd@0: error(['dbn was created assuming observed nodes per slice were '... wolffd@0: num2str(bnet.observed(:)') ' but the evidence in slice ' num2str(t) ... wolffd@0: ' has observed nodes ' num2str(onodes(:)')]); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: obslik = mk_hmm_obs_lik_matrix(engine, evidence); wolffd@0: wolffd@0: %[alpha, beta, gamma, loglik, xi] = fwdback(engine.startprob, engine.transprob, obslik, ... wolffd@0: [alpha, beta, gamma, loglik, xi] = fwdback_twoslice(engine, engine.startprob,... wolffd@0: engine.transprob, obslik, ... wolffd@0: 'maximize', maximize, 'fwd_only', filter, ... wolffd@0: 'compute_xi', ~oneslice); wolffd@0: wolffd@0: engine.one_slice_marginal = gamma; % gamma(:,t) for t=1:T wolffd@0: if ~oneslice wolffd@0: Q = size(gamma,1); wolffd@0: engine.two_slice_marginal = reshape(xi, [Q*Q T-1]); % xi(:,t) for t=1:T-1 wolffd@0: end