Matlab MultiNest

Joe Romano and myself have written an implementation of MultiNest in Matlab, which (after a while of sitting just on our computers) is now available as a Beta release from the MultiNest repository.

This can be used to perform evidence calculation and posterior parameter estimation using nested sampling for a given dataset, likelihood function and model function. The release includes examples of how to set up these functions for use with the nested sampling routine. The code can use the MultiNest algorithm (implementing the method in “Algorithm 1” of Section 5.2 and Section 5.3 of Feroz, Hobson & Bridges, MNRAS, 398, pp. 1601-1614, 2009, but not the extra steps in Sections 5.4 through 5.7) to draw samples from the prior volume. Or, it can use an MCMC method to draw new samples (based on the method proposed in Veitch & Vecchio, PRD, 81, 062003, 2010), where the proposal is a Student-t distribution with 2 degrees of freedom defined by the covariance of the current “live points” (and also using differential evolution 10% of the time to aid hopping between modes in the likelihood).

These functions haven’t undergone much testing or been optimsed for efficiency, so if you use them and have any feedback please let me know.

The code has a license allowing free academic use and modification.

Update (24/03/15): I have now placed the development version of the code in github, so others can easily download it and contribute to its development.

Installing and running MultiNest

[Note – this post refered to MultiNest version 2.7. More up-to-date versions now exist that don’t require many of the things suggested below. But, there are some useful suggestions in the comments.]

Here I will detail the process I’ve gone through to install the Nested Sampling libraries produced by the MultiNest code (see Feroz, Hobson & Bridges, 2009 for a description of what the code does). The code comes with a README file giving some information on the installation process. The code also come with a Makefile, which may work for some straight away, but did not work for me – so below are the details of how I get everything installed (in particular the supplied example C-wrapper function).

This may only work on a system set up very similar to my own:

>> lsb_release -a
LSB Version:	core-2.0-ia32:core-2.0-noarch:core-3.0-ia32:core-3.0-noarch:\
Distributor ID:	Ubuntu
Description:	Ubuntu 9.10
Release:	9.10
Codename:	karmic

If you have a 64-bit Linux install and a non-Intel processor then these instructions may not work.

You may also need to have su access to install various libraries such as LAPACK. You may want get install a variety of things like:

>> sudo apt-get install gfortran libblas-dev liblapack-dev

Installing without C-wrapper eggbox example
The supplied Makefile assumes that you are compiling to run the code with MPI and uses MPI Fortran compilers. If you just want to build the MultiNest library libnest3.o without MPI (which I did) then this can be achieved by editing the Makefile by changing the first couple of lines to:

#FC = mpif90 -DMPI
FC = gfortran
#FFLAGS +=  -w -O3
FFLAGS +=  -w -O3 -ffree-line-length-none


The Makefiles for the examples given in the directories example_gauss_shell and example_obj_detect may also need editting e.g. change FC, FFLAGS and LIBS to the following:

FC = gfortran
FFLAGS +=  -w -O3 -ffree-line-length-none
LIBS=-L$(NESTED_lib) -lnest3 -llapack

However, without various major changes described below, the example in example_eggbox (a function to wrap the Fortran-based MultiNest library in C) will not work.

Installing with C-wrapper eggbox example
[Please see the helpful comment by Fred below that gives information on how to compile this without having to use the Intel compilers and just using gfortran]

To get the C-wrapper example in example_eggbox to run you will need to install some Intel libraries (there may be other ways of doing this, but I’m not sure what they would be). This advice below assumes you have a 32-bit architecture and are installing the IA-32 versions of the libraries.

You will need to go to the Intel website and download the free IntelĀ® Compiler Suite Professional Edition for Linux – you will need to register an email address for this, to which you will be sent a serial number to register the software. I had to do this twice to download the C++ compiler and the Fortran compiler seperately, although there may be a way of getting it all together as they contain a lot of the same libraries.

These will be download as l_cproc_p_11.1.072_ia32.tgz and l_cprof_p_11.1.072_ia32.tgz respectively. You should then untar the tarballs:

>> tar -xvzf l_cproc_p_11.1.072_ia32.tgz
>> tar -xvzf l_cprof_p_11.1.072_ia32.tgz

and you’ll have two directories, l_cproc_p_11.1.072_ia32 and l_cprof_p_11.1.072_ia32, in which there will be an installer script

