annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/HHMM/Mgram/mgram1.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 % a multigram is a degenerate 2HHMM where the bottom level HMMs emit deterministic strings
Daniel@0 2 % and the the top level abstract states are independent of each other
Daniel@0 3 % cf. HSMM/test_mgram2
Daniel@0 4
Daniel@0 5 words = {'the', 't', 'h', 'e'};
Daniel@0 6 data = 'the';
Daniel@0 7 nwords = length(words);
Daniel@0 8 word_len = zeros(1, nwords);
Daniel@0 9 word_prob = normalise(ones(1,nwords));
Daniel@0 10 word_logprob = log(word_prob);
Daniel@0 11 for wi=1:nwords
Daniel@0 12 word_len(wi)=length(words{wi});
Daniel@0 13 end
Daniel@0 14 D = max(word_len);
Daniel@0 15
Daniel@0 16 alphasize = 26;
Daniel@0 17 data = letter2num(data);
Daniel@0 18 T = length(data);
Daniel@0 19
Daniel@0 20 % node numbers
Daniel@0 21 W = 1; % top level state = word id
Daniel@0 22 L = 2; % bottom level state = letter position within word
Daniel@0 23 F = 3;
Daniel@0 24 O = 4;
Daniel@0 25
Daniel@0 26 ss = 4;
Daniel@0 27 intra = zeros(ss,ss);
Daniel@0 28 intra(W,[F L O])=1;
Daniel@0 29 intra(L,[O F])=1;
Daniel@0 30
Daniel@0 31 inter = zeros(ss,ss);
Daniel@0 32 inter(W,W)=1;
Daniel@0 33 inter(L,L)=1;
Daniel@0 34 inter(F,[W L])=1;
Daniel@0 35
Daniel@0 36 % node sizes
Daniel@0 37 ns = zeros(1,ss);
Daniel@0 38 ns(W) = nwords;
Daniel@0 39 ns(L) = D;
Daniel@0 40 ns(F) = 2;
Daniel@0 41 ns(O) = alphasize;
Daniel@0 42
Daniel@0 43
Daniel@0 44 % Make the DBN
Daniel@0 45 bnet = mk_dbn(intra, inter, ns, 'observed', O);
Daniel@0 46 eclass = bnet.equiv_class;
Daniel@0 47
Daniel@0 48
Daniel@0 49
Daniel@0 50 % uniform start distrib over words, uniform trans mat
Daniel@0 51 Wstart = normalise(ones(1,nwords));
Daniel@0 52 Wtrans = mk_stochastic(ones(nwords,nwords));
Daniel@0 53
Daniel@0 54 % always start in state 1 for each bottom level HMM
Daniel@0 55 delta1_start = zeros(1, D);
Daniel@0 56 delta1_start(1) = 1;
Daniel@0 57 Lstart = repmat(delta1_start, nwords, 1);
Daniel@0 58 LRtrans = mk_leftright_transmat(D, 0); % 0 self loop prob
Daniel@0 59 Ltrans = repmat(LRtrans, [1 1 nwords]);
Daniel@0 60
Daniel@0 61 % Finish in the last letter of each word
Daniel@0 62 Fprob = zeros(nwords, D, 2);
Daniel@0 63 Fprob(:,:,1)=1;
Daniel@0 64 for i=1:nwords
Daniel@0 65 Fprob(i,length(words{i}),2)=1;
Daniel@0 66 Fprob(i,length(words{i}),1)=0;
Daniel@0 67 end
Daniel@0 68
Daniel@0 69 % Each state uniquely emits a letter
Daniel@0 70 Oprob = zeros(nwords, D, alphasize);
Daniel@0 71 for i=1:nwords
Daniel@0 72 for l=1:length(words{i})
Daniel@0 73 a = double(words{i}(l))-96;
Daniel@0 74 Oprob(i,l,a)=1;
Daniel@0 75 end
Daniel@0 76 end
Daniel@0 77
Daniel@0 78
Daniel@0 79 % Define CPDs for slice
Daniel@0 80 bnet.CPD{eclass(W,1)} = tabular_CPD(bnet, W, 'CPT', Wstart);
Daniel@0 81 bnet.CPD{eclass(L,1)} = tabular_CPD(bnet, L, 'CPT', Lstart);
Daniel@0 82 bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', Fprob);
Daniel@0 83 bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', Oprob);
Daniel@0 84
Daniel@0 85 % Define CPDs for slice 2
Daniel@0 86 bnet.CPD{eclass(W,2)} = hhmmQ_CPD(bnet, W+ss, 'Fbelow', F, 'startprob', Wstart, 'transprob', Wtrans);
Daniel@0 87 bnet.CPD{eclass(L,2)} = hhmmQ_CPD(bnet, L+ss, 'Fself', F, 'Qps', W+ss, 'startprob', Lstart, 'transprob', Ltrans);
Daniel@0 88
Daniel@0 89 evidence = cell(ss,T);
Daniel@0 90 evidence{W,1}=1;
Daniel@0 91 sample = cell2num(sample_dbn(bnet, 'length', T, 'evidence', evidence));
Daniel@0 92 str = lower(sample(4,:))
Daniel@0 93
Daniel@0 94 engine = jtree_dbn_inf_engine(bnet);
Daniel@0 95 evidence = cell(ss,T);
Daniel@0 96 evidence(O,:) = num2cell(data);
Daniel@0 97 [engine, ll_dbn] = enter_evidence(engine, evidence);
Daniel@0 98
Daniel@0 99 gamma = zeros(nwords, T);
Daniel@0 100 for t=1:T
Daniel@0 101 m = marginal_nodes(engine, [W F], t);
Daniel@0 102 gamma(:,t) = m.T(:,2);
Daniel@0 103 end
Daniel@0 104 gamma
Daniel@0 105
Daniel@0 106 xidbn = zeros(nwords, nwords);
Daniel@0 107 for t=1:T-1
Daniel@0 108 m = marginal_nodes(engine, [W F W+ss], t);
Daniel@0 109 xidbn = xidbn + squeeze(m.T(:,2,:));
Daniel@0 110 end
Daniel@0 111
Daniel@0 112 % thee
Daniel@0 113 % xidbn(1,4) = 0.9412 the->e
Daniel@0 114 % (2,3)=0.0588 t->h
Daniel@0 115 % (3,4)=0.0588 h-e
Daniel@0 116 % (4,4)=0.0588 e-e