forked from PRML/PRMLT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinRegPred.m
32 lines (30 loc) · 870 Bytes
/
linRegPred.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function [y, sigma, p] = linRegPred(model, X, t)
% Compute linear regression model reponse y = w'*X+w0 and likelihood
% Input:
% model: trained model structure
% X: d x n testing data
% t (optional): 1 x n testing response
% Output:
% y: 1 x n prediction
% sigma: variance
% p: 1 x n likelihood of t
% Written by Mo Chen ([email protected]).
w = model.w;
w0 = model.w0;
y = w'*X+w0;
%% probability prediction
if nargout > 1
beta = model.beta;
if isfield(model,'U')
U = model.U; % 3.54
Xo = bsxfun(@minus,X,model.xbar);
XU = U'\Xo;
sigma = sqrt((1+dot(XU,XU,1))/beta); % 3.59
else
sigma = sqrt(1/beta)*ones(1,size(X,2));
end
end
if nargin == 3 && nargout == 3
p = exp(logGauss(t,y,sigma));
% p = exp(-0.5*(((t-y)./sigma).^2+log(2*pi))-log(sigma));
end