Skip to content

Commit

Permalink
refined doc for ch11. demo needs check
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Feb 20, 2016
1 parent 530a17f commit a83ac01
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 55 deletions.
1 change: 1 addition & 0 deletions chapter11/Gauss.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
% Class for Gaussian distribution used by Dirichlet process
classdef Gauss
properties
n_
Expand Down
36 changes: 19 additions & 17 deletions chapter11/GaussWishart.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
% Class for Gaussian-Wishart distribution used by Dirichlet process

classdef GaussWishart
properties
kappa_
Expand All @@ -18,23 +20,23 @@
function obj = clone(obj)
end

% function obj = addData(obj, X)
% kappa0 = obj.kappa_;
% m0 = obj.m_;
% nu0 = obj.nu_;
% U0 = obj.U_;
%
% n = size(X,2);
% kappa = kappa0+n;
% m = (kappa0*m0+sum(X,2))/kappa;
% nu = nu0+n;
% U = chol(U0'*U0+X*X');
%
% obj.kappa_ = kappa;
% obj.m_ = m;
% obj.nu_ = nu;
% obj.U_ = U;
% end
function obj = addData(obj, X)
kappa0 = obj.kappa_;
m0 = obj.m_;
nu0 = obj.nu_;
U0 = obj.U_;

n = size(X,2);
kappa = kappa0+n;
m = (kappa0*m0+sum(X,2))/kappa;
nu = nu0+n;
U = chol(U0'*U0+X*X');

obj.kappa_ = kappa;
obj.m_ = m;
obj.nu_ = nu;
obj.U_ = U;
end

function obj = addSample(obj, x)
kappa = obj.kappa_;
Expand Down
44 changes: 17 additions & 27 deletions chapter11/demo.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@

%% demo for sequential update Gaussian
% demos for ch11
%% DPGM
close all; clear;
d = 2;
k = 3;
n = 1000;
[X,label] = mixGaussRnd(d,k,n);
plotClass(X,label);

[y,model] = mixGaussGb(X);
figure
plotClass(X,y);
%% Sequential update for Gaussian
% close all; clear;
% d = 2;
% n = 100;
Expand All @@ -11,10 +23,10 @@
% Sigma = Xo*Xo'/n;
% p1 = logGauss(x,mu,Sigma);
%
% gauss = Gaussian(X(:,3:end)).addSample(X(:,1)).addSample(X(:,2)).addSample(X(:,3)).delSample(X(:,3));
% gauss = Gauss(X(:,3:end)).addSample(X(:,1)).addSample(X(:,2)).addSample(X(:,3)).delSample(X(:,3));
% p2 = gauss.logPdf(x);
% abs(p1-p2)
%% Gaussian Wishart
% maxdiff(p1,p2)
% %% Sequential update for Gaussian-Wishart
% close all; clear;
% d = 2;
% n = 100;
Expand All @@ -33,7 +45,6 @@
% Xo = bsxfun(@minus,X,m);
% X0 = m0-m;
% S = S0+Xo*Xo'+kappa0*(X0*X0');
% % S = S0+X*X'+kappa0*m0*m0'-kappa*m*m';
%
% v = (nu-d+1);
% r = (1+1/kappa)/v;
Expand All @@ -48,26 +59,5 @@
% gw = gw.addSample(X(:,i));
% end
% p2 = gw.logPredPdf(x);
% abs(p1-p2)
%% Demo for DPGM
% close all; clear;
% d = 2;
% k = 3;
% n = 1000;
% [X,label] = mixGaussRnd(d,k,n);
% plotClass(X,label);
% maxdiff(p1,p2)
%
% [y,model] = mixGaussGb(X);
% figure
% plotClass(X,y);
%% Demo for DPGM
close all; clear;
d = 2;
k = 3;
n = 200;
[X,label] = mixGaussRnd(d,k,n);
plotClass(X,label);

