wolffd@0: function [prior, transmat] = dbn_to_hmm(bnet) wolffd@0: % DBN_TO_HMM Compute the discrete HMM matrices from a simple DBN wolffd@0: % [prior, transmat] = dbn_to_hmm(bnet) wolffd@0: wolffd@0: onodes = bnet.observed; wolffd@0: ss = length(bnet.intra); wolffd@0: evidence = cell(1,2*ss); wolffd@0: hnodes = mysetdiff(1:ss, onodes); wolffd@0: prior = multiply_CPTs(bnet, [], hnodes, evidence); wolffd@0: transmat = multiply_CPTs(bnet, hnodes, hnodes+ss, evidence); wolffd@0: %obsmat1 = multiply_CPTs(bnet, hnodes, onodes, evidence); wolffd@0: %obsmat = multiply_CPTs(bnet, hnodes+ss, onodes+ss, evidence); wolffd@0: %obsmat1 = obsmat if the observation matrices are tied across slices wolffd@0: wolffd@0: wolffd@0: wolffd@0: %%%%%%%%%%%% wolffd@0: wolffd@0: function mat = multiply_CPTs(bnet, pdom, cdom, evidence) wolffd@0: wolffd@0: % MULTIPLY_CPTS Make a matrix Pr(Y|X), where X represents all the parents, and Y all the children wolffd@0: % We assume the children have no intra-connections. wolffd@0: % wolffd@0: % e.g., Consider the DBN with interconnectivity i->i', j->j',k', k->i',k' wolffd@0: % Then transition matrix = Pr(i,j,k -> i',j',k') = Pr(i,k->i') Pr(j->j') Pr(j,k->k') wolffd@0: wolffd@0: dom = [pdom cdom]; wolffd@0: ns = bnet.node_sizes; wolffd@0: bigpot = dpot(dom, ns(dom)); wolffd@0: for j=cdom(:)' wolffd@0: e = bnet.equiv_class(j); wolffd@0: fam = family(bnet.dag, j); wolffd@0: pot = convert_to_pot(bnet.CPD{e}, 'd', fam(:), evidence); wolffd@0: bigpot = multiply_by_pot(bigpot, pot); wolffd@0: end wolffd@0: psize = prod(ns(pdom)); wolffd@0: csize = prod(ns(cdom)); wolffd@0: T = pot_to_marginal(bigpot); wolffd@0: mat = reshape(T.T, [psize csize]); wolffd@0: wolffd@0: