wolffd@0
|
1 function CPD = maximize_params(CPD, temp)
|
wolffd@0
|
2 % MAXIMIZE_PARAMS Set the params of a CPD to their ML values (Gaussian)
|
wolffd@0
|
3 % CPD = maximize_params(CPD, temperature)
|
wolffd@0
|
4 %
|
wolffd@0
|
5 % Temperature is currently only used for entropic prior on Sigma
|
wolffd@0
|
6
|
wolffd@0
|
7 % For details, see "Fitting a Conditional Gaussian Distribution", Kevin Murphy, tech. report,
|
wolffd@0
|
8 % 1998, available at www.cs.berkeley.edu/~murphyk/papers.html
|
wolffd@0
|
9 % Refering to table 2, we use equations 1/2 to estimate the covariance matrix in the untied/tied case,
|
wolffd@0
|
10 % and equation 9 to estimate the weight matrix and mean.
|
wolffd@0
|
11 % We do not implement spherical Gaussians - the code is already pretty complicated!
|
wolffd@0
|
12
|
wolffd@0
|
13 if ~adjustable_CPD(CPD), return; end
|
wolffd@0
|
14
|
wolffd@0
|
15 %assert(approxeq(CPD.nsamples, sum(CPD.Wsum)));
|
wolffd@0
|
16 assert(~any(isnan(CPD.WXXsum)))
|
wolffd@0
|
17 assert(~any(isnan(CPD.WXYsum)))
|
wolffd@0
|
18 assert(~any(isnan(CPD.WYYsum)))
|
wolffd@0
|
19
|
wolffd@0
|
20 [self_size cpsize dpsize] = size(CPD.weights);
|
wolffd@0
|
21
|
wolffd@0
|
22 % Append 1s to the parents, and derive the corresponding cross products.
|
wolffd@0
|
23 % This is used when estimate the means and weights simultaneosuly,
|
wolffd@0
|
24 % and when estimatting Sigma.
|
wolffd@0
|
25 % Let x2 = [x 1]'
|
wolffd@0
|
26 XY = zeros(cpsize+1, self_size, dpsize); % XY(:,:,i) = sum_l w(l,i) x2(l) y(l)'
|
wolffd@0
|
27 XX = zeros(cpsize+1, cpsize+1, dpsize); % XX(:,:,i) = sum_l w(l,i) x2(l) x2(l)'
|
wolffd@0
|
28 YY = zeros(self_size, self_size, dpsize); % YY(:,:,i) = sum_l w(l,i) y(l) y(l)'
|
wolffd@0
|
29 for i=1:dpsize
|
wolffd@0
|
30 XY(:,:,i) = [CPD.WXYsum(:,:,i) % X*Y
|
wolffd@0
|
31 CPD.WYsum(:,i)']; % 1*Y
|
wolffd@0
|
32 % [x * [x' 1] = [xx' x
|
wolffd@0
|
33 % 1] x' 1]
|
wolffd@0
|
34 XX(:,:,i) = [CPD.WXXsum(:,:,i) CPD.WXsum(:,i);
|
wolffd@0
|
35 CPD.WXsum(:,i)' CPD.Wsum(i)];
|
wolffd@0
|
36 YY(:,:,i) = CPD.WYYsum(:,:,i);
|
wolffd@0
|
37 end
|
wolffd@0
|
38
|
wolffd@0
|
39 w = CPD.Wsum(:);
|
wolffd@0
|
40 % Set any zeros to one before dividing
|
wolffd@0
|
41 % This is valid because w(i)=0 => WYsum(:,i)=0, etc
|
wolffd@0
|
42 w = w + (w==0);
|
wolffd@0
|
43
|
wolffd@0
|
44 if CPD.clamped_mean
|
wolffd@0
|
45 % Estimating B2 and then setting the last column (the mean) to the clamped mean is *not* equivalent
|
wolffd@0
|
46 % to estimating B and then adding the clamped_mean to the last column.
|
wolffd@0
|
47 if ~CPD.clamped_weights
|
wolffd@0
|
48 B = zeros(self_size, cpsize, dpsize);
|
wolffd@0
|
49 for i=1:dpsize
|
wolffd@0
|
50 if det(CPD.WXXsum(:,:,i))==0
|
wolffd@0
|
51 B(:,:,i) = 0;
|
wolffd@0
|
52 else
|
wolffd@0
|
53 % Eqn 9 in table 2 of TR
|
wolffd@0
|
54 %B(:,:,i) = CPD.WXYsum(:,:,i)' * inv(CPD.WXXsum(:,:,i));
|
wolffd@0
|
55 B(:,:,i) = (CPD.WXXsum(:,:,i) \ CPD.WXYsum(:,:,i))';
|
wolffd@0
|
56 end
|
wolffd@0
|
57 end
|
wolffd@0
|
58 %CPD.weights = reshape(B, [self_size cpsize dpsize]);
|
wolffd@0
|
59 CPD.weights = B;
|
wolffd@0
|
60 end
|
wolffd@0
|
61 elseif CPD.clamped_weights % KPM 1/25/02
|
wolffd@0
|
62 if ~CPD.clamped_mean % ML estimate is just sample mean of the residuals
|
wolffd@0
|
63 for i=1:dpsize
|
wolffd@0
|
64 CPD.mean(:,i) = (CPD.WYsum(:,i) - CPD.weights(:,:,i) * CPD.WXsum(:,i)) / w(i);
|
wolffd@0
|
65 end
|
wolffd@0
|
66 end
|
wolffd@0
|
67 else % nothing is clamped, so estimate mean and weights simultaneously
|
wolffd@0
|
68 B2 = zeros(self_size, cpsize+1, dpsize);
|
wolffd@0
|
69 for i=1:dpsize
|
wolffd@0
|
70 if det(XX(:,:,i))==0 % fix by U. Sondhauss 6/27/99
|
wolffd@0
|
71 B2(:,:,i)=0;
|
wolffd@0
|
72 else
|
wolffd@0
|
73 % Eqn 9 in table 2 of TR
|
wolffd@0
|
74 %B2(:,:,i) = XY(:,:,i)' * inv(XX(:,:,i));
|
wolffd@0
|
75 B2(:,:,i) = (XX(:,:,i) \ XY(:,:,i))';
|
wolffd@0
|
76 end
|
wolffd@0
|
77 CPD.mean(:,i) = B2(:,cpsize+1,i);
|
wolffd@0
|
78 CPD.weights(:,:,i) = B2(:,1:cpsize,i);
|
wolffd@0
|
79 end
|
wolffd@0
|
80 end
|
wolffd@0
|
81
|
wolffd@0
|
82 % Let B2 = [W mu]
|
wolffd@0
|
83 if cpsize>0
|
wolffd@0
|
84 B2(:,1:cpsize,:) = reshape(CPD.weights, [self_size cpsize dpsize]);
|
wolffd@0
|
85 end
|
wolffd@0
|
86 B2(:,cpsize+1,:) = reshape(CPD.mean, [self_size dpsize]);
|
wolffd@0
|
87
|
wolffd@0
|
88 % To avoid singular covariance matrices,
|
wolffd@0
|
89 % we use the regularization method suggested in "A Quasi-Bayesian approach to estimating
|
wolffd@0
|
90 % parameters for mixtures of normal distributions", Hamilton 91.
|
wolffd@0
|
91 % If the ML estimate is Sigma = M/N, the MAP estimate is (M+gamma*I) / (N+gamma),
|
wolffd@0
|
92 % where gamma >=0 is a smoothing parameter (equivalent sample size of I prior)
|
wolffd@0
|
93
|
wolffd@0
|
94 gamma = CPD.cov_prior_weight;
|
wolffd@0
|
95
|
wolffd@0
|
96 if ~CPD.clamped_cov
|
wolffd@0
|
97 if CPD.cov_prior_entropic % eqn 12 of Brand AI/Stat 99
|
wolffd@0
|
98 Z = 1-temp;
|
wolffd@0
|
99 % When temp > 1, Z is negative, so we are dividing by a smaller
|
wolffd@0
|
100 % number, ie. increasing the variance.
|
wolffd@0
|
101 else
|
wolffd@0
|
102 Z = 0;
|
wolffd@0
|
103 end
|
wolffd@0
|
104 if CPD.tied_cov
|
wolffd@0
|
105 S = zeros(self_size, self_size);
|
wolffd@0
|
106 % Eqn 2 from table 2 in TR
|
wolffd@0
|
107 for i=1:dpsize
|
wolffd@0
|
108 S = S + (YY(:,:,i) - B2(:,:,i)*XY(:,:,i));
|
wolffd@0
|
109 end
|
wolffd@0
|
110 %denom = max(1, CPD.nsamples + gamma + Z);
|
wolffd@0
|
111 denom = CPD.nsamples + gamma + Z;
|
wolffd@0
|
112 S = (S + gamma*eye(self_size)) / denom;
|
wolffd@0
|
113 if strcmp(CPD.cov_type, 'diag')
|
wolffd@0
|
114 S = diag(diag(S));
|
wolffd@0
|
115 end
|
wolffd@0
|
116 CPD.cov = repmat(S, [1 1 dpsize]);
|
wolffd@0
|
117 else
|
wolffd@0
|
118 for i=1:dpsize
|
wolffd@0
|
119 % Eqn 1 from table 2 in TR
|
wolffd@0
|
120 S = YY(:,:,i) - B2(:,:,i)*XY(:,:,i);
|
wolffd@0
|
121 %denom = max(1, w(i) + gamma + Z); % gives wrong answers on mhmm1
|
wolffd@0
|
122 denom = w(i) + gamma + Z;
|
wolffd@0
|
123 S = (S + gamma*eye(self_size)) / denom;
|
wolffd@0
|
124 CPD.cov(:,:,i) = S;
|
wolffd@0
|
125 end
|
wolffd@0
|
126 if strcmp(CPD.cov_type, 'diag')
|
wolffd@0
|
127 for i=1:dpsize
|
wolffd@0
|
128 CPD.cov(:,:,i) = diag(diag(CPD.cov(:,:,i)));
|
wolffd@0
|
129 end
|
wolffd@0
|
130 end
|
wolffd@0
|
131 end
|
wolffd@0
|
132 end
|
wolffd@0
|
133
|
wolffd@0
|
134
|
wolffd@0
|
135 check_covars = 0;
|
wolffd@0
|
136 min_covar = 1e-5;
|
wolffd@0
|
137 if check_covars % prevent collapsing to a point
|
wolffd@0
|
138 for i=1:dpsize
|
wolffd@0
|
139 if min(svd(CPD.cov(:,:,i))) < min_covar
|
wolffd@0
|
140 disp(['resetting singular covariance for node ' num2str(CPD.self)]);
|
wolffd@0
|
141 CPD.cov(:,:,i) = CPD.init_cov(:,:,i);
|
wolffd@0
|
142 end
|
wolffd@0
|
143 end
|
wolffd@0
|
144 end
|
wolffd@0
|
145
|
wolffd@0
|
146
|
wolffd@0
|
147
|