Daniel@0: function [marginals, fwd, back, loglik] = smooth_evidence(engine, CPDpot, observed, pot_type) Daniel@0: % [marginals, fwd, back, loglik] = smooth_evidence(engine, CPDpot, observed, pot_type) (ff) Daniel@0: Daniel@0: error('ff smoothing is broken'); Daniel@0: Daniel@0: [ss T] = size(CPDpot); Daniel@0: fwd = cell(ss,T); Daniel@0: hnodes = engine.hnodes(:)'; Daniel@0: onodes = engine.onodes(:)'; Daniel@0: bnet = bnet_from_engine(engine); Daniel@0: ns = bnet.node_sizes; Daniel@0: onodes2 = [onodes onodes+ss]; Daniel@0: ns(onodes2) = 1; Daniel@0: Daniel@0: logscale = zeros(1,T); Daniel@0: H = length(hnodes); Daniel@0: local_logscale = zeros(1,ss); Daniel@0: Daniel@0: t = 1; Daniel@0: for i=hnodes Daniel@0: fwd{i,t} = CPDpot{i,t}; Daniel@0: c = engine.obschild(i); Daniel@0: if 0 % c > 0 Daniel@0: fwd{i,t} = multiply_by_pot(fwd{i,t}, CPDpot{c, t}); Daniel@0: end Daniel@0: [fwd{i,t}, local_logscale(i)] = normalize_pot(fwd{i,t}); Daniel@0: end Daniel@0: logscale(t) = sum(local_logscale); Daniel@0: Daniel@0: for t=2:T Daniel@0: for i=hnodes Daniel@0: ps = parents(bnet.dag, i+ss); Daniel@0: assert(all(ps<=ss)); % in previous slice Daniel@0: prior = CPDpot{i,t}; Daniel@0: for p=ps(:)' Daniel@0: prior = multiply_by_pot(prior, fwd{p,t-1}); Daniel@0: end Daniel@0: fwd{i,t} = marginalize_pot(prior, i+ss); Daniel@0: fwd{i,t} = set_domain_pot(fwd{i,t}, i); Daniel@0: c = engine.obschild(i); Daniel@0: if 0 % c > 0 Daniel@0: fwd{i,t} = multiply_by_pot(fwd{i,t}, CPDpot{c,t}); Daniel@0: end Daniel@0: [fwd{i,t}, local_logscale(i)] = normalize_pot(fwd{i,t}); Daniel@0: end Daniel@0: logscale(t) = sum(local_logscale); Daniel@0: end Daniel@0: Daniel@0: loglik = sum(logscale); Daniel@0: Daniel@0: back = cell(ss,T); Daniel@0: t = T; Daniel@0: for i=hnodes Daniel@0: pot = dpot(i, ns(i)); Daniel@0: cs = children(bnet.intra, i); Daniel@0: for c=cs(:)' Daniel@0: pot = multiply_pots(pot, CPDpot{c,t}); Daniel@0: end Daniel@0: back{i,t} = marginalize_pot(pot, i); Daniel@0: back{i,t} = normalize_pot(back{i,t}); Daniel@0: back{i,t} = set_domain_pot(back{i,t}, i+ss); Daniel@0: end Daniel@0: for t=T-1:-1:1 Daniel@0: for i=hnodes Daniel@0: pot = dpot(i, ns(i)); Daniel@0: cs = children(bnet.inter, i); Daniel@0: for c=cs(:)' Daniel@0: pot = multiply_pots(pot, back{c,t+1}); Daniel@0: pot = multiply_pots(pot, CPDpot{c,t+1}); Daniel@0: end Daniel@0: cs = children(bnet.intra, i); Daniel@0: for c=cs(:)' Daniel@0: pot = multiply_pots(pot, CPDpot{c,t}); Daniel@0: end Daniel@0: back{i,t} = marginalize_pot(pot, i); Daniel@0: back{i,t} = normalize_pot(back{i,t}); Daniel@0: back{i,t} = set_domain_pot(back{i,t}, i+ss); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: Daniel@0: % COMBINE Daniel@0: for t=1:T Daniel@0: for i=hnodes Daniel@0: back{i,t} = set_domain_pot(back{i,t}, i); Daniel@0: fwd{i,t} = multiply_by_pot(fwd{i,t}, back{i,t}); Daniel@0: marginals{i,t} = normalize_pot(fwd{i,t}); Daniel@0: end Daniel@0: end