def execute(self, ws, debug=0):
        print self.legend, 'Markov chain MC calculation started...'

        # time the action
        t = TStopwatch()
        t.Start()

        # model config is guaranteed to be here
        # the action won't configure without it
        # and this code will never run unless valid model config is found
        mconf = ws.obj(self._model_config_name)

        _poi = mconf.GetParametersOfInterest()

        mcInt = self._mc.GetInterval()

        # stop watch and print how long it took to get the interval
        t.Print()

        # iterate over all parameters of interest and print out the intervals
        # (could there be more than one?)
        _iter = _poi.createIterator()
        while True:
            _poi_name = _iter().GetTitle()

            lower_limit = mcInt.LowerLimit(_poi[_poi_name])
            upper_limit = mcInt.UpperLimit(_poi[_poi_name])

            print self.legend, 'MCMC interval for', _poi_name, 'is ['+ \
                  str(lower_limit) + ', ' + \
                  str(upper_limit) + ']'

            if _iter.Next() == None:
                break

        if self._posterior:
            # draw posterior plot

            print self.legend, 'making the posterior plot'
            _plot_name = _poi_name + '_mcmc_posterior_exost.' + self._plot_format
            c1 = TCanvas("c1", "c1", 600, 600)
            mcPlot = RooStats.MCMCIntervalPlot(mcInt)
            gROOT.SetStyle("Plain")
            mcPlot.Draw()
            c1.SaveAs(_plot_name)

        return (lower_limit, upper_limit)
Example #2
0
def GetBayesianInterval(filename="workspace.root",
                        wsname='myWS',
                        interactive=False):

    pInFile = ROOT.TFile(filename, "read")

    # load workspace
    pWs = pInFile.Get("myWS")
    if not pWs:
        print "workspace ", wsname, " not found"
        return -1

    # printout workspace content
    pWs.Print()

    # load and print data from workspace
    data = pWs.data("data")
    data.Print()

    # load and print S+B Model Config
    pSbHypo = pWs.obj("SbHypo")
    pSbHypo.Print()

    # create RooStats Bayesian calculator and set parameters
    # Metropolis-Hastings algorithm needs a proposal function
    sp = RooStats.SequentialProposal(10.0)

    mcmc = RooStats.MCMCCalculator(data, pSbHypo)
    mcmc.SetConfidenceLevel(0.95)
    mcmc.SetNumIters(100000)  # Metropolis-Hastings algorithm iterations
    mcmc.SetProposalFunction(sp)
    mcmc.SetNumBurnInSteps(500)  # first N steps to be ignored as burn-in
    mcmc.SetLeftSideTailFraction(0.0)
    mcmc.SetNumBins(
        40)  # for plotting only - does not affect limit calculation

    # estimate credible interval
    # NOTE: unfortunate notation: the UpperLimit() name refers
    #       to the upper boundary of an interval,
    #       NOT to the upper limit on the parameter of interest
    #       (it just happens to be the same for the one-sided
    #       interval starting at 0)
    pMcmcInt = mcmc.GetInterval()
    upper_bound = pMcmcInt.UpperLimit(pWs.var("xsec"))
    lower_bound = pMcmcInt.LowerLimit(pWs.var("xsec"))

    print "one-sided 95%.C.L. bayesian credible interval for xsec: ", "[", lower_bound, ", ", upper_bound, "]"

    # make posterior PDF plot for POI
    c1 = ROOT.TCanvas("posterior", "posterior")
    plot = RooStats.MCMCIntervalPlot(pMcmcInt)
    plot.Draw()
    c1.SaveAs("bayesian_mcmc_posterior.pdf")

    # make scatter plots to visualise the Markov chain
    c2 = ROOT.TCanvas("xsec_vs_beta_lumi", "xsec vs lumi_beta")
    plot.DrawChainScatter(pWs.var("xsec"), pWs.var("lumi_beta"))
    c2.SaveAs("scatter_mcmc_xsec_vs_beta_lumi.pdf")

    c3 = ROOT.TCanvas("xsec_vs_beta_efficiency", "xsec vs eff_beta")
    plot.DrawChainScatter(pWs.var("xsec"), pWs.var("eff_beta"))
    c3.SaveAs("scatter_mcmc_xsec_vs_beta_efficiency.pdf")

    c4 = ROOT.TCanvas("xsec_vs_beta_nbkg", "xsec vs nbkg_beta")
    plot.DrawChainScatter(pWs.var("xsec"), pWs.var("nbkg_beta"))
    c4.SaveAs("scatter_mcmc_xsec_vs_beta_nbkg.pdf")

    ROOT.gPad.Update()

    if interactive:
        raw_input("\npress <enter> to continue")