Daniel@0: function [startprob, transprob, obsprob] = dbn_to_hmm(bnet) Daniel@0: % DBN_TO_HMM % Convert DBN params to HMM params Daniel@0: % [startprob, transprob, obsprob] = dbn_to_hmm(bnet, onodes) Daniel@0: % startprob(i) Daniel@0: % transprob(i,j) Daniel@0: % obsprob{k}.big_CPT(i,o) if k'th observed node is discrete Daniel@0: % obsprob{k}.big_mu(:,i), .big_Sigma(:,:,i) if k'th observed node is Gaussian Daniel@0: % Big means the domain contains all the hidden discrete nodes, not just the parents. Daniel@0: Daniel@0: % Called by constructor and by update_engine Daniel@0: Daniel@0: ss = length(bnet.intra); Daniel@0: onodes = bnet.observed; Daniel@0: hnodes = mysetdiff(1:ss, onodes); Daniel@0: evidence = cell(ss, 2); Daniel@0: ns = bnet.node_sizes(:); Daniel@0: Qh = prod(ns(hnodes)); Daniel@0: tmp = dpot_to_table(compute_joint_pot(bnet, hnodes, evidence)); Daniel@0: startprob = reshape(tmp, Qh, 1); Daniel@0: Daniel@0: tmp = dpot_to_table(compute_joint_pot(bnet, hnodes+ss, evidence, [hnodes hnodes+ss])); Daniel@0: transprob = mk_stochastic(reshape(tmp, Qh, Qh)); Daniel@0: Daniel@0: % P(o|ps) is used by mk_hmm_obs_lik_vec for a single time slice Daniel@0: % P(o|h) (the big version), where h = all hidden nodes, is used by enter_evidence Daniel@0: Daniel@0: obsprob = cell(1, length(onodes)); Daniel@0: for i=1:length(onodes) Daniel@0: o = onodes(i); Daniel@0: if bnet.auto_regressive(o) Daniel@0: % We assume the parents of this node are all the hidden nodes in the slice, Daniel@0: % so the params already are "big". Also, we assume we regress only on our old selves. Daniel@0: % slice 1 Daniel@0: e = bnet.equiv_class(o); Daniel@0: CPD = struct(bnet.CPD{e}); Daniel@0: O = ns(o); Daniel@0: ps = bnet.parents{o}; Daniel@0: Qps = prod(ns(ps)); Daniel@0: obsprob{i}.big_mu0 = reshape(CPD.mean, [O Qps]); Daniel@0: obsprob{i}.big_Sigma0 = reshape(CPD.cov, [O O Qps]); Daniel@0: Daniel@0: % slice t>1 Daniel@0: e = bnet.equiv_class(o+ss); Daniel@0: CPD = struct(bnet.CPD{e}); Daniel@0: O = ns(o); Daniel@0: dps = mysetdiff(bnet.parents{o+ss}, o); Daniel@0: Qdps = prod(ns(dps)); Daniel@0: obsprob{i}.big_mu = reshape(CPD.mean, [O Qdps]); Daniel@0: obsprob{i}.big_Sigma = reshape(CPD.cov, [O O Qdps]); Daniel@0: obsprob{i}.big_W = reshape(CPD.weights, [O O Qdps]); Daniel@0: else Daniel@0: e = bnet.equiv_class(o+ss); Daniel@0: CPD = struct(bnet.CPD{e}); Daniel@0: O = ns(o); Daniel@0: ps = bnet.parents{o}; Daniel@0: Qps = prod(ns(ps)); Daniel@0: % We make a big potential, replicating the params if necessary Daniel@0: % e.g., for a 2 chain coupled HMM, mu(:,Q1) becomes mu(:,Q1,Q2) Daniel@0: bigpot = pot_to_marginal(compute_joint_pot(bnet, onodes(i), evidence, [hnodes onodes(i)])); Daniel@0: Daniel@0: if myismember(o, bnet.dnodes) Daniel@0: obsprob{i}.CPT = reshape(CPD.CPT, [Qps O]); Daniel@0: obsprob{i}.big_CPT = reshape(bigpot.T, Qh, O); Daniel@0: else Daniel@0: obsprob{i}.big_mu = bigpot.mu; Daniel@0: obsprob{i}.big_Sigma = bigpot.Sigma; Daniel@0: Daniel@0: if 1 Daniel@0: obsprob{i}.mu = reshape(CPD.mean, [O Qps]); Daniel@0: C = reshape(CPD.cov, [O O Qps]); Daniel@0: obsprob{i}.Sigma = C; Daniel@0: d = size(obsprob{i}.mu, 1); Daniel@0: for j=1:Qps Daniel@0: obsprob{i}.inv_Sigma(:,:,j) = inv(C(:,:,j)); Daniel@0: obsprob{i}.denom(j) = (2*pi)^(d/2)*sqrt(abs(det(C(:,:,j)))); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: end % if discrete Daniel@0: end % if ar Daniel@0: end % for