Exemplo n.º 1
0
def test_trunc_exp():
    x = np.linspace(0, 10, 100)
    k = 0.25
    xlim = [3, 5]
    # replaced with from astroML.stats.random import trunc_exp
    # trunc_exp = trunc_exp_gen(name="trunc_exp", shapes='a, b, k')
    myfunc = trunc_exp(xlim[0], xlim[1], k)
    y = myfunc.pdf(x)
    zeros = np.zeros(len(y))

    # Test that the function is zero outside of defined limits
    assert_array_equal(y[x < xlim[0]], zeros[x < xlim[0]])
    assert_array_equal(y[x > xlim[1]], zeros[x > xlim[1]])
    inlims = (x < xlim[1]) & (x > xlim[0])
    C = k / (np.exp(k * xlim[1]) - np.exp(k * xlim[0]))

    # Test that within defined limits, function is exponential
    assert_array_equal(y[inlims], C*np.exp(k * x[inlims]))

    # Test that the PDF integrates to just about 1
    dx = x[1] - x[0]
    integral = np.sum(y * dx)
    assert np.round(integral, 1) == 1
Exemplo n.º 2
0
# Author: Jake VanderPlas <*****@*****.**>
# License: BSD
#   The figure produced by this code is published in the textbook
#   "Statistics, Data Mining, and Machine Learning in Astronomy" (2013)
#   For more information, see http://astroML.github.com
import numpy as np
from matplotlib import pyplot as plt
from astroML.stats.random import trunc_exp

#------------------------------------------------------------
# Sample from a truncated exponential distribution
N = 1E6
hmin = 4.3
hmax = 5.7
k = 0.6 * np.log(10)
true_dist = trunc_exp(hmin - 1.4, hmax + 3.4, 0.6 * np.log(10))

# draw the true distributions and heteroscedastic noise
np.random.seed(0)
h_true = true_dist.rvs(N)
dh = 0.5 * (1 + np.random.random(N))
h_obs = np.random.normal(h_true, dh)

# create observational cuts
cut = (h_obs < hmax) & (h_obs > hmin)

# select a random (not observationally cut) subsample
rand = np.arange(len(h_obs))
np.random.shuffle(rand)
rand = rand[:cut.sum()]
Exemplo n.º 3
0
#----------------------------------------------------------------------
# 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)

#------------------------------------------------------------
# Sample from a truncated exponential distribution
N = 1E6
hmin = 4.3
hmax = 5.7
k = 0.6 * np.log(10)
true_dist = trunc_exp(hmin - 1.4,
                      hmax + 3.4,
                      0.6 * np.log(10))

# draw the true distributions and heteroscedastic noise
np.random.seed(0)
h_true = true_dist.rvs(N)
dh = 0.5 * (1 + np.random.random(N))
h_obs = np.random.normal(h_true, dh)

# create observational cuts
cut = (h_obs < hmax) & (h_obs > hmin)

# select a random (not observationally cut) subsample
rand = np.arange(len(h_obs))
np.random.shuffle(rand)
rand = rand[:cut.sum()]