Example 1: One-dimensional multi-modal problem

Get a copy of the tutorial example code:

$ git clone https://github.com/JohannesBuchner/pymultinest-tutorial

In the folder example1, find the file 1d_multimodal.py. It illustrates how to call pymultinest with

  • one dimension only
  • a uniform prior
  • some made-up likelihood function
import json
import numpy
from numpy import log, exp, pi
import scipy.stats, scipy
import pymultinest
import matplotlib.pyplot as plt

# we define the problem: we need a prior function which maps from [0:1] to the parameter space

# we only have one parameter, the position of the gaussian (ndim == 1)
# map it from the unity interval 0:1 to our problem space 0:2 under a uniform prior
def prior(cube, ndim, nparams):
	cube[0] = cube[0] * 2

# our likelihood function consists of 6 gaussians modes (solutions) at the positions
positions = numpy.array([0.1, 0.2, 0.5, 0.55, 0.9, 1.1])
width = 0.01

def loglike(cube, ndim, nparams):
	# get the current parameter (is between 0:2 now)
	pos = cube[0]
	likelihood = exp(-0.5 * ((pos - positions) / width)**2) / (2*pi*width**2)**0.5
	return log(likelihood.mean())

# number of dimensions our problem has
parameters = ["position"]
n_params = len(parameters)

# run MultiNest
pymultinest.run(loglike, prior, n_params, outputfiles_basename='out/',
	resume = False, verbose = True)
json.dump(parameters, open('out/params.json', 'w')) # save parameter names

# now run the script and analyse the output using multinest_marginals.py::
# 
#    $ python 1_1d_multimodal.py &&  multinest_marginals.py 1_1d_multimodal_out
#
# then open the file 1_1d_multimodal_outmarg.pdf
#
# Btw, ln(ev) should be ln(1 / 2)

Your task

  1. Study the example to understand how to call pymultinest.

  2. Run it, and analyse the output with multinest_marginals.py
    # run the script
    python 1d_multimodal.py
    # analyse the output, which is in out/.
    # multinest_marginals.py is a plotting tool that comes with pymultinest.
    multinest_marginals.py out/
    # open the marginal plots at marg.pdf
    xdg-open out/marg.pdf
    
  3. (optional) compare with the minimal demo script of pymultinest pymultinest_demo_minimal.py.

    The not-so-minimal demo at pymultinest_demo.py shows how to do plotting yourself, and how to access the computed evidence.

Continue with Example 2: Gaussian Emission lines ...

Table Of Contents

Previous topic

Welcome to the pymultinest tutorial!

Next topic

Example 2: Gaussian Emission lines

This Page