コード例 #1
0
/*
  This routine clusters a dataset into an optimal number of groups using the 
  E-M algorithm and a choice of AIC or BIC and with start points provided by 
  the shortems function.  
  The function returns    the classification ids as well as the parameter 
  estimates. The parameter estimates are created in the routine and should not
  be allocated before the function call. This also means that the parameter 
  estimates have to be cleaned after the function call. (NOTE: This is an 
  important fact to remember. */
int em_EM(double **x,int n,int p,int nclass,double *pi,double **Mu,
	  double **LTSigma,double *llhdval,int *nc,int shortiter,
	  double shorteps)
{
  int flag=0;
  double like;

  if (nclass==1) {
/* These formulae are not correct with two errors,
   1. n-1 should be replace by n in meandispersion() for LTSigma, and
   2. determinant() will change the values of LTSigma[0], see "initials.c".
   Modified: Wei-Chen Chen on 2008/12/05.

    meandispersion(x,n,p,Mu[0],LTSigma[0]);
*/
    meandispersion_MLE(x,n,p,Mu[0],LTSigma[0]);
    like=-0.5*n*p-0.5*n*log(determinant(LTSigma[0],p))-0.5*n*p*log(2*PI);
    (*llhdval)=like;
  }
  else {
    shortems(n,p,nclass,pi,x,Mu,LTSigma,shortiter,shorteps);
    emcluster(n,p,nclass,pi,x,Mu,LTSigma,1000,0.0001,&like);
    (*llhdval)=like;
  } 
  return flag;
}
コード例 #2
0
ファイル: mb_init.c プロジェクト: cran/EMCluster
void cut_sub(double **X, int n, int p, int G, int min_n, double lambda,
    double *prob, double **Mu, double **LTSigma){
  int i, index_center, size_nb, *index_prob;
  int tmp_G = G - 1, tmp_min_n;
  double new_pi[1] = {1.0}, **new_Mu, **new_LTSigma, **new_X;
  double tmp_center;

  /* Get the seed state from R. */
  GetRNGstate();

  /* Use inverse CDF to sample a new center according to the given prob. */
  for(i = 1; i < n; i++) prob[i] = prob[i] + prob[i - 1];
  tmp_center = runif(0, prob[n - 1]); 

  if(tmp_center <= prob[0]){
    index_center = 0;
  } else{
    for(index_center = 1; index_center < n; index_center++){
      if(tmp_center > prob[index_center - 1] &&
         tmp_center <= prob[index_center]) break;
    }
  }

  /* Based on the new center to estimate the new ltsigma. */
  new_Mu = allocate_double_array(1);
  new_LTSigma = allocate_double_array(1);
  new_Mu[0] = Mu[tmp_G];
  new_LTSigma[0] = LTSigma[tmp_G];
  for(i = 0; i < p; i++) new_Mu[0][i] = X[index_center][i];
  est_ltsigma_mle_given_mu(X, n, p, new_Mu[0], new_LTSigma[0]);

  /* Compute prob based on the new center and ltsigma, and according
     to the prob to find the neighbors with size min.n + rpois(1, lambda). */
  for(i = 0; i < n; i++){
    prob[i] = mixllhd(p, 1, X[i], new_pi, new_Mu, new_LTSigma);
  }
  index_prob = (int *) orderDouble(prob, n); /* This is an increasing order. */
  size_nb = min_n + (int) rpois(lambda);

  /* Based on the neighbors to estimate Mu and LTSigma. */
  new_X = allocate_double_array(size_nb);
  tmp_min_n = n - size_nb;
  for(i = 0; i < size_nb; i++) new_X[i] = X[index_prob[tmp_min_n + i]];
  meandispersion_MLE(new_X, size_nb, p, new_Mu[0], new_LTSigma[0]);

  /* Release memory and set new seed state to R. */
  PutRNGstate();
  free(new_X);
  free(new_Mu);
  free(new_LTSigma);
  FREE_VECTOR(index_prob);
} /* End of cut_sub(). */
コード例 #3
0
/* This is the model-based em.EM. */
int mb_em_EM(double **x, int n, int p, int nclass, double *pi, double **Mu,
    double **LTSigma, double *llhdval, int shortiter, double shorteps){
  if(nclass == 1){
    meandispersion_MLE(x, n, p, Mu[0], LTSigma[0]);
    *llhdval = -0.5 * n * p - 0.5 * n * log(determinant(LTSigma[0], p)) -
               0.5 * n * p * log(2 * PI);
  }
  else {
    shortems_mb(n, p, nclass, pi, x, Mu, LTSigma, shortiter, shorteps);
    emcluster(n, p, nclass, pi, x, Mu, LTSigma, 1000, 0.0001, llhdval);
  } 

  return 0;
} /* END of mb_em_EM(). */