annotate toolboxes/FullBNT-1.0.7/bnt/inference/dynamic/@bk_ff_hmm_inf_engine/private/dbn_to_hmm.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 [prior, transmat] = dbn_to_hmm(bnet)
wolffd@0 2 % DBN_TO_HMM Compute the discrete HMM matrices from a simple DBN
wolffd@0 3 % [prior, transmat] = dbn_to_hmm(bnet)
wolffd@0 4
wolffd@0 5 onodes = bnet.observed;
wolffd@0 6 ss = length(bnet.intra);
wolffd@0 7 evidence = cell(1,2*ss);
wolffd@0 8 hnodes = mysetdiff(1:ss, onodes);
wolffd@0 9 prior = multiply_CPTs(bnet, [], hnodes, evidence);
wolffd@0 10 transmat = multiply_CPTs(bnet, hnodes, hnodes+ss, evidence);
wolffd@0 11 %obsmat1 = multiply_CPTs(bnet, hnodes, onodes, evidence);
wolffd@0 12 %obsmat = multiply_CPTs(bnet, hnodes+ss, onodes+ss, evidence);
wolffd@0 13 %obsmat1 = obsmat if the observation matrices are tied across slices
wolffd@0 14
wolffd@0 15
wolffd@0 16
wolffd@0 17 %%%%%%%%%%%%
wolffd@0 18
wolffd@0 19 function mat = multiply_CPTs(bnet, pdom, cdom, evidence)
wolffd@0 20
wolffd@0 21 % MULTIPLY_CPTS Make a matrix Pr(Y|X), where X represents all the parents, and Y all the children
wolffd@0 22 % We assume the children have no intra-connections.
wolffd@0 23 %
wolffd@0 24 % e.g., Consider the DBN with interconnectivity i->i', j->j',k', k->i',k'
wolffd@0 25 % Then transition matrix = Pr(i,j,k -> i',j',k') = Pr(i,k->i') Pr(j->j') Pr(j,k->k')
wolffd@0 26
wolffd@0 27 dom = [pdom cdom];
wolffd@0 28 ns = bnet.node_sizes;
wolffd@0 29 bigpot = dpot(dom, ns(dom));
wolffd@0 30 for j=cdom(:)'
wolffd@0 31 e = bnet.equiv_class(j);
wolffd@0 32 fam = family(bnet.dag, j);
wolffd@0 33 pot = convert_to_pot(bnet.CPD{e}, 'd', fam(:), evidence);
wolffd@0 34 bigpot = multiply_by_pot(bigpot, pot);
wolffd@0 35 end
wolffd@0 36 psize = prod(ns(pdom));
wolffd@0 37 csize = prod(ns(cdom));
wolffd@0 38 T = pot_to_marginal(bigpot);
wolffd@0 39 mat = reshape(T.T, [psize csize]);
wolffd@0 40
wolffd@0 41