Skip to content

Commit

Permalink
fix chapter03
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Dec 3, 2015
1 parent ed9e167 commit 8768953
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 124 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
Pattern Recognition and Machine Learning
===========

This package contains the matlab implementation of the algorithms described in the book:
Pattern Recognition and Machine Learning by C. Bishop (http://research.microsoft.com/en-us/um/people/cmbishop/prml/)


License
-------
Currently Released Under GPLv3


Contact
-------
sth4nth(CHEN, Mo) [username] at gmail dot com
sth4nth at gmail dot com

KuantKid(LI,Wei) [username] at gmail dot com
5 changes: 4 additions & 1 deletion chapter01/nmi.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function z = nmi(x, y)
% Compute nomalized mutual information I(x,y)/sqrt(H(x)*H(y)).
% Compute normalized mutual information I(x,y)/sqrt(H(x)*H(y)).
% x, y: two vectors of integers of the same length
% Written by Mo Chen ([email protected]).
assert(numel(x) == numel(y));
Expand All @@ -18,6 +18,8 @@
Pxy = nonzeros(Mx'*My/n); %joint distribution of x and y
Hxy = -dot(Pxy,log2(Pxy));


% hacking, to elimative the 0log0 issue
Px = nonzeros(mean(Mx,1));
Py = nonzeros(mean(My,1));

Expand All @@ -31,3 +33,4 @@
% normalized mutual information
z = sqrt((MI/Hx)*(MI/Hy));
z = max(0,z);

2 changes: 1 addition & 1 deletion chapter01/nvi.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function z = nvi(x, y)
% Compute nomalized variation information (1-I(x,y)/H(x,y)).
% Compute normalized variation information (1-I(x,y)/H(x,y)).
% x, y: two vectors of integers of the same length
% Written by Mo Chen ([email protected]).
assert(numel(x) == numel(y));
Expand Down
47 changes: 12 additions & 35 deletions chapter03/demo.m
Original file line number Diff line number Diff line change
@@ -1,48 +1,25 @@
% Done
% demo for chapter 03
clear; close all;
n = 100;
beta = 1e-1;
X = rand(1,n);
w = randn;
b = randn;
t = w'*X+b+beta*randn(1,n);
x = linspace(min(X)-1,max(X)+1,n); % test data

% d = 1;
% n = 200;
% [X,t,model] = linRnd(d,n);
% plotBand(X,t,2*model.sigma);
d = 1;
n = 200;
[x,t,model] = linRnd(d,n);
linPlot(model,x,t);
%%
model = linReg(X,t);
y = linPred(model,x);
figure;
hold on;
plot(X,t,'o');
plot(x,y,'r-');
hold off
model = linReg(x,t);
linPlot(model,x,t);
fprintf('Press any key to continue. \n');
pause
%%
[model,llh] = linRegEbEm(X,t);
[y, sigma] = linPred(model,x,t);
[model,llh] = linRegEbEm(x,t);
linPlot(model,x,t);
figure;
hold on;
plotBand(x,y,2*sigma);
plot(X,t,'o');
plot(x,y,'r-');
hold off
figure
plot(llh);
linPlot(model,x,t)
fprintf('Press any key to continue. \n');
pause
%%
[model,llh] = linRegEbFp(X,t);
[model,llh] = linRegEbFp(x,t);
[y, sigma] = linPred(model,x,t);
linPlot(model,x,t);
figure;
hold on;
plotBand(x,y,2*sigma);
plot(X,t,'o');
plot(x,y,'r-');
hold off
figure
plot(llh);
48 changes: 0 additions & 48 deletions chapter03/demo.m~

This file was deleted.

15 changes: 9 additions & 6 deletions chapter03/linPlot.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
function linPlot(model, x, t)
% Plot linear function and data
% X: 1xn data
% t: 1xn response
% Written by Mo Chen ([email protected]).
color = [255,228,225]/255; %pink
[y,sigma] = linPred(model,x,t);
h = 2*sigma;

[x,idx] = sort(x);
t = t(idx);
[y,s] = linPred(model,x);
figure;
hold on;
x = x(:);
y = y(:);
fill([x;flipud(x)],[y+h;flipud(y-h)],color);
fill([x,fliplr(x)],[y+s,fliplr(y-s)],color);
plot(x,t,'o');
plot(x,y,'r-');
hold off

19 changes: 0 additions & 19 deletions chapter03/linPlot.m~

This file was deleted.

8 changes: 5 additions & 3 deletions chapter03/linPred.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
w = model.w;
w0 = model.w0;
y = w'*X+w0;
if nargin == 3
if nargout > 1
beta = model.beta;
if isfield(model,'V') % V*V'=inv(S) 3.54
U = model.V'*bsxfun(@minus,X,model.xbar);
sigma = sqrt(1/beta+dot(U,U,1)); % 3.59
else
sigma = sqrt(1/beta);
end
p = exp(logGauss(t,y,sigma));
% p = exp(-0.5*(((t-y)./sigma).^2+log(2*pi))-log(sigma));
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
end
3 changes: 2 additions & 1 deletion chapter03/linReg.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
U = chol(S);
w = U\(U'\(X*t')); % 3.15 & 3.28
w0 = tbar-dot(w,xbar); % 3.19

beta = 1/mean((t-w'*X).^2); % 3.21
model.w = w;
model.w0 = w0;
model.beta = beta;
6 changes: 3 additions & 3 deletions chapter03/linRnd.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
% X is generated form [0,1]
% d: dimension of data
% n: number of data
beta = gamrnd(1,1); % need statistcs toolbox
beta = gamrnd(10,10); % need statistcs toolbox
X = rand(d,n);
w = randn(d,1);
w0 = randn(1,1);
epsilon = randn(1,n)/beta;
t = w'*X+w0+epsilon;
err = randn(1,n)/sqrt(beta);
t = w'*X+w0+err;

model.w = w;
model.w0 = w0;
Expand Down
4 changes: 0 additions & 4 deletions chapter03/test.m

This file was deleted.

2 changes: 1 addition & 1 deletion helper/plotBand.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ function plotBand(x, y, h, color)
x = x(:);
y = y(:);
h = h(:);
fill([x;flipud(x)],[y+h;flipud(y-h)],color);
fill([x;flipud(x)]',[y+h;flipud(y-h)]',color);

0 comments on commit 8768953

Please sign in to comment.