wolffd@0: function bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying) wolffd@0: % MK_HMM_BNET Make a (static) bnet to represent a hidden Markov model wolffd@0: % bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying) wolffd@0: % wolffd@0: % T = num time slices wolffd@0: % Q = num hidden states wolffd@0: % O = size of the observed node (num discrete values or length of vector) wolffd@0: % cts_obs - 1 means the observed node is a continuous-valued vector, 0 means it's discrete wolffd@0: % param_tying - 1 means we create 3 CPDs, 0 means we create 1 CPD per node wolffd@0: wolffd@0: N = 2*T; wolffd@0: dag = zeros(N); wolffd@0: %hnodes = 1:2:2*T; wolffd@0: hnodes = 1:T; wolffd@0: for i=1:T-1 wolffd@0: dag(hnodes(i), hnodes(i+1))=1; wolffd@0: end wolffd@0: %onodes = 2:2:2*T; wolffd@0: onodes = T+1:2*T; wolffd@0: for i=1:T wolffd@0: dag(hnodes(i), onodes(i)) = 1; wolffd@0: end wolffd@0: wolffd@0: if cts_obs wolffd@0: dnodes = hnodes; wolffd@0: else wolffd@0: dnodes = 1:N; wolffd@0: end wolffd@0: ns = ones(1,N); wolffd@0: ns(hnodes) = Q; wolffd@0: ns(onodes) = O; wolffd@0: wolffd@0: if param_tying wolffd@0: H1class = 1; Hclass = 2; Oclass = 3; wolffd@0: eclass = ones(1,N); wolffd@0: eclass(hnodes(2:end)) = Hclass; wolffd@0: eclass(hnodes(1)) = H1class; wolffd@0: eclass(onodes) = Oclass; wolffd@0: else wolffd@0: eclass = 1:N; wolffd@0: end wolffd@0: wolffd@0: bnet = mk_bnet(dag, ns, 'observed', onodes, 'discrete', dnodes, 'equiv_class', eclass); wolffd@0: wolffd@0: hnodes = mysetdiff(1:N, onodes); wolffd@0: if ~param_tying wolffd@0: for i=hnodes(:)' wolffd@0: bnet.CPD{i} = tabular_CPD(bnet, i); wolffd@0: end wolffd@0: if cts_obs wolffd@0: for i=onodes(:)' wolffd@0: bnet.CPD{i} = gaussian_CPD(bnet, i); wolffd@0: end wolffd@0: else wolffd@0: for i=onodes(:)' wolffd@0: bnet.CPD{i} = tabular_CPD(bnet, i); wolffd@0: end wolffd@0: end wolffd@0: else wolffd@0: bnet.CPD{H1class} = tabular_CPD(bnet, hnodes(1)); % prior wolffd@0: bnet.CPD{Hclass} = tabular_CPD(bnet, hnodes(2)); % transition matrix wolffd@0: if cts_obs wolffd@0: bnet.CPD{Oclass} = gaussian_CPD(bnet, onodes(1)); wolffd@0: else wolffd@0: bnet.CPD{Oclass} = tabular_CPD(bnet, onodes(1)); wolffd@0: end wolffd@0: end