int
main ( void )
{
	double bp ;

	//	problem we were assigned to solve
	bp = binomial_probability ( 35 , 10 , 0.1L ) ;
	printf ( "Probability is: %lf\n" , bp ) ;

	// Worst case scenario from listed requirements
	bp = binomial_probability ( 100 , 50 , 0.1L ) ;
	printf ( "Probability is: %.100lf\n" , bp ) ;
	return 0 ;
}
double get_likelihood()
{
	//Variables for all the LF/SM function like tests
	double *binsamdata, *samdata;
	//variables for the bulge-blackhole mass test using binomial
	double binblackholeup[2]={0.0,0.0}, binblackholedown[2]={0.0,0.0};
	double final_probability, redshift_probability, current_probability;
	double prob_SMF_z0;
	int i, j, snap;
	double color_UV, color_VJ;
	//OBSERVATIONAL CUT
	//double offset_color_cut[NOUT]={0.00, 0.69, 0.59, 0.59, 0.59}; //not used at z=0
	//double slope_color_cut[NOUT]={0.00, 0.88, 0.88, 0.88, 0.88};
	//BEST FIT TO MODEL CUT
#ifndef HALOMODEL
	double offset_color_cut[NOUT]={0.00, 1.085, 1.1, 1.0, 1.15}; //not used at z=0
	double slope_color_cut[NOUT]={0.00, 0.5, 0.48, 0.38, 0.18};
#else
	double offset_color_cut[NOUT]={0.00}; //not used at z=0
	double slope_color_cut[NOUT]={0.00};
#endif

	/* Bin samdata into binsamdata according to observational constraints.
	 * The first argument of the bin functions and of the likelihood
	 * function (Chi^2 ot MLM) indicates the observational data set to use.*/

	//ALL Luminosity Function to be compared in units of M-5logh and phi(Mpc-3h-3mag-1)

	final_probability=1.;
	for(snap=0;snap<NOUT;snap++)
	{
		printf("snap=%d\n",snap);
		redshift_probability=1.;

		if((samdata = malloc(sizeof(double) * TotMCMCGals[snap])) == NULL)
			terminate("get_likelihood");

#ifdef HALOMODEL
		correct_for_correlation(snap);
#endif

		for(i=0;i<MCMCNConstraints;i++)
		{
			if(MCMC_Obs[i].ObsTest_Switch_z[snap]==1)
			{

				if((binsamdata = malloc(sizeof(double) * Nbins[snap][i])) == NULL)
					terminate("get_likelihood");


/******************************
 **     Chi_Sq TESTS         **
 ******************************/
				if(strcmp(MCMC_Obs[i].TestType,"chi_sq")==0)
				{

					//bin all the galaxie into binsamdata for properties that just require normal histograms
					for(j = 0; j < TotMCMCGals[snap]; j++)
					{
						samdata[j] = 0.; //initialize
						if(strcmp(MCMC_Obs[i].Name,"StellarMassFunction")==0)
							samdata[j] = MCMC_GAL[j].StellarMass[snap];
						if(strcmp(MCMC_Obs[i].Name,"KBandLF")==0)
							samdata[j] = MCMC_GAL[j].MagK[snap];
						if(strcmp(MCMC_Obs[i].Name,"BBandLF")==0)
						{
							if((double)((int)((MCMCConstraintsZZ[snap]*10)+0.5)/10.)<0.2) //Bj band at z=0
								samdata[j]=MCMC_GAL[j].MagB[snap]-0.28*(MCMC_GAL[j].MagB[snap]-MCMC_GAL[j].MagV[snap]);
							else //BBand at all other z
								samdata[j]=MCMC_GAL[j].MagB[snap];
						}
						if(strcmp(MCMC_Obs[i].Name,"uBandLF")==0)
							samdata[j] = MCMC_GAL[i].Magu[snap];
						if(strcmp(MCMC_Obs[i].Name,"ColdGasMassFunction")==0)
							samdata[j] = MCMC_GAL[j].ColdGas[snap];

						//Stellar Mass Function of Passive Galaxies
						if(strcmp(MCMC_Obs[i].Name,"StellarMassFunctionPassive")==0)
								if( MCMC_GAL[j].Sfr[snap]* 1.e9 /pow(10.,MCMC_GAL[j].StellarMass[snap]) < 0.01 )
									samdata[j] = MCMC_GAL[j].StellarMass[snap];

						//Stellar Mass Function of Active Galaxies
						if(strcmp(MCMC_Obs[i].Name,"StellarMassFunctionActive")==0)
								if( MCMC_GAL[j].Sfr[snap]* 1.e9 /pow(10.,MCMC_GAL[j].StellarMass[snap]) > 0.3 )
									samdata[j] = MCMC_GAL[j].StellarMass[snap];

						//Stellar Mass Function of Red Galaxies
						//original cut in bladry 2004 (2.06-0.244*tanh((MCMC_GAL[j].Magr[snap]+20.07)/1.09))
						if(strcmp(MCMC_Obs[i].Name,"StellarMassFunctionRed")==0)
						{
							if((double)((int)((MCMCConstraintsZZ[snap]*10)+0.5)/10.)<0.2) //z=0
							{
								if( (MCMC_GAL[j].Magu[snap]-MCMC_GAL[j].Magr[snap]) > (1.9-0.244*tanh((MCMC_GAL[j].Magr[snap]+20.07)/1.09)))
									samdata[j] = MCMC_GAL[j].StellarMass[snap];
							}
							else //z>0
							{
								color_UV=(MCMC_GAL[j].MagU[snap]-MCMC_GAL[j].MagV[snap]);
								color_VJ=(MCMC_GAL[j].MagV[snap]-MCMC_GAL[j].MagJ[snap]);
								if( (color_VJ < (1.3-offset_color_cut[snap])/slope_color_cut[snap] && color_UV > 1.3) ||
										(color_VJ > (1.3-offset_color_cut[snap])/slope_color_cut[snap] && color_UV > color_VJ*slope_color_cut[snap]+offset_color_cut[snap]) )
									samdata[j] = MCMC_GAL[j].StellarMass[snap];
							}
						}

						//Stellar Mass Function of Blue Galaxies
						//original cut in bladry 2004 (2.06-0.244*tanh((MCMC_GAL[j].Magr[snap]+20.07)/1.09))
						if(strcmp(MCMC_Obs[i].Name,"StellarMassFunctionBlue")==0)
						{
							if((double)((int)((MCMCConstraintsZZ[snap]*10)+0.5)/10.)<0.2) //z=0
							{
								if( (MCMC_GAL[j].Magu[snap]-MCMC_GAL[j].Magr[snap]) < (1.9-0.244*tanh((MCMC_GAL[j].Magr[snap]+20.07)/1.09)))
									samdata[j] = MCMC_GAL[j].StellarMass[snap];
							}
							else //z>0
							{
								color_UV=(MCMC_GAL[j].MagU[snap]-MCMC_GAL[j].MagV[snap]);
								color_VJ=(MCMC_GAL[j].MagV[snap]-MCMC_GAL[j].MagJ[snap]);

								if( (color_VJ < (1.3-offset_color_cut[snap])/slope_color_cut[snap] && color_UV < 1.3) ||
										(color_VJ > (1.3-offset_color_cut[snap])/slope_color_cut[snap] && color_UV < color_VJ*slope_color_cut[snap]+offset_color_cut[snap]) )
									samdata[j] = MCMC_GAL[j].StellarMass[snap];
							}
						}

					}//end loop on number of galaxies
					bin_function(i, binsamdata, samdata, snap);

					//SFRD only has one bin
					if(strcmp(MCMC_Obs[i].Name,"SFRD")==0)
					{
						binsamdata[0]=0;
						for(j = 0; j < TotMCMCGals[snap]; j++)
							binsamdata[0]+=MCMC_GAL[j].Sfr[snap]*MCMC_GAL[j].Weight[snap];
					}

#ifdef HALOMODEL
					//Correlation Function - requires more than just binning
					if(strncmp(MCMC_Obs[i].Name,"Clustering_MassBins_8.77_9.27",28)==0)
						compute_correlation_func(i, binsamdata, snap, 8.77, 9.27);
					if(strncmp(MCMC_Obs[i].Name,"Clustering_MassBins_9.27_9.77",28)==0)
						compute_correlation_func(i, binsamdata, snap, 9.27, 9.77);
					if(strncmp(MCMC_Obs[i].Name,"Clustering_MassBins_9.77_10.27",28)==0)
						compute_correlation_func(i, binsamdata, snap, 9.77, 10.27);
					if(strncmp(MCMC_Obs[i].Name,"Clustering_MassBins_10.27_10.77",28)==0)
						compute_correlation_func(i, binsamdata, snap, 10.27, 10.77);
					if(strncmp(MCMC_Obs[i].Name,"Clustering_MassBins_10.77_11.27",28)==0)
						compute_correlation_func(i, binsamdata, snap, 10.77, 11.27);
					if(strncmp(MCMC_Obs[i].Name,"Clustering_MassBins_11.27_11.47",28)==0)
						compute_correlation_func(i, binsamdata, snap, 11.27, 11.77);
#endif

					current_probability=chi_square_probability(i, binsamdata, snap);

				}//end chi_sq tests


/******************************
 ** Maximum Likelihood TESTS **
 ******************************/
				if(strcmp(MCMC_Obs[i].TestType,"maxlike")==0)
				{
					if(strcmp(MCMC_Obs[i].Name,"RedFraction")==0)
					{
						bin_red_fraction(i, binsamdata, snap);
						current_probability=maximum_likelihood_probability(i, binsamdata, snap);
					}
				}

				if(strcmp(MCMC_Obs[i].TestType,"maxlike")==0)
				{
					if(strcmp(MCMC_Obs[i].Name,"PassiveFraction")==0)
					{
						bin_passive_fraction(i, binsamdata, snap);
						current_probability=maximum_likelihood_probability(i, binsamdata, snap);
					}
				}

/******************************
 **    Binomial TESTS        **
 ******************************/
				if(strcmp(MCMC_Obs[i].TestType,"binomial")==0)
				{
					if(strcmp(MCMC_Obs[i].Name,"BlackHoleBulgeMass")==0)
					{
						bin_bhbm(binblackholeup, binblackholedown, snap);
						current_probability=binomial_probability(i, binblackholeup, binblackholedown, snap);
					}
				}
//END BINOMIAL TESTS


				free(binsamdata);


				redshift_probability*=current_probability;
				printf("prob[%d]=%0.5e\n",i,current_probability);
				//write likelihood for each constraint at each MCMC step
				fprintf(FILE_MCMC_PredictionsPerStep[snap][i], " %0.5e", current_probability);

				//save probability of the stellar mass function at z=0
				//if(snap==0 && strcmp(MCMC_Obs[i].Name,"StellarMassFunction")==0)
				//	prob_SMF_z0=current_probability;

			}//end if(MCMC_Obs[i].ObsTest_Switch_z[snap]==1)
		}//end loop on MCMCNConstraints

		printf("snap[%d] prob=%0.5e\n",snap, redshift_probability);

		final_probability*=redshift_probability;
		free(samdata);
#ifdef HALOMODEL
		free(MCMC_FOF2);
		free(HashTable);
#endif
	}//end loop on snaps


	printf("final prob=%0.5e\nlog_like=%0.8g\n",final_probability, -log10(final_probability));

	//write total likelihood into separate files and into
	//the end of the line on the file with the comparison to each constraint
	fprintf(FILE_MCMC_LIKELIHOOD,"%0.8g\n",-log10(final_probability));
	fflush(FILE_MCMC_LIKELIHOOD);
	for(i=0;i<MCMCNConstraints;i++)
		for(snap=0;snap<NOUT;snap++)
			if(MCMC_Obs[i].ObsTest_Switch_z[snap]==1)
			{
				fprintf(FILE_MCMC_PredictionsPerStep[snap][i], " %0.8g\n", -log10(final_probability));
				fflush(FILE_MCMC_PredictionsPerStep[snap][i]);
			}
	//if(final_probability>1.e-22 && prob_SMF_z0>1.e-4)
	//	printf("ola\n");

	return final_probability;
}