# RandomWalkMetropolisHastings¶

class RandomWalkMetropolisHastings(*args)

Random Walk Metropolis-Hastings method.

Available constructor:

RandomWalkMetropolisHastings(targetDistribution, initialState, proposal, marginalIndices)

RandomWalkMetropolisHastings(targetLogPDF, support, initialState, proposal, marginalIndices)

Parameters:
targetDistributionDistribution

Target distribution sampled

targetLogPDFFunction

Target log-density up to an additive constant

supportDomain

Support of the target when defined with targetLogPDF

initialStatesequence of float

Initial state of the chain

proposalDistribution

Distribution of the steps of the random walk

marginalIndicessequence of int, optional

Indices of the components to be updated. If not specified, all components are updated. The number of updated components must be equal to the dimension of proposal.

Notes

The random walk Metropolis-Hastings algorithm is a Markov Chain Monte-Carlo algorithm. It draws candidates for the next state of the chain as follows: denoting the current state by , the candidate for can be expressed as where the distribution of is the provided proposal distribution.

Examples

>>> import openturns as ot
>>> import math as m
>>> ot.RandomGenerator.SetSeed(0)


Sample from a target distribution defined through its log-PDF (defined up to some additive constant) and its support:

>>> log_density = ot.SymbolicFunction('x', 'log(2 + sin(x)^2) - (2 + cos(3*x)^3 + sin(2*x)^3) * x')
>>> support = ot.Interval([0.0], [2.0 * m.pi])
>>> proposal = ot.Normal(0.0, 2.0)
>>> initialState = [3.0]
>>> sampler = ot.RandomWalkMetropolisHastings(log_density, support, initialState, proposal)
>>> x = sampler.getSample(10)


The target distribution can also be a Distribution. Since all Distribution objects have a getSample() method, this is only useful in Bayesian settings where we also define a likelihood. The likelihood function penalizes the initially provided target distribution (now viewed as the prior) in order to get the posterior distribution. We sample from the posterior.

>>> prior = ot.JointDistribution([ot.Uniform(-100.0, 100.0)] * 2)
>>> proposal = ot.Normal([0.0] * 2, [0.5, 0.05], ot.IdentityMatrix(2))
>>> initialState = [0.0] * 2
>>> sampler = ot.RandomWalkMetropolisHastings(prior, initialState, proposal)
>>> conditional = ot.Bernoulli()
>>> data = ot.Sample([[53, 1], [57, 1], [58, 1], [63, 1], [66, 0], [67, 0],
... [67, 0], [67, 0], [68, 0], [69, 0], [70, 0], [70, 0], [70, 1], [70, 1],
... [72, 0], [73, 0], [75, 0], [75, 1], [76, 0], [76, 0], [78, 0], [79, 0], [81, 0]])
>>> observations = data[:, 1]
>>> covariates = data[:, 0]
>>> fun = ot.SymbolicFunction(['alpha', 'beta', 'x'], ['exp(alpha + beta * x) / (1 + exp(alpha + beta * x))'])
>>> linkFunction = ot.ParametricFunction(fun, [2], [0.0])
>>> sampler.setLikelihood(conditional, observations, linkFunction, covariates)
>>> alpha_beta = sampler.getSample(10)


