MetropolisHastings

class MetropolisHastings(*args)

Base class for Metropolis-Hastings algorithms.

Refer to Bayesian calibration, The Metropolis-Hastings Algorithm.

Notes

Metropolis-Hastings algorithms are Markov Chain Monte-Carlo algorithms: they can sample from a target distribution defined as having a PDF proportional to some known function.

If setLikelihood() is used, then a likelihood factor is applied to the PDF of the target distribution: in Bayesian terms, the initially targeted distribution is the prior, and multiplying its PDF by the likelihood yields the PDF of the posterior up to a multiplicative constant.

Methods

computeLogLikelihood(state)

Compute the logarithm of the likelihood w.r.t.

computeLogPosterior(currentState)

Compute the logarithm of the unnormalized posterior density.

getAcceptanceRate()

Get acceptance rate.

getBurnIn()

Get the length of the burn-in period.

getClassName()

Accessor to the object's name.

getConditional()

Get the conditional distribution.

getCovariates()

Get the parameters.

getDimension()

Accessor to the dimension of the RandomVector.

getHistory()

Get the history storage.

getId()

Accessor to the object's id.

getImplementation()

Accessor to the underlying implementation.

getInitialState()

Get the initial state.

getLinkFunction()

Get the model.

getName()

Accessor to the object's name.

getObservations()

Get the observations.

getRealization()

Compute one realization of the RandomVector.

getTargetDistribution()

Get the target distribution.

getTargetLogPDF()

Get the target log-pdf.

getTargetLogPDFSupport()

Get the target log-pdf support.

getThinning()

Get the thinning parameter.

setBurnIn(burnIn)

Set the length of the burn-in period.

setHistory(strategy)

Set the history storage.

setLikelihood(*args)

Set the likelihood.

setName(name)

Accessor to the object's name.

setThinning(thinning)

Set the thinning parameter.

getMarginalIndices

__init__(*args)
computeLogLikelihood(state)

Compute the logarithm of the likelihood w.r.t. observations.

Parameters:
currentStatesequence of float

Current state.

Returns:
logLikelihoodfloat

Logarithm of the likelihood w.r.t. observations (\vect{y}^1, \dots, \vect{y}^n).

computeLogPosterior(currentState)

Compute the logarithm of the unnormalized posterior density.

Parameters:
currentStatesequence of float

Current state.

Returns:
logPosteriorfloat

Target log-PDF plus log-likelihood if the log-likelihood is defined

getAcceptanceRate()

Get acceptance rate.

Returns:
acceptanceRatefloat

Global acceptance rates over all the MCMC iterations performed.

getBurnIn()

Get the length of the burn-in period.

Returns:
burninint

Length of the burn-in period, that is the number of first iterates of the MCMC chain which will be thrown away when generating the sample.

getClassName()

Accessor to the object’s name.

Returns:
class_namestr

The object class name (object.__class__.__name__).

getConditional()

Get the conditional distribution.

Returns:
conditionalDistribution

The conditional argument provided to setLikelihood()

getCovariates()

Get the parameters.

Returns:
parametersPoint

Fixed parameters of the model g required to define the likelihood.

getDimension()

Accessor to the dimension of the RandomVector.

Returns:
dimensionpositive int

Dimension of the RandomVector.

getHistory()

Get the history storage.

Returns:
historyHistoryStrategy

Used to record the chain.

getId()

Accessor to the object’s id.

Returns:
idint

Internal unique identifier.

getImplementation()

Accessor to the underlying implementation.

Returns:
implImplementation

A copy of the underlying implementation object.

getInitialState()

Get the initial state.

Returns:
initialStatesequence of float

Initial state of the chain

getLinkFunction()

Get the model.

Returns:
linkFunctionFunction

The linkFunction argument provided to setLikelihood()

getName()

Accessor to the object’s name.

Returns:
namestr

The name of the object.

getObservations()

Get the observations.

