Ejemplo n.º 1
0
def test_jackknife_results():
    np.random.seed(0)
    x = np.random.normal(0, 1, 100)
    mu1, sig1 = jackknife(x, np.mean, kwargs=dict(axis=1))
    mu2, sig2 = jackknife(x, np.std, kwargs=dict(axis=1))

    assert_allclose([mu1, sig1, mu2, sig2], [0.0598080155345, 0.100288031685, 1.01510470168, 0.0649020337599])
Ejemplo n.º 2
0
def test_jackknife_pass_indices():
    np.random.seed(0)
    x = np.random.normal(0, 1, 100)

    res1 = jackknife(x, np.mean, kwargs=dict(axis=1))
    res2 = jackknife(x, lambda i: np.mean(x[i], axis=1), pass_indices=True)

    assert_allclose(res1, res2)
Ejemplo n.º 3
0
def test_jackknife_pass_indices():
    np.random.seed(0)
    x = np.random.normal(0, 1, 100)

    res1 = jackknife(x, np.mean, kwargs=dict(axis=1))
    res2 = jackknife(x, lambda i: np.mean(x[i], axis=1), pass_indices=True)

    assert_allclose(res1, res2)
Ejemplo n.º 4
0
def test_jackknife_results():
    np.random.seed(0)
    x = np.random.normal(0, 1, 100)
    mu1, sig1 = jackknife(x, np.mean, kwargs=dict(axis=1))
    mu2, sig2 = jackknife(x, np.std, kwargs=dict(axis=1))

    assert_allclose(
        [mu1, sig1, mu2, sig2],
        [0.0598080155345, 0.100288031685, 1.01510470168, 0.0649020337599])
Ejemplo n.º 5
0
def test_jackknife_multiple():
    np.random.seed(0)
    x = np.random.normal(0, 1, 100)

    mu1, sig1 = jackknife(x, np.mean, kwargs=dict(axis=1))
    mu2, sig2 = jackknife(x, np.std, kwargs=dict(axis=1))

    res = jackknife(x, mean_sigma, kwargs=dict(axis=1))

    assert_allclose(res[0], (mu1, sig1))
    assert_allclose(res[1], (mu2, sig2))
Ejemplo n.º 6
0
def test_jackknife_multiple():
    np.random.seed(0)
    x = np.random.normal(0, 1, 100)

    mu1, sig1 = jackknife(x, np.mean, kwargs=dict(axis=1))
    mu2, sig2 = jackknife(x, np.std, kwargs=dict(axis=1))

    res = jackknife(x, mean_sigma, kwargs=dict(axis=1))

    assert_allclose(res[0], (mu1, sig1))
    assert_allclose(res[1], (mu2, sig2))
Ejemplo n.º 7
0
def median_value(x,y, nbin=6, xmin= None, xmax = None, err=1 ):
    if xmin==None: xmin = x.min()
    if xmax==None: xmax = x.max()
    step = (xmax - xmin)/nbin
    bin_ = np.linspace(xmin-step/1.99, xmax+step/1.99, nbin+2)
    mid_bin  = (bin_+step/2.)[:nbin+1]
    dig  = np.digitize(x,bin_)
    if not err: return mid_bin, np.array([np.median(y[dig==i+1]) for i in xrange(len(mid_bin))])
    yout = np.zeros(len(mid_bin))
    yerr = np.zeros(len(mid_bin))
    std  = np.zeros(len(mid_bin))
    for i in xrange(len(mid_bin)):
        s = y[dig==i+1]
        s = s[(~np.isnan(s))&(np.isfinite(s))]
        if not len(s):continue
        mu, sig   = jackknife(s, np.mean, kwargs=dict(axis=1))
        yerr[i]   = sig
        yout[i]   = mu
        std[i]    = np.std(s)


    return mid_bin, yout, yerr, std #np.array([np.mean(y[dig==i+1]) for i in xrange(len(mid_bin))])
setup_text_plots(fontsize=8, usetex=True)

#------------------------------------------------------------
# sample values from a normal distribution
np.random.seed(123)
m = 1000  # number of points
data = norm(0, 1).rvs(m)

#------------------------------------------------------------
# Compute jackknife resamplings of data
from astroML.resample import jackknife
from astroML.stats import sigmaG

# mu1 is the mean of the standard-deviation-based width
mu1, sigma_mu1, mu1_raw = jackknife(data,
                                    np.std,
                                    kwargs=dict(axis=1, ddof=1),
                                    return_raw_distribution=True)

pdf1_theory = norm(1, 1. / np.sqrt(2 * (m - 1)))
pdf1_jackknife = norm(mu1, sigma_mu1)

# mu2 is the mean of the interquartile-based width
#  WARNING: do not use the following in practice.  This example
#           shows that jackknife fails for rank-based statistics.
mu2, sigma_mu2, mu2_raw = jackknife(data,
                                    sigmaG,
                                    kwargs=dict(axis=1),
                                    return_raw_distribution=True)
pdf2_theory = norm(data.std(), 1.06 / np.sqrt(m))
pdf2_jackknife = norm(mu2, sigma_mu2)
print(mu2, sigma_mu2)
Ejemplo n.º 9
0
from matplotlib import pyplot as plt

#------------------------------------------------------------
# sample values from a normal distribution
np.random.seed(123)
m = 1000  # number of points
data = norm(0, 1).rvs(m)

#------------------------------------------------------------
# Compute jackknife resamplings of data
from astroML.resample import jackknife
from astroML.stats import sigmaG

# mu1 is the mean of the standard-deviation-based width
mu1, sigma_mu1, mu1_raw = jackknife(data, np.std,
                                    kwargs=dict(axis=1, ddof=1),
                                    return_raw_distribution=True)

pdf1_theory = norm(1, 1. / np.sqrt(2 * (m - 1)))
pdf1_jackknife = norm(mu1, sigma_mu1)

# mu2 is the mean of the interquartile-based width
#  WARNING: do not use the following in practice.  This example
#           shows that jackknife fails for rank-based statistics.
mu2, sigma_mu2, mu2_raw = jackknife(data, sigmaG,
                                    kwargs=dict(axis=1),
                                    return_raw_distribution=True)
pdf2_theory = norm(data.std(), 1.06 / np.sqrt(m))
pdf2_jackknife = norm(mu2, sigma_mu2)
print mu2, sigma_mu2