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