Daniel@0: function smallpot = marginalize_pot(bigpot, keep) Daniel@0: % MARGINALIZE_POT Marginalize a cgpot onto a smaller domain. Daniel@0: % smallpot = marginalize_pot(bigpot, keep) Daniel@0: Daniel@0: sumover = mysetdiff(bigpot.domain, keep); Daniel@0: csumover = myintersect(sumover, bigpot.cdom); Daniel@0: dsumover = myintersect(sumover, bigpot.ddom); Daniel@0: dkeep = myintersect(keep, bigpot.ddom); Daniel@0: ckeep = myintersect(keep, bigpot.cdom); Daniel@0: %ns = sparse(1, max(bigpot.domain)); % must be full, so I is an integer Daniel@0: ns = zeros(1, max(bigpot.domain)); Daniel@0: ns(bigpot.ddom) = bigpot.dsizes; Daniel@0: ns(bigpot.cdom) = bigpot.csizes; Daniel@0: Daniel@0: % sum(ns(csumover))==0 is like isempty(csumover) but handles observed nodes. Daniel@0: % Similarly, prod(ns(dsumover))==1 is like isempty(dsumover) Daniel@0: Daniel@0: % Marginalize the cts parts. Daniel@0: % If we are in canonical form, we stay that way, since moment form might not exist. Daniel@0: % Besides, we would like to minimize the number of conversions. Daniel@0: if sum(ns(csumover)) > 0 Daniel@0: if bigpot.subtype == 'm' Daniel@0: for i=1:bigpot.dsize Daniel@0: bigpot.mom{i} = marginalize_pot(bigpot.mom{i}, ckeep); Daniel@0: end Daniel@0: else Daniel@0: for i=1:bigpot.dsize Daniel@0: bigpot.can{i} = marginalize_pot(bigpot.can{i}, ckeep); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % If we are not marginalizing over any discrete nodes, we are done. Daniel@0: if prod(ns(dsumover))==1 Daniel@0: smallpot = cgpot(dkeep, ckeep, ns, bigpot.can, bigpot.mom, bigpot.subtype); Daniel@0: return; Daniel@0: end Daniel@0: Daniel@0: % To marginalize the discrete parts, we must be in moment form. Daniel@0: bigpot = cg_can_to_mom(bigpot); Daniel@0: Daniel@0: I = prod(ns(dkeep)); Daniel@0: J = prod(ns(dsumover)); Daniel@0: C = sum(ns(ckeep)); Daniel@0: Daniel@0: % Reshape bigpot into the form mu1(:,j,i), where i is in dkeep, j is in dsumover Daniel@0: T1 = zeros(I,J); Daniel@0: mu1 = zeros(C,J,I); Daniel@0: Sigma1 = zeros(C,C,J,I); Daniel@0: sum_map = find_equiv_posns(dsumover, bigpot.ddom); Daniel@0: keep_map = find_equiv_posns(dkeep, bigpot.ddom); Daniel@0: iv = zeros(1, length(bigpot.ddom)); % index vector Daniel@0: for i=1:I Daniel@0: keep_iv = ind2subv(ns(dkeep), i); Daniel@0: iv(keep_map) = keep_iv; Daniel@0: for j=1:J Daniel@0: sum_iv = ind2subv(ns(dsumover), j); Daniel@0: iv(sum_map) = sum_iv; Daniel@0: k = subv2ind(ns(bigpot.ddom), iv); Daniel@0: mom = struct(bigpot.mom{k}); % violate object privacy Daniel@0: T1(i,j) = exp(mom.logp); Daniel@0: if C > 0 % so mu1 and Sigma1 are non-empty Daniel@0: mu1(:,j,i) = mom.mu; Daniel@0: Sigma1(:,:,j,i) = mom.Sigma; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % Collapse the mixture of Gaussians Daniel@0: coef = mk_stochastic(T1); % coef must be convex combination Daniel@0: T2 = sum(T1,2); Daniel@0: T2 = T2 + (T2==0)*eps; Daniel@0: %if C > 0, disp('collapsing onto '); disp(leep); end Daniel@0: mu = []; Daniel@0: Sigma = []; Daniel@0: mom = cell(1,I); Daniel@0: for i=1:I Daniel@0: if C > 0 Daniel@0: [mu, Sigma] = collapse_mog(mu1(:,:,i), Sigma1(:,:,:,i), coef(i,:)); Daniel@0: end Daniel@0: logp = log(T2(i)); Daniel@0: mom{i} = mpot(ckeep, ns(ckeep), logp, mu, Sigma); Daniel@0: end Daniel@0: Daniel@0: smallpot = cgpot(dkeep, ckeep, ns, [], mom, 'm'); Daniel@0: