def check_median_sigmaG_approx(a, axis, keepdims, atol=0.15):
    med, sigmaG = median_sigmaG(a, axis=axis, keepdims=keepdims)
    mu, sigma = mean_sigma(a, axis=axis, ddof=1, keepdims=keepdims)

    print np.max(abs(med - mu))
    print np.max(abs(sigmaG - sigma))
    assert_allclose(med, mu, atol=atol)
    assert_allclose(sigmaG, sigma, atol=atol)
Beispiel #2
0
def check_median_sigmaG_approx(a, axis, keepdims, atol=0.15):
    med, sigmaG = median_sigmaG(a, axis=axis, keepdims=keepdims)
    mu, sigma = mean_sigma(a, axis=axis, ddof=1, keepdims=keepdims)

    print np.max(abs(med - mu))
    print np.max(abs(sigmaG - sigma))
    assert_allclose(med, mu, atol=atol)
    assert_allclose(sigmaG, sigma, atol=atol)
Beispiel #3
0
def test_median_sigmaG_approx(axis, keepdims, atol=0.02):
    np.random.seed(0)
    a = np.random.normal(0, 1, size=(10, 10000))

    med, sigmaG = median_sigmaG(a, axis=axis, keepdims=keepdims)
    mu, sigma = mean_sigma(a, axis=axis, ddof=1, keepdims=keepdims)

    assert_allclose(med, mu, atol=atol)
    assert_allclose(sigmaG, sigma, atol=atol)
def check_median_sigmaG(a, axis):
    from scipy.special import erfinv
    factor = 1. / (2 * np.sqrt(2) * erfinv(0.5))

    med1, sigmaG1 = median_sigmaG(a, axis=axis)
    med2 = np.median(a, axis=axis)
    q25, q75 = np.percentile(a, [25, 75], axis=axis)
    sigmaG2 = factor * (q75 - q25)

    assert_array_almost_equal(med1, med2)
    assert_array_almost_equal(sigmaG1, sigmaG2)
Beispiel #5
0
def check_median_sigmaG(a, axis):
    from scipy.special import erfinv
    factor = 1. / (2 * np.sqrt(2) * erfinv(0.5))

    med1, sigmaG1 = median_sigmaG(a, axis=axis)
    med2 = np.median(a, axis=axis)
    q25, q75 = np.percentile(a, [25, 75], axis=axis)
    sigmaG2 = factor * (q75 - q25)

    assert_array_almost_equal(med1, med2)
    assert_array_almost_equal(sigmaG1, sigmaG2)
Beispiel #6
0
def test_median_sigmaG(axis):
    np.random.seed(0)
    a = np.random.random((20, 40, 60))

    from scipy.special import erfinv
    factor = 1. / (2 * np.sqrt(2) * erfinv(0.5))

    med1, sigmaG1 = median_sigmaG(a, axis=axis)
    med2 = np.median(a, axis=axis)
    q25, q75 = np.percentile(a, [25, 75], axis=axis)
    sigmaG2 = factor * (q75 - q25)

    assert_array_almost_equal(med1, med2)
    assert_array_almost_equal(sigmaG1, sigmaG2)
Beispiel #7
0
def approximate_mu_sigma(xi, ei, axis=None):
    """Estimates of mu0 and sigma0 via equations 5.67 - 5.68"""
    if axis is not None:
        xi = np.rollaxis(xi, axis)
        ei = np.rollaxis(ei, axis)
        axis = 0

    mu_approx, sigmaG = median_sigmaG(xi, axis=axis)
    e50 = np.median(ei, axis=axis)
    var_twiddle = (sigmaG**2 + ei**2 - e50**2)
    sigma_twiddle = np.sqrt(np.maximum(0, var_twiddle))

    med = np.median(sigma_twiddle, axis=axis)
    mu = np.mean(sigma_twiddle, axis=axis)

    zeta = np.ones_like(mu)
    zeta[mu != 0] = med[mu != 0] / mu[mu != 0]

    var_approx = zeta**2 * sigmaG**2 - e50**2
    sigma_approx = np.sqrt(np.maximum(0, var_approx))

    return mu_approx, sigma_approx
