D = 2; %% Dimensions N_PTS = [100 100 100 100]; N_GRPS = length(N_PTS); %% How many points per cluster, how many clusters RADIUS = 0.8; %DENSITY = 'uniform'; DENSITY = 'gaussian'; clrs = ['r'; 'g'; 'b'; 'k'; 'c']; nclrs = length(clrs); %% Generate data and show them as well as original densities X = []; switch DENSITY, case 'uniform', X = rand(sum(N_PTS),D); figure(1); plot(X(:,1),X(:,2),'.'); set(1,'DoubleBuffer','on'); title('Ground truth is uniform'); case 'gaussian', MIX_TRUE = gmm(D, N_GRPS, 'full'); %MIX_TRUE.centres = MIX_TRUE.centres * RADIUS; figure(1); clf; set(1,'DoubleBuffer','on'); for g=1:N_GRPS, clr = clrs(mod(g-1,nclrs)+1); SS = RADIUS*(rand(D,D)-0.5); MIX_TRUE.covars(:,:,g) = SS*SS'; X1 = gsamp(MIX_TRUE.centres(g,:),MIX_TRUE.covars(:,:,g),N_PTS(g)); X = [X ; X1]; draw_ellipse(MIX_TRUE.centres(g,:),MIX_TRUE.covars(:,:,g),clr, MIX_TRUE.priors(g)*30); hold on;plot(X1(:,1),X1(:,2),['o' clr]);hold off; end; title(['Ground truth is a mixture model with ' num2str(N_GRPS) ' components']); end; pause(4); %% Starting from data try and estimate densities using gmmem SHOW_ELLIPSES = 5; model_types = {'full','diag', 'spherical'}; P_RATIO = [6 9 12 16]; %Ratio between n. of training examples and number of model parameters model_params = [ D+D*(D+1)/2; 2*D; D+1]; N_GROUPS_TEST = ceil( 0.5*sum(N_PTS)./(model_params * P_RATIO)); OPTIONS = zeros(1,14); OPTIONS(1) = 0; OPTIONS(3) = 1; OPTIONS(5)=1; OPTIONS(14)=SHOW_ELLIPSES; N_ITE = 100; for m=1:length(model_types), for NG=N_GROUPS_TEST(m,:), i_g = find(NG==N_GROUPS_TEST(m,:)); MIX = gmm(D, NG, model_types{m}); MIX = gmminit(MIX, X(1:2:end,:), OPTIONS); loger = []; ERR_MIN = 0; for it=1:ceil(N_ITE/SHOW_ELLIPSES), figure(2); clf [MIX, OPTIONS, ERRLOG] = gmmem(MIX,X(1:2:end,:),OPTIONS); loger(it) = -OPTIONS(8); if it>1, if abs(loger(it)-loger(it-1))