Example #1
0
// Natural logarithm of posterior probability density for one star, given parameters x, where
//
//     x = {DM, M_r, [Fe/H]}
double logP_single_star_emp(const double *x, double EBV, double RV,
                            const TGalacticLOSModel &gal_model, const TStellarModel &stellar_model,
                            TExtinctionModel &ext_model, const TStellarData::TMagnitudes &d, TSED *tmp_sed) {
	#define neginf -std::numeric_limits<double>::infinity()
	double logP = 0.;
	
	/*
	 *  Likelihood
	 */
	bool del_sed = false;
	if(tmp_sed == NULL) {
		del_sed = true;
		tmp_sed = new TSED(true);
	}
	if(!stellar_model.get_sed(x+1, *tmp_sed)) {
		if(del_sed) { delete tmp_sed; }
		return neginf;
	}
	
	double logL = 0.;
	double tmp;
	for(unsigned int i=0; i<NBANDS; i++) {
		if(d.err[i] < 1.e9) {
			tmp = tmp_sed->absmag[i] + x[_DM] + EBV * ext_model.get_A(RV, i);	// Model apparent magnitude
			logL += log( 0.5 - 0.5 * erf((tmp - d.maglimit[i] + 0.5) / 0.25) );	// Completeness fraction
			//std::cout << tmp << ", " << d.maglimit[i] << std::endl;
			tmp = (d.m[i] - tmp) / d.err[i];
			logL -= 0.5*tmp*tmp;
		}
	}
	logP += logL - d.lnL_norm;
	
	if(del_sed) { delete tmp_sed; }
	
	/*
	 *  Priors
	 */
	logP += gal_model.log_prior_emp(x) + stellar_model.get_log_lf(x[1]);
	
	//double lnp0 = -100.;
	//tmp = exp(logP - lnp0);
	//logP = lnp0 + log(tmp + exp(-tmp));	// p --> p + p0 exp(-p/p0)  (Smooth floor on outliers)
	
	#undef neginf
	return logP;
}
Example #2
0
void draw_from_emp_model(size_t nstars, double RV, TGalacticLOSModel& gal_model, TStellarModel& stellar_model,
                     TStellarData& stellar_data, TExtinctionModel& ext_model, double (&mag_limit)[5]) {
	unsigned int samples = 1000;
	void* gal_model_ptr = static_cast<void*>(&gal_model);
	void* stellar_model_ptr = static_cast<void*>(&stellar_model);
	
	double DM_min = 0.;
	double DM_max = 25.;
	TDraw1D draw_DM(&log_dNdmu_draw, DM_min, DM_max, gal_model_ptr, samples, true);
	
	double FeH_min = -2.5;
	double FeH_max = 1.;
	TDraw1D draw_FeH_disk(&disk_FeH_draw, FeH_min, FeH_max, gal_model_ptr, samples, false);
	TDraw1D draw_FeH_halo(&halo_FeH_draw, FeH_min, FeH_max, gal_model_ptr, samples, false);
	
	double Mr_min = -1.;
	double Mr_max = mag_limit[1];
	TDraw1D draw_Mr(&Mr_draw, Mr_min, Mr_max, stellar_model_ptr, samples, true);
	
	stellar_data.clear();
	gal_model.get_lb(stellar_data.l, stellar_data.b);
	
	gsl_rng *r;
	seed_gsl_rng(&r);
	double EBV, DM, Mr, FeH;
	double f_halo;
	bool halo, in_lib, observed;
	TSED sed;
	double mag[NBANDS];
	double err[NBANDS];
	std::cout << "#         Component E(B-V)    DM        Mr        [Fe/H]    g         r         i         z         y        " << std::endl;
	std::cout << "=============================================================================================================" << std::endl;
	std::cout.flags(std::ios::left);
	std::cout.precision(3);
	for(size_t i=0; i<nstars; i++) {
		observed = false;
		while(!observed) {
			// Draw DM
			DM = draw_DM();
			
			// Draw E(B-V)
			//EBV = gsl_ran_chisq(r, 1.);
			
			EBV = 0.;
			//if(DM > 5.) { EBV += 0.05; }
			if(DM > 10.) { EBV += 2.5; }
			
			// Draw stellar type
			f_halo = gal_model.f_halo(DM);
			halo = (gsl_rng_uniform(r) < f_halo);
			in_lib = false;
			while(!in_lib) {
				if(halo) {
					FeH = draw_FeH_halo();
				} else {
					FeH = draw_FeH_disk();
				}
				Mr = draw_Mr();
				in_lib = stellar_model.get_sed(Mr, FeH, sed);
			}
			
			// Generate magnitudes
			observed = true;
			unsigned int N_nonobs = 0;
			double p_det;
			for(size_t k=0; k<NBANDS; k++) {
				mag[k] = sed.absmag[k] + DM + EBV * ext_model.get_A(RV, k);
				err[k] = 0.02 + 0.3*exp(mag[k]-mag_limit[k]);
				if(err[k] > 1.) { err[k] = 1.; }
				mag[k] += gsl_ran_gaussian_ziggurat(r, err[k]);
				
				// Require detection in g band and 3 other bands
				p_det = 0.5 - 0.5 * erf((mag[k] - mag_limit[k] + 0.5) / 0.25);
				if(gsl_rng_uniform(r) > p_det) {
					mag[k] = 0.;
					err[k] = 1.e10;
					
					N_nonobs++;
					if((k == 0) || N_nonobs > 1) {
						observed = false;
						break;
					}
				}
			}
		}
		
		std::cout << std::setw(9) << i+1 << " ";
		std::cout << (halo ? "halo" : "disk") << "      ";
		std::cout << std::setw(9) << EBV << " ";
		std::cout << std::setw(9) << DM << " ";
		std::cout << std::setw(9) << Mr << " ";
		std::cout << std::setw(9) << FeH << " ";
		for(size_t k=0; k<NBANDS; k++) {
			std::cout << std::setw(9) << mag[k] << " ";
		}
		std::cout << std::endl;
		
		TStellarData::TMagnitudes mag_tmp(mag, err);
		mag_tmp.obj_id = i;
		mag_tmp.l = stellar_data.l;
		mag_tmp.b = stellar_data.b;
		stellar_data.star.push_back(mag_tmp);
		
	}
	std::cout << std::endl;
	
	gsl_rng_free(r);
	
	/*std::vector<bool> filled;
	DM_of_P.get_filled(filled);
	for(std::vector<bool>::iterator it = filled.begin(); it != filled.end(); ++it) {
		std::cout << *it << std::endl;
	}
	*/
	
}
Example #3
0
void draw_from_synth_model(size_t nstars, double RV, TGalacticLOSModel& gal_model, TSyntheticStellarModel& stellar_model,
                     TStellarData& stellar_data, TExtinctionModel& ext_model, double (&mag_limit)[5]) {
	unsigned int samples = 1000;
	void* gal_model_ptr = static_cast<void*>(&gal_model);
	
	double DM_min = 0.;
	double DM_max = 25.;
	TDraw1D draw_DM(&log_dNdmu_draw, DM_min, DM_max, gal_model_ptr, samples, true);
	
	double logMass_min = -0.9;
	double logMass_max = 1.1;
	TDraw1D draw_logMass_disk(&disk_IMF_draw, logMass_min, logMass_max, gal_model_ptr, samples, false);
	TDraw1D draw_logMass_halo(&halo_IMF_draw, logMass_min, logMass_max, gal_model_ptr, samples, false);
	
	double tau_min = 1.e6;
	double tau_max = 13.e9;
	TDraw1D draw_tau_disk(&disk_SFR_draw, tau_min, tau_max, gal_model_ptr, samples, false);
	TDraw1D draw_tau_halo(&halo_SFR_draw, tau_min, tau_max, gal_model_ptr, samples, false);
	
	double FeH_min = -2.5;
	double FeH_max = 1.;
	TDraw1D draw_FeH_disk(&disk_FeH_draw, FeH_min, FeH_max, gal_model_ptr, samples, false);
	TDraw1D draw_FeH_halo(&halo_FeH_draw, FeH_min, FeH_max, gal_model_ptr, samples, false);
	
	stellar_data.clear();
	gal_model.get_lb(stellar_data.l, stellar_data.b);
	
	gsl_rng *r;
	seed_gsl_rng(&r);
	double EBV, DM, logtau, logMass, FeH;
	double f_halo;
	bool halo, in_lib, observed;
	TSED sed;
	double mag[NBANDS];
	double err[NBANDS];
	std::cout << "Component E(B-V)    DM        log(Mass) log(tau)  [Fe/H]    g         r         i         z         y        " << std::endl;
	std::cout << "=============================================================================================================" << std::endl;
	std::cout.flags(std::ios::left);
	std::cout.precision(3);
	for(size_t i=0; i<nstars; i++) {
		observed = false;
		while(!observed) {
			// Draw E(B-V)
			EBV = gsl_ran_chisq(r, 1.);
			
			// Draw DM
			DM = draw_DM();
			
			// Draw stellar type
			f_halo = gal_model.f_halo(DM);
			halo = (gsl_rng_uniform(r) < f_halo);
			in_lib = false;
			while(!in_lib) {
				if(halo) {
					logMass = draw_logMass_halo();
					logtau = log10(draw_tau_halo());
					FeH = draw_FeH_halo();
				} else {
					logMass = draw_logMass_disk();
					logtau = log10(draw_tau_disk());
					FeH = draw_FeH_disk();
				}
				in_lib = stellar_model.get_sed(logMass, logtau, FeH, sed);
			}
			
			// Generate magnitudes
			observed = true;
			unsigned int N_nonobs = 0;
			for(size_t k=0; k<NBANDS; k++) {
				mag[k] = sed.absmag[k] + DM + EBV * ext_model.get_A(RV, k);
				err[k] = 0.02 + 0.1*exp(mag[i]-mag_limit[i]-1.5);
				mag[k] += gsl_ran_gaussian_ziggurat(r, err[k]);
				
				// Require detection in g band and 3 other bands
				if(mag[k] > mag_limit[k]) {
					N_nonobs++;
					if((k == 0) || N_nonobs > 1) {
						observed = false;
						break;
					}
				}
			}
		}
		
		std::cout << (halo ? "halo" : "disk") << "      ";
		std::cout << std::setw(9) << EBV << " ";
		std::cout << std::setw(9) << DM << " ";
		std::cout << std::setw(9) << logMass << " ";
		std::cout << std::setw(9) << logtau << " ";
		std::cout << std::setw(9) << FeH << " ";
		for(size_t k=0; k<NBANDS; k++) {
			std::cout << std::setw(9) << mag[k] << " ";
		}
		std::cout << std::endl;
		
		TStellarData::TMagnitudes mag_tmp(mag, err);
		mag_tmp.obj_id = i;
		mag_tmp.l = stellar_data.l;
		mag_tmp.b = stellar_data.b;
		stellar_data.star.push_back(mag_tmp);
		
	}
	std::cout << std::endl;
	
	gsl_rng_free(r);
	
	/*std::vector<bool> filled;
	DM_of_P.get_filled(filled);
	for(std::vector<bool>::iterator it = filled.begin(); it != filled.end(); ++it) {
		std::cout << *it << std::endl;
	}
	*/
	
}