def approximate_mu_sigma(xi, ei, axis=None):
    """Estimates of mu0 and sigma0 via equations 5.67 - 5.68"""
    if axis is not None:
        xi = np.rollaxis(xi, axis)
        ei = np.rollaxis(ei, axis)
        axis = 0

    mu_approx, sigmaG = median_sigmaG(xi, axis=axis)
    e50 = np.median(ei, axis=axis)
    var_twiddle = (sigmaG ** 2 + ei ** 2 - e50 ** 2)
    sigma_twiddle = np.sqrt(np.maximum(0, var_twiddle))

    med = np.median(sigma_twiddle, axis=axis)
    mu = np.mean(sigma_twiddle, axis=axis)

    zeta = np.ones_like(mu)
    zeta[mu != 0] = med[mu != 0] / mu[mu != 0]

    var_approx = zeta ** 2 * sigmaG ** 2 - e50 ** 2
    sigma_approx = np.sqrt(np.maximum(0, var_approx))

    return mu_approx, sigma_approx
Beispiel #9
0
# ----------------------------------------------------------------------
# Compute the Eddington-Malmquist bias & scatter

mtrue = generate_magnitudes(1e6, m_min=20, m_max=25)
photomErr = mag_errors(mtrue)

m1 = mtrue + np.random.normal(0, photomErr)
m2 = mtrue + np.random.normal(0, photomErr)
dm = m1 - m2

mGrid = np.linspace(21, 24, 50)
medGrid = np.zeros(mGrid.size)
sigGrid = np.zeros(mGrid.size)

for i in range(mGrid.size):
    medGrid[i], sigGrid[i] = median_sigmaG(dm[m1 < mGrid[i]])

# ----------------------------------------------------------------------
# Lutz-Kelker bias and scatter

mtrue = generate_magnitudes(1e6, m_min=17, m_max=20)
relErr = 0.3 * 10 ** (0.4 * (mtrue - 20))

pErrGrid = np.arange(0.02, 0.31, 0.01)

deltaM2 = 5 * np.log(1 + 2 * relErr ** 2)
deltaM4 = 5 * np.log(1 + 4 * relErr ** 2)

med2 = [np.median(deltaM2[relErr < e]) for e in pErrGrid]
med4 = [np.median(deltaM4[relErr < e]) for e in pErrGrid]
# draw underlying points
np.random.seed(0)
Npts = int(1E6)
x = np.random.normal(loc=0, scale=1, size=Npts)

# add error for each point
e = 3 * np.random.random(Npts)
x += np.random.normal(0, e)

# compute anderson-darling test
A2, sig, crit = anderson(x)
print("anderson-darling A^2 = %.1f" % A2)

# compute point statistics
mu_sample, sig_sample = mean_sigma(x, ddof=1)
med_sample, sigG_sample = median_sigmaG(x)

#------------------------------------------------------------
# plot the results
fig, ax = plt.subplots(figsize=(5, 3.75))
ax.hist(x, 100, histtype='stepfilled', alpha=0.2, color='k', density=True)

# plot the fitting normal curves
x_sample = np.linspace(-15, 15, 1000)
ax.plot(x_sample,
        norm(mu_sample, sig_sample).pdf(x_sample),
        '-k',
        label='$\sigma$ fit')
