import numpy as np from matplotlib import pyplot as plt from scipy.stats import lognorm from sklearn.linear_model import LinearRegression, Ridge, Lasso from astroML.cosmology import Cosmology from astroML.datasets import generate_mu_z import matplotlib #---------------------------------------------------------------------- # generate data np.random.seed(0) z_sample, mu_sample, dmu = generate_mu_z(100, random_state=0) cosmo = Cosmology() z = np.linspace(0.01, 2, 1000) mu = np.asarray(map(cosmo.mu, z)) #------------------------------------------------------------ # Manually convert data to a gaussian basis # note that we're ignoring errors here, for the sake of example. def gaussian_basis(x, mu, sigma): return np.exp(-0.5 * ((x - mu) / sigma) ** 2) centers = np.linspace(0, 1.8, 100) widths = 0.2 X = gaussian_basis(z_sample[:, np.newaxis], centers, widths)
from astroML.cosmology import Cosmology from astroML.plotting.mcmc import convert_to_stdev from astroML.decorators import pickle_results #---------------------------------------------------------------------- # 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 the data z_sample, mu_sample, dmu = generate_mu_z(100, z0=0.3, dmu_0=0.05, dmu_1=0.004, random_state=1) #------------------------------------------------------------ # define a log likelihood in terms of the parameters # beta = [omegaM, omegaL] 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) #------------------------------------------------------------ # Define a function to compute (and save to file) the log-likelihood @pickle_results('mu_z_nonlinear.pkl')
from sklearn.gaussian_process import GaussianProcess 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,
# from astroML.cosmology import Cosmology from astropy.cosmology import FlatLambdaCDM as 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) z_sample, mu_sample, dmu = generate_mu_z(100, random_state=0, Om0=0.27, H0=71) # cosmo = Cosmology() cosmo = Cosmology(Om0=0.27, H0=71) z = np.linspace(0.01, 2, 1000) # mu_true = np.asarray([cosmo.mu(zi) for zi in z]) mu_true = np.asarray(cosmo.distmod(z).value) #------------------------------------------------------------ # 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) #------------------------------------------------------------ # using scikit-learn