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