Ejemplo n.º 1
0
def plotPhi2(v_phi2, min_chi2, ndf, f_out, target):
    import scipy.stats as st
    from matplotlib import pyplot as plt
    import matplotlib as mpl
    plt.style.use('ggplot')
    plt.figure(figsize=(8, 8))
    goftarget = np.percentile(v_phi2, target)
    goftarget68 = np.percentile(v_phi2, 68.8)
    plt.hist(v_phi2,
             np.linspace(min(v_phi2), max(v_phi2)),
             alpha=0.6,
             density=True,
             label="Bootstrapped $\phi^2$")
    _x = np.linspace(0, max(v_phi2), 1000)
    plt.plot(_x, st.chi2.pdf(_x, ndf), label=r'$\chi^2(N_{df})=%i$' % (ndf))
    plt.axvline(min_chi2, color="b", label="Tuning")
    plt.axvline(goftarget,
                color="k",
                label="Target %.2f-th percentile of $\phi^2$" % target)
    plt.axvline(goftarget68, color="m", label="68.8-th percentile of $\phi^2$")
    plt.xlabel("$\phi^2$")
    plt.axvline(ndf, label="$N_{df}=%i$" % (ndf))
    plt.legend()
    plt.savefigs(f_out)
    plt.close()
Ejemplo n.º 2
0
def plotToyDef(xedges,
               xmids,
               yvals,
               yerrs,
               corr,
               MIN,
               MAX,
               outfname="toy-defn"):
    ## Visualise the toy model
    from matplotlib import pyplot as plt
    import matplotlib as mpl
    plt.style.use('ggplot')
    plt.figure(figsize=(14, 5))
    #
    plt.subplot(121)
    plt.fill_between(xmids, MIN, MAX, color="pink")
    plt.errorbar(xmids,
                 yvals,
                 xerr=xedges[1:] - xmids,
                 yerr=yerrs,
                 marker="o",
                 linestyle="none",
                 color="k")
    plt.yscale("log")
    plt.xlabel("Observable")
    plt.ylabel("Entries")
    #
    plt.subplot(122)
    plt.imshow(corr,
               interpolation="none",
               norm=mpl.colors.Normalize(-1, 1),
               cmap="RdBu_r")
    #plt.matshow(cov)
    plt.colorbar()
    #
    plt.savefigs(outfname)
    #plt.show()
    plt.close()
Ejemplo n.º 3
0
def plotChi2(v_chi2, v_chi2_full, ndf, f_out):
    import scipy.stats as st
    from matplotlib import pyplot as plt
    import matplotlib as mpl
    plt.style.use('ggplot')
    plt.figure(figsize=(8, 8))
    plt.hist(v_chi2,
             np.linspace(min(v_chi2), max(v_chi2)),
             alpha=0.6,
             density=True,
             label="Bootstrapped $\chi^2$ w/o correlation")
    plt.hist(v_chi2_full,
             np.linspace(min(v_chi2_full), max(v_chi2_full)),
             alpha=0.6,
             density=True,
             label="Bootstrapped $\chi^2$ with correlation")
    _x = np.linspace(0, max(v_chi2), 1000)
    plt.plot(_x, st.chi2.pdf(_x, ndf), label=r'$\chi^2(N_{df})=%i$' % (ndf))
    plt.axvline(ndf, label="$N_{df}=%i$" % (ndf))
    plt.xlabel("$\chi^2$")
    plt.legend()
    plt.savefigs(f_out)
    plt.close()
Ejemplo n.º 4
0
plt.hist(chi2s[1:],
         bins=xchi2bins,
         density=True,
         histtype="stepfilled",
         color="aliceblue",
         edgecolor="darkblue",
         hatch="",
         label="Smear only",
         alpha=0.7)
plt.axvline(chi2s_fit[0], ls="--", color="darkred", alpha=0.7)
plt.hist(chi2s_fit[1:],
         bins=xchi2bins,
         density=True,
         histtype="stepfilled",
         color="mistyrose",
         edgecolor="darkred",
         hatch=r"",
         label="Smear+fit",
         alpha=0.7)
plt.plot(xchi2s, [st.chi2.pdf(x, args.NDATABINS) for x in xchi2s],
         color="darkblue",
         label=r"$\chi^2(k = N_\mathrm{bin})$")
plt.plot(xchi2s, [st.chi2.pdf(x, args.NDATABINS - 2) for x in xchi2s],
         color="darkred",
         label=r"$\chi^2(k = N_\mathrm{bin}-2)$")
