Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/potentials/@scgpot/complement_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 [margpot, comppot] = complement_pot(pot, keep) | |
2 % COMPLEMENT_POT complement means decompose of a potential into its strong marginal and | |
3 % its complement corresponds exactly to the decomposition of a probability distribution | |
4 % into its marginal and conditional | |
5 % [margpot, comppot] = complement_pot(pot, keep) | |
6 | |
7 % keep can only include continuous head nodes and discrete nodes | |
8 % margpot is the stable CG potential of keep nodes | |
9 % comppot is the stable CG potential of others in corresponds exactly to | |
10 % the discomposition of a probability distribution of its marginal and conditional | |
11 | |
12 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
13 % Calculation of the marginal requires integration over % | |
14 % all variables in csumover. Thus cheadkeep contains all % | |
15 % continuous variables in the marginal potential % | |
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
17 %keyboard; | |
18 csumover = mysetdiff(pot.cheaddom, keep); | |
19 cheadkeep = mysetdiff(pot.cheaddom, csumover); | |
20 | |
21 nodesizes = zeros(1, max(pot.domain)); | |
22 nodesizes(pot.ddom) = pot.dsizes; | |
23 nodesizes(pot.cheaddom) = pot.cheadsizes; | |
24 nodesizes(pot.ctaildom) = pot.ctailsizes; | |
25 | |
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
27 % Description of the variables in the marginal domain % | |
28 % For the calculation of a strong marginal first integration % | |
29 % over all continuous variables in the head takes place. % | |
30 % The calculation of the marginal over the head variables % | |
31 % might result in a smaller or empty tail % | |
32 % If there are no head variables, and therefore no tail % | |
33 % variables, left marginalisation over discrete variables % | |
34 % may take place % | |
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
36 margdom = mysetdiff(pot.domain,keep); | |
37 % margddom = pot.ddom; | |
38 margcheaddom = cheadkeep; | |
39 | |
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
41 % Marginalisation over discrete variables is only allowed when % | |
42 % the tail is empty % | |
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
44 margddom = myintersect(pot.ddom,keep); % Discrete domain of marginal | |
45 margctaildom = myintersect(pot.ctaildom,keep); % Tail domain | |
46 assert(isempty(mysetdiff(pot.ddom,margddom)) | isempty(margctaildom)) | |
47 | |
48 | |
49 %margctaildom = pot.ctaildom; | |
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
51 % Even if marginalisation over continuous variables is only defined % | |
52 % for head variables, the marginalisation over haed-variables might % | |
53 % result in a smaller tail % | |
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
55 margctaildom = myintersect(pot.ctaildom,keep); | |
56 | |
57 margcheadsizes = nodesizes(margcheaddom); | |
58 margcheadsize = sum(margcheadsizes); | |
59 margctailsizes = nodesizes(margctaildom); | |
60 margctailsize = sum(margctailsizes); | |
61 | |
62 compdom = pot.domain; | |
63 compddom = pot.ddom; | |
64 compcheaddom = csumover; | |
65 compctaildom = myunion(pot.ctaildom, cheadkeep); | |
66 compcheadsizes = nodesizes(compcheaddom); | |
67 compcheadsize = sum(compcheadsizes); | |
68 compctailsizes = nodesizes(compctaildom); | |
69 compctailsize = sum(compctailsizes); | |
70 | |
71 dkeep = myintersect(pot.ddom, keep); | |
72 %if dom is only contain discrete node | |
73 if isempty(pot.cheaddom) | |
74 dsumover = mysetdiff(pot.ddom, dkeep); | |
75 | |
76 if isempty(dsumover) | |
77 margpot = pot; | |
78 comppot = scgpot([], [], [], []); | |
79 return; | |
80 end | |
81 | |
82 | |
83 I = prod(nodesizes(dkeep)); | |
84 J = prod(nodesizes(dsumover)); | |
85 sum_map = find_equiv_posns(dsumover, pot.ddom); | |
86 keep_map = find_equiv_posns(dkeep, pot.ddom); | |
87 iv = zeros(1, length(pot.ddom)); % index vector | |
88 p1 = zeros(I,J); | |
89 for i=1:I | |
90 keep_iv = ind2subv(nodesizes(dkeep), i); | |
91 iv(keep_map) = keep_iv; | |
92 for j=1:J | |
93 sum_iv = ind2subv(nodesizes(dsumover), j); | |
94 iv(sum_map) = sum_iv; | |
95 k = subv2ind(nodesizes(pot.ddom), iv); | |
96 potc = struct(pot.scgpotc{k}); % violate object privacy | |
97 p1(i,j) = potc.p; | |
98 end | |
99 end | |
100 p2 = sum(p1,2); | |
101 p2 = p2 + (p2==0)*eps; | |
102 | |
103 margscpot = cell(1, I); | |
104 compscpot = cell(1, I*J); | |
105 iv = zeros(1, length(pot.ddom)); % index vector | |
106 for i=1:I | |
107 margscpot{i} = scgcpot(0, 0, p2(i)); | |
108 keep_iv = ind2subv(nodesizes(dkeep), i); | |
109 iv(keep_map) = keep_iv; | |
110 for j=1:J | |
111 sum_iv = ind2subv(nodesizes(dsumover), j); | |
112 iv(sum_map) = sum_iv; | |
113 k = subv2ind(nodesizes(pot.ddom), iv); | |
114 q = p1(i,j)/p2(i); | |
115 compscpot{k} = scgcpot(0, 0, q); | |
116 end | |
117 end | |
118 | |
119 margpot = scgpot(dkeep, [], [], nodesizes, margscpot); | |
120 comppot = scgpot(pot.ddom, [], [], nodesizes,compscpot); | |
121 return; | |
122 end | |
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
124 % head of the potential is not empty % | |
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
126 dsize = pot.dsize; | |
127 compscpot = cell(1, dsize); | |
128 | |
129 fmaskh = find_equiv_posns(margcheaddom, compctaildom); | |
130 fmaskt = find_equiv_posns(margctaildom, compctaildom); | |
131 | |
132 fh = block(fmaskh, compctailsizes); | |
133 ft = block(fmaskt, compctailsizes); | |
134 | |
135 | |
136 if ~isempty(margcheaddom) | |
137 for i=1:dsize | |
138 potc = struct(pot.scgpotc{i}); | |
139 q = 1; | |
140 p = potc.p; | |
141 [A1, A2, B1, B2, C11, C12, C21, C22] = partition_matrix_vec_3(potc.A, potc.B, potc.C, margcheaddom, compcheaddom, nodesizes); | |
142 | |
143 if ~isempty(margcheaddom) | |
144 margscpot{i} = scgcpot(margcheadsize, margctailsize, p, A1, B1, C11); | |
145 else | |
146 margscpot{i} = scgcpot(margcheadsize, margctailsize, p); | |
147 end | |
148 | |
149 if ~isempty(compcheaddom) | |
150 if ~isempty(margcheaddom) | |
151 E = A2 - C21*pinv(C11)*A1; | |
152 tmp1 = C21*pinv(C11); | |
153 tmp2 = B2 - C21*pinv(C11)*B1; | |
154 F = zeros(compcheadsize, compctailsize); | |
155 F(:, fh) = tmp1; | |
156 F(:, ft) = tmp2; | |
157 G = C22 - C21*pinv(C11)*C12; | |
158 else | |
159 E = A2; | |
160 F = B2; | |
161 G = C22; | |
162 end | |
163 compscpot{i} = scgcpot(compcheadsize, compctailsize, q, E, F, G); | |
164 else | |
165 compscpot{i} = scgcpot(compcheadsize, 0, q); | |
166 end | |
167 if isempty(margcheaddom) | |
168 margpot = scgpot(margddom, [], [], nodesizes, margscpot); | |
169 else | |
170 margpot = scgpot(margddom, margcheaddom, margctaildom, nodesizes, margscpot); | |
171 end | |
172 end | |
173 else | |
174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
175 % Marginalisation took place over all head variables. % | |
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
177 | |
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
179 % Calculate the strong marginal % | |
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
181 margpot = marginalize_pot(pot,keep); | |
182 mPot = struct(margpot); | |
183 for i =1:dsize | |
184 potc = struct(pot.scgpotc{i}); | |
185 % Get the probability of the original potential % | |
186 q = potc.p; | |
187 | |
188 % Get the configuration defined by the index i% | |
189 config = ind2subv(pot.dsizes,i); | |
190 | |
191 % Calculate the corresponding configuration in the marginal potential | |
192 if isempty(margpot.dsizes) | |
193 % keep == [] | |
194 indMargPot = 1; | |
195 else | |
196 equivPos = find_equiv_posns(dkeep,pot.ddom); | |
197 indMargPot = subv2ind(margpot.dsizes,config(equivPos)); | |
198 end | |
199 % Figure out the corresponding marginal potential | |
200 mPotC = struct(mPot.scgpotc{indMargPot}); | |
201 p = mPotC.p; | |
202 if p == 0 | |
203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
204 % The following assignment is correct as p is only zero if q is also zero % | |
205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
206 compscpot{i} = scgcpot(compcheadsize,compctailsize,0,potc.A,potc.B,potc.C); | |
207 else | |
208 compscpot{i} = scgcpot(compcheadsize,compctailsize,q/p,potc.A,potc.B,potc.C); | |
209 end | |
210 end | |
211 end | |
212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
213 % Put all components in one potential % | |
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
215 if isempty(compcheaddom) | |
216 comppot = scgpot(compddom, [], [], nodesizes,compscpot); | |
217 else | |
218 comppot = scgpot(compddom, compcheaddom, compctaildom, nodesizes,compscpot); | |
219 end | |
220 | |
221 |