annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@mlp_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 CPD (MLP)
Daniel@0 3 % CPD = update_ess(CPD, family_marginal, evidence, node_sizes, cnodes, hidden_bitv)
Daniel@0 4 %
Daniel@0 5 % fmarginal = overall posterior distribution of self and its parents
Daniel@0 6 % fmarginal(i1,i2...,ik,s)=prob(Pa1=i1,...,Pak=ik, self=s| X)
Daniel@0 7 %
Daniel@0 8 % => 1) prob(self|Pa1,...,Pak)=fmarginal/prob(Pa1,...,Pak) with prob(Pa1,...,Pak)=sum{s,fmarginal}
Daniel@0 9 % [self estimation -> CPD.self_vals]
Daniel@0 10 % 2) prob(Pa1,...,Pak) [SCG weights -> CPD.eso_weights]
Daniel@0 11 %
Daniel@0 12 % Hidden_bitv is ignored
Daniel@0 13
Daniel@0 14 % Written by Pierpaolo Brutti
Daniel@0 15
Daniel@0 16 if ~adjustable_CPD(CPD), return; end
Daniel@0 17
Daniel@0 18 dom = fmarginal.domain;
Daniel@0 19 cdom = myintersect(dom, cnodes);
Daniel@0 20 assert(~any(isemptycell(evidence(cdom))));
Daniel@0 21 ns(cdom)=1;
Daniel@0 22
Daniel@0 23 self = dom(end);
Daniel@0 24 ps=dom(1:end-1);
Daniel@0 25 dpdom=mysetdiff(ps,cdom);
Daniel@0 26
Daniel@0 27 dnodes = mysetdiff(1:length(ns), cnodes);
Daniel@0 28
Daniel@0 29 ddom = myintersect(ps, dnodes); %
Daniel@0 30 if isempty(evidence{self}), % if self is hidden in what follow we must
Daniel@0 31 ddom = myintersect(dom, dnodes); % consider its dimension
Daniel@0 32 end %
Daniel@0 33
Daniel@0 34 odom = dom(~isemptycell(evidence(dom)));
Daniel@0 35 hdom = dom(isemptycell(evidence(dom))); % hidden parents in domain
Daniel@0 36
Daniel@0 37 dobs = myintersect(ddom, odom);
Daniel@0 38 dvals = cat(1, evidence{dobs});
Daniel@0 39 ens = ns; % effective node sizes
Daniel@0 40 ens(dobs) = 1;
Daniel@0 41
Daniel@0 42 dpsz=prod(ns(dpdom));
Daniel@0 43 S=prod(ens(ddom));
Daniel@0 44 subs = ind2subv(ens(ddom), 1:S);
Daniel@0 45 mask = find_equiv_posns(dobs, ddom);
Daniel@0 46 for i=1:length(mask),
Daniel@0 47 subs(:,mask(i)) = dvals(i);
Daniel@0 48 end
Daniel@0 49 supportedQs = subv2ind(ns(ddom), subs);
Daniel@0 50
Daniel@0 51 Qarity = prod(ns(ddom));
Daniel@0 52 if isempty(ddom),
Daniel@0 53 Qarity = 1;
Daniel@0 54 end
Daniel@0 55 fullm.T = zeros(Qarity, 1);
Daniel@0 56 fullm.T(supportedQs) = fmarginal.T(:);
Daniel@0 57
Daniel@0 58 % For dynamic (recurrent) net-------------------------------------------------------------
Daniel@0 59 % ----------------------------------------------------------------------------------------
Daniel@0 60 high=size(evidence,1); % slice height
Daniel@0 61 ss_ns=ns(1:high); % single slice nodes sizes
Daniel@0 62 pos=self; %
Daniel@0 63 slice_num=0; %
Daniel@0 64 while pos>high, %
Daniel@0 65 slice_num=slice_num+1; % find active slice
Daniel@0 66 pos=pos-high; % pos=self posistion into a single slice
Daniel@0 67 end %
Daniel@0 68
Daniel@0 69 last_dim=pos-1; %
Daniel@0 70 if isempty(evidence{self}), %
Daniel@0 71 last_dim=pos; %
Daniel@0 72 end % last_dim=last reshaping dimension
Daniel@0 73 reg=dom-slice_num*high;
Daniel@0 74 dex=myintersect(reg(find(reg>=0)), [1:last_dim]); %
Daniel@0 75 rs_dim=ss_ns(dex); % reshaping dimensions
Daniel@0 76
Daniel@0 77 if slice_num>0,
Daniel@0 78 act_slice=[]; past_ancest=[]; %
Daniel@0 79 act_slice=slice_num*high+[1:high]; % recover the active slice nodes
Daniel@0 80 % past_ancest=mysetdiff(ddom, act_slice);
Daniel@0 81 past_ancest=mysetdiff(ps, act_slice); % recover ancestors contained into past slices
Daniel@0 82 app=ns(past_ancest);
Daniel@0 83 rs_dim=[app(:)' rs_dim(:)']; %
Daniel@0 84 end %
Daniel@0 85 if length(rs_dim)==1, rs_dim=[1 rs_dim]; end %
Daniel@0 86 if size(rs_dim,1)~=1, rs_dim=rs_dim'; end %
Daniel@0 87
Daniel@0 88 fullm.T=reshape(fullm.T, rs_dim); % reshaping the marginal
Daniel@0 89
Daniel@0 90 % ----------------------------------------------------------------------------------------
Daniel@0 91 % ----------------------------------------------------------------------------------------
Daniel@0 92
Daniel@0 93 % X = cts parent, R = discrete self
Daniel@0 94
Daniel@0 95 % 1) observations vector -> CPD.parents_vals -------------------------------------------------
Daniel@0 96 x = cat(1, evidence{cdom});
Daniel@0 97
Daniel@0 98 % 2) weights vector -> CPD.eso_weights -------------------------------------------------------
Daniel@0 99 if isempty(evidence{self}) % R is hidden
Daniel@0 100 sum_over=length(rs_dim);
Daniel@0 101 app=sum(fullm.T, sum_over);
Daniel@0 102 pesi=reshape(app,[dpsz,1]);
Daniel@0 103 clear app;
Daniel@0 104 else
Daniel@0 105 pesi=reshape(fullm.T,[dpsz,1]);
Daniel@0 106 end
Daniel@0 107
Daniel@0 108 assert(approxeq(sum(pesi),1));
Daniel@0 109
Daniel@0 110 % 3) estimate (if R is hidden) or recover (if R is obs) self'value----------------------------
Daniel@0 111 if isempty(evidence{self}) % R is hidden
Daniel@0 112 app=mk_stochastic(fullm.T); % P(self|Pa1,...,Pak)=fmarginal/prob(Pa1,...,Pak)
Daniel@0 113 app=reshape(app,[dpsz ns(self)]); % matrix size: prod{j,ns(Paj)} x ns(self)
Daniel@0 114 r=app;
Daniel@0 115 clear app;
Daniel@0 116 else
Daniel@0 117 r = zeros(dpsz,ns(self));
Daniel@0 118 for i=1:dpsz
Daniel@0 119 if pesi(i)~=0, r(i,evidence{self}) = 1; end
Daniel@0 120 end
Daniel@0 121 end
Daniel@0 122 for i=1:dpsz
Daniel@0 123 if pesi(i) ~=0, assert(approxeq(sum(r(i,:)),1)); end
Daniel@0 124 end
Daniel@0 125
Daniel@0 126 CPD.nsamples = CPD.nsamples + 1;
Daniel@0 127 CPD.parent_vals(CPD.nsamples,:) = x(:)';
Daniel@0 128 for i=1:dpsz
Daniel@0 129 CPD.eso_weights(CPD.nsamples,:,i)=pesi(i);
Daniel@0 130 CPD.self_vals(CPD.nsamples,:,i) = r(i,:);
Daniel@0 131 end