MATLAB Answers

Maximum a Posteriori (MAP) and Maximum Likelihood (ML)

160 views (last 30 days)
Amine STOFF
Amine STOFF on 2 Feb 2014
Commented: Sonny Revell on 4 Mar 2021
Hello everyone. I have a serious problem here with developping this matlab code. I'm new with Matlab.
We want to solve a problem of binary classification . Forthis we have two distributions that have a degree of overlap determined. We want to apply the Bayesian hypothesis test to discriminate between the two classes.
P(Wi|x)=(P(Wi)*P(x|Wi))/P(x)
In this case, we use a parametric estimation of the fdp (probability density functions), and we will consider fdp's from normal distributions. The work involved is as followings:
1- generate 2 gausiens distributions with a degree of overlap determined.
2- Estimate the parameters of distributions: the mean vector and matrix covariance.
3- Conceive the Bayesian test : P(Wi|x)>P(Wj|x).decide the class i
4- Draw the decision boundary
For the two first questions i already solved it by this matlab code :
clear all, close all , clc
sigma=1; % sigma is changed depending on the dispersion desired
N=500;
P1=sigma*randn(2,500)+[2;2]*ones(1,500);
P2=sigma*randn(2,500)+[-2;-2]*ones(1,500);
T1=ones(1,500);
T2=zeros(1,500);
%Data generation
plot(P1(1,:),P1(2,:),'ro',P2(1,:),P2(2,:),'go')
hold on
%we generates the random values
P=[P1,P2];
T=[T1,T2];
ind=randperm(2*N);
P=P(:,ind);
T=T(ind);
Mx1 = mean(P1,2);
Mx2 = mean(P2,2);
for i=1:N
p=P(:,i)
s1= exp(-(p-Mx1)'*(p-Mx1)/(2*sigma^2)) % First Maximum Likelihood classifier
s2= exp(-(p-Mx2)'*(p-Mx2)/(2*sigma^2))
For the last question here is the code of the function that defines the boundary:
function []=frontiere(w,P,T,x)
%--------------------------------------------------------------------------
% function []=frontiere(W,P,T,x)
% This function displays the border of classification determined by the Bayesian
%discriminant designed to separate between two distribution
% W: weight matrices model
% P: dataset
% T: desired output (Target)
% x: coordinates which determines the central position of distributions.
%--------------------------------------------------------------------------
%disp(' ')
%disp(' Determining the decision boundary')
%disp(' ')
X = [-x:0.02:x]; Y = [-x:0.02:x];
for i=1:size(X,2)
for j=1:size(Y,2)
p = [X(i);Y(j)];
% IN THIS PART WE MUST IMPLEMENT THE BAYESIAN DISCRIMINANT
% a2 = output of Bayes classifier
end
end
plotpv(P,T==1);
hold on
contour(X,Y,a2,1);
drawnow
  2 Comments

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!