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 / Old / update_CPT.m @ 8:b5b38998ef3b
History | View | Annotate | Download (2.62 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]), [Qsz 1 1]); % replicate over i |
| 23 |
else % no F from self, hence no startprob |
| 24 |
% Fb(t-1) P(Q(t)=j| Q(t-1)=i, Qps(t)=k) |
| 25 |
% ------------------------------------------------------ |
| 26 |
% 1 delta(i,j) |
| 27 |
% 2 transprob(i,k,j) |
| 28 |
|
| 29 |
nps = length(CPD.dom_sz)-1; % num parents |
| 30 |
CPT = 0*myones(CPD.dom_sz); |
| 31 |
%CPT = zeros(Qsz, 2, Qpsz, Qsz); % assumes CPT(Q(t-1), F(t-1), Qps, Q(t)) |
| 32 |
% but a member of Qps may preceed Q(t-1) or F(t-1) in the ordering |
| 33 |
|
| 34 |
I = repmat(eye(Qsz), [1 1 Qpsz]); % i,j,k |
| 35 |
I = permute(I, [1 3 2]); % i,k,j |
| 36 |
|
| 37 |
% the following fails if there is a member of Qps with a lower |
| 38 |
% number than F |
| 39 |
%CPT(:, 1, :, :) = I; |
| 40 |
%CPT(:, 2, :, :) = CPD.transprob; |
| 41 |
|
| 42 |
ndx = mk_multi_index(nps+1, CPD.Fbelow_ndx, 1); |
| 43 |
CPT(ndx{:}) = I;
|
| 44 |
ndx = mk_multi_index(nps+1, CPD.Fbelow_ndx, 2); |
| 45 |
CPT(ndx{:}) = CPD.transprob;
|
| 46 |
keyboard |
| 47 |
end |
| 48 |
else % no F signal from below |
| 49 |
if ~isempty(CPD.Fself_ndx) |
| 50 |
% Q(t-1), Fself(t-1), Qps, Q(t) |
| 51 |
|
| 52 |
% if condition start on previous concrete state (as in map learning), |
| 53 |
% CPT(:, 1, :, :, :) = CPD.transprob(Q(t-1), Qps, Q(t)) |
| 54 |
% CPT(:, 2, :, :, :) = CPD.startprob(Q(t-1), Qps, Q(t)) |
| 55 |
|
| 56 |
% Fself(t-1) P(Q(t-1)=i, Qps(t)=k -> Q(t)=j) |
| 57 |
% ------------------------------------------------------ |
| 58 |
% 1 transprob(i,k,j) |
| 59 |
% 2 startprob(k,j) |
| 60 |
CPT = zeros(Qsz, 2, Qpsz, Qsz); |
| 61 |
I = repmat(eye(Qsz), [1 1 Qpsz]); % i,j,k |
| 62 |
I = permute(I, [1 3 2]); % i,k,j |
| 63 |
CPT(:, 1, :, :) = CPD.transprob; |
| 64 |
if CPD.fullstartprob |
| 65 |
CPT(:, 2, :, :) = CPD.startprob; |
| 66 |
else |
| 67 |
CPT(:, 2, :, :) = repmat(reshape(CPD.startprob, [1 Qpsz Qsz]), [Qsz 1 1]); % replicate over i |
| 68 |
end |
| 69 |
else % no F from self |
| 70 |
error('An hhmmQ node without any F parents is just a tabular_CPD')
|
| 71 |
end |
| 72 |
end |
| 73 |
|
| 74 |
CPD = set_fields(CPD, 'CPT', CPT); |