Skip to content

Commit

Permalink
fix demo in chapter03
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Dec 2, 2015
1 parent d971fd8 commit 8339820
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
26 changes: 14 additions & 12 deletions chapter01/demo.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

% Done
% demo for information theory toolbox
clear;
k = 10; % variable range
n = 100; % number of variables

Expand All @@ -12,27 +14,27 @@
Hx = entropy(x);
Hy = entropy(y);
%% joint entropy H(x,y)
Hxy = jointEntropy(x, y);
Hxy = jointEntropy(x,y);
%% conditional entropy H(x|y)
Hx_y = condEntropy(x, y);
Hx_y = condEntropy(x,y);
%% mutual information I(x,y)
Ixy = mutInfo(x, y);
Ixy = mutInfo(x,y);
%% relative entropy (KL divergence) KL(p(x)|p(y))
Dxy = relatEntropy(x, y);
Dxy = relatEntropy(x,y);
%% normalized mutual information I_n(x,y)
nIxy = nmi(x, y);
nIxy = nmi(x,y);
%% nomalized variation information I_v(x,y)
vIxy = nvi(x, y);
vIxy = nvi(x,y);
%% H(x|y) = H(x,y)-H(y)
isequalf(Hx_y, Hxy-Hy)
isequalf(Hx_y,Hxy-Hy)
%% I(x,y) = H(x)-H(x|y)
isequalf(Ixy, Hx-Hx_y)
isequalf(Ixy,Hx-Hx_y)
%% I(x,y) = H(x)+H(y)-H(x,y)
isequalf(Ixy, Hx+Hy-Hxy)
isequalf(Ixy,Hx+Hy-Hxy)
%% I_n(x,y) = I(x,y)/sqrt(H(x)*H(y))
isequalf(nIxy, Ixy/sqrt(Hx*Hy))
isequalf(nIxy,Ixy/sqrt(Hx*Hy))
%% I_v(x,y) = (1-I(x,y)/H(x,y))
isequalf(vIxy, 1-Ixy/Hxy)
isequalf(vIxy,1-Ixy/Hxy)



25 changes: 15 additions & 10 deletions chapter03/demo.m
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
% 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
% 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);
%%
model = regress(X, t);
model = linReg(X,t);
y = linInfer(x, model);
figure;
hold on;
Expand All @@ -18,7 +23,7 @@
hold off
pause
%%
[model,llh] = regressEbEm(X,t);
[model,llh] = linRegEbEm(X,t);
[y, sigma] = linInfer(x,model,t);
figure;
hold on;
Expand All @@ -30,7 +35,7 @@
plot(llh);
pause
%%
[model,llh] = regressEbFp(X,t);
[model,llh] = linRegEbFp(X,t);
[y, sigma] = linInfer(x,model,t);
figure;
hold on;
Expand Down
19 changes: 19 additions & 0 deletions chapter03/linRnd.asv
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function [X, t, model] = linRnd(d, n, prior)
% Generate a data from a linear model p(t|w,x)=G(w'x+w0,sigma), where w and w0 are
% generated from G(0,
% d: dimension of data
% n: number of data
% prior: a structure specify the prior

if nargin < 3
sigma = 1;
else
sigma = prior.sigma;
end
X = rand(d,n);
w = randn(d,1);
w0 = randn(1,1);
epsilon = sigma^2*randn(1,n);
t = w'*X+w0+
model.w = w;
model.w0 = w0;
20 changes: 20 additions & 0 deletions chapter03/linRnd.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function [X, t, model] = linRnd(d, n, prior)
% Generate a data from a linear model p(t|w,x)=G(w'x+w0,sigma), where w and w0 are
% generated from G(0,1)
% d: dimension of data
% n: number of data
% prior: a structure specify the prior

if nargin < 3
sigma = 1;
else
sigma = prior.sigma;
end
X = rand(d,n);
w = randn(d,1);
w0 = randn(1,1);
epsilon = sigma^2*randn(1,n);
t = w'*X+w0+epsilon;
model.w = w;
model.w0 = w0;
model.sigma = sigma;

0 comments on commit 8339820

Please sign in to comment.