ax.plot(x_sample,
        norm(med_sample, sigG_sample).pdf(x_sample),
        '--k',
# Compute the statistics and plot the results
fig = plt.figure(figsize=(5, 7))
fig.subplots_adjust(left=0.13, right=0.95,
                    bottom=0.06, top=0.95,
                    hspace=0.1)

for i in range(2):
    ax = fig.add_subplot(2, 1, 1 + i)  # 2 x 1 subplot

    # compute some statistics
    A2, sig, crit = stats.anderson(vals[i])
    D, pD = stats.kstest(vals[i], "norm")
    W, pW = stats.shapiro(vals[i])

    mu, sigma = mean_sigma(vals[i], ddof=1)
    median, sigmaG = median_sigmaG(vals[i])

    N = len(vals[i])
    Z1 = 1.3 * abs(mu - median) / sigma * np.sqrt(N)
    Z2 = 1.1 * abs(sigma / sigmaG - 1) * np.sqrt(N)

    print 70 * '_'
    print "  Kolmogorov-Smirnov test: D = %.2g  p = %.2g" % (D, pD)
    print "  Anderson-Darling test: A^2 = %.2g" % A2
    print "    significance  | critical value "
    print "    --------------|----------------"
    for j in range(len(sig)):
        print "    %.2f          | %.1f%%" % (sig[j], crit[j])
    print "  Shapiro-Wilk test: W = %.2g p = %.2g" % (W, pW)
    print "  Z_1 = %.1f" % Z1
    print "  Z_2 = %.1f" % Z2
# draw underlying points
np.random.seed(0)
Npts = 1E6
x = np.random.normal(loc=0, scale=1, size=Npts)

# add error for each point
e = 3 * np.random.random(Npts)
x += np.random.normal(0, e)

# compute anderson-darling test
A2, sig, crit = anderson(x)
print "anderson-darling A^2 = %.1f" % A2

# compute point statistics
mu_sample, sig_sample = mean_sigma(x, ddof=1)
med_sample, sigG_sample = median_sigmaG(x)

#------------------------------------------------------------
# plot the results
fig, ax = plt.subplots(figsize=(5, 3.75))
ax.hist(x, 100, histtype='stepfilled', alpha=0.2,
        color='k', normed=True)

# plot the fitting normal curves
x_sample = np.linspace(-15, 15, 1000)
ax.plot(x_sample, norm(mu_sample, sig_sample).pdf(x_sample),
        '-k', label='$\sigma$ fit')
ax.plot(x_sample, norm(med_sample, sigG_sample).pdf(x_sample),
        '--k', label='$\sigma_G$ fit')
ax.legend()
mu0 = 0
gamma0 = 2
xi = cauchy(mu0, gamma0).rvs(10)

logL = cauchy_logL(xi, gamma[:, np.newaxis], mu)
logL -= logL.max()

#------------------------------------------------------------
# Find the max and print some information
i, j = np.where(logL >= np.max(logL))

print "mu from likelihood:", mu[j]
print "gamma from likelihood:", gamma[i]
print

med, sigG = median_sigmaG(xi)
print "mu from median", med
print "gamma from quartiles:", sigG / 1.483  # Equation 3.54

#------------------------------------------------------------
# Plot the results
plt.imshow(logL, origin='lower', cmap=plt.cm.binary,
           extent=(mu[0], mu[-1], gamma[0], gamma[-1]),
           aspect='auto')
plt.colorbar().set_label(r'$\log(L)$')
plt.clim(-5, 0)

plt.contour(mu, gamma, convert_to_stdev(logL),
            levels=(0.683, 0.955, 0.997),
            colors='k', linewidths=2)
                            hspace=0.1)
        ax = fig.add_subplot(1, 1, 1)

        avg = np.mean(qsos_m[:, i])
        std = np.std(qsos_m[:, i])
        data = (qsos_m[:, i] - avg) / std

        x = np.linspace(-5, 5, 1000)
        pdf = stats.norm(0, 1).pdf(x)

        A2, sig, crit = stats.anderson(data)
        D, pD = stats.kstest(data, "norm")
        W, pW = stats.shapiro(data)

        mu, sigma = mean_sigma(data, ddof=1)
        median, sigmaG = median_sigmaG(data)

        N = len(data)
        Z1 = 1.3 * abs(mu - median) / sigma * np.sqrt(N)
        Z2 = 1.1 * abs(sigma / sigmaG - 1) * np.sqrt(N)

        print 70 * '_'
        print "  Kolmogorov-Smirnov test: D = %.2g  p = %.2g" % (D, pD)
        print "  Anderson-Darling test: A^2 = %.2g" % A2
        print "    significance  | critical value "
        print "    --------------|----------------"
        for j in range(len(sig)):
            print "    %.2f          | %.1f%%" % (sig[j], crit[j])
        print "  Shapiro-Wilk test: W = %.2g p = %.2g" % (W, pW)
        print "  Z_1 = %.1f" % Z1
        print "  Z_2 = %.1f" % Z2
