wolffd@0: % Make an HMM with discrete observations wolffd@0: % X1 -> X2 wolffd@0: % | | wolffd@0: % v v wolffd@0: % Y1 Y2 wolffd@0: wolffd@0: intra = zeros(2); wolffd@0: intra(1,2) = 1; wolffd@0: inter = zeros(2); wolffd@0: inter(1,1) = 1; wolffd@0: n = 2; wolffd@0: wolffd@0: Q = 2; % num hidden states wolffd@0: O = 2; % num observable symbols wolffd@0: wolffd@0: ns = [Q O]; wolffd@0: dnodes = 1:2; wolffd@0: onodes = [2]; wolffd@0: eclass1 = [1 2]; wolffd@0: eclass2 = [3 2]; wolffd@0: bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ... wolffd@0: 'observed', onodes); wolffd@0: wolffd@0: rand('state', 0); wolffd@0: prior1 = normalise(rand(Q,1)); wolffd@0: transmat1 = mk_stochastic(rand(Q,Q)); wolffd@0: obsmat1 = mk_stochastic(rand(Q,O)); wolffd@0: bnet.CPD{1} = tabular_CPD(bnet, 1, prior1); wolffd@0: bnet.CPD{2} = tabular_CPD(bnet, 2, obsmat1); wolffd@0: bnet.CPD{3} = tabular_CPD(bnet, 3, transmat1); wolffd@0: wolffd@0: wolffd@0: T = 5; % fixed length sequences wolffd@0: wolffd@0: engine = {}; wolffd@0: engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); wolffd@0: engine{end+1} = hmm_inf_engine(bnet); wolffd@0: engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); wolffd@0: engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); wolffd@0: if 1 wolffd@0: %engine{end+1} = frontier_inf_engine(bnet); % broken wolffd@0: engine{end+1} = bk_inf_engine(bnet, 'clusters', {[1]}); wolffd@0: engine{end+1} = jtree_dbn_inf_engine(bnet); wolffd@0: end wolffd@0: wolffd@0: inf_time = cmp_inference_dbn(bnet, engine, T); wolffd@0: wolffd@0: ncases = 2; wolffd@0: max_iter = 2; wolffd@0: [learning_time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, 'ncases', ncases, 'max_iter', max_iter); wolffd@0: wolffd@0: % Compare to HMM toolbox wolffd@0: wolffd@0: data = zeros(ncases, T); wolffd@0: for i=1:ncases wolffd@0: %data(i,:) = cat(2, cases{i}{onodes,:}); wolffd@0: data(i,:) = cell2num(cases{i}(onodes,:)); wolffd@0: end wolffd@0: [LL2, prior2, transmat2, obsmat2] = dhmm_em(data, prior1, transmat1, obsmat1, 'max_iter', max_iter); wolffd@0: wolffd@0: e = 1; wolffd@0: assert(approxeq(prior2, CPD{e,1}.CPT)) wolffd@0: assert(approxeq(obsmat2, CPD{e,2}.CPT)) wolffd@0: assert(approxeq(transmat2, CPD{e,3}.CPT)) wolffd@0: assert(approxeq(LL2, LL{e})) wolffd@0: