forked from PRML/PRMLT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refined ch02. logMn needs to be checked
- Loading branch information
Showing
8 changed files
with
58 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|