annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/mk_chmm.m @ 0:cc4b1211e677 tip

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