This commit is contained in:
Michael Zhang 2023-10-08 01:50:20 -05:00
parent 563015a0fb
commit d79046d7b9

View file

@ -6,6 +6,13 @@ clf;
prior1 = 0.3; prior1 = 0.3;
prior2 = 0.7; prior2 = 0.7;
% mvndis = @(X, mu, Sigma, prior) ( ...
% -1/2 * log(2*pi) ...
% - log(Sigma) ...
% - power(X - mu, 2) / (2 * power(Sigma, 2)) ...
% + log(prior) ...
% );
mu1 = [-1 -1]; mu1 = [-1 -1];
mu2 = [1 1]; mu2 = [1 1];
@ -30,13 +37,15 @@ mu2 = [1 1];
[X1,X2] = meshgrid(x1,x2); [X1,X2] = meshgrid(x1,x2);
%pdf1 %pdf1
F1 = mvnpdf([X1(:) X2(:)],mu1,Sigma1); % F1 = mvnpdf([X1(:) X2(:)],mu1,Sigma1);
F1 = mvndis([X1(:) X2(:)], mu1, Sigma1, prior1);
F1 = reshape(prior1 * F1,length(x2),length(x1)); F1 = reshape(prior1 * F1,length(x2),length(x1));
subplot(1,2,1); subplot(1,2,1);
surf(x1,x2,F1); hold on; surf(x1,x2,F1); hold on;
%pdf2 %pdf2
F2 = mvnpdf([X1(:) X2(:)],mu2,Sigma2); % F2 = mvnpdf([X1(:) X2(:)],mu2,Sigma2);
F2 = mvndis([X1(:) X2(:)], mu2, Sigma2, prior2);
F2 = reshape(prior2 * F2,length(x2),length(x1)); F2 = reshape(prior2 * F2,length(x2),length(x1));
surf(x1,x2,F2); surf(x1,x2,F2);
caxis([min(F2(:))-.5*range(F2(:)),max(F2(:))]); caxis([min(F2(:))-.5*range(F2(:)),max(F2(:))]);
@ -54,3 +63,30 @@ subplot(1,2,2);
imagesc(X1(:),X2(:),cmp); imagesc(X1(:),X2(:),cmp);
xlabel('x1'); ylabel('x2'); xlabel('x1'); ylabel('x2');
function res = mvndis(X, mu, Sigma, prior)
[len, ~] = size(X);
res = zeros(len, 1);
for i = 1:len
x = X(i,:);
mdist = (x - mu) * inv(Sigma) * (x - mu).';
res(i) = -log(2*pi) - 1/2*log(det(Sigma)) - 1/2*mdist + log(prior);
end
% 1 x 2
% (1 x 2) x ((2 x 2) x (2 x 1))
% X - mu = 40401 x 2
% (40401 x 2) x (2 x 2) x (2 x 40401)
% mdist = (X - mu) * inv(Sigma) * (X - mu).';
% res = -log(2*pi) - 1/2*log(det(Sigma)) - 1/2*mdist + log(prior)
% res = zeros(size(X));
% [l1, l2] = size(X);
%
% for i1 = 1:l1
% for i2 = 1:l2
% cell = -log(2*pi) - 1/2*log(det(Sigma))
% res(i1, i2) = cell
% end
% end
end