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