Beispiel #15
0
def doAstr511HW2(truth, obs, diskLF, haloLF):
    """Make a six-panel plot illustrating Astr511 homework #2"""

    # a) Define a ``gold parallax sample" by piObs/piErr > 10 and compare distance 
    #    modulus from the parallax measurement (D/kpc=1 milliarcsec/piObs) 
    #    to the distance modulus determined from r and Mr listed in the ``truth" file 
    piObs = obs[14]
    piErr = obs[15]
    piSNR = piObs / piErr
    D = np.where(piObs>0, 1/piObs, 1.0e6) 
    mask = (piSNR>10) 
    Dok = D[mask]
    DM = 5*np.log10(Dok/0.01)
    DMtrue = truth[4] - truth[8]
    diffDM = DM - DMtrue[mask]

    if (0):
        print '  mean =', np.mean(diffDM)
        print '   rms =', np.std(diffDM)
        med, sigG = median_sigmaG(diffDM)
        print 'median =', med
        print 'sigmaG =', sigG

    ### make vectors of x=distance modulus and y=absolute magnitude to run Cminus
    # sample faint limit
    rFaintLimit = 24.5
    # use either true distances, or estimated from parallax
    if (0):
        # using "truth" values of distance modulus, DM, to compute LF
        Mr = truth[8]
        rTrue = truth[4]
        DMtrue = rTrue - Mr 
        # type could be estimated from colors...
        typeWD = truth[13]
        # here the sample is defined using true apparent magnitude
        maskC = (rTrue < rFaintLimit) & (typeWD == 1) 
        xC = DMtrue[maskC]
        yC = Mr[maskC]
    else:
        # observed apparent magnitude
        rObs = obs[6]
        if (1):
            # fit a 4-th order polynomial to Mr(gr) relation (high-SNR sample)
            A, B, C, D, E = fitPhotomParallax(truth, obs)
            # and now apply
            gr = obs[4]-obs[6]
            MrPP = WDppFit(gr, A, B, C, D, E)
        else:
            # testing here: take true Mr...
            MrPP = truth[8]
        # here the sample is defined using observed apparent magnitude
        DMobs = rObs - MrPP
        # type could be estimated from colors...
        typeWD = truth[13]
        maskC = (rObs < rFaintLimit)  & (typeWD == 1) 
        xC = DMobs[maskC]
        yC = MrPP[maskC]


    ## the sample boundary is a simple linear function because using DM
    xCmax = rFaintLimit - yC
    yCmax = rFaintLimit - xC 
    ## split disk and halo, assume populations are known
    pop = truth[14]
    popC = pop[maskC]
    maskD = (popC==1) 
    xCD = xC[maskD]
    yCD = yC[maskD]
    xCmaxD = xCmax[maskD]
    yCmaxD = yCmax[maskD]
    maskH = (popC==2) 
    xCH = xC[maskH]
    yCH = yC[maskH]
    xCmaxH = xCmax[maskH]
    yCmaxH = yCmax[maskH]

    print 'observed sample size of xCD (disk)', np.size(xCD)
    print 'observed sample size of xCH (halo)', np.size(xCH)

    ### and now corrected Mr distributions: application of the Cminus method
    ## note that Cminus returns **cumulative** distribution functions
    ## however, bootstrap_Cminus (which calls Cminus) then does conversion 
    ## and returns **differential** distribution functions 

    #### this is where we run Cminus method (or use stored results) ####
    archive_file = 'CminusZI.npz'
    if not os.path.exists(archive_file):
        print ("- computing bootstrapped differential luminosity function ")
        #------------------------------------------------------------
        # aux arrays 
        x_fit = np.linspace(0, 15, 42)
        y_fit = np.linspace(10, 17, 42)
        #------------------------------------------------------------
        ## compute the Cminus distributions (with bootstrap)
        # disk subsample 
        print ("Disk sample with %i points" % len(xCD))
        xD_dist, dxD_dist, yD_dist, dyD_dist = bootstrap_Cminus(xCD, yCD, xCmaxD, yCmaxD,
                                                    x_fit, y_fit, Nbootstraps=20, normalize=True)
        # halo subsample 
        print ("Halo sample with %i points" % len(xCH))
        xH_dist, dxH_dist, yH_dist, dyH_dist = bootstrap_Cminus(xCH, yCH, xCmaxH, yCmaxH,
                                                    x_fit, y_fit, Nbootstraps=20, normalize=True)
        np.savez(archive_file,
                 x_fit=x_fit, xD_dist=xD_dist, dxD_dist=dxD_dist,
                 y_fit=y_fit, yD_dist=yD_dist, dyD_dist=dyD_dist)
        archive_file = 'CminusZIhalo.npz'
        np.savez(archive_file,
                 xH_dist=xH_dist, dxH_dist=dxH_dist,
                 yH_dist=yH_dist, dyH_dist=dyH_dist)

        #------------------------------------------------------------
    else:
        print "- using ZI's precomputed bootstrapped luminosity function results"
        # disk
        archive_file = 'CminusZI.npz'
        archive = np.load(archive_file)
        x_fit = archive['x_fit']
        xD_dist = archive['xD_dist']
        dxD_dist = archive['dxD_dist']
        y_fit = archive['y_fit']
        yD_dist = archive['yD_dist']
        dyD_dist = archive['dyD_dist']
        # halo
        archive_file = 'CminusZIhalo.npz'
        archive = np.load(archive_file)
        xH_dist = archive['xH_dist']
        dxH_dist = archive['dxH_dist']
        yH_dist = archive['yH_dist']
        dyH_dist = archive['dyH_dist']
        
    # bin mid positions for plotting the results:
    x_mid = 0.5 * (x_fit[1:] + x_fit[:-1])
    y_mid = 0.5 * (y_fit[1:] + y_fit[:-1])


    ### determine normalization of luminosity functions and number density distributions
    ## we need to renormalize LFs: what we have are differential distributions
    ## normalized so that their cumulative LF is 1 at the last point;
    ## we need to account for the fractional sky coverage and for flux selection effect
    ## and do a bit of extrapolation to our position (DM=0) at the end
    # 1) the fraction of covered sky: we know that the sample is defined by bGal > bMin
    bMin = 60.0  # in degrees
    # in steradians, given b > 60deg, dOmega = 2*pi*[1-cos(90-60)] = 0.8418 sr
    dOmega = 2*math.radians(180)*(1-math.cos(math.radians(90-bMin)))

    # 2) renormalization constants due to selection effects
    # these correspond to the left-hand side of eq. 12 from lecture 4 (page 26)
    DMmax0 = 11.0
    MrMax0 = rFaintLimit - DMmax0
    NmaskD = ((xCD < DMmax0) & (yCD < MrMax0)) 
    Ndisk = np.sum(NmaskD)
    NmaskH = ((xCH < DMmax0) & (yCH < MrMax0)) 
    Nhalo = np.sum(NmaskH)
    print 'Size of volume-limited samples D and H:', Ndisk, ' and ', Nhalo,  '(DM<', DMmax0, ', Mr<', MrMax0, ')'
       ## Size of volume-limited samples D and H: 49791  and  2065 (DM< 11.0 , Mr< 13.5 )

    # now we need to find what fraction of the total sample (without flux selection effects)
    # is found for x < DMmax0 and y < MrMax0. To do so, we need to integrate differential 
    # distributions returned by bootstrap_Cminus up to x=DMmax0 and y=MrMax0
    # (NB: we could have EASILY read off these from the original cumulative distributions returned
    #      from Cminus, but unfortunately bootstrap_Cminus returns only differential distributions;
    #      a design bug??) 
    # n.b. verified that all four distributions integrate to 1
    XfracD = IntDiffDistr(x_fit, xD_dist, DMmax0)
    YfracD = IntDiffDistr(y_fit, yD_dist, MrMax0)
    XfracH = IntDiffDistr(x_fit, xH_dist, DMmax0)
    YfracH = IntDiffDistr(y_fit, yH_dist, MrMax0)
    # and use eq. 12 to obtain normalization constant C for both subsamples
    Cdisk = Ndisk / (XfracD * YfracD)
    Chalo = Nhalo / (XfracH * YfracH)
    print 'Cdisk = ', Cdisk, 'Chalo = ', Chalo
       ## Cdisk = 608,082  Chalo = 565,598

    ## number density normalization
    # need to go from per DM to per pc:  dDM/dV = 5 / [ln(10) * dOmega * D^3] 
    Dpc = 10 * 10**(0.2*x_mid)
    # the units are "number of stars per pc^3" (of all absolute magnitudes Mr) 
    # note that here we assume isotropic sky distribution (division by dOmega!) 
    rhoD = Cdisk * 5/np.log(10)/dOmega / Dpc**3 * xD_dist
    rhoH = Chalo * 5/np.log(10)/dOmega / Dpc**3 * xH_dist
    # same fractional errors remain after renormalization 
    dxD_distN = rhoD * dxD_dist / xD_dist
    dxH_distN = rhoH * dxH_dist / xH_dist

    # differential luminosity function: per pc3 and mag, ** at the solar position **
    # to get proper normalization, we simply multiply differential distributions (whose
    # integrals are already normalized to 1) by the local number density distributions)
    # Therefore, we need to extrapolate rhoD and rhoH to D=0
    # and then use these values as normalization factors
    # The choice of extrapolation function is tricky (much more so than in case of
    # interpolation): based on our "prior knowledge", we choose
    # for disk: fit exponential disk, ln(rho) = ln(rho0) - H*D for 100 < D/pc < 500
    xFitExpDisk = Dpc[(Dpc > 100) & (Dpc < 500)]
    yFitExpDisk = np.log(rhoD[(Dpc > 100) & (Dpc < 500)])
    A, H = curve_fit(ExpDiskDensity, xFitExpDisk, yFitExpDisk)[0]
    rhoD0 = np.exp(A) 
    print 'rhoD0', rhoD0, ' scale height H (pc) =', 1/H
    # and for halo: simply take the median value for 500 < D/pc < 2000
    rhoH0 = np.median(rhoH[(Dpc > 500) & (Dpc < 2000)])
    print 'rhoH0', rhoH0
    # and now renormalize (rhoD0, rhoH0 = 0.0047 and 0.0000217) 
    # the units are "number of stars per pc^3 AND per mag" (at D=0)
    yD_distN = yD_dist * rhoD0
    yH_distN = yH_dist * rhoH0 
    # same fractional errors remain after renormalization 
    dyD_distN = yD_distN * dyD_dist / yD_dist
    dyH_distN = yH_distN * dyH_dist / yH_dist


    ### PLOTTING ###
    plot_kwargs = dict(color='k', linestyle='none', marker='.', markersize=1)
    plt.subplots_adjust(bottom=0.09, top=0.96, left=0.09, right=0.96, wspace=0.31, hspace=0.32)

    # plot the distribution of the distance modulus difference and compute its median
    # and root-mean-square scatter (hint: beware of outliers and clip at 3sigma!)
    hist, bins = np.histogram(diffDM, bins=50)    ## N.B. diffDM defined at the top
    center = (bins[:-1]+bins[1:])/2
    ax1 = plt.subplot(3,2,1)
    ax1.plot(center, hist, drawstyle='steps')   
    ax1.set_xlim(-1, 1)
    ax1.set_xlabel(r'$\mathrm{\Delta DM (mag)}$')
    ax1.set_ylabel(r'$\mathrm{dN/d(\Delta DM)}$')
    ax1.set_title('parallax SNR>10')
    ax1.plot(label=r'mpj legend')

    plt.text(0.25, 0.9,
         r'$\bar{x}=-0.023$',
         fontsize=12, bbox=dict(ec='k', fc='w', alpha=0.9),
         ha='center', va='center', transform=plt.gca().transAxes)
    plt.text(0.75, 0.9,
         r'$med.=-0.035$',
         fontsize=12, bbox=dict(ec='k', fc='w', alpha=0.9),
         ha='center', va='center', transform=plt.gca().transAxes)
    plt.text(0.2, 0.7,
         r'$\sigma=0.158$',
         fontsize=12, bbox=dict(ec='k', fc='w', alpha=0.9),
         ha='center', va='center', transform=plt.gca().transAxes)
    plt.text(0.75, 0.7,
         r'$\sigma_G=0.138$',
         fontsize=12, bbox=dict(ec='k', fc='w', alpha=0.9),
         ha='center', va='center', transform=plt.gca().transAxes)


    # plot uncorrected Mr vs. DM for disk
    ax2 = plt.subplot(3,2,2)
    ax2.set_xlim(4, 15)
    ax2.set_ylim(17, 10)
    ax2.set_xlabel(r'$\mathrm{DM}$')
    ax2.set_ylabel(r'$\mathrm{M_{r}}$')
    scatter_contour(xCD, yCD, threshold=20, log_counts=True, ax=ax2,
                histogram2d_args=dict(bins=40),
                plot_args=dict(marker='.', linestyle='none',
                               markersize=1, color='black'),
                contour_args=dict(cmap=plt.cm.bone))
    # ax2.plot(xCmaxD, yCD, color='blue')
    plt.text(0.75, 0.20,
         r'$disk$',
         fontsize=22, bbox=dict(ec='k', fc='w', alpha=0.3),
         ha='center', va='center', transform=plt.gca().transAxes)


    # plot uncorrected Mr vs. DM for halo
    ax3 = plt.subplot(3,2,3)
    ax3.set_xlim(4, 15)
    ax3.set_ylim(17, 10)
    ax3.set_xlabel(r'$\mathrm{DM}$')
    ax3.set_ylabel(r'$\mathrm{M_{r}}$')
    scatter_contour(xCH, yCH, threshold=20, log_counts=True, ax=ax3,
                histogram2d_args=dict(bins=40),
                plot_args=dict(marker='.', linestyle='none',
                               markersize=1, color='black'),
                contour_args=dict(cmap=plt.cm.bone))
    # ax3.plot(xCH, yCmaxH, '.k', markersize=0.1, color='blue')
    plt.text(0.75, 0.20,
         r'$halo$',
         fontsize=22, bbox=dict(ec='k', fc='w', alpha=0.3),
         ha='center', va='center', transform=plt.gca().transAxes)


    # uncorrected Mr distributions 
    ax4 = plt.subplot(3,2,4)
    ax4.hist(yCD, bins=30, log=True, histtype='step', color='red')
    ax4.hist(yCH, bins=30, log=True, histtype='step', color='blue')
    ax4.set_xlim(10, 17)
    ax4.set_ylim(1, 50000)
    ax4.set_xlabel(r'$\mathrm{M_r}$')
    ax4.set_ylabel(r'$\mathrm{dN/dM_r}$')
    plt.text(0.52, 0.20,
         r'$uncorrected \, M_r \, distribution$',
         fontsize=12, bbox=dict(ec='k', fc='w', alpha=0.3),
         ha='center', va='center', transform=plt.gca().transAxes)


    ## now plot the two remaining panels: corrected rho and Mr distributions
    # density distributions
    ax5 = plt.subplot(325, yscale='log')
    ax5.errorbar(Dpc, rhoD, dxD_distN, fmt='^k', ecolor='k', lw=1, color='red', label='disk')
    ax5.errorbar(Dpc, rhoH, dxH_distN, fmt='^k', ecolor='k', lw=1, color='blue', label='halo')

    ax5.set_xlim(0, 5000)
    ax5.set_ylim(0.000001, 0.01)
    ax5.set_xlabel(r'$\mathrm{D (pc)}$')
    ax5.set_ylabel(r'$\mathrm{\rho(D)} (pc^{-3}) $')
    ax5.legend(loc='upper right')

    # luminosity functions
    ax6 = plt.subplot(326, yscale='log')
    ax6.errorbar(y_mid, yD_distN, dyD_distN, fmt='^k', ecolor='k', lw=1, color='red')
    ax6.errorbar(y_mid, yH_distN, dyH_distN, fmt='^k', ecolor='k', lw=1, color='blue')

    # true luminosity functions
    MbinD = diskLF[0]
    psiD = diskLF[1] 
    MbinH = haloLF[0]
    psiH = haloLF[1] / 200
    ax6.plot(MbinD, psiD, color='red')
    ax6.plot(MbinH, psiH, color='blue')
 
    plt.text(0.62, 0.15,
         r'$\Psi \, in \, pc^{-3} mag^{-1}$',
         fontsize=12, bbox=dict(ec='k', fc='w', alpha=0.3),
         ha='center', va='center', transform=plt.gca().transAxes)

    ax6.set_xlim(10, 17)
    ax6.set_xlabel(r'$\mathrm{M_r}$')
    ax6.set_ylabel(r'$\Psi(M_r)=\mathrm{dN^2/dV \,dM_r}$')

    plt.savefig('Astr511HW2.png')
    plt.show() 
