function [compParams,loglikes,compHistory] = EM(X, k, initfunc, initparams, ...
						histfreq, ...
						epsilon, maxittr, minittr)
  if nargin<3, initfunc = 'initRandPoints'; end;
  if nargin<4, initparams = {}; end;
  if nargin<5, histfreq = 100; end;
  if nargin<6, epsilon = 1e-8; end;
  if nargin<7, maxittr = 2e3; end;
  if nargin<8, minittr = 2; end;

  compHistory = {};
  loglikes = [];
  compParams = feval(initfunc,k,X,initparams{:}) ;
  
  nittr = 1;
  cont = 1;
  lastll = -inf;
  while (cont)
    if (histfreq & (rem(nittr-1,histfreq)==0))
      compHistory = {compHistory{:} compParams};
    end;

    p = EstepX(X,compParams);
    compParams = Mstep(X,p);
    loglikelihood = mixtureLLX(X,compParams);
    
    loglikes = [loglikes, loglikelihood];
    
    cont = (((nittr<minittr) | (loglikelihood>lastll+epsilon)) ...
	& nittr<maxittr) ;
    lastll = loglikelihood;
    nittr = nittr+1;
  end;

      