Beispiel #1
0
parser.add_argument("-b",
                    "--xmax",
                    dest="b",
                    type=float,
                    default=1e2,
                    help="Maximum power law range (0 < a <= b).")
args = parser.parse_args()

# Set up the HAWCNest framework and RNG service
nest = HAWCNest()

nest.Service("StdRNGService", "rng", seed=args.seed)

nest.Configure()

rng = rng_service.GetService("rng")

x0, x1, A, idx = [args.a, args.b, 1., args.idx1]
p1 = PowerLaw(x0, x1, A, x0, idx)
p2 = PowerLaw(x0, x1, A, x0, args.idx2)

x = []
wt = []
for i in range(args.size[0]):
    u = rng.PowerLaw(p1.spectral_index(x0), p1.xmin, p1.xmax)
    x.append(u)

    w = p2.reweight(p1, u)
    wt.append(w)

mpl.rc("font", family="serif", size=14)
Beispiel #2
0
def main():
    usage = "%prog [options] [N = size of random data set]"
    parser = OptionParser(usage)
    parser.add_option("-s",
                      "--seed",
                      dest="seed",
                      type=int,
                      default=54112,
                      help="Seed for random number generator")
    parser.add_option("",
                      "--notest",
                      dest="test",
                      action="store_false",
                      default=True,
                      help="Skip integral tests")

    (opts, args) = parser.parse_args()

    if (len(args) != 1):
        parser.error("Wrong number of arguments!")

    nest = HAWCNest()
    nest.Service("StdRNGService", "rng", seed=opts.seed)

    nest.Configure()

    rng = rng_service.GetService("rng")

    x0, x1, idx1, idx2 = [1.0, 100.0, -2.0, -0.0]
    norm1 = 1.0 / PowerLaw(x0, x1, 1.0, x0, idx1).integrate(x0, x1)
    norm2 = 1.0 / PowerLaw(x0, x1, 1.0, x0, idx2).integrate(x0, x1)
    norm3 = 1.0 / BrokenPowerLaw(x0, x1, 1.0, x0, -1.0, 10.0, -2.0).integrate(
        x0, x1)
    norm4 = 1.0 / BrokenPowerLaw(x0, x1, 1.0, x0, -2.0, 10.0, -1.0).integrate(
        x0, x1)
    norm5 = 1.0/DoubleBrokenPowerLaw(x0,x1,1.0,x0,-1.4,4.5,0.4,34.2,-1.0). \
            integrate(x0,x1)
    norm6 = 1.0 / CutoffPowerLaw(x0, x1, 1.0, x0, -1.0, 10.0).integrate(x0, x1)
    norm7 = 1.0 / PowerLaw(x0, x1, 1.0, x0, -4.7).integrate(x0, x1)
    p1 = PowerLaw(x0, x1, norm1, x0, idx1)
    p2 = PowerLaw(x0, x1, norm2, x0, idx2)
    p3 = BrokenPowerLaw(x0, x1, 10.0 * norm3, x0, -1.0, 10.0, -2.0)
    p4 = BrokenPowerLaw(x0, x1, 0.1 * norm4, x0, -2.0, 10.0, -1.0)
    p5 = DoubleBrokenPowerLaw(x0, x1, norm6, x0, -1.4, 4.5, 0.4, 34.2, -1.0)
    p6 = CutoffPowerLaw(x0, x1, norm5, x0, -1.0, 10.0)
    p7 = PowerLaw(x0, x1, norm7, x0, -4.7)
    p8 = LogParabola(x0, x1, 1.0, x0, -0.3, -0.7)

    # Test the various power laws
    if (opts.test):
        runIntTests()

    print "Sampling", args[0], ("point" if int(args[0]) == 1 else "points")
    p1sample = []
    p2weight = []
    p3weight = []
    p4weight = []
    p5sample = []
    p6weight = []
    p7weight = []
    p8weight = []
    p2keeps = []
    for i in range(0, int(args[0])):
        val = p1.invert_integral(rng.Uniform())
        p1sample.append(val)
        p2weight.append(p2.reweight(p1, val))
        p3weight.append(p3.reweight(p1, val))
        p4weight.append(p4.reweight(p1, val))
        val2 = p5.invert_integral(rng.Uniform())
        p5sample.append(val2)
        p6weight.append(p6.reweight(p5, val2))
        p7weight.append(p7.reweight(p5, val2))
        p8weight.append(p8.reweight(p5, val2))
        if (rng.Uniform() < p2.prob_to_keep(p1, val)):
            p2keeps.append(val)

    # Get the power law points
    xplot = 10.0**N.linspace(P.log10(x0), P.log10(x1), 1000)
    y1plot = []
    y2plot = []
    y3plot = []
    y4plot = []
    y5plot = []
    y6plot = []
    y7plot = []
    y8plot = []
    for i in range(0, len(xplot)):
        y1plot.append(p1.evaluate(xplot[i]) / p1.integrate(x0, x1))
        y2plot.append(p2.evaluate(xplot[i]))
        y3plot.append(p3.evaluate(xplot[i]))
        y4plot.append(p4.evaluate(xplot[i]))
        y5plot.append(p5.evaluate(xplot[i]) / p5.integrate(x0, x1))
        y6plot.append(p6.evaluate(xplot[i]))
        y7plot.append(p7.evaluate(xplot[i]))
        y8plot.append(p8.evaluate(xplot[i]))

    # Make a log-log histogram of the random numbers
    gph1 = PrettyGraph(usetitle=True,
                       xtitle="X",
                       title="Reweighting power law with index " + str(idx1),
                       ytitle="Counts",
                       ylabeladjust=0.92,
                       xlabeladjust=0.96,
                       limits=[0.11, 0.97, 0.12, 0.95])
    hc1 = HistogramConverter(data=p1sample,
                             lowedge=x0,
                             highedge=x1,
                             bincount=21,
                             binstyle="log",
                             opts="ro",
                             label="Index -2",
                             norm=True)
    hc2 = HistogramConverter(data=p1sample,
                             lowedge=x0,
                             highedge=x1,
                             bincount=21,
                             binstyle="log",
                             opts="bo",
                             label="Index -1",
                             norm=True,
                             weights=p2weight)
    hc3 = HistogramConverter(data=p1sample,
                             lowedge=x0,
                             highedge=x1,
                             bincount=21,
                             binstyle="log",
                             opts="go",
                             label="Broken -2 to -1",
                             norm=True,
                             weights=p3weight)
    hc4 = HistogramConverter(data=p1sample,
                             lowedge=x0,
                             highedge=x1,
                             bincount=21,
                             binstyle="log",
                             opts="yo",
                             label="Broken -1 to -2",
                             norm=True,
                             weights=p4weight)
    fig, ax = gph1.make_graph()
    ax.set_xscale("log")
    ax.set_yscale("log")
    ax.set_autoscaley_on(False)
    ax.set_ylim([1.0e-4, 10.0])
    hc1.make_plot(ax)
    hc2.make_plot(ax)
    hc3.make_plot(ax)
    hc4.make_plot(ax)
    ax.plot(xplot, y1plot, "r--")
    ax.plot(xplot, y2plot, "b--")
    ax.plot(xplot, y3plot, "g--")
    ax.plot(xplot, y4plot, "y--")
    handles, labels = ax.get_legend_handles_labels()
    leg = ax.legend(handles, labels, numpoints=1, bbox_to_anchor=[0.97, 0.95])
    leg.get_frame().set_linewidth(0)

    # Now make it for the more complicated cases
    gph2 = PrettyGraph(usetitle=True,
                       xtitle="X",
                       title="Reweighting double broken power law",
                       ytitle="Counts",
                       ylabeladjust=0.92,
                       xlabeladjust=0.96,
                       limits=[0.11, 0.97, 0.12, 0.95])
    hc5 = HistogramConverter(data=p5sample,
                             lowedge=x0,
                             highedge=x1,
                             bincount=21,
                             binstyle="log",
                             opts="ro",
                             label="Double break sample",
                             norm=True)
    hc6 = HistogramConverter(data=p5sample,
                             lowedge=x0,
                             highedge=x1,
                             bincount=21,
                             binstyle="log",
                             opts="bo",
                             label="Cutoff",
                             norm=True,
                             weights=p6weight)
    hc7 = HistogramConverter(data=p5sample,
                             lowedge=x0,
                             highedge=x1,
                             bincount=21,
                             binstyle="log",
                             opts="go",
                             label="Power law",
                             norm=True,
                             weights=p7weight)
    hc8 = HistogramConverter(data=p5sample,
                             lowedge=x0,
                             highedge=x1,
                             bincount=21,
                             binstyle="log",
                             opts="yo",
                             label="Log parabola",
                             norm=True,
                             weights=p8weight)
    fig2, ax2 = gph2.make_graph()
    ax2.set_xscale("log")
    ax2.set_yscale("log")
    ax2.set_autoscaley_on(False)
    ax2.set_ylim([1.0e-9, 10.0])
    hc5.make_plot(ax2)
    hc6.make_plot(ax2)
    hc7.make_plot(ax2)
    hc8.make_plot(ax2)
    ax2.plot(xplot, y5plot, "r--")
    ax2.plot(xplot, y6plot, "b--")
    ax2.plot(xplot, y7plot, "g--")
    ax2.plot(xplot, y8plot, "y--")
    handles2, labels2 = ax2.get_legend_handles_labels()
    leg2 = ax2.legend(handles2,
                      labels2,
                      numpoints=1,
                      bbox_to_anchor=[0.45, 0.32])
    leg2.get_frame().set_linewidth(0)

    gph3 = PrettyGraph(usetitle=True,
                       xtitle="X",
                       title="Resampling power law",
                       ytitle="Counts",
                       ylabeladjust=0.92,
                       xlabeladjust=0.96,
                       limits=[0.11, 0.97, 0.12, 0.95])
    hc9 = HistogramConverter(data=p2keeps,
                             lowedge=x0,
                             highedge=x1,
                             bincount=21,
                             binstyle="log",
                             opts="bo",
                             label="Resampled",
                             norm=True)
    fig3, ax3 = gph3.make_graph()
    ax3.set_xscale("log")
    ax3.set_yscale("log")
    ax3.set_autoscaley_on(False)
    ax3.set_ylim([1.0e-4, 1.0])
    hc1.make_plot(ax3)
    hc9.make_plot(ax3)
    ax3.plot(xplot, y1plot, "r--")
    ax3.plot(xplot, y2plot, "b--")
    handles3, labels3 = ax3.get_legend_handles_labels()
    leg3 = ax3.legend(handles3,
                      labels3,
                      numpoints=1,
                      bbox_to_anchor=[0.97, 0.95])
    leg3.get_frame().set_linewidth(0)

    P.show()