annotate _FullBNT/KPMstats/linear_regression.m @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
rev   line source
matthiasm@8 1 function [muY, SigmaY, weightsY] = linear_regression(X, Y, varargin)
matthiasm@8 2 % LINEAR_REGRESSION Fit params for P(Y|X) = N(Y; W X + mu, Sigma)
matthiasm@8 3 %
matthiasm@8 4 % X(:, t) is the t'th input example
matthiasm@8 5 % Y(:, t) is the t'th output example
matthiasm@8 6 %
matthiasm@8 7 % Kevin Murphy, August 2003
matthiasm@8 8 %
matthiasm@8 9 % This is a special case of cwr_em with 1 cluster.
matthiasm@8 10 % You can also think of it as a front end to clg_Mstep.
matthiasm@8 11
matthiasm@8 12 [cov_typeY, clamp_weights, muY, SigmaY, weightsY,...
matthiasm@8 13 cov_priorY, regress, clamp_covY] = process_options(...
matthiasm@8 14 varargin, ...
matthiasm@8 15 'cov_typeY', 'full', 'clamp_weights', 0, ...
matthiasm@8 16 'muY', [], 'SigmaY', [], 'weightsY', [], ...
matthiasm@8 17 'cov_priorY', [], 'regress', 1, 'clamp_covY', 0);
matthiasm@8 18
matthiasm@8 19 [nx N] = size(X);
matthiasm@8 20 [ny N2] = size(Y);
matthiasm@8 21 if N ~= N2
matthiasm@8 22 error(sprintf('nsamples X (%d) ~= nsamples Y (%d)', N, N2));
matthiasm@8 23 end
matthiasm@8 24
matthiasm@8 25 w = 1/N;
matthiasm@8 26 WYbig = Y*w;
matthiasm@8 27 WYY = WYbig * Y';
matthiasm@8 28 WY = sum(WYbig, 2);
matthiasm@8 29 WYTY = sum(diag(WYbig' * Y));
matthiasm@8 30 if ~regress
matthiasm@8 31 % This is just fitting an unconditional Gaussian
matthiasm@8 32 weightsY = [];
matthiasm@8 33 [muY, SigmaY] = ...
matthiasm@8 34 mixgauss_Mstep(1, WY, WYY, WYTY, ...
matthiasm@8 35 'cov_type', cov_typeY, 'cov_prior', cov_priorY);
matthiasm@8 36 % There is a much easier way...
matthiasm@8 37 assert(approxeq(muY, mean(Y')))
matthiasm@8 38 assert(approxeq(SigmaY, cov(Y') + 0.01*eye(ny)))
matthiasm@8 39 else
matthiasm@8 40 % This is just linear regression
matthiasm@8 41 WXbig = X*w;
matthiasm@8 42 WXX = WXbig * X';
matthiasm@8 43 WX = sum(WXbig, 2);
matthiasm@8 44 WXTX = sum(diag(WXbig' * X));
matthiasm@8 45 WXY = WXbig * Y';
matthiasm@8 46 [muY, SigmaY, weightsY] = ...
matthiasm@8 47 clg_Mstep(1, WY, WYY, WYTY, WX, WXX, WXY, ...
matthiasm@8 48 'cov_type', cov_typeY, 'cov_prior', cov_priorY);
matthiasm@8 49 end
matthiasm@8 50 if clamp_covY, SigmaY = SigmaY; end
matthiasm@8 51 if clamp_weights, weightsY = weightsY; end
matthiasm@8 52
matthiasm@8 53 if nx==1 & ny==1 & regress
matthiasm@8 54 P = polyfit(X,Y); % Y = P(1) X^1 + P(2) X^0 = ax + b
matthiasm@8 55 assert(approxeq(muY, P(2)))
matthiasm@8 56 assert(approxeq(weightsY, P(1)))
matthiasm@8 57 end
matthiasm@8 58
matthiasm@8 59 %%%%%%%% Test
matthiasm@8 60 if 0
matthiasm@8 61 c1 = randn(2,100); c2 = randn(2,100);
matthiasm@8 62 y = c2(1,:); X = [ones(size(c1,2),1) c1'];
matthiasm@8 63 b = regress(y(:), X); % stats toolbox
matthiasm@8 64 [m,s,w] = linear_regression(c1, y);
matthiasm@8 65 assert(approxeq(b(1),m))
matthiasm@8 66 assert(approxeq(b(2), w(1)))
matthiasm@8 67 assert(approxeq(b(3), w(2)))
matthiasm@8 68 end