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

Class problem_definition

source code


This class implements the problem to be solved. It's main function in life is to compute the probability that a given parameter vector is acceptable. Mcmc.py then uses that to run a Markov-Chain Monte-Carlo sampling. You probably want to derive a class from this and override most of the functions defined here, or implement your own class with the same functions. (Of course, in either case, it can contain extra functions also.)

For instance, it can be a good idea to define a function "model" that computes the model that you are fitting to data (if that is your plan). Then logp() can be something like return -numpy.sum((self.model()-self.data)**2). Also, it can be good to define a "guess" function that computes a reasonable initial guess to the parameters, somehow.

Instance Methods
 
__init__(self)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
float
logp(self, x)
Compute the log of the probability density at x.
source code
numpy.ndarray
fixer(self, x)
This is called on each candidate position vector.
source code
 
log(self, p, i)
Some code calls this function every iteration to log the current state of the MCMC process.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Decorators:
  • @g_implements.make_optional
  • @g_implements.make_varargs
Overrides: object.__init__
(inherited documentation)

logp(self, x)

source code 

Compute the log of the probability density at x.

Parameters:
  • x (numpy.ndarray) - a parameter vector
Returns: float
The log of the probability that the model assigns to parameters x.
Raises:
  • NotGoodPosition - This exception is used to indicate that the position x is not valid. This is equivalent to returning an extremely negative logp.

fixer(self, x)

source code 

This is called on each candidate position vector. Generally, it is used to restrict the possible solution space by folding position vectors that escape outside the solution space back into the solution space. It can also allow for symmetries in equations.

Formally, it defines a convex region. All vectors outside the region are mapped into the region, and the mapping must be continuous at the boundary. (More precisely, logp(fixer(x)) must be continuous everywhere that logp(x) is continuous, including the boundary.) For instance, mapping x[0] into abs(x[0]) defines a convex region (the positive half-space), and the mapping is continuous near x[0]=0.

Additionally, it may re-normalize parameters at will subject to the restriction that logp(fixer(x))==logp(x). For instance, it can implement a constraint that sum(x)==0 by mapping x into x - average(x), so long as the value of logp() is unaffected by that substitution. Other folds can sometimes lead to problems.

Parameters:
  • x (numpy.ndarray) - a parameter vector
Returns: numpy.ndarray
a (possibly modified) parameter vector.
Raises:
  • NotGoodPosition - This exception is used to indicate that the position x is not valid. Fixer has the option of either mapping invalid parameter vectors into valid ones or raising this exception.

Attention: Within a convex region (presumably one that contains the optimal x), fixer must not change the value of logp(): logp(fixer(x)) == logp(x).

log(self, p, i)

source code 

Some code calls this function every iteration to log the current state of the MCMC process.

Parameters:
  • p - the current parameter vector, and
  • i - an integer iteration counter.
Returns:
nothing.
Decorators:
  • @g_implements.make_optional