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 / @gaussian_CPD / Old / update_ess.m @ 8:b5b38998ef3b
History | View | Annotate | Download (2.84 KB)
| 1 |
function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv) |
|---|---|
| 2 |
% UPDATE_ESS Update the Expected Sufficient Statistics of a Gaussian node |
| 3 |
% function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv) |
| 4 |
|
| 5 |
%if nargin < 6 |
| 6 |
% hidden_bitv = zeros(1, max(fmarginal.domain)); |
| 7 |
% hidden_bitv(find(isempty(evidence)))=1; |
| 8 |
%end |
| 9 |
|
| 10 |
dom = fmarginal.domain; |
| 11 |
self = dom(end); |
| 12 |
ps = dom(1:end-1); |
| 13 |
hidden_self = hidden_bitv(self); |
| 14 |
cps = myintersect(ps, cnodes); |
| 15 |
dps = mysetdiff(ps, cps); |
| 16 |
hidden_cps = all(hidden_bitv(cps)); |
| 17 |
hidden_dps = all(hidden_bitv(dps)); |
| 18 |
|
| 19 |
CPD.nsamples = CPD.nsamples + 1; |
| 20 |
[ss cpsz dpsz] = size(CPD.weights); % ss = self size |
| 21 |
|
| 22 |
% Let X be the cts parent (if any), Y be the cts child (self). |
| 23 |
|
| 24 |
if ~hidden_self & (isempty(cps) | ~hidden_cps) & hidden_dps % all cts nodes are observed, all discrete nodes are hidden |
| 25 |
% Since X and Y are observed, SYY = 0, SXX = 0, SXY = 0 |
| 26 |
% Since discrete parents are hidden, we do not need to add evidence to w. |
| 27 |
w = fmarginal.T(:); |
| 28 |
CPD.Wsum = CPD.Wsum + w; |
| 29 |
y = evidence{self};
|
| 30 |
Cyy = y*y'; |
| 31 |
if ~CPD.useC |
| 32 |
W = repmat(w(:)',ss,1); % W(y,i) = w(i) |
| 33 |
W2 = repmat(reshape(W, [ss 1 dpsz]), [1 ss 1]); % W2(x,y,i) = w(i) |
| 34 |
CPD.WYsum = CPD.WYsum + W .* repmat(y(:), 1, dpsz); |
| 35 |
CPD.WYYsum = CPD.WYYsum + W2 .* repmat(reshape(Cyy, [ss ss 1]), [1 1 dpsz]); |
| 36 |
else |
| 37 |
W = w(:)'; |
| 38 |
W2 = reshape(W, [1 1 dpsz]); |
| 39 |
CPD.WYsum = CPD.WYsum + rep_mult(W, y(:), size(CPD.WYsum)); |
| 40 |
CPD.WYYsum = CPD.WYYsum + rep_mult(W2, Cyy, size(CPD.WYYsum)); |
| 41 |
end |
| 42 |
if cpsz > 0 % X exists |
| 43 |
x = cat(1, evidence{cps}); x = x(:);
|
| 44 |
Cxx = x*x'; |
| 45 |
Cxy = x*y'; |
| 46 |
if ~CPD.useC |
| 47 |
CPD.WXsum = CPD.WXsum + W .* repmat(x(:), 1, dpsz); |
| 48 |
CPD.WXXsum = CPD.WXXsum + W2 .* repmat(reshape(Cxx, [cpsz cpsz 1]), [1 1 dpsz]); |
| 49 |
CPD.WXYsum = CPD.WXYsum + W2 .* repmat(reshape(Cxy, [cpsz ss 1]), [1 1 dpsz]); |
| 50 |
else |
| 51 |
CPD.WXsum = CPD.WXsum + rep_mult(W, x(:), size(CPD.WXsum)); |
| 52 |
CPD.WXXsum = CPD.WXXsum + rep_mult(W2, Cxx, size(CPD.WXXsum)); |
| 53 |
CPD.WXYsum = CPD.WXYsum + rep_mult(W2, Cxy, size(CPD.WXYsum)); |
| 54 |
end |
| 55 |
end |
| 56 |
return; |
| 57 |
end |
| 58 |
|
| 59 |
% general (non-vectorized) case |
| 60 |
fullm = add_evidence_to_gmarginal(fmarginal, evidence, ns, cnodes); % slow! |
| 61 |
|
| 62 |
if dpsz == 1 % no discrete parents |
| 63 |
w = 1; |
| 64 |
else |
| 65 |
w = fullm.T(:); |
| 66 |
end |
| 67 |
|
| 68 |
CPD.Wsum = CPD.Wsum + w; |
| 69 |
xi = 1:cpsz; |
| 70 |
yi = (cpsz+1):(cpsz+ss); |
| 71 |
for i=1:dpsz |
| 72 |
muY = fullm.mu(yi, i); |
| 73 |
SYY = fullm.Sigma(yi, yi, i); |
| 74 |
CPD.WYsum(:,i) = CPD.WYsum(:,i) + w(i)*muY; |
| 75 |
CPD.WYYsum(:,:,i) = CPD.WYYsum(:,:,i) + w(i)*(SYY + muY*muY'); % E[X Y] = Cov[X,Y] + E[X] E[Y] |
| 76 |
if cpsz > 0 |
| 77 |
muX = fullm.mu(xi, i); |
| 78 |
SXX = fullm.Sigma(xi, xi, i); |
| 79 |
SXY = fullm.Sigma(xi, yi, i); |
| 80 |
CPD.WXsum(:,i) = CPD.WXsum(:,i) + w(i)*muX; |
| 81 |
CPD.WXXsum(:,:,i) = CPD.WXXsum(:,:,i) + w(i)*(SXX + muX*muX'); |
| 82 |
CPD.WXYsum(:,:,i) = CPD.WXYsum(:,:,i) + w(i)*(SXY + muX*muY'); |
| 83 |
end |
| 84 |
end |
| 85 |
|