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