wolffd@0: function [muY, SigmaY, weightsY] = linear_regression(X, Y, varargin) wolffd@0: % LINEAR_REGRESSION Fit params for P(Y|X) = N(Y; W X + mu, Sigma) wolffd@0: % wolffd@0: % X(:, t) is the t'th input example wolffd@0: % Y(:, t) is the t'th output example wolffd@0: % wolffd@0: % Kevin Murphy, August 2003 wolffd@0: % wolffd@0: % This is a special case of cwr_em with 1 cluster. wolffd@0: % You can also think of it as a front end to clg_Mstep. wolffd@0: wolffd@0: [cov_typeY, clamp_weights, muY, SigmaY, weightsY,... wolffd@0: cov_priorY, regress, clamp_covY] = process_options(... wolffd@0: varargin, ... wolffd@0: 'cov_typeY', 'full', 'clamp_weights', 0, ... wolffd@0: 'muY', [], 'SigmaY', [], 'weightsY', [], ... wolffd@0: 'cov_priorY', [], 'regress', 1, 'clamp_covY', 0); wolffd@0: wolffd@0: [nx N] = size(X); wolffd@0: [ny N2] = size(Y); wolffd@0: if N ~= N2 wolffd@0: error(sprintf('nsamples X (%d) ~= nsamples Y (%d)', N, N2)); wolffd@0: end wolffd@0: wolffd@0: w = 1/N; wolffd@0: WYbig = Y*w; wolffd@0: WYY = WYbig * Y'; wolffd@0: WY = sum(WYbig, 2); wolffd@0: WYTY = sum(diag(WYbig' * Y)); wolffd@0: if ~regress wolffd@0: % This is just fitting an unconditional Gaussian wolffd@0: weightsY = []; wolffd@0: [muY, SigmaY] = ... wolffd@0: mixgauss_Mstep(1, WY, WYY, WYTY, ... wolffd@0: 'cov_type', cov_typeY, 'cov_prior', cov_priorY); wolffd@0: % There is a much easier way... wolffd@0: assert(approxeq(muY, mean(Y'))) wolffd@0: assert(approxeq(SigmaY, cov(Y') + 0.01*eye(ny))) wolffd@0: else wolffd@0: % This is just linear regression wolffd@0: WXbig = X*w; wolffd@0: WXX = WXbig * X'; wolffd@0: WX = sum(WXbig, 2); wolffd@0: WXTX = sum(diag(WXbig' * X)); wolffd@0: WXY = WXbig * Y'; wolffd@0: [muY, SigmaY, weightsY] = ... wolffd@0: clg_Mstep(1, WY, WYY, WYTY, WX, WXX, WXY, ... wolffd@0: 'cov_type', cov_typeY, 'cov_prior', cov_priorY); wolffd@0: end wolffd@0: if clamp_covY, SigmaY = SigmaY; end wolffd@0: if clamp_weights, weightsY = weightsY; end wolffd@0: wolffd@0: if nx==1 & ny==1 & regress wolffd@0: P = polyfit(X,Y); % Y = P(1) X^1 + P(2) X^0 = ax + b wolffd@0: assert(approxeq(muY, P(2))) wolffd@0: assert(approxeq(weightsY, P(1))) wolffd@0: end wolffd@0: wolffd@0: %%%%%%%% Test wolffd@0: if 0 wolffd@0: c1 = randn(2,100); c2 = randn(2,100); wolffd@0: y = c2(1,:); X = [ones(size(c1,2),1) c1']; wolffd@0: b = regress(y(:), X); % stats toolbox wolffd@0: [m,s,w] = linear_regression(c1, y); wolffd@0: assert(approxeq(b(1),m)) wolffd@0: assert(approxeq(b(2), w(1))) wolffd@0: assert(approxeq(b(3), w(2))) wolffd@0: end