First go into the l_cproc_p_11.1.072_ia32 directory for the C++ library and follow these instructions (much of which you will be prompted through on screen, so I will not document every step):

  • Run:

    >> ./
  • Step 1.
    Chose Option no. 3 – to install as non-root user (unless you want to install as root, in which case you should run the install script with sudo).
  • Step 3.
    Chose Option 2 – “I want to activate and install my product”
    Give the serial number you were emailed when you downloaded the product – should be of the form XXXX-XXXXXXXX. Hopefully this should be successful – if not then you can try again with the “Trial period acctivation” which gives you a 30 day trial.

    [If you’ve run the install process as sudo then this might not work (particularly on the Glasgow astro network) due to the http_proxy environment variable not being set if you run as route, so the license server cannot get online to register your serial number.]

  • Step 4.
    We only need to install the MKL and Intel C compiler, so we can do a “Custom install” –
    Option 2. Option 2 again “Custom Install – specify individual product components to install”. You can do the “Standard Install”, but this will install a lot of stuff that isn’t necessary for compiling MulitNest.

    Select components to install:
    The first option “Install the Intel(R) C++ Compiler for applications running on IA-32” is required, so select this. The second option “Install the Intel(R) Debugger for applications running on IA-32” may not be necessary, so can be selected if you like. The third option “Install the Intel(R) Math Kernel Library for applications running on IA-32” is required, so select this. The fourth option “Install the Intel(R) TBB for applications running on IA-32” is not necessary, so can be selected if you want it. The fifth option “Install the Intel(R) IPP for applications running on IA-32” is not necessary.

    You will then be asked for the install location – make sure this is somewhere you have write access to and has enough space – the default will be ${HOME}/intel/Compiler/11.1/072. Rather than my home directory (which is a network directory) I chose a location on my desktop computers hard disk (which for brevity I will call my_intel_dir).

    After this you may get a screen saying:

    Installation configuration – Missing Optional Pre-requisite
    There is one or more optional unresolved issues. It is highly recommended to fix
    it all before you continue the installation. You can fix it without exiting from
    the installation and re-check. Or you can quit from the installation, fix it and
    run the installation again.

    For example I have these various missing optional pre-requisites:

    Missing optional pre-requisite
    — No compatible Java* Runtime Environment (JRE) found
    — operating system type is not supported.
    — system glibc or kernel version not supported or not detectable
    — binutils version not supported or not detectable

    However, the pre-requisites are optional, so you should be able to choose to “Skip missing optional pre-requisites” and everything still work.

  • Step 5. Configuration summary
    Select option 1 to start the install – installation should only take a few minute or so.

Now install the Fortran libraries by going into the l_cprof_p_11.1.072_ia32 directory and again running the script. This is basically the same as the steps above, so follow the same process as before (it should also see the license you already entered for the other product – if not enter the serial number you were emailed).

At Step 4 – “Installation Type” again select a “Custom Install”. You will need to install the Fortran compiler, but other stuff such as the MKL will already be installed, so select “Custom Install – specify individual product components to install”. Select to install “Install the Intel(R) Fortran Compiler for applications running on IA-32” and after that it should see the other things are already installed, so you can select to not install them.

You will be prompted for an install location, but it will not allow you to use the same location as before (later you can move the libraries into the same location as the previously installed ones) – e.g. my_intel_fortran

Again there may be missing optional pre-requisites, but you can ignore these and continue with the installation.

Once installed you can copy the installed Fortran libraries into the directories previously created during the C compiler install (various things are in both installs, but these should be identical, so can either be overwritten or not when copying) e.g.

