wolffd@0
|
1 function bnet = mk_chmm(N, Q, Y, discrete_obs, coupled, CPD)
|
wolffd@0
|
2 % MK_CHMM Make a coupled Hidden Markov Model
|
wolffd@0
|
3 %
|
wolffd@0
|
4 % There are N hidden nodes, each connected to itself and its two nearest neighbors in the next
|
wolffd@0
|
5 % slice (apart from the edges, where there is 1 nearest neighbor).
|
wolffd@0
|
6 %
|
wolffd@0
|
7 % Example: If N = 3, the hidden backbone is as follows, where all arrows point to the righ+t
|
wolffd@0
|
8 %
|
wolffd@0
|
9 % X1--X2
|
wolffd@0
|
10 % \/
|
wolffd@0
|
11 % /\
|
wolffd@0
|
12 % X2--X2
|
wolffd@0
|
13 % \/
|
wolffd@0
|
14 % /\
|
wolffd@0
|
15 % X3--X3
|
wolffd@0
|
16 %
|
wolffd@0
|
17 % Each hidden node has a "private" observed child (not shown).
|
wolffd@0
|
18 %
|
wolffd@0
|
19 % BNET = MK_CHMM(N, Q, Y)
|
wolffd@0
|
20 % Each hidden node is discrete and has Q values.
|
wolffd@0
|
21 % Each observed node is a Gaussian vector of length Y.
|
wolffd@0
|
22 %
|
wolffd@0
|
23 % BNET = MK_CHMM(N, Q, Y, DISCRETE_OBS)
|
wolffd@0
|
24 % If discrete_obs = 1, the observations are discrete (values in {1, .., Y}).
|
wolffd@0
|
25 %
|
wolffd@0
|
26 % BNET = MK_CHMM(N, Q, Y, DISCRETE_OBS, COUPLED)
|
wolffd@0
|
27 % If coupled = 0, the chains are not coupled, i.e., we make N parallel HMMs.
|
wolffd@0
|
28 %
|
wolffd@0
|
29 % BNET = MK_CHMM(N, Q, Y, DISCRETE_OBS, COUPLED, CPDs)
|
wolffd@0
|
30 % means use the specified CPD structures instead of creating random params.
|
wolffd@0
|
31 % CPD{i}.CPT, i=1:N specifies the prior
|
wolffd@0
|
32 % CPD{i}.CPT, i=2N+1:3N specifies the transition model
|
wolffd@0
|
33 % CPD{i}.mean, CPD{i}.cov, i=N+1:2N specifies the observation model if Gaussian
|
wolffd@0
|
34 % CPD{i}.CPT, i=N+1:2N if discrete
|
wolffd@0
|
35
|
wolffd@0
|
36
|
wolffd@0
|
37 if nargin < 2, Q = 2; end
|
wolffd@0
|
38 if nargin < 3, Y = 1; end
|
wolffd@0
|
39 if nargin < 4, discrete_obs = 0; end
|
wolffd@0
|
40 if nargin < 5, coupled = 1; end
|
wolffd@0
|
41 if nargin < 6, rnd = 1; else rnd = 0; end
|
wolffd@0
|
42
|
wolffd@0
|
43 ss = N*2;
|
wolffd@0
|
44 hnodes = 1:N;
|
wolffd@0
|
45 onodes = (1:N)+N;
|
wolffd@0
|
46
|
wolffd@0
|
47 intra = zeros(ss);
|
wolffd@0
|
48 for i=1:N
|
wolffd@0
|
49 intra(hnodes(i), onodes(i))=1;
|
wolffd@0
|
50 end
|
wolffd@0
|
51
|
wolffd@0
|
52 inter = zeros(ss);
|
wolffd@0
|
53 if coupled
|
wolffd@0
|
54 for i=1:N
|
wolffd@0
|
55 inter(i, max(i-1,1):min(i+1,N))=1;
|
wolffd@0
|
56 end
|
wolffd@0
|
57 else
|
wolffd@0
|
58 inter(1:N, 1:N) = eye(N);
|
wolffd@0
|
59 end
|
wolffd@0
|
60
|
wolffd@0
|
61 ns = [Q*ones(1,N) Y*ones(1,N)];
|
wolffd@0
|
62
|
wolffd@0
|
63 eclass1 = [hnodes onodes];
|
wolffd@0
|
64 eclass2 = [hnodes+ss onodes];
|
wolffd@0
|
65 if discrete_obs
|
wolffd@0
|
66 dnodes = 1:ss;
|
wolffd@0
|
67 else
|
wolffd@0
|
68 dnodes = hnodes;
|
wolffd@0
|
69 end
|
wolffd@0
|
70 bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ...
|
wolffd@0
|
71 'observed', onodes);
|
wolffd@0
|
72
|
wolffd@0
|
73 if rnd
|
wolffd@0
|
74 for i=hnodes(:)'
|
wolffd@0
|
75 bnet.CPD{i} = tabular_CPD(bnet, i);
|
wolffd@0
|
76 end
|
wolffd@0
|
77 for i=onodes(:)'
|
wolffd@0
|
78 if discrete_obs
|
wolffd@0
|
79 bnet.CPD{i} = tabular_CPD(bnet, i);
|
wolffd@0
|
80 else
|
wolffd@0
|
81 bnet.CPD{i} = gaussian_CPD(bnet, i);
|
wolffd@0
|
82 end
|
wolffd@0
|
83 end
|
wolffd@0
|
84 for i=hnodes(:)'+ss
|
wolffd@0
|
85 bnet.CPD{i} = tabular_CPD(bnet, i);
|
wolffd@0
|
86 end
|
wolffd@0
|
87 else
|
wolffd@0
|
88 for i=hnodes(:)'
|
wolffd@0
|
89 bnet.CPD{i} = tabular_CPD(bnet, i, CPD{i}.CPT);
|
wolffd@0
|
90 end
|
wolffd@0
|
91 for i=onodes(:)'
|
wolffd@0
|
92 if discrete_obs
|
wolffd@0
|
93 bnet.CPD{i} = tabular_CPD(bnet, i, CPD{i}.CPT);
|
wolffd@0
|
94 else
|
wolffd@0
|
95 bnet.CPD{i} = gaussian_CPD(bnet, i, CPD{i}.mean, CPD{i}.cov);
|
wolffd@0
|
96 end
|
wolffd@0
|
97 end
|
wolffd@0
|
98 for i=hnodes(:)'+ss
|
wolffd@0
|
99 bnet.CPD{i} = tabular_CPD(bnet, i, CPD{i}.CPT);
|
wolffd@0
|
100 end
|
wolffd@0
|
101 end
|
wolffd@0
|
102
|
wolffd@0
|
103
|