Skip to content

Commit

Permalink
refined ch02. logMn needs to be checked
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Feb 19, 2016
1 parent 22a3ef5 commit 0400bf7
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 15 deletions.
6 changes: 4 additions & 2 deletions chapter02/logDirichlet.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
function y = logDirichlet(X, a)
% Compute log pdf of a Dirichlet distribution.
% Input:
% X: d x n data matrix satifying (sum(X,1)==ones(1,n) && X>=0)
% a: d x k parameters
% X: d x n data matrix, each column sums to one (sum(X,1)==ones(1,n) && X>=0)
% a: d x k parameter of Dirichlet
% y: k x n probability density
% Output:
% y: k x n probability density in logrithm scale y=log p(x)
% Written by Mo Chen ([email protected]).
X = bsxfun(@times,X,1./sum(X,1));
if size(a,1) == 1
Expand Down
6 changes: 6 additions & 0 deletions chapter02/logGauss.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function y = logGauss(X, mu, sigma)
% Compute log pdf of a Gaussian distribution.
% Input:
% X: d x n data matrix
% mu: mean of Gaussian
% sigma: variance of Gaussian
% Output:
% y: probability density in logrithm scale y=log p(x)
% Written by Mo Chen ([email protected]).

[d,n] = size(X);
Expand Down
7 changes: 6 additions & 1 deletion chapter02/logKde.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function z = logKde (X, Y, sigma)
% Compute log pdf of kernel density estimator.
% Input:
% X: d x n data matrix to be evaluate
% Y: d x k data matrix served as database
% Output:
% z: probability density in logrithm scale z=log p(x|y)
% Written by Mo Chen ([email protected]).
D = bsxfun(@plus,full(dot(X,X,1)),full(dot(Y,Y,1))')-full(2*(Y'*X));
z = logSumExp(D/(-2*sigma^2),1)-0.5*log(2*pi)-log(sigma*size(Y,2));
z = logsumexp(D/(-2*sigma^2),1)-0.5*log(2*pi)-log(sigma*size(Y,2),1);
21 changes: 13 additions & 8 deletions chapter02/logMn.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
function z = logMn (x, p)
% Compute log pdf of a multinomial distribution.
% Input:
% x: d x n data matrix
% p: 1 x k probability
% Output:
% z: probability density in logrithm scale z=log p(x)
% Written by Mo Chen ([email protected]).
if numel(x) ~= numel(p)
n = numel(x);
x = reshape(x,1,n);
[u,~,label] = unique(x);
x = full(sum(sparse(label,1:n,1,n,numel(u),n),2));
end
z = gammaln(sum(x)+1)-sum(gammaln(x+1))+dot(x,log(p));
endfunction
if numel(x) ~= numel(p)
n = numel(x);
x = reshape(x,1,n);
label = zeros(1,n);
[u,~,label(:)] = unique(x);
x = full(sum(sparse(label,1:n,1,n,numel(u),n),2));
end
z = gammaln(sum(x)+1)-sum(gammaln(x+1))+dot(x,log(p));
12 changes: 9 additions & 3 deletions chapter02/logMvGamma.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
function y = logMvGamma(x,d)
% Compute logarithm multivariate Gamma function.
% Gamma_p(x) = pi^(d(d-1)/4) \prod_(j=1)^d Gamma(x+(1-j)/2)
% log(Gamma_p(x)) = d(d-1)/4 log(pi) + \sum_(j=1)^d log(Gamma(x+(1-j)/2))
% Compute logarithm multivariate Gamma function
% which is used in the probability density function of the Wishart and inverse Wishart distributions.
% Gamma_d(x) = pi^(d(d-1)/4) \prod_(j=1)^d Gamma(x+(1-j)/2)
% log(Gamma_d(x)) = d(d-1)/4 log(pi) + \sum_(j=1)^d log(Gamma(x+(1-j)/2))
% Input:
% x: m x n data matrix
% d: dimension
% Output:
% y: m x n logarithm multivariate Gamma
% Written by Michael Chen ([email protected]).
s = size(x);
x = reshape(x,1,prod(s));
Expand Down
7 changes: 7 additions & 0 deletions chapter02/logSt.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function y = logSt(X, mu, sigma, v)
% Compute log pdf of a Student's t distribution.
% Input:
% X: d x n data matrix
% mu: mean
% sigma: variance
% v: degree of freedom
% Output:
% y: probability density in logrithm scale y=log p(x)
% Written by mo Chen ([email protected]).
[d,k] = size(mu);

Expand Down
6 changes: 6 additions & 0 deletions chapter02/logVmf.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function y = logVmf(X, mu, kappa)
% Compute log pdf of a von Mises-Fisher distribution.
% Input:
% X: d x n data matrix
% mu: d x k mean
% kappa: 1 x k variance
% Output:
% y: k x n probability density in logrithm scale y=log p(x)
% Written by Mo Chen ([email protected]).
d = size(X,1);
c = (d/2-1)*log(kappa)-(d/2)*log(2*pi)-logbesseli(d/2-1,kappa);
Expand Down
8 changes: 7 additions & 1 deletion chapter02/logWishart.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function y = logWishart(Sigma, v, W)
function y = logWishart(Sigma, W, v)
% Compute log pdf of a Wishart distribution.
% Input:
% Sigma: d x d covariance matrix
% W: d x d covariance parameter
% v: degree of freedom
% Output:
% y: probability density in logrithm scale y=log p(Sigma)
% Written by Mo Chen ([email protected]).
d = length(Sigma);
B = -0.5*v*logdet(W)-0.5*v*d*log(2)-logmvgamma(0.5*v,d);
Expand Down

0 comments on commit 0400bf7

Please sign in to comment.