%% 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 1; 1 5 ]
%   Sigma = [.25 0; 0 1];
  %  Sigma = [.25 .3; .3 1];
x1 = -6:.2:6; x2 = -6:.2:6;
[X1,X2] = meshgrid(x1,x2);
F = mvnpdf([X1(:) X2(:)],mu,Sigma);
F = reshape(F,length(x2),length(x1));
figure(10); clf;
surf(x1,x2,F);
caxis([min(F(:))-.5*range(F(:)),max(F(:))]);
axis([-6 6 -6 6 0 2])
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
pim = [0.7; 0.3];
figure(20);

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

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

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



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

figure(30);

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

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

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