Beispiel #16
0
# Compute the statistics and plot the results
fig = plt.figure(figsize=(5, 7))
fig.subplots_adjust(left=0.13, right=0.95,
                    bottom=0.06, top=0.95,
                    hspace=0.1)

for i in range(2):
    ax = fig.add_subplot(2, 1, 1 + i)  # 2 x 1 subplot

    # compute some statistics
    A2, sig, crit = stats.anderson(vals[i])
    D, pD = stats.kstest(vals[i], "norm")
    W, pW = stats.shapiro(vals[i])

    mu, sigma = mean_sigma(vals[i], ddof=1)
    median, sigmaG = median_sigmaG(vals[i])

    N = len(vals[i])
    Z1 = 1.3 * abs(mu - median) / sigma * np.sqrt(N)
    Z2 = 1.1 * abs(sigma / sigmaG - 1) * np.sqrt(N)

    print(70 * '_')
    print("  Kolmogorov-Smirnov test: D = %.2g  p = %.2g" % (D, pD))
    print("  Anderson-Darling test: A^2 = %.2g" % A2)
    print("    significance  | critical value ")
    print("    --------------|----------------")
    for j in range(len(sig)):
        print("    {0:.2f}          | {1:.1f}%".format(sig[j], crit[j]))
    print("  Shapiro-Wilk test: W = %.2g p = %.2g" % (W, pW))
    print("  Z_1 = %.1f" % Z1)
    print("  Z_2 = %.1f" % Z2)
