| 1 |
+++ |
| 2 |
title = "Bayesian Sampling" |
| 3 |
weight = 40 |
| 4 |
+++ |
| 5 |
|
| 6 |
## Bayesian sampling |
| 7 |
|
| 8 |
Bayesian sampling of reflectometry models is a common tool in the analysis of specular reflectometry data. |
| 9 |
The Python programming language has a powerful infrastructure for this modelling, including packages such as [PyMC3](https://docs.pymc.io) and [PyStan](https://pystan.readthedocs.io/en/latest/). |
| 10 |
Here, we will show how the [emcee](https://emcee.readthedocs.io/en/stable/) Python may be used to enable Bayesian sampling in BornAgain and the differential evoulation optimisation algorithm from the [scipy](https://docs.scipy.org/doc/scipy/). |
| 11 |
|
| 12 |
### Example script |
| 13 |
|
| 14 |
To generate these images of the probability distributions of the parameters and the maximum likelihood reflectometry profile |
| 15 |
|
| 16 |
{{< figscg src="/files/fitted/CornerPlotBayes.png" width="500" class="center">}} |
| 17 |
{{< figscg src="/files/fitted/ReflectivityBayes.png" width="500" class="center">}} |
| 18 |
|
| 19 |
run this script |
| 20 |
|
| 21 |
{{< show-ex file="bayesian/likelihood_sampling.py" >}} |
| 22 |
|
| 23 |
|
| 24 |
### Explanation |
| 25 |
|
| 26 |
The system under investigation in the above example is a Ni-Ti multilayer material at the interface between an Si substrate and a vacuum. |
| 27 |
There are two parameters of interest, the thicknesses of the Ni and Ti layers. |
| 28 |
We know the scattering length densities for each, and that in total there are 10 repetitions of the Ni-Ti sandwich. |
| 29 |
This sample is created in the `get_sample` function. |
| 30 |
|
| 31 |
Having built the sample, it is necessary to obtain the real experimental data. |
| 32 |
This example requires the data file {{% ref-data-spec genx_alternating_layers.dat.gz %}} |
| 33 |
from the BornAgain repository. |
| 34 |
|
| 35 |
The environment variable `BA_DATA_DIR` must point to the `testdata/` directory. |
| 36 |
From the build directory, run: |
| 37 |
```bash |
| 38 |
BA_DATA_DIR=../testdata python3 ../auto/Examples/bayesian/likelihood_sampling.py |
| 39 |
``` |
| 40 |
|
| 41 |
The `get_real_data` function defines an uncertainty in the reflectivity of 10 %. |
| 42 |
|
| 43 |
The simulation is then defined in the `get_simulation` function, which is passed a series of angles, however, this may be modified to perform a Q-scan as necessary. |
| 44 |
The final function that is necessary is the simulation of specular reflectometry is the `run_simulation` function. |
| 45 |
This will take the angle-value to be simulated and thickness for the Ni and Ti layers and then return the result of the simulation as a `numpy.array`. |
| 46 |
|
| 47 |
We will use the `emcee` package to sampling the likelihood of the data (we are follow their [data fitting example](https://emcee.readthedocs.io/en/stable/tutorials/line/)). |
| 48 |
Therefore, it is necessary to define a likelihood (the `log_likelihood` function) objective. |
| 49 |
Then, within the main body of the scirpt we firstly find the maximum likelihood solution using a differential evolution algorithm from the `scipy.optimize` library. |
| 50 |
This should print that then thicknesses are around `7 nm` and `3 nm` for the Ni and Ti respectively. |
| 51 |
|
| 52 |
We can use the `emcee.EnsembleSampler` to probe the uncertainties in the parameters and there correlation. |
| 53 |
This will perform the sampling for some time (on my machine it took about 2.5 minutes to sample 1000 steps with 32 walkers). |
| 54 |
Having collected the samples, we can then unpack them and using the `corner` package visualise them. |
| 55 |
This will give the first image shown above. |
| 56 |
|
| 57 |
Finally, we can plot the maximum likelihood estimate for the model along with the experimental data. |
| 58 |
This is the second image above. |
| 59 |
|
| 60 |
Note that the `flat_samples` object describes the distributions shown in the corner plot. |
| 61 |
Therefore we can find values of interest, such as the standard deviation or confident intervals. |