function KLDistanceAndLikelihoodFuction = SingleCluster(N1,N2,Shift1,Shift2); X1 = randn(N1,1)+Shift1; %Generate the first normal distribution with prior N1/(N1+N2) and mean at Shift1; X2 = randn(N2,1)+Shift2; %Generate the second normal distribution with prior N2/(N1+N2) and mean at Shift2; Xaxis = -10:0.01:10; subplot(2,2,1); hist(X1); title('Input data set 1'); subplot(2,2,2); hist(X2); title('Input data set 2'); meanX1 = mean(X1); meanX2 = mean(X2); covX1 = cov(X1); covX2= cov(X2); %Calculate the mean and covariance of both sets of data X3 = [X1, X2] ; %Combine the two sets of data meanX3 = mean(X3); %Calculate the mean of the combined data covX3 = cov(X3); %Calculate the covariance of the combined data P =0.4*(1/((2*pi*abs(covX1))^0.5))*exp(-0.5*covX1^-1*(Xaxis-meanX1).*(Xaxis-meanX1))+0.6*(1/((2*pi*abs(covX2))^0.5))*exp(-0.5*covX2^-1*(Xaxis-meanX2).*(Xaxis-meanX2)); %Calculate the input sets distribution subplot(2,2,3); scatter(Xaxis,P); title('Input data distribution'); P1 = (1/((2*pi*abs(covX3))^0.5))*exp(-0.5*covX3^-1*(Xaxis-meanX3).*(Xaxis-meanX3)); %Estimate the combined data by one Gaussian distribution subplot(2,2,4); scatter(Xaxis,P1); title('Single Gaussian Cluster Estimation'); D= sum(P1.*log(P1./P)); %Calculate the KL distance L = sum(log(P1)); %Calculate the log-Likelihood function value fprintf( 'The Value of KL Distance is %g.\n',D); fprintf( 'The Value of log likelihood fuction is %g.\n',L);