예제 #1
0
파일: em.cpp 프로젝트: MekaBot/SEDSros
void fgmm_m_step(struct gmm * GMM,
                 const _fgmm_real * data,
                 int data_len,
                 _fgmm_real * pix,
                 int * reestimate_flag,
                 enum COVARIANCE_TYPE covar_t)
{
    int state_i,k;
    int random_point = 0;
    for(state_i=0; state_i<GMM->nstates; state_i++)
    {
        GMM->gauss[state_i].prior = 0;
        for(k=0; k<GMM->dim; k++)
            GMM->gauss[state_i].mean[k] = 0;

        switch(covar_t)
        {
        case COVARIANCE_DIAG :
            GMM->gauss[state_i].prior = smat_covariance_diag(GMM->gauss[state_i].covar,
                                        data_len,
                                        &pix[state_i*data_len],
                                        data,
                                        GMM->gauss[state_i].mean);
            break;

        case COVARIANCE_SPHERE :
            GMM->gauss[state_i].prior = smat_covariance_single(GMM->gauss[state_i].covar,
                                        data_len,
                                        &pix[state_i*data_len],
                                        data,
                                        GMM->gauss[state_i].mean);
            break;

        default :
            GMM->gauss[state_i].prior = smat_covariance(GMM->gauss[state_i].covar,
                                        data_len,
                                        &pix[state_i*data_len],
                                        data,
                                        GMM->gauss[state_i].mean);
            break;
        }


        // If no point belong to us, reassign to a random one ..
        if(GMM->gauss[state_i].prior == 0)
        {
            random_point = rand()%data_len;
            for(k=0; k<GMM->dim; k++)
                GMM->gauss[state_i].mean[k] = data[random_point*GMM->dim + k];
            *reestimate_flag = 1; // then we shall restimate mean/covar of this cluster
        }
        else
        {
            GMM->gauss[state_i].prior /= data_len;
            invert_covar(&GMM->gauss[state_i]);
        }
    }


}
예제 #2
0
int main(int argc,char ** argv)
{
  int i;
  float vn=0;
  int sig_perc=0, sig3_perc=0;
  struct gaussian g;
  float v[] = {1.,1.,1.};
  float pdf=0;
  _fgmm_real samp[3];
  _fgmm_real * data;
  _fgmm_real * weights;
  int k=0;
  struct smat * cv=NULL;
  _fgmm_real mean[3];
  _fgmm_real * pcv;
  
  srand(time(NULL));
  printf("gaussian test suite, each test uses %d samples \n",randn_samples);

  printf("box_muller testing :\n");
  for(i=0;i<randn_samples;i++)
    {
      vn = randn_boxmuller();
      if( fabs(vn) < 1.)
	sig_perc++;
      else if( fabs(vn) >= 2.)
	sig3_perc++;
    }
  //printf("%d %d\n",sig_perc,sig3_perc);
  assert( abs(sig_perc - one_sigma*randn_samples) < randn_samples / 400);
  assert( abs(sig3_perc - three_sigma*randn_samples) < randn_samples / 400);
  printf("..pass \n");

  /* ----------------------------------------------- */
  printf("simple gaussian pdf test :\n");
  
  gaussian_init(&g,3);
  invert_covar(&g);
  assert(g.dim == 3);

  pdf = gaussian_pdf(&g,v);
  assert(fabs(pdf - value) < 1e-5);
  printf("..pass \n");
  
  /* ----------------------------------------------- */
  printf("drawing sample from gaussian test :\n");
  data = (_fgmm_real *) malloc( sizeof(_fgmm_real) * randn_samples * 3);
  weights = (_fgmm_real *) malloc(sizeof(_fgmm_real) * randn_samples);
  for(i=0;i<randn_samples;i++)
    {
      weights[i] = 1.;
      gaussian_draw(&g,samp);
      //printf("%f %f %f\n",samp[0],samp[1],samp[2]);
      for(k=0;k<3;k++)
	{
	  data[3*i+k] = samp[k];
	}
    }

  smat_zero(&cv,3);
  smat_covariance(cv,randn_samples,weights,data,mean);
  //  smat_pmatrix(cv);
  /* checking covariance is identity */
  pcv = cv->_;
  for(i=0;i<3;i++)
    {
      assert(fabs(mean[i]) < 1e-2);
      assert( fabs(*(pcv++)- 1.) < 2e-2);
      for(k=i+1;k<3;k++)
	assert(fabs(*pcv++) < 1e-2);
      
    }
  printf("..pass\n");  
  return EXIT_SUCCESS;
}