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