annotate toolboxes/FullBNT-1.0.7/bnt/potentials/@cgpot/Old/simple_marginalize_pot.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 smallpot = marginalize_pot(bigpot, keep)
Daniel@0 2 % MARGINALIZE_POT Marginalize a cgpot onto a smaller domain.
Daniel@0 3 % smallpot = marginalize_pot(bigpot, keep)
Daniel@0 4
Daniel@0 5 sumover = mysetdiff(bigpot.domain, keep);
Daniel@0 6 csumover = myintersect(sumover, bigpot.cdom);
Daniel@0 7 dsumover = myintersect(sumover, bigpot.ddom);
Daniel@0 8 dkeep = myintersect(keep, bigpot.ddom);
Daniel@0 9 ckeep = myintersect(keep, bigpot.cdom);
Daniel@0 10 %ns = sparse(1, max(bigpot.domain)); % must be full, so I is an integer
Daniel@0 11 ns = zeros(1, max(bigpot.domain));
Daniel@0 12 ns(bigpot.ddom) = bigpot.dsizes;
Daniel@0 13 ns(bigpot.cdom) = bigpot.csizes;
Daniel@0 14
Daniel@0 15 % sum(ns(csumover))==0 is like isempty(csumover) but handles observed nodes.
Daniel@0 16 % Similarly, prod(ns(dsumover))==1 is like isempty(dsumover)
Daniel@0 17
Daniel@0 18 % Marginalize the cts parts.
Daniel@0 19 % If we are in canonical form, we stay that way, since moment form might not exist.
Daniel@0 20 % Besides, we would like to minimize the number of conversions.
Daniel@0 21 if sum(ns(csumover)) > 0
Daniel@0 22 if bigpot.subtype == 'm'
Daniel@0 23 for i=1:bigpot.dsize
Daniel@0 24 bigpot.mom{i} = marginalize_pot(bigpot.mom{i}, ckeep);
Daniel@0 25 end
Daniel@0 26 else
Daniel@0 27 for i=1:bigpot.dsize
Daniel@0 28 bigpot.can{i} = marginalize_pot(bigpot.can{i}, ckeep);
Daniel@0 29 end
Daniel@0 30 end
Daniel@0 31 end
Daniel@0 32
Daniel@0 33 % If we are not marginalizing over any discrete nodes, we are done.
Daniel@0 34 if prod(ns(dsumover))==1
Daniel@0 35 smallpot = cgpot(dkeep, ckeep, ns, bigpot.can, bigpot.mom, bigpot.subtype);
Daniel@0 36 return;
Daniel@0 37 end
Daniel@0 38
Daniel@0 39 % To marginalize the discrete parts, we must be in moment form.
Daniel@0 40 bigpot = cg_can_to_mom(bigpot);
Daniel@0 41
Daniel@0 42 I = prod(ns(dkeep));
Daniel@0 43 J = prod(ns(dsumover));
Daniel@0 44 C = sum(ns(ckeep));
Daniel@0 45
Daniel@0 46 % Reshape bigpot into the form mu1(:,j,i), where i is in dkeep, j is in dsumover
Daniel@0 47 T1 = zeros(I,J);
Daniel@0 48 mu1 = zeros(C,J,I);
Daniel@0 49 Sigma1 = zeros(C,C,J,I);
Daniel@0 50 sum_map = find_equiv_posns(dsumover, bigpot.ddom);
Daniel@0 51 keep_map = find_equiv_posns(dkeep, bigpot.ddom);
Daniel@0 52 iv = zeros(1, length(bigpot.ddom)); % index vector
Daniel@0 53 for i=1:I
Daniel@0 54 keep_iv = ind2subv(ns(dkeep), i);
Daniel@0 55 iv(keep_map) = keep_iv;
Daniel@0 56 for j=1:J
Daniel@0 57 sum_iv = ind2subv(ns(dsumover), j);
Daniel@0 58 iv(sum_map) = sum_iv;
Daniel@0 59 k = subv2ind(ns(bigpot.ddom), iv);
Daniel@0 60 mom = struct(bigpot.mom{k}); % violate object privacy
Daniel@0 61 T1(i,j) = exp(mom.logp);
Daniel@0 62 if C > 0 % so mu1 and Sigma1 are non-empty
Daniel@0 63 mu1(:,j,i) = mom.mu;
Daniel@0 64 Sigma1(:,:,j,i) = mom.Sigma;
Daniel@0 65 end
Daniel@0 66 end
Daniel@0 67 end
Daniel@0 68
Daniel@0 69 % Collapse the mixture of Gaussians
Daniel@0 70 coef = mk_stochastic(T1); % coef must be convex combination
Daniel@0 71 T2 = sum(T1,2);
Daniel@0 72 T2 = T2 + (T2==0)*eps;
Daniel@0 73 %if C > 0, disp('collapsing onto '); disp(leep); end
Daniel@0 74 mu = [];
Daniel@0 75 Sigma = [];
Daniel@0 76 mom = cell(1,I);
Daniel@0 77 for i=1:I
Daniel@0 78 if C > 0
Daniel@0 79 [mu, Sigma] = collapse_mog(mu1(:,:,i), Sigma1(:,:,:,i), coef(i,:));
Daniel@0 80 end
Daniel@0 81 logp = log(T2(i));
Daniel@0 82 mom{i} = mpot(ckeep, ns(ckeep), logp, mu, Sigma);
Daniel@0 83 end
Daniel@0 84
Daniel@0 85 smallpot = cgpot(dkeep, ckeep, ns, [], mom, 'm');
Daniel@0 86