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