hopsy.sample#

class hopsy.sample(markov_chains, rngs, n_samples, thinning=1, n_procs=1)#

Draw n_samples from every passed chain in markov_chains using the respective random number generator from rngs. Thus, len(rngs) has to match len(markov_chains).

Parameters:
  • markov_chains (list[hopsy.MarkovChain] or hopsy.MarkovChain) – (List of) Markov chain(s) to simulate to generate samples.

  • rngs (list[hopsy.RandomNumberGenerator] or hopsy.RandomNumberGenerator) – (List of) random number generator(s) to simulate the Markov chains. If a single hopsy.MarkovChain was passed to sample, then rng also must be a single hopsy.RandomNumberGenerator.

  • n_samples (int) – Number of samples to draw from every chain.

  • thinning (int) – Number of samples to discard inbetween two saved states. This will increase the number of samples actually produced by the chain to thinning * n_samples.

  • n_threads (int) – (deprecated) Number of parallel processes to use. Parallelization is achieved using multiprocessing. The worker pool size will be min(n_procs, len(markov_chains))

  • n_procs (int) – Number of parallel processes to use. Parallelization is achieved using multiprocessing. The worker pool size will be min(n_procs, len(markov_chains))

  • record_meta (list[str] or bool) – Strings defining hopsy.MarkovChain attributes or acceptance_rate, which will then be recorded and returned. All attributes of hopsy.MarkovChain can be used here, e.g. record_meta=['state_negative_log_likelihood', 'proposal.proposal'].

  • in_memory (bool) – Flag for enabling or disabling in-memory saving of states and metadata.

  • callback (derived from hopsy.Callback) – Observer callback to which states and metadata are passed during the run. The callback is e.g. used to write the obtained information online to permanent storage. This enables online analysis of the MCMC run.

Returns:

First value of the tuple holds meta information about the states. Without using record_meta, this is a list containing the acceptance rates of each chain. If record_meta is used, then this is a dict containing the values of the hopsy.MarkovChain attributes defined in record_meta. If the attribute was not found (e.g. because of a typo), it will have value None.

Second value of the tuple holds produced states. Will have shape (n_chains, n_draws, dim). For single chains, it will thus be (1, n_draws, dim).

If in_memory=False, None will be returned.

Return type:

optional[tuple[list or dict, numpy.ndarray]]