annotate _FullBNT/BNT/CPDs/@gaussian_CPD/maximize_params_debug.m @ 8:b5b38998ef3b

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