Namespaces

Automatically generated documentation for harmonic APIs. All functionality is accessible through a pip installation of the harmonic package.

class harmonic.Chains(ndim)

Class to store samples from multiple MCMC chains.

__init__(ndim)

Construct empty Chains for parameter space of dimension ndim.

Constructor simply sets ndim. Chain samples are added by the add_chain* methods since we want to support setting up data for chains from different input data formats (e.g. data from a single chain or multiple chains at once).

Parameters

ndim (long) – Dimension of the parameter space.

add(other)

Add other Chain object to this object.

Parameters

other (Chains) – Other Chain object to be added to this object.

Raises

ValueError – Raised if the new chain has a different ndim.

add_chain(samples, ln_posterior)

Add a single chain to a Chains object.

Parameters
  • samples (double ndarray[nsamples, ndim]) – Samples of a single chain.

  • ln_posterior (double ndarray[n_new_samples]) – log_e posterior values.

Raises

ValueError – Raised when ndim of new chain does not match previous chains.

add_chains_2d(samples, ln_posterior, nchains_in)

Add a number of chains to a Chains object assuming all chains are of the same length.

Parameters
  • samples (double ndarray[nsamples_in * nchains_in, ndim]) – Samples of multiple chains.

  • ln_posterior (double ndarray[nsamples_in * nchains_in]) – log_e posterior values.

  • nchains_in (long) – Number of chains to be added.

Raises
  • ValueError – Raised when number of samples is not multiple of the number of chains.

  • ValueError – Raised when ndim of new chains does not match previous chains.

  • ValueError – Raised when posterior and samples first length are different.

add_chains_2d_list(samples, ln_posterior, nchains_in, chain_indexes)

Add a number of chains to the chain class. Uses a list of indexes to determine where each chain starts and stops.

Parameters
  • samples (double ndarray[nsamples_in * nchains_in, ndim]) – Samples of multiple chains.

  • ln_posterior (double ndarray[nsamples_in * nchains_in]) – log_e posterior values.

  • nchains_in (long) – Number of chains to be added.

  • chain_indexes (list) – List of the starting index of the chains.

Raises
  • ValueError – Raised when ndim of new chains does not match previous chains.

  • ValueError – Raised when posterior and samples first length are different.

  • ValueError – Raised when the length of the list is not nchains_in + 1.

add_chains_3d(samples, ln_posterior)

Add a number of chains to a Chain object from 3D array.

Parameters
  • samples (double ndarray[(nchains_in, nsamples_in, ndim]) – Samples from multiple chains.

  • ln_posterior (double ndarray[nchains_in, nsamples_in]) – log_e posterior values.

Raises
  • ValueError – Raised when ndim of new chains does not match previous chains.

  • ValueError – Raised when posterior and samples first and second length are different.

deepcopy()

Performs deep copy of the chain class (calls the module copy).

get_chain_indices(i)

Gets the start and end index of samples from a chain.

The end index specifies the index one passed the end of the chain, i.e. the chain samples can be accessed by self.samples[start:end,:].

Parameters

i (long) – Index of chain of which to determine start and end indices.

Returns

A tuple of the start and end index, i.e. (start, end).

Return type

(long, long)

Raises

ValueError – Raised when chain number invalid.

get_sub_chains(chains_wanted)

Creates a new chain instance with the chains indexed in chains_wanted. (Useful for cross-validation.)

Parameters

chains_wanted (list) – List of indexes of chains that the new chain instance will contain.

Returns

Chains object containing the chains wanted.

Return type

Chains

Raises

ValueError – If any of the chains_wanted indexes are out of bounds i.e. outside of range 0 to nchains - 1.

nsamples_per_chain()

Compute list containing number of samples in each chain.

Parameters

None.

Returns

1D list of length self.nchains containing the

number of samples in each chain.

Return type

nsamples_per_chain (list)

remove_burnin(nburn=100)

Remove burn-in samples from each chain.

Parameters

nburn (int) – Number of burn-in samples to remove from each chain.

Raises

ValueError – Raised when nburn not less then number of samples in each chain.

shallowcopy()

Performs shallow copy of the chain class (calls the module copy).

split_into_blocks(nblocks=100)

Split chains into larger number of blocks.

The intention of this method is to break chains into blocks that are (approximately) independent in order to get more independent chains for computing various statistics.

Each existing chain is split into blocks (i.e. new chains), proportionally to the size of the current chains. Final blocks within each chain end up containing slightly different numbers of samples (since we do not ever want to throw away samples!). One could improve this, if required, to distribute the additional samples across all of the blocks of the chain.

Parameters

nblocks (int) – Number of new (blocked) chains to split existing chains into.

Raises

ValueError – Returned if nblocks < the number chains