comparison toolboxes/FullBNT-1.0.7/KPMstats/cwr_em.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 cwr = cwr_em(X, Y, nc, varargin)
2 % CWR_LEARN Fit the parameters of a cluster weighted regression model using EM
3 % function cwr = cwr_learn(X, Y, ...)
4 %
5 % X(:, t) is the t'th input example
6 % Y(:, t) is the t'th output example
7 % nc is the number of clusters
8 %
9 % Kevin Murphy, May 2003
10
11 [max_iter, thresh, cov_typeX, cov_typeY, clamp_weights, ...
12 muX, muY, SigmaX, SigmaY, weightsY, priorC, create_init_params, ...
13 cov_priorX, cov_priorY, verbose, regress, clamp_covX, clamp_covY] = process_options(...
14 varargin, 'max_iter', 10, 'thresh', 1e-2, 'cov_typeX', 'full', ...
15 'cov_typeY', 'full', 'clamp_weights', 0, ...
16 'muX', [], 'muY', [], 'SigmaX', [], 'SigmaY', [], 'weightsY', [], 'priorC', [], ...
17 'create_init_params', 1, 'cov_priorX', [], 'cov_priorY', [], 'verbose', 0, ...
18 'regress', 1, 'clamp_covX', 0, 'clamp_covY', 0);
19
20 [nx N] = size(X);
21 [ny N2] = size(Y);
22 if N ~= N2
23 error(sprintf('nsamples X (%d) ~= nsamples Y (%d)', N, N2));
24 end
25 %if N < nx
26 % fprintf('cwr_em warning: dim X (%d) > nsamples X (%d)\n', nx, N);
27 %end
28 if (N < nx) & regress
29 fprintf('cwr_em warning: dim X = %d, nsamples X = %d\n', nx, N);
30 end
31 if (N < ny)
32 fprintf('cwr_em warning: dim Y = %d, nsamples Y = %d\n', ny, N);
33 end
34 if (nc > N)
35 error(sprintf('cwr_em: more centers (%d) than data', nc))
36 end
37
38 if nc==1
39 % No latent variable, so there is a closed-form solution
40 w = 1/N;
41 WYbig = Y*w;
42 WYY = WYbig * Y';
43 WY = sum(WYbig, 2);
44 WYTY = sum(diag(WYbig' * Y));
45 cwr.priorC = 1;
46 cwr.SigmaX = [];
47 if ~regress
48 % This is just fitting an unconditional Gaussian
49 cwr.weightsY = [];
50 [cwr.muY, cwr.SigmaY] = ...
51 mixgauss_Mstep(1, WY, WYY, WYTY, ...
52 'cov_type', cov_typeY, 'cov_prior', cov_priorY);
53 % There is a much easier way...
54 assert(approxeq(cwr.muY, mean(Y')))
55 assert(approxeq(cwr.SigmaY, cov(Y') + 0.01*eye(ny)))
56 else
57 % This is just linear regression
58 WXbig = X*w;
59 WXX = WXbig * X';
60 WX = sum(WXbig, 2);
61 WXTX = sum(diag(WXbig' * X));
62 WXY = WXbig * Y';
63 [cwr.muY, cwr.SigmaY, cwr.weightsY] = ...
64 clg_Mstep(1, WY, WYY, WYTY, WX, WXX, WXY, ...
65 'cov_type', cov_typeY, 'cov_prior', cov_priorY);
66 end
67 if clamp_covY, cwr.SigmaY = SigmaY; end
68 if clamp_weights, cwr.weightsY = weightsY; end
69 return;
70 end
71
72
73 if create_init_params
74 [cwr.muX, cwr.SigmaX] = mixgauss_init(nc, X, cov_typeX);
75 [cwr.muY, cwr.SigmaY] = mixgauss_init(nc, Y, cov_typeY);
76 cwr.weightsY = zeros(ny, nx, nc);
77 cwr.priorC = normalize(ones(nc,1));
78 else
79 cwr.muX = muX; cwr.muY = muY; cwr.SigmaX = SigmaX; cwr.SigmaY = SigmaY;
80 cwr.weightsY = weightsY; cwr.priorC = priorC;
81 end
82
83
84 if clamp_covY, cwr.SigmaY = SigmaY; end
85 if clamp_covX, cwr.SigmaX = SigmaX; end
86 if clamp_weights, cwr.weightsY = weightsY; end
87
88 previous_loglik = -inf;
89 num_iter = 1;
90 converged = 0;
91
92 while (num_iter <= max_iter) & ~converged
93
94 % E step
95
96 [likXandY, likYgivenX, post] = cwr_prob(cwr, X, Y);
97 loglik = sum(log(likXandY));
98 % extract expected sufficient statistics
99 w = sum(post,2); % post(c,t)
100 WYY = zeros(ny, ny, nc);
101 WY = zeros(ny, nc);
102 WYTY = zeros(nc,1);
103
104 WXX = zeros(nx, nx, nc);
105 WX = zeros(nx, nc);
106 WXTX = zeros(nc, 1);
107 WXY = zeros(nx,ny,nc);
108 %WYY = repmat(reshape(w, [1 1 nc]), [ny ny 1]) .* repmat(Y*Y', [1 1 nc]);
109 for c=1:nc
110 weights = repmat(post(c,:), ny, 1);
111 WYbig = Y .* weights;
112 WYY(:,:,c) = WYbig * Y';
113 WY(:,c) = sum(WYbig, 2);
114 WYTY(c) = sum(diag(WYbig' * Y));
115
116 weights = repmat(post(c,:), nx, 1); % weights(nx, nsamples)
117 WXbig = X .* weights;
118 WXX(:,:,c) = WXbig * X';
119 WX(:,c) = sum(WXbig, 2);
120 WXTX(c) = sum(diag(WXbig' * X));
121 WXY(:,:,c) = WXbig * Y';
122 end
123
124 % M step
125 % Q -> X is called Q->Y in Mstep_clg
126 [cwr.muX, cwr.SigmaX] = mixgauss_Mstep(w, WX, WXX, WXTX, ...
127 'cov_type', cov_typeX, 'cov_prior', cov_priorX);
128 for c=1:nc
129 assert(is_psd(cwr.SigmaX(:,:,c)))
130 end
131
132 if clamp_weights % affects estimate of mu and Sigma
133 W = cwr.weightsY;
134 else
135 W = [];
136 end
137 [cwr.muY, cwr.SigmaY, cwr.weightsY] = ...
138 clg_Mstep(w, WY, WYY, WYTY, WX, WXX, WXY, ...
139 'cov_type', cov_typeY, 'clamped_weights', W, ...
140 'cov_prior', cov_priorY);
141 %'xs', X, 'ys', Y, 'post', post); % debug
142 %a = linspace(min(Y(2,:)), max(Y(2,:)), nc+2);
143 %cwr.muY(2,:) = a(2:end-1);
144
145 cwr.priorC = normalize(w);
146
147 for c=1:nc
148 assert(is_psd(cwr.SigmaY(:,:,c)))
149 end
150
151 if clamp_covY, cwr.SigmaY = SigmaY; end
152 if clamp_covX, cwr.SigmaX = SigmaX; end
153 if clamp_weights, cwr.weightsY = weightsY; end
154
155 if verbose, fprintf(1, 'iteration %d, loglik = %f\n', num_iter, loglik); end
156 num_iter = num_iter + 1;
157 converged = em_converged(loglik, previous_loglik, thresh);
158 previous_loglik = loglik;
159
160 end
161