plt.xlabel(r"$\phi^2$")
plt.xlim(0, 2.5 * args.NDATABINS)
plt.legend()
plt.savefigs("simplechi2")
plt.show()
Ejemplo n.º 5
0
def plotEigenTuneHist2(jd, xmids, central, ETs, truth, plot_prefix=""):

    TUNED = np.array(mkSignal2d(central, xmids))
    ETD = [[
        np.array(mkSignal2d(ETs[0], xmids)),
        np.array(mkSignal2d(ETs[1], xmids))
    ],
           [
               np.array(mkSignal2d(ETs[2], xmids)),
               np.array(mkSignal2d(ETs[3], xmids))
           ]]

    # from IPython import embed
    # embed()
    dfp = []
    for num, y in enumerate(TUNED):
        a = max(map(abs, [ETD[0][0][num] - y, ETD[0][1][num] - y]))
        b = max(map(abs, [ETD[1][0][num] - y, ETD[1][1][num] - y]))
        dfp.append(np.sqrt(a**2 + b**2))

    dfm = []
    for num, y in enumerate(TUNED):
        a = max(map(abs, [y - ETD[0][0][num], y - ETD[0][1][num]]))
        b = max(map(abs, [y - ETD[1][0][num], y - ETD[1][1][num]]))
        dfm.append(np.sqrt(a**2 + b**2))

    DFP = TUNED + dfp
    DFM = TUNED - dfm

    ERR = np.sqrt(1. / np.array(jd["TUNEINVDATACOV"]).reshape(
        (jd["NBINS"], jd["NBINS"])).diagonal())
    YV = jd["YVALS"]

    from matplotlib import pyplot as plt
    import matplotlib as mpl
    plt.style.use('ggplot')
    plt.figure(figsize=(10, 8))
    #
    X = np.array(range(len(YV))) + 0.5
    plt.fill_between(X, DFP, DFM, alpha=0.5, color="red")
    plt.plot(X, truth, label="Truth", linewidth=3, color="black")
    plt.yscale("log")
    plt.xlabel("# Bin")
    plt.ylabel("Entries")

    etcols = ["b", "m"]
    for num, ET in enumerate(ETD):
        plt.plot(X,
                 ET[0],
                 "%s-" % etcols[num],
                 linewidth=3,
                 label="ET%i+" % (num + 1))
        plt.plot(X,
                 ET[1],
                 "%s--" % etcols[num],
                 linewidth=3,
                 label="ET%i-" % (num + 1))

    plt.errorbar(X,
                 YV,
                 yerr=ERR,
                 marker="o",
                 linestyle="none",
                 color="k",
                 label="'Data'")
    plt.plot(X,
             TUNED,
             "r-",
             linewidth=3,
             label="Central tune ($\chi^2=%.1f$)" % jd["TUNECHI2"])
    plt.legend()
    plt.ylim((100, 580))
    #
    plt.savefigs("result-%s-%i-%s" %
                 (plot_prefix, jd["NBINS"], jd["CORRMODE"]))
    plt.close()
Ejemplo n.º 6
0
        iwin = np.argmin(allminPhi2)
        winnerP = allminP[iwin]
        winnerPhi2 = allminPhi2[iwin]

        import matplotlib.pyplot as plt
        plt.figure()
        plt.hist(np.array(allallPhi2).flatten(),
                 100,
                 density=True,
                 color="red")
        plt.axvline(winnerPhi2, c="black")
        xchi2 = np.linspace(0, 50, 200)
        fchi2 = st.chi2.pdf(xchi2, opts.NBINS - 1)
        plt.plot(xchi2, fchi2, color="blue")
        plt.savefigs(opts.PLOTPREFIX + "_phi2")
        plt.close()

        # This is the tuning main minimisation --- note that this uses the covariance WITHOUT sqrt(2)
        Cinv_phi2 = np.diag([1. / e**2 for e in yerrs])

        # Note add multistart here
        MIN_tune = optimize.minimize(
            lambda x: chi2wCov(yvals, xmids, Cinv_phi2, x), center, bounds=box)
        Cinv_tune = MIN_tune["hess_inv"].todense()

        jd = {
            "CORRMODE": opts.CORRMODE,
            "NBINS": opts.NBINS,
            "NDF": opts.NBINS - 2,  # TODO eventually nparams
            "TUNECHI2": MIN_tune["fun"],