Methods

 If the random vector can be viewed as the composition of several ThresholdEvent objects, this method builds and returns the composition. Compute the logarithm of the likelihood w.r.t. Compute the logarithm of the unnormalized posterior density. Get acceptance rate. Get the adaptation expansion factor. Get the adaptation factor. Get the adaptation step. Get the expected range for the acceptance rate. Get the adaptation shrink factor. Accessor to the antecedent RandomVector in case of a composite RandomVector. Get the length of the burn-in period. Accessor to the object's name. Get the conditional distribution. Accessor to the covariance of the RandomVector. Get the parameters. Accessor to the description of the RandomVector. Accessor to the dimension of the RandomVector. Accessor to the distribution of the RandomVector. Accessor to the domain of the Event. getFrozenRealization(fixedPoint) Compute realizations of the RandomVector. getFrozenSample(fixedSample) Compute realizations of the RandomVector. Accessor to the Function in case of a composite RandomVector. Get the history storage. Get the initial state. Get the model. getMarginal(*args) Get the random vector corresponding to the marginal component(s). Get the indices of the sampled components. Accessor to the mean of the RandomVector. Accessor to the object's name. Get the observations. Accessor to the comparaison operator of the Event. Accessor to the parameter of the distribution. Accessor to the parameter description of the distribution. Get the stochastic process. Get the proposal distribution. Compute one realization of the RandomVector. getSample(size) Compute realizations of the RandomVector. Get the target distribution. Get the target log-pdf. Get the target log-pdf support. Accessor to the threshold of the Event. Test if the object is named. Accessor to know if the RandomVector is a composite one. Whether the random vector is an event. setAdaptationExpansionFactor(expansionFactor) Set the adaptation expansion factor. setAdaptationPeriod(period) Set the adaptation period. setAdaptationRange(range) Set the expected range for the acceptance rate. setAdaptationShrinkFactor(shrinkFactor) Set the adaptation shrink factor. setBurnIn(burnIn) Set the length of the burn-in period. setDescription(description) Accessor to the description of the RandomVector. setHistory(strategy) Set the history storage. setLikelihood(*args) Set the likelihood. setName(name) Accessor to the object's name. setParameter(parameters) Accessor to the parameter of the distribution. setProposal(proposal) Set the proposal distribution.
__init__(*args)
asComposedEvent()

If the random vector can be viewed as the composition of several ThresholdEvent objects, this method builds and returns the composition. Otherwise throws.

Returns:
composedRandomVector

Composed event.

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 .

computeLogPosterior(state)

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.

Get the adaptation expansion factor.

Returns:
expansionFactorfloat

Expansion factor of the adaptation factor.

Get the adaptation factor.

Returns:
factorfloat

Current adaptation factor, for inspection.

Get the adaptation step.

Returns:
periodpositive int

During burn-in, adaptation is performed once every period iterations.

Get the expected range for the acceptance rate. During burn-in, at the end of every adaptation period, if the acceptance rate does not belong to this range, the adaptation factor is multiplied either by the expansion factor (if the acceptance rate is larger than the upperBound) or by the shrink factor (if the acceptance rate is smaller than the lowerBound).

Returns:
rangeInterval of dimension 1

Range [lowerBound, upperBound] of the expected acceptance rate.

Get the adaptation shrink factor.

Returns:
shrinkFactorfloat

Shrink factor of the adaptation factor.

getAntecedent()

Accessor to the antecedent RandomVector in case of a composite RandomVector.

Returns:
antecedentRandomVector

Antecedent RandomVector in case of a CompositeRandomVector such as: .

getBurnIn()

Get the length of the burn-in period.

Returns:
burninint

Length of the burn-in period: this is the number of iterations after which adaptation stops.

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()

getCovariance()

Accessor to the covariance of the RandomVector.

Returns:
covarianceCovarianceMatrix

Covariance of the considered UsualRandomVector.

Examples

>>> import openturns as ot
>>> distribution = ot.Normal([0.0, 0.5], [1.0, 1.5], ot.CorrelationMatrix(2))
>>> randomVector = ot.RandomVector(distribution)
>>> ot.RandomGenerator.SetSeed(0)
>>> print(randomVector.getCovariance())
[[ 1    0    ]
[ 0    2.25 ]]

getCovariates()

Get the parameters.

Returns:
parametersPoint

Fixed parameters of the model required to define the likelihood.

getDescription()

Accessor to the description of the RandomVector.

Returns:
descriptionDescription

Describes the components of the RandomVector.

getDimension()

Accessor to the dimension of the RandomVector.

Returns:
dimensionpositive int

Dimension of the RandomVector.

getDistribution()

