function O = ccgmbuild(x,xt,naive)

%ccgmbuild Build a class conditional gaussian mixture model
%
%         Description
%
%         O = ccgmbuild(x,xt,naive) builds a class conditional
%         gaussian mixture model for a set of training data. The 
%         attribute values for the training data are given in x, and
%         the classes are given in xt (in a matrix with one column per
%         class, with 1 if the case belongs to the column's class and 
%         0 otherwise). If the parameter 'naive' is 1, a naive
%         bayesian classifier is built (ie, a diagonal covariance
%         matrix is used). Otherwise the function makes a full
%         conditional gaussian model.  
%
%         The output of the function is a cell array O, with the
%         multidimensional means of the classes (O{1}), their
%         covariance matrices (O{2}), and the prior probabilities of
%         the classes (O{3}). 
%
%         Frederick Ducatelle, januari 2002, University of Edinburgh

dim = size(xt);
for i = 1:dim(2)
  X{i} = x(xt(:,i)==1,:);       % Split the data according to class
  N{i} = size(X{i});            % Define the number in each class
  M{i} = mean(X{i});            % The multidimensional mean in each class
  if (naive == 1)
    C{i} = diag(diag(cov(X{i}))); % The naive covariance matrix
  else 
    C{i} = cov(X{i});           % The full covariance matrix for each class
  end
  P{i} = N{i}(1)/dim(1);        % Unconditional probability of each class
end

O{1} = M;
O{2} = C;
O{3} = P;
