Exemple #1
0
def testSimpleARGaus():

    print('testSimpleARGaus')

    # Test generating and fitting back with the same model

    model = simpleARGausModel(5400., 1.1, 8., 30., 10000.)

    dataGen = model.sample(minVal=4800., maxVal=6000.)

    plt.hist(dataGen, bins=150)
    plt.savefig('dataHist.pdf')
    plt.clf()

    print('Fitting')

    fitter = gl.Fitter(model, backend='minuit')
    res = fitter.fit(dataGen, verbose=True)

    from pprint import pprint

    pprint(model.parameters)

    plotter = gl.Plotter(model, dataGen)
    plotter.plotDataModel(nDataBins=100)
    plt.savefig('simpleARGausTest.pdf')
    plt.clf()

    exit(0)

    fitterB = gl.Fitter(model, backend='emcee')
    res = fitterB.fit(dataGen, verbose=True, nIterations=10000,
                      nWalkers=64)  # * nparams

    import corner

    fig = plt.figure(figsize=(16, 12))

    samples = res.chain[:, 1000:, :].reshape((-1, 5))
    c = corner.corner(samples, lw=1.0)
    c.savefig('argaus-corner.pdf')
    plt.clf()

    pprint(model.parameters)
Exemple #2
0
def testPhysical():

    with gl.name_scope('massFit'):
        with gl.name_scope("expBkg"):
            yBkg = gl.Parameter(500, name = 'yieldExp', minVal = -1000)

            minParam =  gl.Parameter(0.0, name = 'minVal', fixed = True)
            maxParam =  gl.Parameter(10.0, name = 'maxVal', fixed = True)

            aExp = gl.Parameter(-0.2, name = 'aExp')
            # expBkg = gl.Exponential({'a' : aExp, 'min' : gl.Parameter(0.0, fixed = True), 'max' : gl.Parameter(10.0, fixed = True)})
            expBkg = gl.Exponential({'a' : aExp, 'min' : minParam, 'max' : maxParam})

        with gl.name_scope("gaussSignal"):
            ySig = gl.Parameter(500, name = 'yieldSig', minVal = -1000)

            m = gl.Parameter(5.0, name = 'mean')
            s = gl.Parameter(1.0, name = 'sigma')

            g = gl.Gaussian({'mean' : m, 'sigma' : s})

    initialFitYields = {g.name : ySig, expBkg.name : yBkg}
    initialFitComponents = {g.name : g, expBkg.name : expBkg}

    model = gl.Model(initialFitYields = initialFitYields, initialFitComponents = initialFitComponents, minVal = 0., maxVal = 10.)

    data = model.sample(0, 10)

    # plt.hist(data, bins = 50, histtype = 'stepfilled')
    # plt.savefig('textExp.pdf')

    fitter = gl.Fitter(model, backend = 'minuit')

    res = fitter.fit(data, verbose = True)#, nIterations = 5000)

    # samples = res.chain[:, 500:, :].reshape((-1, model.getNFloatingParameters()))

    plotter = gl.Plotter(model, data)
    plotter.plotDataModel(nDataBins = 30)
    plt.savefig('testPlot.pdf')
    plt.clf()
Exemple #3
0
    from model import Model

    y = gl.Parameter(
        10000.,
        name='yield',
        minVal=5000.,
        maxVal=15000.,
    )

    m = Model(name='model',
              initialFitYields={'yield': y},
              initialFitComponents={'a': a})

    from fitter import Fitter

    fitter = Fitter(m, backend='minuit')
    res = fitter.fit(data, verbose=True)

    from plotter import Plotter

    plotter = gl.Plotter(m, data)
    plotter.plotDataModel(nDataBins=100)
    plt.savefig('argaus_fit.pdf')
    plt.clf()

    # x = np.linspace(5000, 5500, 1000)
    # l = a.prob(x)
    # plt.plot(x, l, lw = 1.0)
    # plt.savefig('argaus_plot.pdf')