Accessor to the distribution of the RandomVector.

Returns:
distributionDistribution

Distribution of the considered UsualRandomVector.

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.getDistribution())
Normal(mu = [0,0], sigma = [1,1], R = [[ 1 0 ]
[ 0 1 ]])

getDomain()

Accessor to the domain of the Event.

Returns:
domainDomain

Describes the domain of an event.

getFrozenRealization(fixedPoint)

Compute realizations of the RandomVector.

In the case of a CompositeRandomVector or an event of some kind, this method returns the value taken by the random vector if the root cause takes the value given as argument.

Parameters:
fixedPointPoint

Point chosen as the root cause of the random vector.

Returns:
realizationPoint

The realization corresponding to the chosen root cause.

Examples

>>> import openturns as ot
>>> distribution = ot.Normal()
>>> randomVector = ot.RandomVector(distribution)
>>> f = ot.SymbolicFunction('x', 'x')
>>> compositeRandomVector = ot.CompositeRandomVector(f, randomVector)
>>> event = ot.ThresholdEvent(compositeRandomVector, ot.Less(), 0.0)
>>> print(event.getFrozenRealization([0.2]))
[0]
>>> print(event.getFrozenRealization([-0.1]))
[1]

getFrozenSample(fixedSample)

Compute realizations of the RandomVector.

In the case of a CompositeRandomVector or an event of some kind, this method returns the different values taken by the random vector when the root cause takes the values given as argument.

Parameters:
fixedSampleSample

Sample of root causes of the random vector.

Returns:
sampleSample

Sample of the realizations corresponding to the chosen root causes.

Examples

>>> import openturns as ot
>>> distribution = ot.Normal()
>>> randomVector = ot.RandomVector(distribution)
>>> f = ot.SymbolicFunction('x', 'x')
>>> compositeRandomVector = ot.CompositeRandomVector(f, randomVector)
>>> event = ot.ThresholdEvent(compositeRandomVector, ot.Less(), 0.0)
>>> print(event.getFrozenSample([[0.2], [-0.1]]))
[ y0 ]
0 : [ 0  ]
1 : [ 1  ]

getFunction()

Accessor to the Function in case of a composite RandomVector.

Returns:
functionFunction

Function used to define a CompositeRandomVector as the image through this function of the antecedent : .

getHistory()

Get the history storage.

Returns:
historyHistoryStrategy

Used to record the chain.

getInitialState()

Get the initial state.

Returns:
initialStatesequence of float

Initial state of the chain

Get the model.

Returns:
linkFunctionFunction

The linkFunction argument provided to setLikelihood()

getMarginal(*args)

Get the random vector corresponding to the marginal component(s).

Parameters:
iint or list of ints,

Indicates the component(s) concerned. is the dimension of the RandomVector.

Returns:
vectorRandomVector

RandomVector restricted to the concerned components.

Notes

Let’s note a random vector and a set of indices. If is a UsualRandomVector, the subvector is defined by . If is a CompositeRandomVector, defined by with , some scalar functions, the subvector is .

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.getMarginal(1).getRealization())
[0.608202]
>>> print(randomVector.getMarginal(1).getDistribution())
Normal(mu = 0, sigma = 1)

getMarginalIndices()

Get the indices of the sampled components.

Returns:
marginalIndicesIndices

The marginalIndices argument provided to the constructor

getMean()

Accessor to the mean of the RandomVector.

Returns:
meanPoint

Mean of the considered UsualRandomVector.

Examples

>>> import openturns as ot
>>> distribution = ot.Normal([0.0, 0.5], [1.0, 1.5], ot.CorrelationMatrix(2))
>>> randomVector = ot.RandomVector(distribution)
>>> ot.RandomGenerator.SetSeed(0)
>>> print(randomVector.getMean())
[0,0.5]

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()

getOperator()

Accessor to the comparaison operator of the Event.

Returns:
operatorComparisonOperator

Comparaison operator used to define the RandomVector.

getParameter()