>> cp -r my_intel_fortran/bin/* my_intel_dir/bin
>> cp -r my_intel_fortran/Documentation my_intel_dir/Documentation
>> cp -r my_intel_fortran/include/* my_intel_dir/include
>> cp -r my_intel_fortran/lib/ia32 my_intel_dir/lib/ia32
>> cp -r my_intel_fortran/man my_intel_dir/man
>> cp -r my_intel_fortran/Samples my_intel_dir/Samples

You can then delete the Fortran installation.

>> rm -rf my_intel_fortran

Once these are installed you will now have the Intel C++ compiler icc, the Intel Fortran compiler ifort and the Intel Math Kernal Libraries. It is also useful to create an environment variable pointing to the libraries and binaries, so I have created INTELLIBS and INTELBIN as environment variables giving the path to the Intel libraries and binaries and MNLIB as the path to my MultiNest library. You may want to add paths to these into your profile setup e.g. for C-shell add the following to your .cshrc file:


You will also want to link the MKL libraries to the same lib directory as the other codes:

>> cd ${INTELLIBS}
>> ln -s ../../mkl/lib/32/* .

Alternatively you could link all the libraries and binary files into somewhere like ${HOME}/lib and ${HOME}/bin.

Now to install MultiNest you will need to do some more editing of the Makefiles. In the Makefile for the main MultiNest directory you will need to make the following changes to the first couple of lines:

#FC = mpif90 -DMPI
FC = ifort
#FFLAGS += -w -O3
FFLAGS +=  -w -O3 -ffree-line-length-none

And here is my Makefile for the example_eggbox directory:

FC = ifort
CC = icc
FFLAGS +=  -w -O3 -ffree-line-length-none
CFLAGS +=  -I. -O3
CXXFLAGS +=  -I. -O3
LINKLIB = ld -shared  
LIBDIR = ${HOME}/intel/Compiler/11.1/072/lib/ia32
NESTED_lib = ..
NESTED_inc = ..
LIBS=-L$(NESTED_lib) -lnest3 -L$(LIBDIR) -lmkl_lapack -lmkl_core \ 
-lmkl_intel_thread -lmkl_intel -lguide -lpthread
OBJFILES = cnest_c.o

all: eggbox 

	$(CC) $(CFLAGS) -c $*.cc

eggbox: $(OBJFILES)
	$(FC) $(FFLAGS) -nofor_main -o ../eggbox $(OBJFILES) $(MCLIBS) $(LIBS)

	rm -f *.o *.mod ../eggbox

Note that the -nofor_main flag is important.

If you run this then the eggbox example should work! However, compiling the MultiNest library with ifort may break the builds in the example_gauss_shell and example_obj_detect directories!

If anyone has any corrections, comments or better advice on how to install this then I would be happy to hear it and update these instructions.

Writing your own C function
I have written an example C code that uses the MultiNest C-wrapper function to perform an evidence calculation and posterior evaluation given a signal model with a sinusoid of given frequency, amplitude and initial phase, a Gaussian likelihood function and uniform priors over a hardcoded range. This can be found here. This uses my own (well Numerical Recipes’) gasdev function to produce Gaussian random numbers (the code and header file for this function can be found here and here). To make this requires the following Makefile:

ICC = icc
IFC = ifort
NESTFLAGS = -w -O3 -ffree-line-length-none -nofor_main
NESTLIBS = -L${MNLIB} -lnest3 -L${INTELLIBS} -lmkl_lapack -lmkl_core -lmkl_intel_thread -lmkl_intel -lguide -lpthread

nested_test: nested_test.c random.c
	$(ICC) -O3 -c nested_test.c random.c
	$(IFC) $(NESTFLAGS) -o nested_test nested_test.o random.o $(NESTLIBS)

This code, which requires three command line inputs: a frequency, and amplitude and an initial phase e.g.

>> nested_test 2.08 3.4 1.243

will put outputs to a directory called nested_test_out. A description of what the outputs contain can be found in the MultiNest README file. As it is code will output it’s progress to the terminal as it goes and give a final log(evidence) value with associated error – this can also be found as the Global Evidence value in nested_test_out/test-stats.dat. The samples from which to create a posterior can be found in nested_test_out/test-.txt – this first column is the sample probability (sample prior mass multiplied by its likelihood & normalized by the evidence), the second is -2*log(likelihood), and the following three are the frequency, amplitude and initial phase of the sample. I’ve written a Matlab function (here) will return either the 1D marginalised posterior, a 2D posterior, or a 3D posterior given this output file:

% function [pars, post] = get_nested_post(pfile, npars, wp, samps)
% This function is for use with the output of the MultiNest code to produce
% posteriors from the output samples. As an input it takes in the filename
% of the MutliNest output *-.txt file, which should have 2+npars columns:
% the first is the sample prior mass weigthed likelihood normalised by the 
% evidence, the second is -2* log likelihood, and the rest are the 
% parameter values for each of the samples. If the value of npars is not 
% the same as the number in file an error will occur.
% wp is a vector containing the parameter(s) for which you want to create
% the posterior e.g. wp=1 then you will get a posterior only on the 1st
% parameter, or wp=[1 3] will give you a 2D posterior on the 1st and 3rd
% parameters. For the moment this can only contain a maximum of three
% parameters.
% samps specifies the griding of the output posterior.