cosmoslik package

Submodules

Python module for dealing with MCMC chains.

Contains utilities to:

  • Load chains in a variety of formats
  • Compute statistics (mean, std-dev, conf intervals, …)
  • Plot 1- and 2-d distributions of one or multiple parameters.
  • Post-process chains
class cosmoslik.chains.Chain(*args, **kwargs)

Bases: dict

An MCMC chain. This is just a Python dictionary mapping parameter names to arrays of values, along with the special keys ‘lnl’ and ‘weight’

__init__(*args, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

__repr__()

Return repr(self).

__str__()

Print a summary of the chain.

__weakref__

list of weak references to the object (if defined)

acceptance()

Returns the acceptance ratio.

add_gauss_prior(params, mean, covstd, nthreads=1, pool=None)

Post-process a gaussian prior into the chain.

Parameters:
  • - a parameter name, or a list of parameters (params) –
  • - the mean (mean) –
  • - if params was a list, this should be a 2-d array holding the covariance (covstd) – if params was a single parameter, this should be the standard devation
Returns:

A new chain, without altering the original chain

best_fit()

Get the best fit sample.

burnin(n)

Remove the first n non-unique samples from the beginning of the chain.

confbound(param, level=0.95, bound=None)

Compute an upper or lower confidence bound.

Parameters:
  • param (string) – name of the parameter
  • level (float) – confidence level
  • bound ('upper', 'lower', or None) – whether to compute an upper or lower confidence bound. If set to None, will guess which based on the skewness of the distribution (will be lower bound for positively skewed distributions)
copy()

Deep copy the chain so post-processing, etc… works right

corr(params=None)

Returns the correlation matrix of the parameters (or some subset of them) in this chain.

cov(params=None)

Returns the covariance of the parameters (or some subset of them) in this chain.

iterrows()

Iterate over the samples in this chain.

join()

Does nothing since already one chain.

length(unique=True)

Returns the number of unique samples. Set unique=False to get total samples.

like1d(p, **kwargs)

Plots 1D likelihood contours for a parameter. See like1d()

like2d(p1, p2, **kwargs)

Plots 2D likelihood contours for a pair of parameters. See like2d()

likegrid(**kwargs)

Make a grid (aka “triangle plot”) of 1- and 2-d likelihood contours. See likegrid()

likegrid1d(**kwargs)

Make a grid of 1-d likelihood contours. See likegrid1d()

likepoints(*args, **kwargs)

Plots samples from the chain as colored points. See likepoints()

matrix(params=None)

Return this chain as an nsamp * nparams matrix.

mean(params=None)

Returns the mean of the parameters (or some subset of them) in this chain.

params(non_numeric=False, fixed=False)

Returns the parameters in this chain (i.e. the keys except ‘lnl’ and ‘weight’) :param numeric: whether to include non-numeric parameters (default: False) :param fixed: whether to include parameters which don’t vary (default: False)

plot(param=None, ncols=5, fig=None, size=4)

Plot the value of a parameter as a function of sample number.

postprocd(func, nthreads=1, pool=None)

Post-process some values into this chain.

Parameters:
  • func – a function which accepts all the keys in the chain and returns a dictionary of new keys to add. func must accept all keys in the chain, if there are ones you don’t need, capture them with **_ in its call signature, e.g. to add in a parameter ‘b’ which is ‘a’ squared, use postprocd(lambda a,**_: {‘b’:a**2})
  • nthreads – the number of threads to use
  • pool – any worker pool which has a pool.map function. default: multiprocessing.Pool(nthreads)
Returns:

A new chain with the new values post-processed in. Does not alter the original chain. If for some rows in the chain func did not return all the keys, these will be filled in with nan.

Note

This repeatedly calls func on rows in the chain, so its very inneficient if you already have a vectorized version of your post-processing function. postprocd is mostly useful for slow non-vectorized post-processing functions, allowing convenient use of the nthreads option to this function.

For the default implementation of pool, func must be picklable, meaning it must be a module-level function.

reweighted(func, nthreads=1, pool=None)

Reweight this chain.

Parameters:
  • func – a function which accepts all keys in the chain, and returns a new weight for the step. func must accept all keys, if there are ones you don’t need, capture them with **_ in its call signature, e.g. to add unit gaussian prior on parameter ‘a’ use reweighted(lambda a,**_: exp(-a**2/2)
  • nthreads – the number of threads to use
  • pool – any worker pool which has a pool.map function. default: multiprocessing.Pool(nthreads)
Returns:

A new chain, without altering the original chain

Note

This repeatedly calls func on rows in the chain, so its very inneficient compared to a vectorized version of your post-processing function. postprocd is mostly useful for slow post-processing functions, allowing you to conveniently use the nthreads option to this function.

For the default implementation of pool, func must be picklable, meaning it must be a module-level function.

sample(s, keys=None)

Return a sample or a range of samples depending on if s is an integer or a slice object.

savechain(file, params=None)

Write the chain to a file where the first line is specifies the parameter names.

savecov(file, params=None)

Write the covariance to a file where the first line is specifies the parameter names.

skew(params=None)

Return skewness of one or more parameters.

std(params=None)

Returns the std of the parameters (or some subset of them) in this chain.

thin(delta)

Take every delta non-unique samples.

thinto(num)

Thin so we end up with num total samples

class cosmoslik.chains.Chains

Bases: list

A list of chains, e.g. from a run of several parallel chains

__repr__()

Return repr(self).

__str__()

Print a summary of the chain.

__weakref__

list of weak references to the object (if defined)

burnin(n)

Remove the first n samples from each chain.

join()

Combine the chains into one.

plot(param=None, fig=None, **kwargs)

Plot the value of a parameter as a function of sample number for each chain.

cosmoslik.chains.likegrid(chains, params=None, lims=None, ticks=None, nticks=4, nsig=4, spacing=0.05, xtick_rotation=30, colors=None, filled=True, nbins1d=30, smooth1d=False, kde1d=True, nbins2d=20, labels=None, fig=None, size=2, legend_loc=None, param_name_mapping=None, param_label_size=None)

Make a grid (aka “triangle plot”) of 1- and 2-d likelihood contours.

Parameters:
  • chains – one or a list of Chain objects
  • optional (nbins2d,) – the chain used to get default parameters names, axes limits, and ticks either an index into chains or a Chain object (default: chains[0])
  • optional – list of parameter names which to show (default: all parameters from default_chain)
  • optional – a dictionary mapping parameter names to (min,max) axes limits (default: +/- 4 sigma from default_chain)
  • optional – a dictionary mapping parameter names to list of [ticks] (default: automatically picks nticks)
  • optional – roughly how many ticks per axes (default: 5)
  • optional – numbers of degrees to rotate the xticks by (default: 30)
  • optional – space in between plots as a fraction of figure width (default: 0.05)
  • optional – figure of figure number in which to plot (default: figure(0))
  • optional – size in inches of one plot (default: 2)
  • optional – colors to cycle through for plotting
  • optional – whether to fill in the contours (default: True)
  • optional – list of names for a legend
  • optional – (x,y) location of the legend (coordinates scaled to [0,1])
  • optional – number (or len(chains) length list) of bins for 1d plots (default: 30)
  • optional – number (or len(chains) length list) of bins for 2d plots (default: 20)
cosmoslik.chains.likegrid1d(chains, params='all', lims=None, ticks=None, nticks=4, nsig=3, colors=None, nbins1d=30, smooth1d=False, kde1d=True, labels=None, fig=None, size=2, aspect=1, legend_loc=None, linewidth=1, param_name_mapping=None, param_label_size=None, tick_label_size=None, titley=1, ncol=4, axes=None)

Make a grid of 1-d likelihood contours.

chains :
one or a list of Chain objects
default_chain, optional :
the chain used to get default parameters names, axes limits, and ticks either an index into chains or a Chain object (default: chains[0])
params, optional :
list of parameter names which to show can also be ‘all’ or ‘common’ which does the union/intersection of the params in all the chains
lims, optional :
a dictionary mapping parameter names to (min,max) axes limits (default: +/- 4 sigma from default_chain)
ticks, optional :
a dictionary giving a list of ticks for each parameter
nticks, optional :
roughly how many x ticks to show. can be dictionary to specify each parameter separately. (default: 4)
fig, optional :
figure of figure number in which to plot (default: new figure)
ncol, optional :
the number of colunms (default: 4)
axes, optional :
an array of axes into which to plot. if this is provided, fig and ncol are ignored. must have len(axes) >= len(params).
size, optional :
size in inches of one plot (default: 2)
aspect, optional :
aspect ratio (default: 1)
colors, optional :
colors to cycle through for plotting
filled, optional :
whether to fill in the contours (default: True)
labels, optional :
list of names for a legend
legend_loc, optional :
(x,y) location of the legend (coordinates scaled to [0,1])
nbins1d, optional :
number of bins for 1d plots (default: 30)
nbins2d, optional :
number of bins for 2d plots (default: 20)
cosmoslik.chains.likepoints(chain, p1, p2, pcolor, npoints=1000, cmap=None, nsig=3, clim=None, marker='.', markersize=10, ax=None, zorder=-1, cbar=True, cax=None, **kwargs)

Plot p1 vs. p2 as points colored by the value of pcolor.

Parameters:
  • p1,p2,pcolor – parameter names
  • npoints – first thins the chain so this number of points are plotted
  • cmap – a colormap (default: jet)
  • nsig – map the range of the color map to +/- nsig
  • ax – axes to use for plotting (default: current axes)
  • cbar – whether to draw a colorbar
  • cax – axes to use for colorbar (default: steal from ax)
  • markersize, zorder, **kwargs (marker,) –

    passed to the plot() command

cosmoslik.chains.load_chain(filename, repack=False)

Load a chain produced by a compatible CosmoSlik sampler like metropolis_hastings or emcee.

repack :
If the chain file is not currently open (i.e. the chain is not currently running), and if the chain is stored in chunks as output from an MPI run, then overwrite the file with a more efficiently stored version which will be faster to load the next time.
cosmoslik.chains.load_cosmomc_chain(path, paramnames=None)

Load a chain from a file or files in a variety of different formats.

If path is a CosmoSlik ini, the read the output_file key from the ini and load that chain.

If path is a file, return a Chain object. The names of the parameters are expected either as a whitespace-separated comment on the first line of the file (CosmoSlik style) or in a separate file called <path>.paramnames (CosmoMC style).

If path is a directory, assumes it contains one file for each parameter (WMAP style) and gets the parameter name from the file name.

If path is a prefix such that there exists <path>_1, <path>_2, etc… then returns a Chains object which is a list of chains.

cosmoslik.cosmoslik.load_script(script)

Read a CosmoSlik script from a file and return the SlikPlugin object.

class cosmoslik.cosmoslik.SlikDict(*args, **kwargs)

Bases: dict

__contains__(k)

True if D has a key k, else False.

__getitem__(k)

x.__getitem__(y) <==> x[y]

__init__(*args, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

__setitem__(k, v)

Set self[key] to value.

__weakref__

list of weak references to the object (if defined)

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
class cosmoslik.cosmoslik.SlikPlugin(*args, **kwargs)

Bases: cosmoslik.cosmoslik.SlikDict

__call__(*args, **kwargs)

Calculation code here.

__init__(*args, **kwargs)

Initialization code here.

class cosmoslik.cosmoslik.SlikSampler(*args, **kwargs)

Bases: cosmoslik.cosmoslik.SlikDict

class cosmoslik.cosmoslik.param(**kwargs)

Bases: object

Marks a parameter to be sampled by the MCMC.

__init__(**kwargs)

Initialize self. See help(type(self)) for accurate signature.

__weakref__

list of weak references to the object (if defined)

cosmoslik.cosmoslik.param_shortcut(*args)

Create a param constructor which splices in keyword arguments for you.

Example:

param = param_shortcut('start','scale')
param(1,2)
> {'start':1, 'scale':2}
cosmoslik.cosmoslik.get_all_plugins(ignore_import_errors=True)

Search recursively through the namespace package cosmoslik_plugins for any `SlikPlugin`s.

Returns:keys are the shortest qualified name for the object and values are a type object.
Return type:dict
cosmoslik.cosmoslik.lsum(*args)

If your likelihood function is the sum of a bunch of others, you can do:

def __call__(self):
    return lsum(lambda: myfunc1(), lambda: myfunc2())

This returns the sum myfunc1()+myfunc2(), but never evaluates myfunc2() if myfunc1() returns inf.

See also lsumk() to automatically store the results of myfunc1/2.

cosmoslik.cosmoslik.lsumk(lnls, args)

If your likelihood function is the sum of a bunch of others, you can do:

def __call__(self):
    self['lnls']={}
    return lsumk(self['lnls'], [('key1',lambda: myfunc1()),
                                ('key2',lambda: myfunc2())])

This returns the sum myfunc1()+myfunc2(), but never evaluates myfunc2() if myfunc1() returns inf. It also stores the result myfunc1/2 to lnls['key1/2'] (stores nan if a function wasn’t called)

cosmoslik.cosmoslik.run_chain(main, nchains=1, pool=None, args=None, kwargs=None)

Run a CosmoSlik chain, or if nchains!=1 run a set of chains in parallel.

Non-trivial parallelization, e.g. using MPI or with communication amongst chains, is handled by each cosmoslik.Sampler module. See individual documentation.

Parameters:
  • main – the class object for your SlikPlugin (i.e. myplugin, not myplugin())
  • pool – any worker pool which has a pool.map function. Defaults to multiprocessing.Pool(nchains)
  • nchains (int) – the number of chains to run in parallel using pool.map
  • *args – args to pass to main
  • **kwargs – kwargs to pass to main
Returns:

A chain.Chain instance if nchains=1, otherwise a chain.Chains instance

cosmoslik.cosmoslik.SlikMain(cls)

Class decorator which marks a plugin as being the main one when running a script from the command line.

Example:

# somefile.py

@SlikMain  #run this plugin when I run `$ cosmoslik somefile.py`
class plugin1(SlikPlugin):
    ...

class plugin2(SlikPlugin):
    ...
cosmoslik.cosmoslik.arguments(exclude=None, exclude_self=True, include_kwargs=True, ifset=False)

Return a dictionary of the current function’s arguments (excluding self, and optionally other user specified ones or default ones)

Parameters:
  • exclude (list) – argument names to exclude
  • exclude_self (bool) – exclude the “self” argument
  • include_kwargs (bool) – include the args captured by kwargs
  • ifset (bool) – exclude keyword arguments whose value is their default

Example:

def foo(x, y=2, **kwargs):
    return arguments()

f(1)        # returns {'x':1, 'y':2}
f(1,3)      # returns {'x':1, 'y':3}
f(1,z=5)    # returns {'x':1, 'y':3, 'z':5}

Adapted from: http://kbyanc.blogspot.fr/2007/07/python-aggregating-function-arguments.html

cosmoslik.cosmoslik.get_caller(depth=1)

Get the calling function. Works in many typical (but not all) cases.

Thanks to: http://stackoverflow.com/a/39079070/1078529

Example:

def foo():
    return get_caller()

foo() #returns the 'foo' function object

or

def foo():
return bar()
def bar():
return get_caller(depth=2)

foo() #returns the ‘foo’ function object

class cosmoslik.cosmoslik.sample(x, lnl, weight=1, extra=None)

Bases: object

A SlikSampler’s sample method should yield objects of this type.

__init__(x, lnl, weight=1, extra=None)

Initialize self. See help(type(self)) for accurate signature.

__weakref__

list of weak references to the object (if defined)

Module contents