comparison toolboxes/FullBNT-1.0.7/bnt/potentials/@scgpot/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 cdom = myunion(bigpot.cheaddom, bigpot.ctaildom);
7 csumover = myintersect(sumover, bigpot.cheaddom);
8 dsumover = myintersect(sumover, bigpot.ddom);
9
10 dkeep = myintersect(keep, bigpot.ddom);
11 ckeep = myintersect(keep, bigpot.cheaddom);
12 cheaddom = myintersect(keep, bigpot.cheaddom);
13
14 assert(isempty(myintersect(csumover,bigpot.ctaildom)));
15 ns = zeros(1, max(bigpot.domain));
16 ns(bigpot.ddom) = bigpot.dsizes;
17 ns(bigpot.cheaddom) = bigpot.cheadsizes;
18 ns(bigpot.ctaildom) = bigpot.ctailsizes;
19
20
21 if sum(ns(csumover)) > 0
22 for i=1:bigpot.dsize
23 bigpot.scgpotc{i} = marginalize_pot(bigpot.scgpotc{i}, ckeep, csumover, ns);
24 end
25 end
26
27 if (isequal(csumover, cheaddom))
28 bigpot.ctaildom = [];
29 end
30 % If we are not marginalizing over any discrete nodes, we are done.
31 if prod(ns(dsumover))==1
32 smallpot = scgpot(dkeep, cheaddom, bigpot.ctaildom, ns, bigpot.scgpotc);
33 return;
34 end
35
36 if (~isempty(bigpot.ctaildom))
37 assert(0);
38 return;
39 end
40
41 I = prod(ns(dkeep));
42 J = prod(ns(dsumover));
43 C = sum(ns(ckeep));
44 sum_map = find_equiv_posns(dsumover, bigpot.ddom);
45 keep_map = find_equiv_posns(dkeep, bigpot.ddom);
46 iv = zeros(1, length(bigpot.ddom)); % index vector
47
48 p1 = zeros(I,J);
49 A1 = zeros(C,J,I);
50 C1 = zeros(C,C,J,I);
51 for i=1:I
52 keep_iv = ind2subv(ns(dkeep), i);
53 iv(keep_map) = keep_iv;
54 for j=1:J
55 sum_iv = ind2subv(ns(dsumover), j);
56 iv(sum_map) = sum_iv;
57 k = subv2ind(ns(bigpot.ddom), iv);
58 pot = struct(bigpot.scgpotc{k}); % violate object privacy
59 p1(i,j) = pot.p;
60 if C > 0 % so mu1 and Sigma1 are non-empty
61 A1(:,j,i) = pot.A;
62 C1(:,:,j,i) = pot.C;
63 end
64 end
65 end
66
67 % Collapse the mixture of Gaussians
68 coef = mk_stochastic(p1); % coef must be convex combination
69 %keyboard
70 p2 = sum(p1,2);
71 if (all(p2 == 0))
72 p2 = p2 + (p2==0)*eps;
73 end
74 A = [];
75 S = [];
76
77 pot = cell(1,I);
78 ctailsize = sum(ns(bigpot.ctaildom));
79 tB = zeros(C, ctailsize);
80 for i=1:I
81 if C > 0
82 [A, S] = collapse_mog(A1(:,:,i), C1(:,:,:,i), coef(i,:));
83 end
84 p = p2(i);
85 pot{i} = scgcpot(C, ctailsize, p, A, tB, S);
86 end
87
88 smallpot = scgpot(dkeep, ckeep, bigpot.ctaildom, ns, pot);
89
90
91
92