Package gmisclib :: Module mcmc
[frames] | no frames]

Module mcmc

source code

Bootstrap Markov-Chain Monte-Carlo algorithm. This can be used to generate samples from a probability distribution, or also as a simulated annealing algorithm for maximization. This can be imported and its functions and classes can be used.

The central interfaces are the BootStepper class, and within it, the BootStepper.step method is used iteratively to take a Markov step. To use this module, the user should derive a class from the problem_definition class and redefine (at least) the problem_definition.logp method.

It was originally inspired by amoeba_anneal (in Numerical Recipes, Press et al.). The essential feature is that it keeps a large archive of previous positions (possibly many times more than N of them). It samples two positions from the archive and subtracts them to generate candidate steps. This has the nice property that when sampling from a multivariate Gaussian distribution, the candidate steps match the distribution nicely.

It can be operated in two modes (or set to automatically switch). One is optimization mode where it heads for the maximum of the probability distribution. The other mode is sampling mode, where it asymptotically follows a Markov sampling procedure and has the proper statistical properties.

The algorithm has been described in:

An earlier version has been used in:

Classes
  problem_definition
This class implements the problem to be solved.
  problem_definition_F
This is a variant that is used bin the bin/mcmc.py script.
  position_base
This class is used internally in the MCMC sampling process to represent a position.
  position_repeatable
This is for the common case where logp is a well-behaved function of its arguments.
  position_nonrepeatable
This is for the (unfortunately common) case where logp is an indpendent random function of its arguments.
  acceptor_base
  T_acceptor
This class implements a normal Metropolis-Hastings acceptance of steps.
  rough_acceptor_base
  rough_T_acceptor
This class implements a normal Metropolis-Hastings acceptance of steps.
  stepper
This is your basic stepper class.
  adjuster
The adjuster class controls the step size.
  NoBoot
This is used internally to signify that some particular method of selecting a step has decided that it is not suitable.
  NotGoodPosition
This is raised by user code (e.g.
  hashcounter_c
Keep a count of how many times we've see various items.
  Archive
This maintains a list of all the recent accepted positions.
  ContPrmArchive
  BootStepper
The BootStepper class is the primary interface.
Functions
 
start_is_list_a(start)
Is the argument a sequence of numpy arrays?
source code
 
start_is_list_p(start)
Is the argument a sequence of position_base objects?
source code
 
make_list_of_positions(x, PositionClass, problem_def)
Turn almost anything into a list of position_base objects.
source code
 
bootstepper(logp, x, v, c=None, strategy='intermediate', fixer=None, repeatable=True)
This is (essentially) another interface to the class constructor.
source code
 
test_(stepper) source code
 
diag_variance(start)
Hand this a list of vectors and it will compute the variance of each component, then return a diagonal covariance matrix.
source code
 
test() source code
Variables
  Debug = 0
  MEMORYS_WORTH_OF_PARAMETERS = 100000000.0
How many bytes can we use to store the archives?
  __package__ = 'gmisclib'

Imports: math, types, random, bisect, threading, numpy, die, gpkmisc, g_implements, MVN


Function Details

make_list_of_positions(x, PositionClass, problem_def)

source code 

Turn almost anything into a list of position_base objects. You can hand it a sequence of numpy vectors or a single 1-dimensional numpy vector; a sequence of position_base objects or a single 1-dimensional position_base object.

Precondition: This depends on PositionClass being callable as PositionClass(vector_of_doubles, problem_definition).

bootstepper(logp, x, v, c=None, strategy='intermediate', fixer=None, repeatable=True)

source code 

This is (essentially) another interface to the class constructor. It's really there for backwards compatibility.