annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@gaussian_CPD/maximize_params_debug.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 CPD = maximize_params(CPD, temp)
Daniel@0 2 % MAXIMIZE_PARAMS Set the params of a CPD to their ML values (Gaussian)
Daniel@0 3 % CPD = maximize_params(CPD, temperature)
Daniel@0 4 %
Daniel@0 5 % Temperature is currently ignored.
Daniel@0 6
Daniel@0 7 if ~adjustable_CPD(CPD), return; end
Daniel@0 8
Daniel@0 9 CPD1 = struct(new_maximize_params(CPD));
Daniel@0 10 CPD2 = struct(old_maximize_params(CPD));
Daniel@0 11 assert(approxeq(CPD1.mean, CPD2.mean))
Daniel@0 12 assert(approxeq(CPD1.cov, CPD2.cov))
Daniel@0 13 assert(approxeq(CPD1.weights, CPD2.weights))
Daniel@0 14
Daniel@0 15 CPD = new_maximize_params(CPD);
Daniel@0 16
Daniel@0 17 %%%%%%%
Daniel@0 18 function CPD = new_maximize_params(CPD)
Daniel@0 19
Daniel@0 20 if CPD.clamped_mean
Daniel@0 21 cl_mean = CPD.mean;
Daniel@0 22 else
Daniel@0 23 cl_mean = [];
Daniel@0 24 end
Daniel@0 25
Daniel@0 26 if CPD.clamped_cov
Daniel@0 27 cl_cov = CPD.cov;
Daniel@0 28 else
Daniel@0 29 cl_cov = [];
Daniel@0 30 end
Daniel@0 31
Daniel@0 32 if CPD.clamped_weights
Daniel@0 33 cl_weights = CPD.weights;
Daniel@0 34 else
Daniel@0 35 cl_weights = [];
Daniel@0 36 end
Daniel@0 37
Daniel@0 38 [ssz psz Q] = size(CPD.weights);
Daniel@0 39
Daniel@0 40 prior = repmat(CPD.cov_prior_weight*eye(ssz,ssz), [1 1 Q]);
Daniel@0 41 [CPD.mean, CPD.cov, CPD.weights] = ...
Daniel@0 42 Mstep_clg('w', CPD.Wsum, 'YY', CPD.WYYsum, 'Y', CPD.WYsum, 'YTY', [], ...
Daniel@0 43 'XX', CPD.WXXsum, 'XY', CPD.WXYsum, 'X', CPD.WXsum, ...
Daniel@0 44 'cov_type', CPD.cov_type, 'clamped_mean', cl_mean, ...
Daniel@0 45 'clamped_cov', cl_cov, 'clamped_weights', cl_weights, ...
Daniel@0 46 'tied_cov', CPD.tied_cov, ...
Daniel@0 47 'cov_prior', prior);
Daniel@0 48
Daniel@0 49
Daniel@0 50 %%%%%%%%%%%
Daniel@0 51
Daniel@0 52 function CPD = old_maximize_params(CPD)
Daniel@0 53
Daniel@0 54
Daniel@0 55 if ~adjustable_CPD(CPD), return; end
Daniel@0 56
Daniel@0 57 %assert(approxeq(CPD.nsamples, sum(CPD.Wsum)));
Daniel@0 58 assert(~any(isnan(CPD.WXXsum)))
Daniel@0 59 assert(~any(isnan(CPD.WXYsum)))
Daniel@0 60 assert(~any(isnan(CPD.WYYsum)))
Daniel@0 61
Daniel@0 62 [self_size cpsize dpsize] = size(CPD.weights);
Daniel@0 63
Daniel@0 64 % Append 1s to the parents, and derive the corresponding cross products.
Daniel@0 65 % This is used when estimate the means and weights simultaneosuly,
Daniel@0 66 % and when estimatting Sigma.
Daniel@0 67 % Let x2 = [x 1]'
Daniel@0 68 XY = zeros(cpsize+1, self_size, dpsize); % XY(:,:,i) = sum_l w(l,i) x2(l) y(l)'
Daniel@0 69 XX = zeros(cpsize+1, cpsize+1, dpsize); % XX(:,:,i) = sum_l w(l,i) x2(l) x2(l)'
Daniel@0 70 YY = zeros(self_size, self_size, dpsize); % YY(:,:,i) = sum_l w(l,i) y(l) y(l)'
Daniel@0 71 for i=1:dpsize
Daniel@0 72 XY(:,:,i) = [CPD.WXYsum(:,:,i) % X*Y
Daniel@0 73 CPD.WYsum(:,i)']; % 1*Y
Daniel@0 74 % [x * [x' 1] = [xx' x
Daniel@0 75 % 1] x' 1]
Daniel@0 76 XX(:,:,i) = [CPD.WXXsum(:,:,i) CPD.WXsum(:,i);
Daniel@0 77 CPD.WXsum(:,i)' CPD.Wsum(i)];
Daniel@0 78 YY(:,:,i) = CPD.WYYsum(:,:,i);
Daniel@0 79 end
Daniel@0 80
Daniel@0 81 w = CPD.Wsum(:);
Daniel@0 82 % Set any zeros to one before dividing
Daniel@0 83 % This is valid because w(i)=0 => WYsum(:,i)=0, etc
Daniel@0 84 w = w + (w==0);
Daniel@0 85
Daniel@0 86 if CPD.clamped_mean
Daniel@0 87 % Estimating B2 and then setting the last column (the mean) to the clamped mean is *not* equivalent
Daniel@0 88 % to estimating B and then adding the clamped_mean to the last column.
Daniel@0 89 if ~CPD.clamped_weights
Daniel@0 90 B = zeros(self_size, cpsize, dpsize);
Daniel@0 91 for i=1:dpsize
Daniel@0 92 if det(CPD.WXXsum(:,:,i))==0
Daniel@0 93 B(:,:,i) = 0;
Daniel@0 94 else
Daniel@0 95 % Eqn 9 in table 2 of TR
Daniel@0 96 %B(:,:,i) = CPD.WXYsum(:,:,i)' * inv(CPD.WXXsum(:,:,i));
Daniel@0 97 B(:,:,i) = (CPD.WXXsum(:,:,i) \ CPD.WXYsum(:,:,i))';
Daniel@0 98 end
Daniel@0 99 end
Daniel@0 100 %CPD.weights = reshape(B, [self_size cpsize dpsize]);
Daniel@0 101 CPD.weights = B;
Daniel@0 102 end
Daniel@0 103 elseif CPD.clamped_weights % KPM 1/25/02
Daniel@0 104 if ~CPD.clamped_mean % ML estimate is just sample mean of the residuals
Daniel@0 105 for i=1:dpsize
Daniel@0 106 CPD.mean(:,i) = (CPD.WYsum(:,i) - CPD.weights(:,:,i) * CPD.WXsum(:,i)) / w(i);
Daniel@0 107 end
Daniel@0 108 end
Daniel@0 109 else % nothing is clamped, so estimate mean and weights simultaneously
Daniel@0 110 B2 = zeros(self_size, cpsize+1, dpsize);
Daniel@0 111 for i=1:dpsize
Daniel@0 112 if det(XX(:,:,i))==0 % fix by U. Sondhauss 6/27/99
Daniel@0 113 B2(:,:,i)=0;
Daniel@0 114 else
Daniel@0 115 % Eqn 9 in table 2 of TR
Daniel@0 116 %B2(:,:,i) = XY(:,:,i)' * inv(XX(:,:,i));
Daniel@0 117 B2(:,:,i) = (XX(:,:,i) \ XY(:,:,i))';
Daniel@0 118 end
Daniel@0 119 CPD.mean(:,i) = B2(:,cpsize+1,i);
Daniel@0 120 CPD.weights(:,:,i) = B2(:,1:cpsize,i);
Daniel@0 121 end
Daniel@0 122 end
Daniel@0 123
Daniel@0 124 % Let B2 = [W mu]
Daniel@0 125 if cpsize>0
Daniel@0 126 B2(:,1:cpsize,:) = reshape(CPD.weights, [self_size cpsize dpsize]);
Daniel@0 127 end
Daniel@0 128 B2(:,cpsize+1,:) = reshape(CPD.mean, [self_size dpsize]);
Daniel@0 129
Daniel@0 130 % To avoid singular covariance matrices,
Daniel@0 131 % we use the regularization method suggested in "A Quasi-Bayesian approach to estimating
Daniel@0 132 % parameters for mixtures of normal distributions", Hamilton 91.
Daniel@0 133 % If the ML estimate is Sigma = M/N, the MAP estimate is (M+gamma*I) / (N+gamma),
Daniel@0 134 % where gamma >=0 is a smoothing parameter (equivalent sample size of I prior)
Daniel@0 135
Daniel@0 136 gamma = CPD.cov_prior_weight;
Daniel@0 137
Daniel@0 138 if ~CPD.clamped_cov
Daniel@0 139 if CPD.cov_prior_entropic % eqn 12 of Brand AI/Stat 99
Daniel@0 140 Z = 1-temp;
Daniel@0 141 % When temp > 1, Z is negative, so we are dividing by a smaller
Daniel@0 142 % number, ie. increasing the variance.
Daniel@0 143 else
Daniel@0 144 Z = 0;
Daniel@0 145 end
Daniel@0 146 if CPD.tied_cov
Daniel@0 147 S = zeros(self_size, self_size);
Daniel@0 148 % Eqn 2 from table 2 in TR
Daniel@0 149 for i=1:dpsize
Daniel@0 150 S = S + (YY(:,:,i) - B2(:,:,i)*XY(:,:,i));
Daniel@0 151 end
Daniel@0 152 %denom = CPD.nsamples + gamma + Z;
Daniel@0 153 denom = CPD.nsamples + Z;
Daniel@0 154 S = (S + gamma*eye(self_size)) / denom;
Daniel@0 155 if strcmp(CPD.cov_type, 'diag')
Daniel@0 156 S = diag(diag(S));
Daniel@0 157 end
Daniel@0 158 CPD.cov = repmat(S, [1 1 dpsize]);
Daniel@0 159 else
Daniel@0 160 for i=1:dpsize
Daniel@0 161 % Eqn 1 from table 2 in TR
Daniel@0 162 S = YY(:,:,i) - B2(:,:,i)*XY(:,:,i);
Daniel@0 163 %denom = w(i) + gamma + Z;
Daniel@0 164 denom = w(i) + Z;
Daniel@0 165 S = (S + gamma*eye(self_size)) / denom;
Daniel@0 166 CPD.cov(:,:,i) = S;
Daniel@0 167 end
Daniel@0 168 if strcmp(CPD.cov_type, 'diag')
Daniel@0 169 for i=1:dpsize
Daniel@0 170 CPD.cov(:,:,i) = diag(diag(CPD.cov(:,:,i)));
Daniel@0 171 end
Daniel@0 172 end
Daniel@0 173 end
Daniel@0 174 end
Daniel@0 175
Daniel@0 176
Daniel@0 177 check_covars = 0;
Daniel@0 178 min_covar = 1e-5;
Daniel@0 179 if check_covars % prevent collapsing to a point
Daniel@0 180 for i=1:dpsize
Daniel@0 181 if min(svd(CPD.cov(:,:,i))) < min_covar
Daniel@0 182 disp(['resetting singular covariance for node ' num2str(CPD.self)]);
Daniel@0 183 CPD.cov(:,:,i) = CPD.init_cov(:,:,i);
Daniel@0 184 end
Daniel@0 185 end
Daniel@0 186 end
Daniel@0 187
Daniel@0 188
Daniel@0 189