Beispiel #17
0
#----------------------------------------------------------------------
# Compute the Eddington-Malmquist bias & scatter

mtrue = generate_magnitudes(1E6, m_min=20, m_max=25)
photomErr = mag_errors(mtrue)

m1 = mtrue + np.random.normal(0, photomErr)
m2 = mtrue + np.random.normal(0, photomErr)
dm = m1 - m2

mGrid = np.linspace(21, 24, 50)
medGrid = np.zeros(mGrid.size)
sigGrid = np.zeros(mGrid.size)

for i in range(mGrid.size):
    medGrid[i], sigGrid[i] = median_sigmaG(dm[m1 < mGrid[i]])

#----------------------------------------------------------------------
# Lutz-Kelker bias and scatter

mtrue = generate_magnitudes(1E6, m_min=17, m_max=20)
relErr = 0.3 * 10**(0.4 * (mtrue - 20))

pErrGrid = np.arange(0.02, 0.31, 0.01)

deltaM2 = 5 * np.log(1 + 2 * relErr**2)
deltaM4 = 5 * np.log(1 + 4 * relErr**2)

med2 = [np.median(deltaM2[relErr < e]) for e in pErrGrid]
med4 = [np.median(deltaM4[relErr < e]) for e in pErrGrid]
mu0 = 0
gamma0 = 2
xi = cauchy(mu0, gamma0).rvs(10)