Exemple #4
0
def testWithFracs():

    data = np.concatenate((np.random.normal(-2, 1., 1000), np.random.normal(-2, 3., 1000), np.random.normal(2., 1., 1000), np.random.normal(2., 3., 1000)))
    data = data[data > -10]
    data = data[data < 10]

    with gl.name_scope('fit'):

        with gl.name_scope('signal1'):

            m1_1 = gl.Parameter(-2.2, name = 'mean', minVal = -4, maxVal = 0)
            s1_1 = gl.Parameter(1.0, name = 'sigma1', minVal = 0., maxVal = 3.)
            s2_1 = gl.Parameter(3.0, name = 'sigma2', minVal = 1., maxVal = 5.)

            f1_1 = gl.Parameter(0.75, name = 'frac1', minVal = 0.0, maxVal = 1.0)

            with gl.name_scope("firstGaussian"):

                # y1 = gl.Parameter(len(data) // 2, name = 'yield', minVal = 0)

                g1_1 = gl.Gaussian({'mean' : m1_1, 'sigma' : s1_1})

            with gl.name_scope("secondGaussian"):

                # y2 = gl.Parameter(len(data) // 2, name = 'yield', minVal = 0)

                g2_1 = gl.Gaussian({'mean' : m1_1, 'sigma' : s2_1})

            # initialFitYields1 = {g1.name : y1, g2.name : y2}
            initialFitFracs = {g1_1.name : f1_1}
            initialFitComponents = {g1_1.name : g1_1, g2_1.name : g2_1}

            # model = gl.Model(initialFitYields = initialFitYields1, initialFitComponents = initialFitComponents1, minVal = -10., maxVal = 10.)

            model1 = gl.Model(initialFitFracs = initialFitFracs, initialFitComponents = initialFitComponents, minVal = -10., maxVal = 10.)

        with gl.name_scope('signal2'):

            m1_2 = gl.Parameter(5.0, name = 'mean', minVal = 0, maxVal = 10)
            s1_2 = gl.Parameter(1.0, name = 'sigma1', minVal = 0., maxVal = 3.)
            s2_2 = gl.Parameter(3.0, name = 'sigma2', minVal = 1., maxVal = 5.)

            f1_2 = gl.Parameter(0.75, name = 'frac1', minVal = 0.0, maxVal = 1.0)

            with gl.name_scope("firstGaussian"):

                # y1 = gl.Parameter(len(data) // 2, name = 'yield', minVal = 0)

                g1_2 = gl.Gaussian({'mean' : m1_2, 'sigma' : s1_2})

            with gl.name_scope("secondGaussian"):

                # y2 = gl.Parameter(len(data) // 2, name = 'yield', minVal = 0)

                g2_2 = gl.Gaussian({'mean' : m1_2, 'sigma' : s2_2})

            # initialFitYields1 = {g1.name : y1, g2.name : y2}
            initialFitFracs = {g1_2.name : f1_2}
            initialFitComponents = {g1_2.name : g1_2, g2_2.name : g2_2}

            # model = gl.Model(initialFitYields = initialFitYields1, initialFitComponents = initialFitComponents1, minVal = -10., maxVal = 10.)

            model2 = gl.Model(initialFitFracs = initialFitFracs, initialFitComponents = initialFitComponents, minVal = -10., maxVal = 10.)

        y1 = gl.Parameter(2000, name = 'yield1')
        y2 = gl.Parameter(2000, name = 'yield2')

        initialFitYields = {model1.name : y1, model2.name : y2}
        initialFitComponents = {model1.name : model1, model2.name : model2}

        model = gl.Model(initialFitYields = initialFitYields, initialFitComponents = initialFitComponents, minVal = -10., maxVal = 10.)

    # fitter = gl.Fitter(model, backend = 'minuit')
    #
    # res = fitter.fit(data, verbose = True)
    #
    # # model.setTotalYield(len(data))
    # plotter = gl.Plotter(model, data)
    # plotter.plotDataModel()
    #
    # plt.savefig('plotWithFracs.pdf')

    fitter = gl.Fitter(model, backend = 'emcee')

    res = fitter.fit(data, verbose = True, nIterations = 2000)

    samples = res.chain[:, 1000:, :].reshape((-1, model.getNFloatingParameters()))
    c = corner.corner(samples, lw = 1.0)
    c.savefig('corner.pdf')
    plt.clf()

    # model.setTotalYield(len(data))
    plotter = gl.Plotter(model, data)
    plotter.plotDataModel()

    plt.savefig('plotWithFracs.pdf')
