Resolve "Add implementation of the UltraNest sampler"
This adds a wrapper to the Ultranest sampler into bilby.
The sampler has two integrator methods:
-
NestedSampler
: a "standard" simple nested sampler. This is used if the number of live points is specified (withnum_live_points
or any of the other aliases). -
ReactiveNestedSampler
: a nested sampler with a reactive exploration strategy. This is used if the number of live points is not specified.
This MR includes test codes. This has been checked to work using a version of the linear_regression_example
:
#!/usr/bin/env python
"""
An example of how to use bilby to perform paramater estimation for
non-gravitational wave data. In this case, fitting a linear function to
data with background Gaussian noise
"""
from __future__ import division
import bilby
import numpy as np
import matplotlib.pyplot as plt
# A few simple setup steps
label = 'linear_regression'
outdir = 'outdir'
bilby.utils.check_directory_exists_and_if_not_mkdir(outdir)
np.random.seed(235641)
# First, we define our "signal model", in this case a simple linear function
def model(time, m, c):
return time * m + c
# Now we define the injection parameters which we make simulated data with
injection_parameters = dict(m=0.5, c=0.2)
# For this example, we'll use standard Gaussian noise
# These lines of code generate the fake data. Note the ** just unpacks the
# contents of the injection_parameters when calling the model function.
sampling_frequency = 10
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
sigma = np.random.normal(1, 0.01, N)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
ax.plot(time, data, 'o', label='data')
ax.plot(time, model(time, **injection_parameters), '--r', label='signal')
ax.set_xlabel('time')
ax.set_ylabel('y')
ax.legend()
fig.savefig('{}/{}_data.png'.format(outdir, label))
# Now lets instantiate a version of our GaussianLikelihood, giving it
# the time, data and signal model
likelihood = bilby.likelihood.GaussianLikelihood(time, data, model, sigma)
# From hereon, the syntax is exactly equivalent to other bilby examples
# We make a prior
priors = dict()
priors['m'] = bilby.core.prior.Uniform(0, 5, 'm')
priors['c'] = bilby.core.prior.Uniform(-2, 2, 'c')
# And run sampler
result = bilby.run_sampler(
likelihood=likelihood, priors=priors, sampler='ultranest', nlive=500,
injection_parameters=injection_parameters, outdir=outdir, label=label)
result.plot_corner()
Resumption and checkpointing works if this script is exited during running and restarted. It has been checked to work for both the standard and reactive nested sampling by removing the nlive
option from run_sampler()
.
Ultranest has a variety of built-in MCMC step samplers that can be used. In this implementation they can be used by creating the step sampler and passing it to run_sampler()
with the step_sampler
keyword argument, e.g., the above example could be changed to include
import ultranest.stepsampler
stepsampler = ultranest.stepsampler.RegionSliceSampler(nsteps=10)
# And run sampler
result = bilby.run_sampler(
likelihood=likelihood, priors=priors, sampler='ultranest',
injection_parameters=injection_parameters, outdir=outdir, label=label,
step_sampler=stepsampler)
Resolves #456 (closed).
Merge request reports
Activity
added Sampling label
added 1 commit
- f7ffe9fc - samplers.txt: update samplers documentation to include ultranest
added 1 commit
- ae339dc3 - Add ability to pass in different ultranest step samplers
added 1 commit
- e6824e2a - Allow use of different ultranest step samplers
mentioned in merge request !768 (merged)
Added the install of ultranest to the Docker files in a separate MR !768 (merged).
- Resolved by Gregory Ashton
So it looks like ultranest isn't getting installed in the docker image because it needs cython: https://hub.docker.com/repository/registry-1.docker.io/bilbydev/v2-dockerfile-test-suite-python36/builds/5b4422fc-85c8-4b2a-a56b-fe3e8e2c9a58. For some reason, it doesn't treat it as a dependency.
I've added cython to the docker images here, checked that they build locally, and will wait for Dockerhub to build and restart this and !772 (merged).
added 36 commits
-
e6824e2a...dded5f6c - 27 commits from branch
lscsoft:master
- 10083d44 - Start adding ultranest sampler
- c1976c0e - Add Ultranest to samplers list
- 2b1ec881 - ultranest.py: add defaults
- 237625b3 - Add ultranest sampler tests
- 009fb6ef - ultranest.py: minor fixes
- e63c6277 - ultranest.py: minor fix
- 1f4ee6bd - samplers.txt: update samplers documentation to include ultranest
- 3edef4b7 - Add ability to pass in different ultranest step samplers
- dcc369ae - Allow use of different ultranest step samplers
Toggle commit list-
e6824e2a...dded5f6c - 27 commits from branch
changed milestone to %0.6.8
mentioned in commit 7ef0ac7d