Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% Load data
- clc;
- clearvars;
- close all;
- load('dataGMM.mat');
- %% Preprocess data
- m = size(Data,2);
- n = size(Data,1);
- PIfunc = @(idx, c) length(idx(idx==c))/length(idx);
- % meanfunc = @(x,n,theta) 1;
- covfunc = @(x,mean) ((x-mean')*(x-mean')')/(length(x)-1);
- covfuncMATLAB = @(x,mean) cov((x-mean')');
- GaussDist = @(x,meanfunc,covfunc) 1;
- loglklhd = @(PI,Mu,SIGMA) 1;
- % plot data
- X = Data;
- figure(1);
- plot(X(1,:),X(2,:),'k*','MarkerSize',5); hold on;
- title 'Data';
- xlabel 'X1';
- ylabel 'X2';
- K = 4; % num of clusters
- %% Init parameters
- [idx, MU] = kmeans(X', K);
- % calc PI
- PI = zeros(K,1);
- for c=1:K
- PI(c) = PIfunc(idx,c);
- end
- SIGMA = zeros(2,2,4);
- for i=1:K
- % SIGMA(:,:,i) = covfuncMATLAB(X(idx==i),MU(i,:))
- SIGMA(:,:,i) = cov((X(:,idx==i))');
- end
- %%
- %Evaluate the pdf of the normal distribution at the grid points.
- for k=1:K
- mu = MU(k,:);
- sigma = SIGMA(:,:,k);
- % Create a grid of 100x100 evenly spaced points in two-dimensional space.
- dim_grid = 100;
- x1 = linspace(-0.1,0.1,dim_grid);
- x2 = linspace(-0.1,0.1,dim_grid);
- [X1,X2] = meshgrid(x1',x2'); % grid 100x100
- X = [X1(:) X2(:)];
- % Evaluate the cdf of the normal distribution at the grid points.
- p = PI(k)*mvnpdf(X,mu,sigma);
- % Plot the cdf values.
- Z = reshape(p,dim_grid,dim_grid);
- figure(2);
- surf(X1,X2,Z);
- caxis([min(Z(:))-0.5*range(Z(:)),max(Z(:))]); hold on;
- scatter(mu(1), mu(2),50,'r'); hold on;
- end
- view(0,90);
- xlabel('x1')
- ylabel('x2')
- zlabel('Probability Density');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement