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