def derive_dist_mod_tables(z_lims, n_bins): ''' Creates functions approximating mu(z) and z(mu) where z is redshift and mu is the distance modulus. Uses the cosmology class and scipy's cubic spline interpolation. These functions will serve as reference tables to speed up computation of the luminosity function. Parameters ---------- z_lims : tuple, float Pair of lower and upper limits in redshift sample. n_bins : int Number of bins in the table. Returns ------- mu_z : function Distance modulus as a function of redshift. z_mu : function Redshift as a function of distance modulus. ''' from astroML.cosmology import Cosmology cosmo = Cosmology() z_sample = np.linspace(z_lims[0], z_lims[1], n_bins) mu_sample = [cosmo.mu(z) for z in z_sample] mu_z = interpolate.interp1d(z_sample, mu_sample) z_mu = interpolate.interp1d(mu_sample, z_sample) return mu_z, z_mu
from astroML.cosmology import Cosmology from astroML.datasets import generate_mu_z #---------------------------------------------------------------------- # This function adjusts matplotlib settings for a uniform feel in the textbook. # Note that with usetex=True, fonts are rendered with LaTeX. This may # result in an error if LaTeX is not installed on your system. In that case, # you can set usetex to False. from astroML.plotting import setup_text_plots setup_text_plots(fontsize=8, usetex=True) #------------------------------------------------------------ # Generate data z_sample, mu_sample, dmu = generate_mu_z(100, random_state=0) cosmo = Cosmology() z = np.linspace(0.01, 2, 1000) mu_true = np.asarray(map(cosmo.mu, z)) #------------------------------------------------------------ # fit the data # Mesh the input space for evaluations of the real function, # the prediction and its MSE z_fit = np.linspace(0, 2, 1000) gp = GaussianProcess(corr='squared_exponential', theta0=1e-1, thetaL=1e-2, thetaU=1, normalize=False, nugget=(dmu / mu_sample)**2, random_start=1)
omegaM, omegaL, res = compute_mu_z_nonlinear() res -= np.max(res) #------------------------------------------------------------ # Plot the results fig = plt.figure(figsize=(5, 2.5)) fig.subplots_adjust(left=0.1, right=0.95, wspace=0.25, bottom=0.15, top=0.9) # left plot: the data and best-fit ax = fig.add_subplot(121) whr = np.where(res == np.max(res)) omegaM_best = omegaM[whr[0][0]] omegaL_best = omegaL[whr[1][0]] cosmo = Cosmology(omegaM=omegaM_best, omegaL=omegaL_best) z_fit = np.linspace(0.04, 2, 100) mu_fit = np.asarray(map(cosmo.mu, z_fit)) ax.plot(z_fit, mu_fit, '-k') ax.errorbar(z_sample, mu_sample, dmu, fmt='.k', ecolor='gray') ax.set_xlim(0, 1.8) ax.set_ylim(36, 46) ax.set_xlabel('$z$') ax.set_ylabel(r'$\mu$') ax.text(0.04, 0.96, "%i observations" % len(z_sample), ha='left', va='top', transform=ax.transAxes)
def compute_logL(beta): cosmo = Cosmology(omegaM=beta[0], omegaL=beta[1]) mu_pred = np.array(map(cosmo.mu, z_sample)) return - np.sum(0.5 * ((mu_sample - mu_pred) / dmu) ** 2)
def compute_logL(beta): cosmo = Cosmology(omegaM=beta[0], omegaL=beta[1]) mu_pred = np.array([cosmo.mu(z) for z in z_sample]) return -np.sum(0.5 * ((mu_sample - mu_pred) / dmu)**2)