%% Compute the Multivariate Normal pdf
%

% Copyright 2015 The MathWorks, Inc.


%%
% Compute and plot the pdf of a multivariate normal distribution.
mu = [0 0];
% Sigma = [1 0; 0 1];
%   Sigma = [.25 0; 0 1];
  Sigma = [.25 .3; .3 1];
x1 = -3:.2:3; x2 = -3:.2:3;
[X1,X2] = meshgrid(x1,x2);
F = mvnpdf([X1(:) X2(:)],mu,Sigma);
F = reshape(F,length(x2),length(x1));
figure(1)
surf(x1,x2,F);
caxis([min(F(:))-.5*range(F(:)),max(F(:))]);
axis([-3 3 -3 3 0 .4])
xlabel('x1'); ylabel('x2'); zlabel('Probability Density');


% first distribution is centered at (0,0), second at (-1,3)
mu = [0 0; 3 3];
% covariance of first is identity matrix, second diagonal
%  sigma = cat(3, eye(2), [5 0; 0 1]);
% sigma = cat(3, eye(2), [4 0; 0 0.9]);
%  sigma = cat(3, eye(2), [4 1.5; 1.5 0.9]);
% again I'm using equal priors
p = [0.7; 0.3];


 mu = [0 0; 3 3; -1 4];
 sigma = cat(3, eye(2), [5 0.25; 0.25 1], [1 0; 0 1]);
 p = [0.1; 0.8; 0.1];

figure(2);

% build GMM
gmm = gmdistribution(mu, sigma, p);

% 2D projection
ezcontourf(@(x,y) pdf(gmm,[x y]));

% view PDF surface
ezsurfc(@(x,y) pdf(gmm,[x y]));