Пример #1
0
	double LogPrior(hvector theta) {
		std::vector<double> mu(theta.begin(), theta.begin() + pchi);
		std::vector<double> logsigma(theta.begin() + pchi, theta.begin() + 2 * pchi);
		std::vector<double> arctanh_corr(theta.begin() + 2 * pchi, theta.end());
		double mean_prior = MeanPrior(mu);
		double sigma_prior = SigmaPrior(logsigma);
		double corr_prior = CorrPrior(arctanh_corr);
		//std::cout << "Priors: " << mean_prior << ", " << sigma_prior << ", " << corr_prior << std::endl;
		return MeanPrior(mu) + SigmaPrior(logsigma) + CorrPrior(arctanh_corr);
	}
Пример #2
0
float loglik(float z[MAXV][MAXC], bool A[MAXV][MAXV],int V, int C, float beta, float Threshold)
{
  static float loglik, field;
  static int i,j,c;
  loglik=0.0;
  for (i=0;i<V;i++){
    for (j=0;j<i+1;j++){ // check if can do this -- otherwise use j<V
      field=0.0;
      for (c=0;c<C;c++){
	field+=z[i][c]*z[j][c];
      }
      if (A[i][j])
	{loglik+=logsigma(field,beta,Threshold);}
      else
	{loglik+=clogsigma(field,beta,Threshold);}
    }
  }
  return loglik;
}
Пример #3
0
void updateZ(char OutputFile[40], float zm[MAXV][MAXC], float zma[MAXV][MAXC], float ma[MAXC], bool A[MAXV][MAXV], int Zloops, int C, int V, int Vtoupdate, float beta, float Threshold,bool DISP_ERRORS) 
{int *permC, *permV, loopz, loopc, cluster, loopvertexj, loopvertexk, vertexi,vertexk, vertexj, cl, i, j,k, errors, tmpi, tmpj;
  float meanfield0,meanfield1,logfn1,logfn0,loglik1,loglik0,tmp, copybeta, dellog;
  float Mold[MAXV][MAXV], zc[MAXV], Mk[MAXV];
  static bool AA[MAXV][MAXV];
  FILE *fpLinkFile, *fpOutputFile, *fpInitialFile;

  for (loopz=0;loopz<Zloops;loopz++)
    {
      for (vertexi=0;vertexi<V;vertexi++)
	{for (vertexj=0;vertexj<V;vertexj++)
	    {
	      Mold[vertexi][vertexj]=0.0;
	      for (cl=0;cl<C;cl++){
		Mold[vertexi][vertexj]+=zma[vertexi][cl]*zma[vertexj][cl];}
	    }
	}
      
      permC=randperm(C);	      
      for (loopc=0;loopc<C;loopc++)
	{cluster=permC[loopc];
	  
	  for (vertexi=0;vertexi<V;vertexi++) {zc[vertexi]=zma[vertexi][cluster];}
	  permV=randperm(V);
	  for (loopvertexk=0;loopvertexk<Vtoupdate;loopvertexk++)
	    {vertexk=permV[loopvertexk];
	      for (vertexi=0;vertexi<V;vertexi++)
		{Mk[vertexi]=Mold[vertexi][vertexk]-zc[vertexi]*zc[vertexk]+zma[vertexi][cluster]*zma[vertexk][cluster];}
	      logfn1=0.0;logfn0=0.0;
	      for (vertexj=0;vertexj<V;vertexj++)
		{meanfield0=Mk[vertexj]-zma[vertexj][cluster]*zma[vertexk][cluster]-0.5;
		  meanfield1=zma[vertexj][cluster]+meanfield0; 
		  if (A[vertexj][vertexk]==1)
		    {logfn1+=logsigma(meanfield1,beta,0.0);
		      logfn0+= logsigma(meanfield0,beta,0.0);
		    }
		  else
		    {logfn1+= logsigma(-meanfield1,beta,0.0);
		      logfn0+=  logsigma(-meanfield0,beta,0.0);
		    }
		}
	      dellog=2*(logfn0-logfn1);
	      zm[vertexk][cluster]=1.0/(1.0+exp(dellog));
	      zma[vertexk][cluster]=zm[vertexk][cluster]*ma[cluster];
	    }
	  
	  for (vertexi=0;vertexi<V;vertexi++)
	    {for (vertexj=0;vertexj<V;vertexj++)
		{Mold[vertexi][vertexj]+=-zc[vertexi]*zc[vertexj]+zma[vertexi][cluster]*zma[vertexj][cluster];
		}
	    }
	}

      printf("\n C[%d] Zloop[%d] beta[%4.2f]",C,loopz,beta);
      /* compute the errors */	 
      if (DISP_ERRORS==1){
	errors=0;
	for (i=0;i<V;i++)
	  {
	    for (j=0;j<i+1;j++)
	      {tmp=0;
		for (k=0;k<C;k++)
		  {tmp+=zma[i][k]*zma[j][k];}
		if (tmp>Threshold){AA[i][j]=1;}
		else {AA[i][j]=0;}
		AA[j][i]=AA[i][j];
		if (AA[i][j]!=A[i][j]){errors+=+1;} 
	      }
	  }
	printf(", errors(threshold<Z><Z'>)=%d",errors);}
      
      
      /* compute the errors */	 
      if (DISP_ERRORS==1){
	errors=0;
	for (i=0;i<V;i++)
	  {
	    for (j=0;j<i+1;j++)
	      {tmp=0;
		for (k=0;k<C;k++)
		  {tmp+= (zm[i][k]>Threshold)*(zm[j][k]>Threshold);}
		if (tmp>0){AA[i][j]=1;}
		else {AA[i][j]=0;}
		AA[j][i]=AA[i][j];
		if (AA[i][j]!=A[i][j]){errors+=+1;} 
	      }
	  }
	printf(", errors(threshold<Z>threshold<Z'>)=%d",errors);}
      

      /* write out the cluster matrix (in transposed form): only those clusters that are on:*/
      fpOutputFile = fopen(OutputFile, "w");
      if(fpOutputFile==NULL){
	printf("Error: can't open OutputFile %s\n",OutputFile);}	  
      for (cluster=0;cluster<C;cluster++){
	if (ma[cluster]>SMALLVALUE){	 
	  for (i=0;i<V;i++){
	    fprintf(fpOutputFile, "%f ", zm[i][cluster]);}}
	fprintf(fpOutputFile,"\n");}
      fclose(fpOutputFile);
      //printf(" written (in transposed form) on %s\n",OutputFile);
    }
}