Accessor to the parameter of the distribution.

Returns:
parameterPoint

Parameter values.

getParameterDescription()

Accessor to the parameter description of the distribution.

Returns:
descriptionDescription

Parameter names.

getProcess()

Get the stochastic process.

Returns:
processProcess

Stochastic process used to define the RandomVector.

getProposal()

Get the proposal distribution.

Returns:
proposalDistribution

The distribution from which the transition kernels of the random walk Metropolis-Hastings algorithm is defined

getRealization()

Compute one realization of the RandomVector.

Returns:
realizationPoint

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).

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]

getSample(size)

Compute realizations of the RandomVector.

Parameters:
nint,

Number of realizations needed.

Returns:
realizationsSample

n sequences of values randomly determined from the RandomVector definition. In the case of an event: n realizations of the event (considered as a Bernoulli variable) which are boolean values (1 for the realization of the event and 0 else).

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.getSample(3))
[ X0        X1        ]
0 : [  0.608202 -1.26617  ]
1 : [ -0.438266  1.20548  ]
2 : [ -2.18139   0.350042 ]

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

getThreshold()

Accessor to the threshold of the Event.

Returns:
thresholdfloat

Threshold of the RandomVector.

hasName()

Test if the object is named.

Returns:
hasNamebool

True if the name is not empty.

isComposite()

Accessor to know if the RandomVector is a composite one.

Returns:
isCompositebool

Indicates if the RandomVector is of type Composite or not.

isEvent()

Whether the random vector is an event.

Returns:
isEventbool

Whether it takes it values in {0, 1}.

Set the adaptation expansion factor.

Parameters:
expansionFactorfloat,

Expansion factor of the adaptation factor.

Set the adaptation period.

Parameters:
periodpositive int

During burn-in, adaptation is performed once every period iterations.

Set the expected range for the acceptance rate. During burn-in, at the end of every adaptation period, if the acceptance rate does not belong to this range, the adaptation factor is multiplied either by the expansion factor (if the acceptance rate is larger than the upperBound) or by the shrink factor (if the acceptance rate is smaller than the lowerBound).

Parameters:
rangeInterval of dimension 1

Range [lowerBound, upperBound] of the expected acceptance rate.

Set the adaptation shrink factor.

Parameters:
shrinkFactorfloat,

Shrink factor of the adaptation factor.

setBurnIn(burnIn)

Set the length of the burn-in period.

Parameters:
burninint

Length of the burn-in period: this is the number of iterations after which adaptation stops.

setDescription(description)

Accessor to the description of the RandomVector.

Parameters:
descriptionstr or sequence of str

Describes the components of the RandomVector.

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 required to define the likelihood.

linkFunctionFunction, optional

Function 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 of the linkFunction for each observation . 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 be the PDF of the prior at the point . The class now samples from the posterior, whose PDF is proportional to , the likelihood being defined from the arguments of this method.

The optional parameters linkFunction and covariates allow several options to define the likelihood . Letting be the PDF of the distribution conditional:

• Without linkFunction and covariates the likelihood term reads:

• If only the linkFunction is provided:

• If both the linkFunction and covariates are provided:

setName(name)

Accessor to the object’s name.

Parameters:
namestr

The name of the object.

setParameter(parameters)

Accessor to the parameter of the distribution.

Parameters:
parametersequence of float

Parameter values.

setProposal(proposal)

Set the proposal distribution.

Parameters:
proposalDistribution

The distribution from which the transition kernels of the random walk Metropolis-Hastings algorithm is defined

## Examples using the class¶

Create a parametric function

Create a parametric function

Sampling from an unnormalized probability density

Sampling from an unnormalized probability density

Posterior sampling using a PythonDistribution

Posterior sampling using a PythonDistribution

Bayesian calibration of a computer code

Bayesian calibration of a computer code

Bayesian calibration of the flooding model

Bayesian calibration of the flooding model

Linear Regression with interval-censored observations

Linear Regression with interval-censored observations