Bayesian sampling of reflectometry models is a common tool in the analysis of specular reflectometry data. The Python programming language has a powerful infrastructure for this modelling, including packages such as PyMC3 and PyStan. Here, we show how emcee enables Bayesian sampling in BornAgain. The initial maximum-likelihood estimate uses the differential evolution algorithm exposed by lmfit.
To generate these images of the probability distributions of the parameters and the maximum likelihood reflectometry profile
run this script
|
|
The system under investigation in the above example is a Ni-Ti multilayer material at the interface between an Si substrate and a vacuum.
There are two parameters of interest, the thicknesses of the Ni and Ti layers.
We know the scattering length densities for each, and that in total there are 10 repetitions of the Ni-Ti sandwich.
This sample is created in the get_sample function.
Having built the sample, it is necessary to obtain the real experimental data. This example requires the data file genx_alternating_layers.dat.gz
from the BornAgain repository.
The environment variable BA_DATA_DIR must point to the testdata/ directory.
From the build directory, run:
BA_DATA_DIR=../testdata python3 ../auto/Examples/bayesian/likelihood_sampling.py
The get_real_data function defines an uncertainty in the reflectivity of 10 %.
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.
The final function that is necessary is the simulation of specular reflectometry is the run_simulation function.
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.
We use the emcee package to sample the likelihood of the data, following its
data fitting example.
Therefore, it is necessary to define a likelihood (the log_likelihood function) objective.
Then, within the main body of the script, we first find the maximum likelihood
solution using differential evolution through lmfit.minimize.
This should print thicknesses around 7 nm and 3 nm for Ni and Ti,
respectively.
We can use the emcee.EnsembleSampler to probe the parameter uncertainties
and their correlation.
This will perform the sampling for some time (on my machine it took about 2.5 minutes to sample 1000 steps with 32 walkers).
Having collected the samples, we can then unpack them and using the corner package visualise them.
This will give the first image shown above.
Finally, we can plot the maximum likelihood estimate for the model along with the experimental data. This is the second image above.
Note that the flat_samples object describes the distributions shown in the corner plot.
Therefore we can find values of interest, such as the standard deviation or confident intervals.