Exemple #1
0
void fit_halo_profile(struct halo *HALO)
{
	double c=0, r=0, rho0, rho0_halo, rs, chi, gof, per; 
	double *x, *y, *e, *R, *y_th, *x_bin, *y_bin, *e_bin, rMin, rMax; 
	int bins, skip, N, j=0;

		r = HALO->Rvir;
		c = HALO->c; 
		bins = HALO->n_bins;
		skip = HALO->neg_r_bins;

		N = bins - skip;

		rho0 = HALO->rho0;
		rs = HALO->r2;

		x = (double*) calloc(N, sizeof(double));
		y = (double*) calloc(N, sizeof(double));
		e = (double*) calloc(N, sizeof(double));
		R = (double*) calloc(N, sizeof(double));
		
		for(j=0; j<N; j++)
		{
			x[j] = HALO->radius[j+skip];
			y[j] = HALO->rho[j+skip];
			e[j] = HALO->err[j+skip];
			R[j] = HALO->radius[j+skip]/r;
		}

			best_fit_nfw(rho0, rs, N, x, y, e);

			HALO->fit_nfw.rho0 = rho0;
			HALO->fit_nfw.rs = rs;
			HALO->fit_nfw.c = HALO->Rvir/rs;

			y_th = (double*) calloc(bins-skip,sizeof(double));

		for(j=skip; j<bins; j++)
		{
			y_th[j-skip] = nfw(HALO->radius[j], HALO->fit_nfw.rs, HALO->fit_nfw.rho0);
			//fprintf(stderr, "%d) R=%e, %e %e  %e\n", j, R[j-skip], rho0, y[j-skip], y_th[j-skip]);
		}

	// Various estimators for the goodness of fit
	chi = chi_square(y_th, y, e, N);
	gof = goodness_of_fit(y_th, y, N);
	per = percentage_error(y_th, y, N);
	
	chi /= (double) (bins-skip);

	HALO->fit_nfw.chi = chi;
	HALO->fit_nfw.gof = gof;
	HALO->fit_nfw.per = per;

		x_bin = (double*) calloc(BIN_PROFILE+1, sizeof(double));
		y_bin = (double*) calloc(BIN_PROFILE, sizeof(double));
		e_bin = (double*) calloc(BIN_PROFILE, sizeof(double));

		rMin = 2 * Rvir_frac_min;
		rMax = F_MAX * 1.01; //HALO->radius[bins-1]/r;
		x_bin = log_stepper(rMin, rMax, BIN_PROFILE+1);

		average_bin(R, y, x_bin, y_bin, e_bin, BIN_PROFILE+1, N);

	for(j=0; j<BIN_PROFILE; j++)
	{
		HALO->nfw.x[j] = 0.5 * (x_bin[j] + x_bin[j+1]);
		HALO->nfw.y[j] = y_bin[j];
		//fprintf(stderr, "%d  %e  %e\n", j, x_bin[j+1], y_bin[j]);
	}

	free(x);
	free(y);
	free(R);
	free(y_th);
	free(e);
//	fprintf(stderr, "ThisTask=%d, skip=%d, bins=%d, rho=%f, rs=%f, ChiSquare=%lf, Red=%lf\n", 
//		ThisTask, skip, bins, rho0, rs, chi_sq, chi_sq/(bins-skip));
}
Exemple #2
0
static int goodness_of_fit_test(FREQ *f,int m,int n)
    {
    int i,j;
    double a,x2;

    if (n!=2)
        {
        sprintf(sbuf,"\nTable must have 2 columns (expected and observed) frquencies!");
        sur_print(sbuf); WAIT; return(-1);
        }
    if (m<2)
        {
        sprintf(sbuf,"\nTable must have at least two rows!");
        sur_print(sbuf); WAIT; return(-1);
        }

    e=(double *)muste_malloc(m*sizeof(double));
    ecum=(double *)muste_malloc(m*sizeof(double));
    o=(int *)muste_malloc(m*sizeof(int));
    n2=0; for (i=0; i<m; ++i) { o[i]=f[i]; n2+=o[i]; }
    n1=0; for (i=0; i<m; ++i) n1+=f[i+m];
    a=(double)n2/(double)n1;

    for (i=0; i<m; ++i) e[i]=a*f[i+m];
    a=0.0; for (i=0; i<m; ++i) { a+=e[i]; ecum[i]=a; }
    for (i=0; i<m; ++i) ecum[i]/=ecum[m-1];

    g_print=&sur_print;

    x2=chi_square(m);
// Rprintf("\nx2=%g n2=%d",x2,n2); getch();

    maxcount=1000000L;
    i=spfind("SIMUMAX");
    if (i>=0) maxcount=atol(spb[i]);
    i=rand_init(); if (i<0) return(-1);

    conf_level=0.95;
    i=spfind("CONF");
    if (i>=0) conf_level=atof(spb[i]);
    if (conf_level<0.8 || conf_level>=1.0)
        {
        sur_print("\nError in CONF=p! Confidence level p must be 0.8<p<1");
        WAIT; return(-1);
        }
    conf_coeff=muste_inv_std(1.0-(1.0-conf_level)/2);

    disp0fit(m,n,x2);

    dn=10000L;
    i=spfind("GAP");
    if (i>=0) dn=atol(spb[i]);
    count=0L; pcount=0L; dcount=0L; ecount=0L;

    while (1)
        {
        ++count; ++dcount;
        for (i=0; i<m; ++i) o[i]=0;
        for (j=0; j<n2; ++j)
            {
            a=(*rand1)();
            i=0;
            while (a>ecum[i]) ++i;
            ++o[i];
            }
// Rprintf("\no: "); for (i=0; i<m; ++i) Rprintf("%d ",o[i]);
// a=chi_square(m); Rprintf("\na=%g",a);
        if (chi_square(m)>=x2) ++pcount;

        if (dcount>=dn)
            {
            tab_disp();
            if (count>=maxcount) break;
            if (sur_kbhit()) { sur_getch(); break; }           
            WAIT;
            dcount=0;
            }

        }
// Rprintf("\np=%g",(double)pcount/(double)count); getch();
    g_print=&print_line;

    output_open(eout);
    disp0fit(m,n,x2);
    tab_disp();
    output_close(eout);

    return(1);
    }