logL = cauchy_logL(xi, gamma[:, np.newaxis], mu)
logL -= logL.max()

#------------------------------------------------------------
# Find the max and print some information
i, j = np.where(logL >= np.max(logL))

print "mu from likelihood:", mu[j]
print "gamma from likelihood:", gamma[i]
print

med, sigG = median_sigmaG(xi)
print "mu from median", med
print "gamma from quartiles:", sigG / 1.483  # Equation 3.54

#------------------------------------------------------------
# Plot the results
fig = plt.figure(figsize=(5, 3.75))
plt.imshow(logL, origin='lower', cmap=plt.cm.binary,
           extent=(mu[0], mu[-1], gamma[0], gamma[-1]),
           aspect='auto')
plt.colorbar().set_label(r'$\log(L)$')
plt.clim(-5, 0)

plt.contour(mu, gamma, convert_to_stdev(logL),
            levels=(0.683, 0.955, 0.997),
            colors='k')
Beispiel #19
0
def check_median_sigmaG_approx(a, axis, keepdims, atol=0.15):
    med, sigmaG = median_sigmaG(a, axis=axis, keepdims=keepdims)
    mu, sigma = mean_sigma(a, axis=axis, ddof=1, keepdims=keepdims)

    assert_allclose(med, mu, atol=atol)
    assert_allclose(sigmaG, sigma, atol=atol)