annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@softmax_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 softmax node
Daniel@0 3 % function CPD = update_ess(CPD, fmarginal, evidence, ns, 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) [WIRLS 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 domain = fmarginal.domain;
Daniel@0 19 self = domain(end);
Daniel@0 20 ps = domain(1:end-1);
Daniel@0 21 cnodes = domain(CPD.cpndx);
Daniel@0 22 cps = myintersect(domain, cnodes);
Daniel@0 23 dps = mysetdiff(ps, cps);
Daniel@0 24 dn_use = dps;
Daniel@0 25 if isempty(evidence{self}) dn_use = [dn_use self]; end % if self is hidden we must consider its dimension
Daniel@0 26 dps_as_cps = domain(CPD.dps_as_cps.ndx);
Daniel@0 27 odom = domain(~isemptycell(evidence(domain)));
Daniel@0 28
Daniel@0 29 ns = zeros(1, max(domain));
Daniel@0 30 ns(domain) = CPD.sizes; % CPD.sizes = bnet.node_sizes([ps self]);
Daniel@0 31 ens = ns; % effective node sizes
Daniel@0 32 ens(odom) = 1;
Daniel@0 33 dpsize = prod(ns(dps));
Daniel@0 34
Daniel@0 35 % Extract the params compatible with the observations (if any) on the discrete parents (if any)
Daniel@0 36 dops = myintersect(dps, odom);
Daniel@0 37 dpvals = cat(1, evidence{dops});
Daniel@0 38
Daniel@0 39 subs = ind2subv(ens(dn_use), 1:prod(ens(dn_use)));
Daniel@0 40 dpmap = find_equiv_posns(dops, dn_use);
Daniel@0 41 if ~isempty(dpmap), subs(:,dpmap) = subs(:,dpmap)+repmat(dpvals(:)',[size(subs,1) 1])-1; end
Daniel@0 42 supportedQs = subv2ind(ns(dn_use), subs); subs=subs(1:prod(ens(dps)),1:length(dps));
Daniel@0 43 Qarity = prod(ns(dn_use));
Daniel@0 44 if isempty(dn_use), Qarity = 1; end
Daniel@0 45
Daniel@0 46 fullm.T = zeros(Qarity, 1);
Daniel@0 47 fullm.T(supportedQs) = fmarginal.T(:);
Daniel@0 48 rs_dim = CPD.sizes; rs_dim(CPD.cpndx) = 1; %
Daniel@0 49 if ~isempty(evidence{self}), rs_dim(end)=1; end % reshaping the marginal
Daniel@0 50 fullm.T = reshape(fullm.T, rs_dim); %
Daniel@0 51
Daniel@0 52 % --------------------------------------------------------------------------------UPDATE--
Daniel@0 53
Daniel@0 54 CPD.nsamples = CPD.nsamples + 1;
Daniel@0 55
Daniel@0 56 % 1) observations vector -> CPD.parents_vals ---------------------------------------------
Daniel@0 57 cpvals = cat(1, evidence{cps});
Daniel@0 58
Daniel@0 59 if ~isempty(dps_as_cps), % ...get in the dp_as_cp parents...
Daniel@0 60 separator = CPD.dps_as_cps.separator;
Daniel@0 61 dp_as_cpmap = find_equiv_posns(dps_as_cps, dps);
Daniel@0 62 for i=1:dpsize,
Daniel@0 63 dp_as_cpvals=zeros(1,sum(ns(dps_as_cps)));
Daniel@0 64 possible_vals = ind2subv(ns(dps),i);
Daniel@0 65 ll=find(ismember(subs(:,dp_as_cpmap), possible_vals(dp_as_cpmap), 'rows')==1);
Daniel@0 66 if ~isempty(ll),
Daniel@0 67 where_one = separator + possible_vals(dp_as_cpmap);
Daniel@0 68 dp_as_cpvals(where_one)=1;
Daniel@0 69 end
Daniel@0 70 CPD.parent_vals(CPD.nsamples,:,i) = [dp_as_cpvals(:); cpvals(:)]';
Daniel@0 71 end
Daniel@0 72 else
Daniel@0 73 CPD.parent_vals(CPD.nsamples,:) = cpvals(:)';
Daniel@0 74 end
Daniel@0 75
Daniel@0 76 % 2) weights vector -> CPD.eso_weights ----------------------------------------------------
Daniel@0 77 if isempty(evidence{self}), % self is hidden
Daniel@0 78 pesi=reshape(sum(fullm.T, length(rs_dim)),[dpsize,1]);
Daniel@0 79 else
Daniel@0 80 pesi=reshape(fullm.T,[dpsize,1]);
Daniel@0 81 end
Daniel@0 82 assert(approxeq(sum(pesi),1)); % check
Daniel@0 83
Daniel@0 84 % 3) estimate (if R is hidden) or recover (if R is obs) self'value-------------------------
Daniel@0 85 if isempty(evidence{self}) % P(self|Pa1,...,Pak)=fmarginal/prob(Pa1,...,Pak)
Daniel@0 86 r=reshape(mk_stochastic(fullm.T), [dpsize ns(self)]); % matrix size: prod{j,ns(Paj)} x ns(self)
Daniel@0 87 else
Daniel@0 88 r = zeros(dpsize,ns(self));
Daniel@0 89 for i=1:dpsize, if pesi(i)~=0, r(i,evidence{self}) = 1; end; end
Daniel@0 90 end
Daniel@0 91 for i=1:dpsize, if pesi(i)~=0, assert(approxeq(sum(r(i,:)),1)); end; end % check
Daniel@0 92
Daniel@0 93 % 4) save the previous values --------------------------------------------------------------
Daniel@0 94 for i=1:dpsize
Daniel@0 95 CPD.eso_weights(CPD.nsamples,:,i)=pesi(i);
Daniel@0 96 CPD.self_vals(CPD.nsamples,:,i) = r(i,:);
Daniel@0 97 end