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