From 39fb3261fdb3f0d30e7fe5aa3da8b19c5222e456 Mon Sep 17 00:00:00 2001 From: marlenaweidenauer <134061260+marlenaweidenauer@users.noreply.github.com> Date: Thu, 1 Jun 2023 18:42:26 +0200 Subject: [PATCH] Update README.md added emcee client to documentation --- clients/README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/clients/README.md b/clients/README.md index 210e5af..88f02c3 100644 --- a/clients/README.md +++ b/clients/README.md @@ -335,4 +335,52 @@ idata = tda.to_inference_data(my_chains, burnin=1000) az.summary(idata) ``` -[Full example sources here.](https://github.com/UM-Bridge/umbridge/blob/main/clients/python/tinyDA-client.ipynb) \ No newline at end of file +[Full example sources here.](https://github.com/UM-Bridge/umbridge/blob/main/clients/python/tinyDA-client.ipynb) + +## emcee client + +emcee is a python implementation of the affine-invariant ensemble sampler for Markov chain Monte Carlo (MCMC). It supports UM-Bridge models and can be installed from pip just like UM-Bridge. + +``` +pip install umbridge emcee +``` + +A basic emcee example using an UM-Bridge model is shown below. + +``` +import emcee +from umbridge.emcee import UmbridgeLogProb +import arviz as az +import argparse +import numpy as np +import matplotlib.pyplot as plt + + +if __name__ == "__main__": + + # Read URL from command line argument + parser = argparse.ArgumentParser(description='Minimal emcee sampler demo.') + parser.add_argument('url', metavar='url', type=str, + help='the ULR on which the model is running, for example http://localhost:4242') + args = parser.parse_args() + print(f'Connecting to host URL {args.url}') + + log_prob = UmbridgeLogProb(args.url, 'posterior') + + nwalkers = 32 + sampler = emcee.EnsembleSampler(nwalkers, log_prob.ndim, log_prob) + + # run sampler + p0 = np.random.rand(nwalkers, log_prob.ndim) + state = sampler.run_mcmc(p0, 100) + + # plot results + inference_data = az.from_emcee(sampler) + az.plot_pair(inference_data) + plt.tight_layout() + plt.savefig('emcee_inference.png') + + print(az.summary(inference_data, round_to=2)) +``` + +[Full example sources here.](https://github.com/UM-Bridge/umbridge/blob/main/clients/python/emcee-client.py)