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