[y,theta,w,llh] = mixGaussGb(X);
figure
plotClass(X,y);
5 changes: 4 additions & 1 deletion chapter11/dirichletRnd.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
function x = dirichletRnd(a, m)
% Sampling from a Dirichlet distribution.
% Generate samples from a Dirichlet distribution.
% Input:
% a: k dimensional vector
% m: k dimensional mean vector
% Outpet:
% x: generated sample x~Dir(a,m)
% Written by Mo Chen ([email protected]).
if nargin == 2
a = a*m;
Expand Down
7 changes: 6 additions & 1 deletion chapter11/discreteRnd.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function x = discreteRnd(p, n)
% Sampling from a discrete distribution (multinomial).
% Generate samples from a discrete distribution (multinomial).
% Input:
% p: k dimensional probability vector
% n: number of samples
% Ouput:
% x: k x n generated samples x~Mul(p)
% Written by Mo Chen ([email protected]).
if nargin == 1
n = 1;
Expand Down
10 changes: 8 additions & 2 deletions chapter11/gaussRnd.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function x = gaussRnd(mu,Sigma,n)
% Sampling from a Gaussian distribution.
function x = gaussRnd(mu, Sigma, n)
% Generate samples from a Gaussian distribution.
% Input:
% mu: d x 1 mean vector
% Sigma: d x d covariance matrix
% n: number of samples
% Outpet:
% x: d x n generated sample x~Gauss(mu,Sigma)
% Written by Mo Chen ([email protected]).
if nargin == 2
n = 1;
Expand Down
14 changes: 12 additions & 2 deletions chapter11/mixDpGb.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
function [label, Theta, w, llh] = mixDpGb(X, alpha, theta)
% Collapsed Gibbs sampling for Dirichlet process (infinite) mixture model (a.k.a.
% DPGM). Any component model can be used, such as Gaussian
% Collapsed Gibbs sampling for Dirichlet process (infinite) mixture model.
% Any component model can be used, such as Gaussian.
% Input:
% X: d x n data matrix
% alpha: parameter for Dirichlet process prior
% theta: class object for prior of component distribution (such as Gauss)
% Output:
% label: 1 x n cluster label
% Theta: 1 x k structure of trained components
% w: 1 x k component weight vector
% llh: loglikelihood
% Written by Mo Chen ([email protected]).
n = size(X,2);
[label,Theta,w] = mixDpGbOl(X,alpha,theta);
nk = n*w;
Expand Down
13 changes: 11 additions & 2 deletions chapter11/mixDpGbOl.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
function [label, Theta, w, llh] = mixDpGbOl(X, alpha, theta)
% Online collapsed Gibbs sampling for Dirichlet process (infinite) mixture model (a.k.a.
% DPGM). Any component model can be used, such as Gaussian
% Online collapsed Gibbs sampling for Dirichlet process (infinite) mixture model.
% Input:
% X: d x n data matrix
% alpha: parameter for Dirichlet process prior
% theta: class object for prior of component distribution (such as Gauss)
% Output:
% label: 1 x n cluster label
% Theta: 1 x k structure of trained components
% w: 1 x k component weight vector
% llh: loglikelihood
% Written by Mo Chen ([email protected]).
n = size(X,2);
Theta = {};
nk = [];
Expand Down
13 changes: 11 additions & 2 deletions chapter11/mixGaussGb.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
function [label, Theta, w, llh] = mixGaussGb(X, opt)
% Collapsed Gibbs sampling for Dirichlet process (infinite) Gaussian mixture model (a.k.a.
% DPGM).
% Collapsed Gibbs sampling for Dirichlet process (infinite) Gaussian mixture model (a.k.a. DPGM).
% This is a wrapper function which calls underlying Dirichlet process mixture model.
% Input:
% X: d x n data matrix
% opt(optional): prior parameters
% Output:
% label: 1 x n cluster label
% Theta: 1 x k structure of trained Gaussian components
% w: 1 x k component weight vector
% llh: loglikelihood
% Written by Mo Chen ([email protected]).
[d,n] = size(X);
mu = mean(X,2);
Xo = bsxfun(@minus,X,mu);
Expand Down
2 changes: 1 addition & 1 deletion chapter12/demo.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
[model, energy] = ppcaVb(X);
plot(energy);
%
%% factor analysis
%% Factor analysis
[W, mu, psi, llh] = fa(X, m);
plot(llh);

0 comments on commit a83ac01

Please sign in to comment.