Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 9/Programming Assignment - 8/ex8/multivariateGaussian.m
616 views
1
function p = multivariateGaussian(X, mu, Sigma2)
2
%MULTIVARIATEGAUSSIAN Computes the probability density function of the
3
%multivariate gaussian distribution.
4
% p = MULTIVARIATEGAUSSIAN(X, mu, Sigma2) Computes the probability
5
% density function of the examples X under the multivariate gaussian
6
% distribution with parameters mu and Sigma2. If Sigma2 is a matrix, it is
7
% treated as the covariance matrix. If Sigma2 is a vector, it is treated
8
% as the \sigma^2 values of the variances in each dimension (a diagonal
9
% covariance matrix)
10
%
11
12
k = length(mu);
13
14
if (size(Sigma2, 2) == 1) || (size(Sigma2, 1) == 1)
15
Sigma2 = diag(Sigma2);
16
end
17
18
X = bsxfun(@minus, X, mu(:)');
19
p = (2 * pi) ^ (- k / 2) * det(Sigma2) ^ (-0.5) * ...
20
exp(-0.5 * sum(bsxfun(@times, X * pinv(Sigma2), X), 2));
21
22
end
23