4 The base sampler class implementing various helpful functions.
8 from __future__
import (division, print_function, absolute_import,
24 An abstract sampler object that implements various helper functions
27 The number of dimensions in the parameter space.
30 A function that takes a vector in the parameter space as input and
31 returns the natural logarithm of the posterior probability for that
34 :param args: (optional)
35 A list of extra arguments for ``lnpostfn``. ``lnpostfn`` will be
36 called with the sequence ``lnpostfn(p, *args)``.
39 def __init__(self, dim, lnprobfn, args=[]):
46 self.
_random = np.random.mtrand.RandomState()
53 The state of the internal random number generator. In practice, it's
54 the result of calling ``get_state()`` on a
55 ``numpy.random.mtrand.RandomState`` object. You can try to set this
56 property but be warned that if you do this and it fails, it will do
60 return self._random.get_state()
65 Try to set the state of the random number generator but fail silently
66 if it doesn't work. Don't say I didn't warn you...
70 self._random.set_state(state)
77 The fraction of proposed steps that were accepted.
85 A pointer to the Markov chain.
93 Alias of ``chain`` provided for compatibility.
101 A list of the log-probability values associated with each step in
110 The autocorrelation time of each parameter in the chain (length:
111 ``dim``) as estimated by the ``acor`` module.
115 raise ImportError(
"acor")
116 return acor.acor(self._chain.T)[0]
119 """Return the log-probability at the given position."""
124 Clear ``chain``, ``lnprobability`` and the bookkeeping parameters.
131 """An alias for :func:`reset` kept for backwards compatibility."""
134 def sample(self, *args, **kwargs):
135 raise NotImplementedError(
"The sampling routine must be implemented "\
138 def run_mcmc(self, pos0, N, rstate0=None, lnprob0=None, **kwargs):
140 Iterate :func:`sample` for ``N`` iterations and return the result.
143 The initial position vector.
146 The number of steps to run.
148 :param lnprob0: (optional)
149 The log posterior probability at position ``p0``. If ``lnprob``
150 is not provided, the initial value is calculated.
152 :param rstate0: (optional)
153 The state of the random number generator. See the
154 :func:`random_state` property for details.
156 :param kwargs: (optional)
157 Other parameters that are directly passed to :func:`sample`.
160 for results
in self.
sample(pos0, lnprob0, rstate0, iterations=N,