Exemple #3
0
void split(tree *t, char *Inname,char *Outname)
{
   FILE  *Infile, *NextInfile1, *NextInfile2, *Outfile;
   char  *S, *T ,*Copy;
   int *B;
   int i, j, k, N, N1, N2, maxi, tempnofile ;
   int String_Len;
   char Outname1[20], Outname2[20];
   double max, Sum;
   fpos_t pos;
   
   Infile = fopen (Inname, "r");
   if  (Infile == NULL)
       {
        fprintf (stderr, "ERROR:  Unable to open file %s\n", Inname);
        exit (EXIT_FAILURE);
       }

   String_Len = 1 + Stop_Position - Start_Position;  

   /* initialize frequencies for consensus */
   for(i=0;i<String_Len;i++)
      	for(j=0;j<4;j++)
      		V[i][j]=0;

   T = (char *) malloc((MAX_STRING_LEN+1)*sizeof(char));
   MemCheck(T,"T");
   S = (char *) malloc((MAX_STRING_LEN+1)*sizeof(char));
   MemCheck(S,"S");

   N = 0;
   while  (fgets (T, MAX_STRING_LEN, Infile) != NULL)
     {
      sscanf (T, "%*s %s", S);
      assert (strlen (S) == NUM_POSITIONS);
      strncpy (T, S + Start_Position, String_Len);
      T [String_Len] = '\0'; 
      N++;

      B = basetoint(T,String_Len); 

      /* compute frequencies for each position */
      
      for(i=0;i<String_Len;i++)
      	V[i][B[i]]++;
      	
      free(B); 
    }  

    t->no=N;

    /* compute consensus */
    for(i=0;i<String_Len;i++)
      {
      	max=0;
      	for(j=0;j<4;j++)
      		if(V[i][j]>max)
      		{
				C[i]=j;
				max=V[i][j];
			}
	  } 

	/*compute match between consensus and variable at position j */
	for(i=0;i<String_Len;i++)
		for(j=0;j<String_Len;j++)
			for(k=0;k<4;k++)
				M[i][j][k]=0;
	/*pos=0;
	  fsetpos(Infile,&pos);*/
	rewind(Infile);
	while  (fgets (T, MAX_STRING_LEN, Infile) != NULL)
     {
      sscanf (T, "%*s %s", S);
      assert (strlen (S) == NUM_POSITIONS);
      strncpy (T, S + Start_Position, String_Len);
      T [String_Len] = '\0'; 
      B = basetoint(T,String_Len);
      for(i=0;i<String_Len;i++)
      	if(C[i]==B[i])
      		for(j=0;j<String_Len;j++)
      				M[i][j][B[j]]++;
      free(B);
      }


    /* for each position compute the chi_square and where to do the partition */


   max=0;
   maxi=-1;
   for(i=0;i<String_Len;i++)
   if(W[i])
   {
	Sum=0;
	W[i]=0;
	for(j=0;j<String_Len;j++)
	{
		/*printf("i=%d,j=%d,Ci=%d,Vj0=%d,Vj1=%d,Vj2=%d,Vj3=%d,Mij0=%d,Mij1=%d,Mij2=%d,Mij3=%d\n",i,j,C[i],V[j][0],V[j][1],V[j][2],V[j][3],M[i][j][0],M[i][j][1],M[i][j][2],M[i][j][3]);
		fflush(stdout);*/

		if(W[j]) 
			Sum+=chi_square(i,j);
	}
	W[i]=1;

	if(Sum>max)
	{
		max=Sum;
		maxi=i;

	}
   } 

   printf("Consens %d pe poz %d\n",C[maxi],maxi);

   /* open output files */

   sprintf(Outname1,"%s%d",Outname,Nofile + 1);
   sprintf(Outname2,"%s%d",Outname,Nofile + 2);

   NextInfile1 = fopen (Outname1, "w");
   if  (NextInfile1 == NULL)
       {
        fprintf (stderr, "ERROR:  Unable to open file %s\n", Outname1);
        exit (EXIT_FAILURE);
       }

   NextInfile2 = fopen (Outname2, "w");
   if  (NextInfile2 == NULL)
       {
        fprintf (stderr, "ERROR:  Unable to open file %s\n", Outname2);
        exit (EXIT_FAILURE);
       }

   Copy = (char *) malloc((MAX_STRING_LEN+1)*sizeof(char));
   MemCheck(Copy,"Copy");
   N1=0;
   N2=0;
   /* split into 2 leaves if necessary */
   /*pos=0;
     fsetpos(Infile,&pos);*/
   rewind(Infile);
   while  (fgets (T, MAX_STRING_LEN, Infile) != NULL)
     {  
     	strcpy(Copy,T);
		sscanf (T, "%*s %s", S);
      	assert (strlen (S) == NUM_POSITIONS);
      	strncpy (T, S + Start_Position, String_Len);
      	T [String_Len] = '\0'; 
	    B = basetoint(T,String_Len); 
	    if(B[maxi] == C[maxi])
	    { 
	    	N1++;
	    	fprintf(NextInfile1,"%s",Copy);
	    }
	    else
	    {
			N2++;
	    	fprintf(NextInfile2,"%s",Copy);
	    }
	}

	free(S);
	free(T);
	free(Copy);

	fclose(NextInfile1);
	fclose(NextInfile2);

	printf("Frunze de %d si %d componente\n",N1,N2);

	tempnofile=Nofile;
	Nofile+=2;

	//if(N1+N2>224)
	//if(N1+N2>1000)
	if(N1>400 && N2>400)
	{   t->consens=C[maxi];
   		t->poz=maxi;   	
		t->left = (tree *) malloc(sizeof(tree));
   		if (t->left == NULL) {fprintf(stderr,"%s: Memory allocation for tree failure.\n"); abort();}
		(t->left)->val=tempnofile+1;
   		(t->left)->consens=-1;
		(t->left)->poz=-1;
		(t->left)->no=N1;
   		(t->left)->left=NULL;
   		(t->left)->right=NULL;	
		if(N1>350) 
			{ 
				W[maxi]=0;
				split(t->left,Outname1,Outname);
			}
		W[maxi]=1;
		t->right = (tree *) malloc(sizeof(tree));
   		if (t->right == NULL) {fprintf(stderr,"%s: Memory allocation for tree failure.\n"); abort();}
   		(t->right)->val=tempnofile+2;
   		(t->right)->consens=-1;
   		(t->right)->poz=-1;
   		(t->right)->no=N2;
   		(t->right)->left=NULL;
   		(t->right)->right=NULL;
		if(N2>350) split(t->right,Outname2,Outname);
	}
   		
   fclose(Infile);

}
void postprocessing()
{
long int i,num;
int j,k,l,s,isNew;
double eva,p_value;
int* loci;
loci=new int[iEpiModel];
vector<double> record;
record.resize(iEpiModel+2);
cout<<"Post-processing"<<endl;
cout<<"Dealing with top ranking SNP sets"<<endl;
num=comb_num(iLociModel,iEpiModel);
cout<<"Number of interatcions evaluated: "<<num*iTopModel<<endl;
int** combination;
combination=new int*[num];
for(i=0;i<num;i++)
combination[i]=new int[iEpiModel];
combination=throughout(iLociModel,iEpiModel,combination);
for(i=iTopModel-1;i>=0;i--)
{
for(j=0;j<num;j++)
{
for(k=0;k<iEpiModel;k++)
{
loci[k]=loci_TopModel[i][combination[j][k]];
}
eva=chi_square(loci,iEpiModel);
p_value=1-gsl_cdf_chisq_P(eva,pow(3,iEpiModel)-1);
if(p_value<pvalue)
{
isNew=1;
for(l=0;l<interactions.size();l++)
{
if(fabs(eva-interactions[l][iEpiModel])<0.00000001)
{
isNew=0;
break;
}
}
if(isNew)
{
for(s=0;s<iEpiModel;s++)
{
record[s]=(double)loci[s];
}
record[iEpiModel]=eva;
record[iEpiModel+1]=p_value;
interactions.push_back(record);
}
}
}
}
for(i=0;i<num;i++)
delete []combination[i];
delete []combination;
cout<<"Dealing with top ranking loci"<<endl;
num=comb_num(iTopLoci,iEpiModel);
//cout<<"hello"<<endl;
int** combination1;
combination1=new int*[num];
for(i=0;i<num;i++)
combination1[i]=new int[iEpiModel];
combination1=throughout(iTopLoci,iEpiModel,combination1);
cout<<"Number of interactions evaluated: "<<num<<endl;
for(i=0;i<num;i++)
{
for(j=0;j<iEpiModel;j++)
{
loci[j]=loci_TopLoci[combination1[i][j]];
//cout<<loci[j]<<" ";
}
//cout<<endl;
eva=chi_square(loci,iEpiModel); 
p_value=1-gsl_cdf_chisq_P(eva,pow(3,iEpiModel)-1); 
if(p_value<pvalue)
{
isNew=1;
for(l=0;l<interactions.size();l++)
{
if(fabs(eva-interactions[l][iEpiModel])<0.00000001)
{
isNew=0;
break;
}
}
if(isNew)
{
for(s=0;s<iEpiModel;s++)
{
record[s]=(double)loci[s];  
}
record[iEpiModel]=eva; 
record[iEpiModel+1]=p_value;  
interactions.push_back(record);
}
}
}
for(i=0;i<num;i++)
delete []combination1[i];
delete []combination1;
}
void project::UpdatePheromone()
{
int i,j,k,tag;
for(i=0;i<iLociCount;i++)
SNPdata.pheromone[i]=SNPdata.pheromone[i]*(1-rou);
double eva;
int* locidata;
locidata=new int[iLociModel];
for(i=0;i<iAntCount;i++)
{
for(j=1;j<=iLociModel;j++)
{
locidata[j-1]=ants[i].tabu[j];
}
eva=chi_square(locidata,iLociModel)/100;
for(j=0;j<iLociModel;j++)
{
SNPdata.pheromone[locidata[j]]+=eva;
}
if(eva>eva_TopModel[0])
{
tag=1;
for(j=0;j<iTopModel;j++)
{
if(fabs(eva-eva_TopModel[j])<0.000001)
{
tag=0;
break;
}
}
if(tag)
{
eva_TopModel[0]=eva_TopModel[1];
for(j=0;j<iLociModel;j++)
loci_TopModel[0][j]=loci_TopModel[1][j];
k=1;
while(k<iTopModel && eva>eva_TopModel[k])
{
eva_TopModel[k-1]=eva_TopModel[k];
for(j=0;j<iLociModel;j++)
loci_TopModel[k-1][j]=loci_TopModel[k][j];
k++;
}
eva_TopModel[k-1]=eva;
for(j=0;j<iLociModel;j++)
loci_TopModel[k-1][j]=locidata[j];
}
}
}
//#################update cdf##################
double * prob;
double temp=0;
prob=new double[iLociCount];
for(i=0;i<iLociCount;i++)
{
temp+=pow(SNPdata.pheromone[i],alpha);
prob[i]=temp;
}
for(i=0;i<iLociCount-1;i++)
{
SNPdata.cdf[i+1]=prob[i]/temp;
}
delete [] prob;
delete [] locidata;
}
Exemple #6
0
int main(int argc, char* argv[]){

  if (argc != 1){
    fprintf( stderr, "Usage: %s\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  int i;

  /* data directory */
  char data_dir[256];
  snprintf(data_dir, 256, "../data/");

  /* load the pointing list */
  char plist_filename[256];
  snprintf(plist_filename, 256, "%stodo_list.ascii.dat", data_dir);

  int N_plist;
  POINTING *plist;
  load_pointing_list(plist_filename, &N_plist, &plist);


  /* Read star data for each pointing */
  for(i = 0; i < N_plist; i++){
    char data_filename[256];
    snprintf(data_filename, 256, "%sstar_%s.ascii.dat", data_dir, plist[i].ID);
    load_data(data_filename, &plist[i].N_data, &plist[i].data);
  }

  fprintf(stderr, "Star data loaded from %s\n", data_dir);

  /* Read model data for each pointing,
     here each file starts with uniformly distributed random */
  for(i = 0; i < N_plist; i++){
    char model_filename[256];
    snprintf(model_filename, 256, "%suniform_%s.ascii.dat", data_dir, plist[i].ID);
    load_data(model_filename, &plist[i].N_model, &plist[i].model);
  }

  fprintf(stderr, "Uniform model data loaded from %s\n", data_dir);

  /* initialize model parameters */
  PARAMETERS params;
  params.N_parameters = 5; 	/* Total number of free paramters. */

  params.thindisk_type = 1;	/* turn on thin disk */
  params.thindisk_r0 = 2.475508;
  params.thindisk_z0 = 0.241209;
  params.thindisk_n0 = 1.0;

  params.thickdisk_type = 1;	/* turn on thick disk */
  params.thickdisk_r0 = 2.417346;
  params.thickdisk_z0 = 0.694395;
  params.thickdisk_n0 = 0.106672;

  params.halo_type = 0; 	/* turn off halo */

  fprintf(stderr, "Set initial parameters..\n");

  int mcmc_steps, efficiency_counter;
  double efficiency;
  MCMC *mcmc_chain;
  double chi2, delta_chi2;
  int dof;

  mcmc_steps = 500000;
  mcmc_chain = calloc(mcmc_steps, sizeof(MCMC));

  FILE *file_chain_realtime;
  file_chain_realtime = fopen("./data/mcmc.dat", "a");

  fprintf(stderr, "Allocate MCMC chain..Max steps = %d \n", mcmc_steps);

  /* set the first element as the initial parameters.
   Then calculate the chi2 for the initial parameters. */
  mcmc_chain[0].params = params;

  set_weights(mcmc_chain[0].params, plist, N_plist);

  /* allocate space for correlation function calculation */
  /* This also does a first time correlation calculation of initial parameters. */
  /* DD pairs may only calculated once here but not later.  */
  correlation_initialize(plist, N_plist);

  /* load the jackknife fractional errors */
  load_errors(plist, N_plist);

  mcmc_chain[0].dof = degrees_of_freedom(plist, N_plist, params);
  mcmc_chain[0].chi2 = chi_square(plist, N_plist);

  fprintf(stderr, "initial: chi2 = %le \n", mcmc_chain[0].chi2);

  fprintf(stderr, "Start MCMC calculation..\n");

  /* Markov chain loop */
  efficiency_counter = 1;
  for(i = 1; i < mcmc_steps; i++){

    /* update new positions in paramter space */
    params = update_parameters(mcmc_chain[i - 1].params);

    /* recalculate weights */
    set_weights(params, plist, N_plist);

    /* recalculate correlation functions */
    calculate_correlation(plist, N_plist);

    /* get chi squares */
    dof = degrees_of_freedom(plist, N_plist, params);

    chi2 = chi_square(plist, N_plist);

    delta_chi2 = chi2 - mcmc_chain[i - 1].chi2;

    fprintf(stderr, "delta_chi2 = %le \n", delta_chi2);

    if(delta_chi2 <= 0.0){
      /* if delta chisquare is smaller then record*/
      mcmc_chain[i].params = params;
      mcmc_chain[i].chi2 = chi2;
      mcmc_chain[i].dof = dof;
      efficiency_counter++;
    }
    else{
      /* if delta chisquare is bigger then use probability to decide */
      /* !!! replace with GSL random generator later !!! */
      double tmp = (double)rand() / (double)RAND_MAX; /* a random number in [0,1] */
      if (tmp < exp(- delta_chi2 / 2.0)){
	mcmc_chain[i].params = params;
	mcmc_chain[i].chi2 = chi2;
	mcmc_chain[i].dof = dof;
	efficiency_counter++;
      }
      else{
	/* record the old position. */
	mcmc_chain[i] = mcmc_chain[i - 1];
      }
    }

    efficiency = (float) efficiency_counter / i;
    /* print out progress */
    /* if(i % (mcmc_steps / 100) == 0){ */
    /*   fprintf(stderr, "MCMC... %d / %d: chi2 = %lf \n", i, mcmc_steps, chi2); */
    /* } */
    fprintf(stderr, "MCMC... %d / %d: efficiency = %lf \n z0_thin = %lf, r0_thin = %lf, z0_thick = %lf, r0_thick = %lf, n0_thick = %lf \n chi2 = %lf, dof = %d, chi2_reduced = %lf \n\n",
	    i, mcmc_steps, efficiency, params.thindisk_z0, params.thindisk_r0, params.thickdisk_z0, params.thickdisk_r0, params.thickdisk_n0, chi2, dof, chi2/dof);

    MCMC tmp_mc;
    PARAMETERS tmp_p;
    tmp_mc = mcmc_chain[i];
    tmp_p = tmp_mc.params;
    // fprintf(stderr, "Made it here\n");
    // fprintf(file_chain_realtime, "%d\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%d\t%lf\n",
	   //  i+50000, tmp_p.thindisk_r0, tmp_p.thindisk_z0, tmp_p.thickdisk_r0, tmp_p.thickdisk_z0, tmp_p.thickdisk_n0,
	   //  tmp_mc.chi2, (int)tmp_mc.dof, tmp_mc.chi2/dof);
    // fprintf(stderr, "Printed to file.\n");
    // if(i % 50 == 0){
    //   fflush(file_chain_realtime);
    // }

  } /* end of mcmc */


  output_mcmc(mcmc_chain, mcmc_steps);

  fprintf(stderr, "End MCMC calculation..\n");

  for(i = 0; i < N_plist; i++){
    free(plist[i].data);
    free(plist[i].model);
    free(plist[i].corr);
  }
  free(plist);

  free(mcmc_chain);

  return EXIT_SUCCESS;

}