annotate toolboxes/FullBNT-1.0.7/bnt/general/dbn_to_hmm.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [startprob, transprob, obsprob] = dbn_to_hmm(bnet)
Daniel@0 2 % DBN_TO_HMM % Convert DBN params to HMM params
Daniel@0 3 % [startprob, transprob, obsprob] = dbn_to_hmm(bnet, onodes)
Daniel@0 4 % startprob(i)
Daniel@0 5 % transprob(i,j)
Daniel@0 6 % obsprob{k}.big_CPT(i,o) if k'th observed node is discrete
Daniel@0 7 % obsprob{k}.big_mu(:,i), .big_Sigma(:,:,i) if k'th observed node is Gaussian
Daniel@0 8 % Big means the domain contains all the hidden discrete nodes, not just the parents.
Daniel@0 9
Daniel@0 10 % Called by constructor and by update_engine
Daniel@0 11
Daniel@0 12 ss = length(bnet.intra);
Daniel@0 13 onodes = bnet.observed;
Daniel@0 14 hnodes = mysetdiff(1:ss, onodes);
Daniel@0 15 evidence = cell(ss, 2);
Daniel@0 16 ns = bnet.node_sizes(:);
Daniel@0 17 Qh = prod(ns(hnodes));
Daniel@0 18 tmp = dpot_to_table(compute_joint_pot(bnet, hnodes, evidence));
Daniel@0 19 startprob = reshape(tmp, Qh, 1);
Daniel@0 20
Daniel@0 21 tmp = dpot_to_table(compute_joint_pot(bnet, hnodes+ss, evidence, [hnodes hnodes+ss]));
Daniel@0 22 transprob = mk_stochastic(reshape(tmp, Qh, Qh));
Daniel@0 23
Daniel@0 24 % P(o|ps) is used by mk_hmm_obs_lik_vec for a single time slice
Daniel@0 25 % P(o|h) (the big version), where h = all hidden nodes, is used by enter_evidence
Daniel@0 26
Daniel@0 27 obsprob = cell(1, length(onodes));
Daniel@0 28 for i=1:length(onodes)
Daniel@0 29 o = onodes(i);
Daniel@0 30 if bnet.auto_regressive(o)
Daniel@0 31 % We assume the parents of this node are all the hidden nodes in the slice,
Daniel@0 32 % so the params already are "big". Also, we assume we regress only on our old selves.
Daniel@0 33 % slice 1
Daniel@0 34 e = bnet.equiv_class(o);
Daniel@0 35 CPD = struct(bnet.CPD{e});
Daniel@0 36 O = ns(o);
Daniel@0 37 ps = bnet.parents{o};
Daniel@0 38 Qps = prod(ns(ps));
Daniel@0 39 obsprob{i}.big_mu0 = reshape(CPD.mean, [O Qps]);
Daniel@0 40 obsprob{i}.big_Sigma0 = reshape(CPD.cov, [O O Qps]);
Daniel@0 41
Daniel@0 42 % slice t>1
Daniel@0 43 e = bnet.equiv_class(o+ss);
Daniel@0 44 CPD = struct(bnet.CPD{e});
Daniel@0 45 O = ns(o);
Daniel@0 46 dps = mysetdiff(bnet.parents{o+ss}, o);
Daniel@0 47 Qdps = prod(ns(dps));
Daniel@0 48 obsprob{i}.big_mu = reshape(CPD.mean, [O Qdps]);
Daniel@0 49 obsprob{i}.big_Sigma = reshape(CPD.cov, [O O Qdps]);
Daniel@0 50 obsprob{i}.big_W = reshape(CPD.weights, [O O Qdps]);
Daniel@0 51 else
Daniel@0 52 e = bnet.equiv_class(o+ss);
Daniel@0 53 CPD = struct(bnet.CPD{e});
Daniel@0 54 O = ns(o);
Daniel@0 55 ps = bnet.parents{o};
Daniel@0 56 Qps = prod(ns(ps));
Daniel@0 57 % We make a big potential, replicating the params if necessary
Daniel@0 58 % e.g., for a 2 chain coupled HMM, mu(:,Q1) becomes mu(:,Q1,Q2)
Daniel@0 59 bigpot = pot_to_marginal(compute_joint_pot(bnet, onodes(i), evidence, [hnodes onodes(i)]));
Daniel@0 60
Daniel@0 61 if myismember(o, bnet.dnodes)
Daniel@0 62 obsprob{i}.CPT = reshape(CPD.CPT, [Qps O]);
Daniel@0 63 obsprob{i}.big_CPT = reshape(bigpot.T, Qh, O);
Daniel@0 64 else
Daniel@0 65 obsprob{i}.big_mu = bigpot.mu;
Daniel@0 66 obsprob{i}.big_Sigma = bigpot.Sigma;
Daniel@0 67
Daniel@0 68 if 1
Daniel@0 69 obsprob{i}.mu = reshape(CPD.mean, [O Qps]);
Daniel@0 70 C = reshape(CPD.cov, [O O Qps]);
Daniel@0 71 obsprob{i}.Sigma = C;
Daniel@0 72 d = size(obsprob{i}.mu, 1);
Daniel@0 73 for j=1:Qps
Daniel@0 74 obsprob{i}.inv_Sigma(:,:,j) = inv(C(:,:,j));
Daniel@0 75 obsprob{i}.denom(j) = (2*pi)^(d/2)*sqrt(abs(det(C(:,:,j))));
Daniel@0 76 end
Daniel@0 77 end
Daniel@0 78
Daniel@0 79 end % if discrete
Daniel@0 80 end % if ar
Daniel@0 81 end % for