% ANDERS_EXP1 Example on estimation density model for Anderson's task
%

% Modiifcations:
% 25-aug-2004, VF

% loads input data
%-------------------------------------------------
trn_data = load('../data/brodatz3_trn');
tst_data = load('../data/brodatz3_tst');

% separate data of the first and the second class
%-------------------------------------------------
inx1 = find(trn_data.y==1);
inx2 = find(trn_data.y==2);
class_data1.X = trn_data.X(:,inx1);
class_data1.y = trn_data.v(inx1);
class_data2.X = trn_data.X(:,inx2);
class_data2.y = trn_data.v(inx2);

% ML estimation of GMM from complete data
%-------------------------------------------------
Pxy1 = mlcgmm(class_data1);
Pxy2 = mlcgmm(class_data2);

% ML estimation of a priori probabilities
%-------------------------------------------------
P1 = length(inx1)/(length(inx1)+length(inx2));
P2 = length(inx2)/(length(inx1)+length(inx2));

% take Gaussian components for Generalized Anderson's task
%-------------------------------------------------
gauss_model.Mean = [Pxy1.Mean Pxy2.Mean];
gauss_model.Cov = [Pxy1.Cov(:)' Pxy2.Cov(:)'];
gauss_model.y = [ones(1,length(Pxy1.Mean)) 2*ones(1,length(Pxy2.Mean))];


% plot models
%-------------------------------------------------
figure; ppatterns(trn_data); hold on; grid on;
a = axis; x = linspace(a(1),a(2),100);

y1 = P1*pdfgmm(x,Pxy1);
y2 = P2*pdfgmm(x,Pxy2);

h1=plot(x,y1,'--b'); h2=plot(x,y2,'--r');
set([h1 h2],'linewidth',2);
legend([h1 h2],'P(y=1)*P(x|y=1)','P(y=2)*P(x|y=2)');

pgauss( gauss_model );

% EOF