コード例 #1
0
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
コード例 #2
0
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
コード例 #3
0
ファイル: fig_gp_mu_z.py プロジェクト: azizur77/astroML
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)
コード例 #4
0
ファイル: fig_lyndenbell_gals.py プロジェクト: hamogu/astroML
data_red = data[flag_red]
data_blue = data[flag_blue]

# truncate sample (optional: speeds up computation)
# data_red = data_red[::10]
# data_blue = data_blue[::10]
print data_red.size, "red galaxies"
print data_blue.size, "blue galaxies"

# ------------------------------------------------------------
# Distance Modulus calculation:
#  We need functions approximating mu(z) and z(mu)
#  where z is redshift and mu is distance modulus.
#  We'll accomplish this using the cosmology class and
#  scipy's cubic spline interpolation.
cosmo = Cosmology()
z_sample = np.linspace(0.01, 1.5, 100)
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)

data = [data_red, data_blue]
titles = ["$u-r > 2.22$", "$u-r < 2.22$"]
markers = ["o", "^"]
archive_files = ["lumfunc_red.npz", "lumfunc_blue.npz"]


def compute_luminosity_function(z, m, M, m_max, archive_file):
    """Compute the luminosity function and archive in the given file.

    If the file exists, then the saved results are returned.
コード例 #5
0
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)
コード例 #6
0
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)
コード例 #7
0
data_red = data[flag_red]
data_blue = data[flag_blue]

# truncate sample (optional: speeds up computation)
#data_red = data_red[::10]
#data_blue = data_blue[::10]
print data_red.size, "red galaxies"
print data_blue.size, "blue galaxies"

#------------------------------------------------------------
# Distance Modulus calculation:
#  We need functions approximating mu(z) and z(mu)
#  where z is redshift and mu is distance modulus.
#  We'll accomplish this using the cosmology class and
#  scipy's cubic spline interpolation.
cosmo = Cosmology()
z_sample = np.linspace(0.01, 1.5, 100)
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)

data = [data_red, data_blue]
titles = ['$u-r > 2.22$', '$u-r < 2.22$']
markers = ['o', '^']
archive_files = ['lumfunc_red.npz', 'lumfunc_blue.npz']


def compute_luminosity_function(z, m, M, m_max, archive_file):
    """Compute the luminosity function and archive in the given file.

    If the file exists, then the saved results are returned.
コード例 #8
0
from astroML.linear_model import LinearRegression, PolynomialRegression, BasisFunctionRegression, NadarayaWatson

# ----------------------------------------------------------------------
# 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([cosmo.mu(zi) for zi in z])

# ------------------------------------------------------------
# Define our classifiers
basis_mu = np.linspace(0, 2, 15)[:, None]
basis_sigma = 3 * (basis_mu[1] - basis_mu[0])

subplots = [221, 222, 223, 224]
classifiers = [
    LinearRegression(),
    PolynomialRegression(4),
    BasisFunctionRegression("gaussian", mu=basis_mu, sigma=basis_sigma),
    NadarayaWatson("gaussian", h=0.1),
]
コード例 #9
0
    BasisFunctionRegression, NadarayaWatson

#----------------------------------------------------------------------
# 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([cosmo.mu(zi) for zi in z])

#------------------------------------------------------------
# Define our classifiers
basis_mu = np.linspace(0, 2, 15)[:, None]
basis_sigma = 3 * (basis_mu[1] - basis_mu[0])

subplots = [221, 222, 223, 224]
classifiers = [
    LinearRegression(),
    PolynomialRegression(4),
    BasisFunctionRegression('gaussian', mu=basis_mu, sigma=basis_sigma),
    NadarayaWatson('gaussian', h=0.1)
]
コード例 #10
0
ファイル: fig_nonlinear_mu_z.py プロジェクト: BTY2684/astroML
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)
コード例 #11
0
ファイル: fig_nonlinear_mu_z.py プロジェクト: BTY2684/astroML
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([cosmo.mu(z) for z in 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)
コード例 #12
0
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)
コード例 #13
0

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([cosmo.mu(z) for z in 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,
コード例 #14
0
ファイル: fig_rbf_ridge_mu_z.py プロジェクト: BTY2684/astroML
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
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([cosmo.mu(zi) for zi in 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)