diff options
| author | ziejd2 | 2017-09-28 15:04:40 -0500 |
|---|---|---|
| committer | ziejd2 | 2017-09-28 15:04:40 -0500 |
| commit | 8070dc963753142bb86c4ed698d91fd623ed28e7 (patch) | |
| tree | d0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/BNT/examples/dynamic/mhmm1.m | |
| parent | 7cc31810d53176e805532b2789955f4eedbce6bb (diff) | |
| download | BNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz | |
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning. I am calling this BNW_1.02. It can be accessed at: compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/BNT/examples/dynamic/mhmm1.m')
| -rw-r--r-- | sourcecodes/bnt-master/BNT/examples/dynamic/mhmm1.m | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mhmm1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mhmm1.m new file mode 100644 index 00000000..6efb9f72 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mhmm1.m @@ -0,0 +1,85 @@ +% Make an HMM with mixture of Gaussian observations +% Q1 ---> Q2 +% / | / | +% M1 | M2 | +% \ v \ v +% Y1 Y2 +% where Pr(m=j|q=i) is a multinomial and Pr(y|m,q) is a Gaussian + +%seed = 3; +%rand('state', seed); +%randn('state', seed); + +intra = zeros(3); +intra(1,[2 3]) = 1; +intra(2,3) = 1; +inter = zeros(3); +inter(1,1) = 1; +n = 3; + +Q = 2; % num hidden states +O = 2; % size of observed vector +M = 2; % num mixture components per state + +ns = [Q M O]; +dnodes = [1 2]; +onodes = [3]; +eclass1 = [1 2 3]; +eclass2 = [4 2 3]; +bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ... + 'observed', onodes); + +prior0 = normalise(rand(Q,1)); +transmat0 = mk_stochastic(rand(Q,Q)); +mixmat0 = mk_stochastic(rand(Q,M)); +mu0 = rand(O,Q,M); +Sigma0 = repmat(eye(O), [1 1 Q M]); +bnet.CPD{1} = tabular_CPD(bnet, 1, prior0); +bnet.CPD{2} = tabular_CPD(bnet, 2, mixmat0); +%% we set the cov prior to 0 to give same results as HMM toolbox +%bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', mu0, 'cov', Sigma0, 'cov_prior_weight', 0); +% new version of HMM toolbox uses the same default prior on Gaussians as BNT +bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', mu0, 'cov', Sigma0); +bnet.CPD{4} = tabular_CPD(bnet, 4, transmat0); + + + +T = 5; % fixed length sequences + +engine = {}; +engine{end+1} = hmm_inf_engine(bnet); +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); +engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); +if 0 +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); +%engine{end+1} = frontier_inf_engine(bnet); +engine{end+1} = bk_inf_engine(bnet, 'clusters', 'exact'); +engine{end+1} = jtree_dbn_inf_engine(bnet); +end + +inf_time = cmp_inference_dbn(bnet, engine, T); + +ncases = 2; +max_iter = 2; +[learning_time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, 'ncases', ncases, 'max_iter', max_iter); + +% Compare to HMM toolbox + +data = zeros(O, T, ncases); +for i=1:ncases + data(:,:,i) = reshape(cell2num(cases{i}(onodes,:)), [O T]); +end +tic; +[LL2, prior2, transmat2, mu2, Sigma2, mixmat2] = ... + mhmm_em(data, prior0, transmat0, mu0, Sigma0, mixmat0, 'max_iter', max_iter); +t=toc; +disp(['HMM toolbox took ' num2str(t) ' seconds ']) + +for e = 1:length(engine) + assert(approxeq(prior2, CPD{e,1}.CPT)) + assert(approxeq(mixmat2, CPD{e,2}.CPT)) + assert(approxeq(mu2, CPD{e,3}.mean)) + assert(approxeq(Sigma2, CPD{e,3}.cov)) + assert(approxeq(transmat2, CPD{e,4}.CPT)) + assert(approxeq(LL2, LL{e})) +end |