Returns:
observationsSample

The observations argument provided to setLikelihood()

getRealization()

Compute one realization of the RandomVector.

Returns:
aRealizationPoint

Sequence of values randomly determined from the RandomVector definition. In the case of an event: one realization of the event (considered as a Bernoulli variable) which is a boolean value (1 for the realization of the event and 0 else).

See also

getSample

Examples

>>> import openturns as ot
>>> distribution = ot.Normal([0.0, 0.0], [1.0, 1.0], ot.CorrelationMatrix(2))
>>> randomVector = ot.RandomVector(distribution)
>>> ot.RandomGenerator.SetSeed(0)
>>> print(randomVector.getRealization())
[0.608202,-1.26617]
>>> print(randomVector.getRealization())
[-0.438266,1.20548]
getTargetDistribution()

Get the target distribution.

Returns:
targetDistributionDistribution

The targetDistribution argument provided to the constructor

getTargetLogPDF()

Get the target log-pdf.

Returns:
targetLogPDFFunction

The targetLogPDF argument provided to the constructor

getTargetLogPDFSupport()

Get the target log-pdf support.

Returns:
supportInterval

The support argument provided to the constructor

getThinning()

Get the thinning parameter.

Returns:
thinningint

Thinning parameter: storing only every k^{th} point after the burn-in period.

Notes

When generating a sample of size q, the number of MCMC iterations performed is l+1+(q-1)k where l is the burn-in period length and k the thinning parameter.

setBurnIn(burnIn)

Set the length of the burn-in period.

Parameters:
burninint

Length of the burn-in period, that is the number of first iterates of the MCMC chain which will be thrown away when generating the sample.

setHistory(strategy)

Set the history storage.

Parameters:
historyHistoryStrategy

Used to record the chain.

setLikelihood(*args)

Set the likelihood.

Parameters:
conditionalDistribution

Required distribution to define the likelihood of the underlying Bayesian statistical model.

observations2-d sequence of float

Observations \vect y^i required to define the likelihood.

linkFunctionFunction, optional

Function g that maps the chain into the conditional distribution parameters. If provided, its input dimension must match the chain dimension and its output dimension must match the conditional distribution parameter dimension. Else it is set to the identity.

covariates2-d sequence of float, optional

Parameters \vect c^i of the linkFunction for each observation \vect y^i. If provided, their dimension must match the parameter dimension of linkFunction.

Notes

Once this method is called, the class no longer samples from the distribution targetDistribution or from the distribution defined by targetLogPDF and support, but considers that distribution as being the prior. Let \pi(\vect \theta) be the PDF of the prior at the point \vect \theta. The class now samples from the posterior, whose PDF is proportional to L(\vect\theta) \, \pi(\vect\theta), the likelihood L(\vect \theta) being defined from the arguments of this method.

The optional parameters linkFunction and covariates allow several options to define the likelihood L(\vect \theta). Letting f be the PDF of the distribution conditional:

  • Without linkFunction and covariates the likelihood term reads:

    L(\vect \theta) = \prod_{i=1}^n f(\vect{y}^i|\vect{\theta})

  • If only the linkFunction is provided:

    L(\vect \theta) = \prod_{i=1}^n f(\vect{y}^i|g(\vect{\theta}))

  • If both the linkFunction and covariates are provided:

    L(\vect \theta) = \prod_{i=1}^n f(\vect{y}^i|g_{\vect c^i}(\vect{\theta}))

setName(name)

Accessor to the object’s name.

Parameters:
namestr

The name of the object.

setThinning(thinning)

Set the thinning parameter.

Parameters:
thinningint, k \geq 0

Thinning parameter: storing only every k^{th} point after the burn-in period.

Notes

When generating a sample of size q, the number of MCMC iterations performed is l+1+(q-1)k where l is the burn-in period length and k the thinning parameter.

Examples using the class

Linear Regression with interval-censored observations

Linear Regression with interval-censored observations