enter_evidence1.m
来自「Bayes网络工具箱」· M 代码 · 共 136 行
M
136 行
function [engine, loglik] = enter_evidence(engine, evidence, filter)% ENTER_EVIDENCE Add the specified evidence to the network (frontier)% [engine, loglik] = enter_evidence(engine, evidence, filter)%% evidence{i,t} = [] if if X(i,t) is hidden, and otherwise contains its observed value (scalar or column vector)% If filter = 1, we do filtering, otherwise smoothing (default).if nargin < 3, filter = 0; end[ss T] = size(evidence);bnet = bnet_from_engine(engine);ns = repmat(bnet.node_sizes_slice(:), 1, T);eclass = [bnet.equiv_class(:,1) repmat(bnet.equiv_class(:,2), 1, T-1)];onodes = find(~isemptycell(evidence));cnodes = unroll_set(bnet.cnodes(:), ss, T);big_dag = unroll_dbn_topology(bnet.intra1, bnet.intra, bnet.inter, T);pot_type = determine_pot_type(onodes, cnodes, big_dag);ns = ns(:)';cnodes = cnodes(:)';% Convert evidence-specific CPDs to potentialsCPD = cell(ss,T);for t=1:T for i=1:ss fam = family(bnet.dag, i, t); CPD{i,t} = CPD_to_pot(pot_type, bnet.CPD{eclass(i,t)}, fam, ns, cnodes, evidence); endend% FORWARDSOPS = engine.ops;fwd = cell(ss,T);ll = zeros(1,T);S = 2*ss; if 0frontier = cell(T,S);t = 1;s = 1;frontier{t,s} = mk_initial_pot(pot_type, [], ns, cnodes, onodes); % start with empty frontier for s=1:ss frontier{t,s+1} = update(frontier{t,s}, OPS1(s), t, CPD);endfrontier{t,S} = frontier{t,ss+1};for t=2:T frontier{t,1} = update(frontier{t-1,S}, OPS(1), t, CPD); for s=2:S frontier{t,s} = update(frontier{t,s-1}, OPS(s), t, CPD); endendend% start with empty frontier, and add each node one at a timefrontier{1} = mk_initial_pot(pot_type, [], ns, cnodes, onodes);for i=1:ss frontier{i+1} = multiply_pots(frontier{i}, CPD{i,1}); fwd{i} = frontier{i+1};endfc = ss+1;[frontier{fc}, ll(1)] = normalize_pot(frontier{fc});add = OPS>0;rem = OPS<0;nodes = [zeros(S,1) unroll_set(abs(OPS(:)), ss, T-1)];for t=2:T for s=1:S j = nodes(s,t); if add(s) frontier{fc+1} = multiply_pots(frontier{fc}, CPD{j}); fwd{j} = frontier{fc+1}; else frontier{fc+1} = marginalize_pot(frontier{fc}, mysetdiff(domain_pot(frontier{fc}), j)); end fc = fc + 1; end [frontier{fc}, ll(t)] = normalize_pot(frontier{fc});endloglik = sum(ll);if filter engine.fwdback = fwd; return;end% BACKWARDSback = cell(ss,T);OPS = -OPS(end:-1:1);OPS1 = 1:ss; % add all nodes in topological orderOPS1 = -OPS1(end:-1:1);t = T;% start with frontier of all 1sfc = 1;dom = (1:ss) + (t-1)*ss;frontier{fc} = mk_initial_pot(pot_type, dom, ns, cnodes, onodes);for t=T:-1:1 offset = max(0,t-2); if t==1 ops = OPS1; else ops = OPS; end for s=1:length(ops) i = ops(s); if i > 0 j = i + offset*ss; % add: extend domain to include i by multiplying by 1 pot = mk_initial_pot(pot_type, j, ns, cnodes, onodes); frontier{fc+1} = multiply_pots(frontier{fc}, pot); else % remove: multiply in CPT and then marginalize it out j = -i + offset*ss; back{j} = frontier{fc}; frontier{fc+1} = multiply_pots(frontier{fc}, CPD{j}); frontier{fc+1} = marginalize_pot(frontier{fc+1}, mysetdiff(domain_pot(frontier{fc+1}), j)); end fc = fc + 1; end frontier{fc} = normalize_pot(frontier{fc});end% end with empty frontier% COMBINEfor t=1:T for i=1:ss engine.fwdback{i,t} = normalize_pot(multiply_pots(fwd{i,t}, back{i,t})); endend
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?