This C++ version of BAT is still being maintained, but addition of new features is unlikely. Check out our new incarnation, BAT.jl, the Bayesian analysis toolkit in Julia. In addition to Metropolis-Hastings sampling, BAT.jl supports Hamiltonian Monte Carlo (HMC) with automatic differentiation, automatic prior-based parameter space transformations, and much more. See the BAT.jl documentation.

Measuring a decay rate - tutorial for BAT 0.9

Physics motivation

When developing a new detector in particle or nuclear physics it is important to measure a detector's efficiency. In practice this is often accomplished in two steps. First the background of the environment is measured. Then a radiation source of known strength is placed near the detector. Finally the counts with and without source are compared to verify that the detector operates properly.

In the following we assume that two measurements have been made, both of duration T = 100s. N1 = 50 is the number of background only counts, and N2 = 55 is the number of counts including the source. N1 and N2 are supposed to arise from a Poisson process. The goal is to extract the background rate RB and the signal rate RS.

An expanded introduction including the necessary formulae for this tutorial is found here.

Tutorial

This tutorial introduces the basics of BAT. It shows how to

We assume the reader has downloaded and installed BAT and is familiar with the C++ programming language. The detailed reference guide documenting BAT's internals provides further assistance.

The tutorial is split into four steps:


Step 1 - Getting started

Make sure the installation of BAT was successful:

  1. Navigate to the tools subdirectory of your BAT installation, e.g., ~/BAT-0.9/tools and run the script CreateProject.sh to create a project named CountingExp. A subdirectory is created.
  2. Have a look at the generated C++ classes and compile the code using make. The makefile is custom fit to the installation on your system, and in the following solutions, we will not display it.

Solution


Step 2 - Fitting the background-only model

The first model we test considers only background.

  1. Create a data point, add it to a data set and register the data set with the model
  2. Define the parameter RB and add it to the model
  3. Define the log likelihood for the Poisson process with parameter RB. The natural logarithm of the factorial is provided by BCMath::LogFact(int n). One can also use the approximation provided by BCMath::ApproxLogFact(int n), which is much faster for large numbers.
  4. Use a flat prior for RB
  5. Start to sample from the posterior using the Markov chain
  6. Find the mode of the posterior
  7. Save the results of the fit in text form and create a plot of the (marginal) posterior distribution

Solution


Step 3 - Include the second measurement

Now add the second measurement which includes the source, and learn about signal and background rate.

  1. Add a second data point, N2, to the data set
  2. Include the second parameter RS with flat prior in the model
  3. Update the likelihood to incorporate N2 and RS
  4. Plot the marginal distributions and compare the values of mean, median and mode for the individual parameters. What is the correlation between RB and RS?
  5. Extract the 95% limit on RS and save the plot.

Solution


Step 4 - Further steps

The basic steps should be clear by now. We want to learn about more detailed features of BAT. A frequent task is to create plots. But how to customize the plots? BAT resorts to the plotting engine provided by ROOT. As a simple example, we create a plot that combines the posterior distributions P(RB|N1) and P(RB|N1,N2) from step 2 and 3. Another issue is speed: during the Markov chain sampling, the BCModel methods LogLikelihood and LogAPrioriProbability are called in each iteration. Accordingly, even a small speedup in these methods may lead to a huge speedup of the overall execution time.

  1. Redo step 2 and 3. Save P(RB|N1) and P(RB|N1,N2) as a ROOT TH1D histogram. Limit RB to the range [0,1], RS to the range [0,0.5] and use more bins (300 instead of the default 100) to store the marginalized distribution.
  2. Normalize and plot the two histograms.
  3. Measure the time it takes to run the program.
  4. Modify LogAPrioriProbability to do nothing else than returning zero. This amounts to setting the prior to 1. Compare execution time.
  5. Multiplying the likelihood by a constant just affects the normalization, but not the values of mode, mean... Thus remove all terms that are added to LogLikelihood and which are independent of RB, RS. You should observe that running the program takes only significantly less time compared to 3.
  6. Redo step 2, but now use the Reference prior (=Jeffrey's prior here) for RB which reads P(RB)∝1/√RB. Does the posterior P(RB|N1) change significantly?

Solution



Frederik Beaujean
Last modified: Tue Apr 17 15:59:33 CEST 2012