Skip to content

Commit

Permalink
knRegPred.m is not finished
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Dec 15, 2015
1 parent 82530f6 commit 8ec5aed
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 33 deletions.
23 changes: 11 additions & 12 deletions chapter03/linPred.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
w = model.w;
w0 = model.w0;
y = w'*X+w0;

if nargout > 1
beta = model.beta;
if isfield(model,'U') % 3.54
U = model.U;
Xo = bsxfun(@minus,X,model.xbar);
XU = U'\Xo;
sigma = sqrt(1/beta+dot(XU,XU,1));
else
sigma = sqrt(1/beta);
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
U = model.U; % 3.54
Xo = bsxfun(@minus,X,model.xbar);
XU = U'\Xo;
sigma = sqrt(1/beta+dot(XU,XU,1));
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

31 changes: 25 additions & 6 deletions chapter06/demo.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,29 @@
x = linspace(0,2*pi,n); % test data
t = sin(x)+rand(1,n)/2;

model = knReg(x,t,1e-4,@knGauss);
y = knRegPred(model, x);
figure;
hold on;
plot(x,t,'o');
plot(x,y,'r-');
% model = knReg(x,t,1e-4,@knGauss);
% y = knRegPred(model, x);
% figure;
% hold on;
% plot(x,t,'o');
% plot(x,y,'r-');


%% test case for kernel regression
lambda = 1e-4;
model_kn = knReg(x,t,lambda,@knLin);
model_lin = linReg(x,t,lambda);

[y_kn, s_kn] = knRegPred(model_kn, x);
[y_lin, s_lin] = linPred(model_lin,x);

maxabsdiff(y_kn,y_lin)
maxabsdiff(s_kn,s_lin)
%% test case for knCenter
% kn = @knGauss;
% X=rand(2,100);
% X1=rand(2,10);
% X2=rand(2,5);
%
% isequalf(knCenter(kn,X,X1),diag(knCenter(kn,X,X1,X1)))
% isequalf(knCenter(kn,X),knCenter(kn,X,X,X));
6 changes: 6 additions & 0 deletions chapter06/knPca.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function [ output_args ] = knPca( input_args )
% Kernel PCA


end

3 changes: 2 additions & 1 deletion chapter06/knReg.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
model.a = a;
model.X = X;
model.tbar = tbar;
model.beta = beta;
model.beta = beta;
model.U = U;
22 changes: 8 additions & 14 deletions chapter06/knRegPred.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@
a = model.a;
X = model.X;
tbar = model.tbar;
y = a'*knCenterize(kn,X,Xt)+tbar;
y = a'*knCenter(kn,X,X,Xt)+tbar;

if nargout > 1
beta = model.beta;
if isfield(model,'U')
U = model.U;
Xo = bsxfun(@minus,X,model.xbar);
XU = U'\Xo;
sigma = sqrt(1/beta+dot(XU,XU,1));

sigma = sqrt(c-k'*C^-1*k);
else
sigma = sqrt(1/beta); % 6.67
end
if nargin == 3 && nargout == 3
p = exp(logGauss(t,y,sigma));
end
U = model.U;

sigma = sqrt(knCenter(kn,X,Xt)+1/beta-dot(XU,XU,1));
end

if nargin == 3 && nargout == 3
p = exp(logGauss(t,y,sigma));
end

0 comments on commit 8ec5aed

Please sign in to comment.