Exemple #5
0
def testPhysicalSimBF():

    with gl.name_scope('massFit'):

        minParam =  gl.Parameter(0.0, name = 'minVal', fixed = True)
        maxParam =  gl.Parameter(10.0, name = 'maxVal', fixed = True)

        m = gl.Parameter(5.0, name = 'mean')
        s = gl.Parameter(1.0, name = 'sigma')

        with gl.name_scope('model1'):

            with gl.name_scope("expBkg"):
                yBkg1 = gl.Parameter(500, name = 'yieldExp', minVal = -1000)

                aExp1 = gl.Parameter(-0.2, name = 'aExp')
                expBkg1 = gl.Exponential({'a' : aExp1, 'min' : minParam, 'max' : maxParam})

            with gl.name_scope("gaussSignal"):
                ySig1 = gl.Parameter(500, name = 'yieldSig', minVal = -1000)

                g1 = gl.Gaussian({'mean' : m, 'sigma' : s})

        with gl.name_scope('model2'):

            with gl.name_scope("expBkg"):
                yBkg2 = gl.Parameter(1500, name = 'yieldExp', minVal = -1000)

                aExp2 = gl.Parameter(-0.1, name = 'aExp')
                expBkg2 = gl.Exponential({'a' : aExp2, 'min' : minParam, 'max' : maxParam})

            with gl.name_scope("gaussSignal"):
                ySig2 = gl.Parameter(1000, name = 'yieldSig', minVal = -1000)

                g2 = gl.Gaussian({'mean' : m, 'sigma' : s})

    initialFitYields1 = {g1.name : ySig1, expBkg1.name : yBkg1}
    initialFitComponents1 = {g1.name : g1, expBkg1.name : expBkg1}

    initialFitYields2= {g2.name : ySig2, expBkg2.name : yBkg2}
    initialFitComponents2 = {g2.name : g2, expBkg2.name : expBkg2}

    model1 = gl.Model(initialFitYields = initialFitYields1, initialFitComponents = initialFitComponents1)
    model2 = gl.Model(initialFitYields = initialFitYields2, initialFitComponents = initialFitComponents2)

    data1 = model1.sample(0, 10)
    data2 = model2.sample(0, 10)

    model = gl.SimultaneousModel(initialFitComponents = [model1, model2])

    # plt.hist(data, bins = 50, histtype = 'stepfilled')
    # plt.savefig('textExp.pdf')

    fitter = gl.Fitter(model, backend = 'emcee')

    res = fitter.fit([data1, data2], verbose = True, nIterations = 2000)

    samples = res.chain[:, 100:, :].reshape((-1, model.getNFloatingParameters()))

    plotter = gl.Plotter(model1, data1)
    plotter.plotDataModel(nDataBins = 50)
    plt.savefig('testPlot1.pdf')
    plt.clf()

    plotter = gl.Plotter(model2, data2)
    plotter.plotDataModel(nDataBins = 50)
    plt.savefig('testPlot2.pdf')
    plt.clf()

    c = corner.corner(samples)#, truths = [-0.2, 500., 5.0, 1.0, 500.])
    c.savefig('corner.pdf')
    plt.clf()
