annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@hhmmQ_CPD/update_CPT.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function CPD = update_CPT(CPD)
wolffd@0 2 % Compute the big CPT for an HHMM Q node (including F parents) given internal transprob and startprob
wolffd@0 3 % function CPD = update_CPT(CPD)
wolffd@0 4
wolffd@0 5 Qsz = CPD.Qsz;
wolffd@0 6 Qpsz = CPD.Qpsz;
wolffd@0 7
wolffd@0 8 if ~isempty(CPD.Fbelow_ndx)
wolffd@0 9 if ~isempty(CPD.Fself_ndx) % general case
wolffd@0 10 % Fb(t-1) Fself(t-1) P(Q(t)=j| Q(t-1)=i, Qps(t)=k)
wolffd@0 11 % ------------------------------------------------------
wolffd@0 12 % 1 1 delta(i,j)
wolffd@0 13 % 2 1 transprob(i,k,j)
wolffd@0 14 % 1 2 impossible
wolffd@0 15 % 2 2 startprob(k,j)
wolffd@0 16 CPT = zeros(Qsz, 2, 2, Qpsz, Qsz);
wolffd@0 17 I = repmat(eye(Qsz), [1 1 Qpsz]); % i,j,k
wolffd@0 18 I = permute(I, [1 3 2]); % i,k,j
wolffd@0 19 CPT(:, 1, 1, :, :) = I;
wolffd@0 20 CPT(:, 2, 1, :, :) = CPD.transprob;
wolffd@0 21 CPT(:, 1, 2, :, :) = I;
wolffd@0 22 CPT(:, 2, 2, :, :) = repmat(reshape(CPD.startprob, [1 Qpsz Qsz]), ...
wolffd@0 23 [Qsz 1 1]); % replicate over i
wolffd@0 24 else % no F from self, hence no startprob
wolffd@0 25 % Fb(t-1) P(Q(t)=j| Q(t-1)=i, Qps(t)=k)
wolffd@0 26 % ------------------------------------------------------
wolffd@0 27 % 1 delta(i,j)
wolffd@0 28 % 2 transprob(i,k,j)
wolffd@0 29
wolffd@0 30 nps = length(CPD.dom_sz)-1; % num parents
wolffd@0 31 CPT = 0*myones(CPD.dom_sz);
wolffd@0 32 %CPT = zeros(Qsz, 2, Qpsz, Qsz); % assumes CPT(Q(t-1), F(t-1), Qps, Q(t))
wolffd@0 33 % but a member of Qps may preceed Q(t-1) or F(t-1) in the ordering
wolffd@0 34
wolffd@0 35 for k=1:CPD.Qpsz
wolffd@0 36 Qps_vals = ind2subv(CPD.Qpsizes, k);
wolffd@0 37 ndx = mk_multi_index(nps+1, [CPD.Fbelow_ndx CPD.Qps_ndx], [1 Qps_vals]);
wolffd@0 38 CPT(ndx{:}) = eye(Qsz); % CPT(:,2,k,:) or CPT(:,k,2,:) etc
wolffd@0 39 end
wolffd@0 40 ndx = mk_multi_index(nps+1, CPD.Fbelow_ndx, 2);
wolffd@0 41 CPT(ndx{:}) = CPD.transprob; % we assume transprob is in topo order
wolffd@0 42 end
wolffd@0 43 else % no F signal from below
wolffd@0 44 if ~isempty(CPD.Fself_ndx)
wolffd@0 45 % Q(t-1), Fself(t-1), Qps, Q(t)
wolffd@0 46
wolffd@0 47 % Fself(t-1) P(Q(t-1)=i, Qps(t)=k -> Q(t)=j)
wolffd@0 48 % ------------------------------------------------------
wolffd@0 49 % 1 transprob(i,k,j)
wolffd@0 50 % 2 startprob(k,j)
wolffd@0 51
wolffd@0 52 nps = length(CPD.dom_sz)-1; % num parents
wolffd@0 53 CPT = 0*myones(CPD.dom_sz);
wolffd@0 54 ndx = mk_multi_index(nps+1, CPD.Fself_ndx, 1);
wolffd@0 55 CPT(ndx{:}) = CPD.transprob;
wolffd@0 56 if CPD.fullstartprob
wolffd@0 57 ndx = mk_multi_index(nps+1, CPD.Fself_ndx, 2);
wolffd@0 58 CPT(ndx{:}) = CPD.startprob;
wolffd@0 59 else
wolffd@0 60 for i=1:CPD.Qsz
wolffd@0 61 ndx = mk_multi_index(nps+1, [CPD.Fself_ndx CPD.old_self_ndx], [2 i]);
wolffd@0 62 CPT(ndx{:}) = CPD.startprob;
wolffd@0 63 end
wolffd@0 64 end
wolffd@0 65 else % no F from self
wolffd@0 66 error('An hhmmQ node without any F parents is just a tabular_CPD')
wolffd@0 67 end
wolffd@0 68 end
wolffd@0 69
wolffd@0 70 CPD = set_fields(CPD, 'CPT', CPT);