Exemple #6
0
def testToy():

    # data = np.concatenate((np.random.normal(-1., 4., 1000), np.random.normal(0., 1., 1000), np.random.normal(3., 1., 1000)))

    data1 = np.concatenate((np.random.normal(0., 1., 1000), np.random.normal(0., 3., 1000)))
    data2 = np.concatenate((np.random.normal(0., 1., 500), np.random.normal(0., 3., 500)))

    with gl.name_scope('massFit'):

        m1 = gl.Parameter(0.2, name = 'mean')
        s1 = gl.Parameter(1.0, name = 'sigma1')
        s2 = gl.Parameter(3.0, name = 'sigma2')

        with gl.name_scope("model1"):

            with gl.name_scope("firstGaussian"):

                y1 = gl.Parameter(len(data1) // 2, name = 'yield', minVal = 0)

                g1 = gl.Gaussian({'mean' : m1, 'sigma' : s1})

            with gl.name_scope("secondGaussian"):

                y2 = gl.Parameter(len(data1) // 2, name = 'yield', minVal = 0)

                g2 = gl.Gaussian({'mean' : m1, 'sigma' : s2})

            initialFitYields1 = {g1.name : y1, g2.name : y2}
            initialFitComponents1 = {g1.name : g1, g2.name : g2}

            model1 = gl.Model(initialFitYields = initialFitYields1, initialFitComponents = initialFitComponents1, minVal = -10., maxVal = 10.)

        with gl.name_scope("model2"):

            with gl.name_scope("firstGaussian"):

                y3 = gl.Parameter(len(data2) // 2, name = 'yield', minVal = 0)

            with gl.name_scope("secondGaussian"):

                y4 = gl.Parameter(len(data2) // 2, name = 'yield', minVal = 0)

            initialFitYields2 = {g1.name : y3, g2.name : y4}

            model2 = gl.Model(initialFitYields = initialFitYields2, initialFitComponents = initialFitComponents1, minVal = -10., maxVal = 10.)

        models = {model1.name : model1, model2.name : model2}

        model = gl.SimultaneousModel(initialFitComponents = [model1, model2])

    # s1, s2 = model.sample(-10, 10)
    #
    # plt.hist(s1, bins = 100, histtype = 'stepfilled')
    # plt.savefig('samples1.pdf')
    # plt.clf()
    # plt.hist(s2, bins = 100, histtype = 'stepfilled')
    # plt.savefig('samples2.pdf')

    fitter = gl.Fitter(model, backend = 'minuit')

    res = fitter.fit([data1, data2], verbose = True)

    # samples = res.chain[:, 200:, :].reshape((-1, 7))
    #
    # means = np.mean(samples, axis = 0)
    # stds = np.std(samples, axis = 0)
    #
    # for i in range(7):
    #
    #     plt.plot(samples[:,i], lw = 2.0)
    #     plt.savefig('samples' + str(i) + '.png')
    #     plt.clf()
    #
    #     plt.hist(samples[:,i], bins = 50)
    #     plt.savefig('samplesHist' + str(i) + '.png')
    #     plt.clf()
    #
    # c = corner.corner(samples, truths = [0.0, 1000., 1000., 500., 500., 1., 3.])
    # c.savefig('corner.pdf')

    f, axarr = plt.subplots(1, 2, sharey = True)

    plotter = gl.Plotter(model1, data1)
    plotter.plotDataModel(ax = axarr[0])

    axarr[0].set_xlabel('The things')
    axarr[0].set_ylabel('How many things')

    plt.xlim(np.min(data1), np.max(data1))

    # axarr[0].set_yscale("log", nonposy='clip')
    # axarr[0].set_ylim(1)

    plotter = gl.Plotter(model2, data2)
    plotter.plotDataModel(ax = axarr[1])

    axarr[1].set_xlabel('The things')

    plt.xlim(np.min(data2), np.max(data2))

    # axarr[1].set_yscale("log", nonposy='clip')
    # axarr[1].set_ylim(1)

    plt.savefig('testPlot.pdf', bbox_inches = 'tight')
Exemple #7
0
    yBkg = gl.Parameter(50000, name = 'cbYield', minVal = -10.)

    # fitYields = {signalGauss1.name : y1, signalGauss2.name : y2, combinatorial.name : yBkg}
    # fitComponents = {signalGauss1.name : signalGauss1, signalGauss2.name : signalGauss2, combinatorial.name : combinatorial}

    fitYields = {signalModel.name : ySig1, combinatorial.name : yBkg}
    fitComponents = {signalModel.name : signalModel, combinatorial.name : combinatorial}

    model = gl.Model(initialFitYields = fitYields, initialFitComponents = fitComponents, minVal = 4800., maxVal = 6000.)

    data = model.sample(minVal = 4800., maxVal = 6000.)

    fitter = gl.Fitter(model, backend = 'minuit')
    res = fitter.fit(data, verbose = False, nIterations = 250)
    pprint(model)
    plotter = gl.Plotter(model, data)
    plotter.plotDataModel(nDataBins = 50)
    plt.savefig('testData.pdf')
    plt.clf()
    #
    # fitter = gl.Fitter(model, backend = 'emcee')
    # res = fitter.fit(data, verbose = True, nIterations = 250, nWalkers = 10)
    #
    # plotter = gl.Plotter(model, data)
    # plotter.plotDataModel(nDataBins = 50)
    # plt.savefig('testData.pdf')
    # plt.clf()
    #
    # samples = res.chain[:, 50:, :].reshape((-1, model.getNFloatingParameters()))
    # c = corner.corner(samples, lw = 1.0)
    # c.savefig('corner.pdf')