Exemplo n.º 1
0
print('Q0=', Q0)

mu0 = [0.0] * chainDim
prior = ot.Normal(mu0, Q0_inv)  # x0 ~ N(mu0, sigma0)
print('x~', prior)

# start from te mean x0=(0.,0.,0.)
print('x0=', mu0)

# conditional distribution y~N(z, 1.0)
conditional = ot.Normal()
print('y~', conditional)

# create a gibbs sampler
mh_coll = [
    ot.RandomWalkMetropolisHastings(prior, mu0, instrumental, [i])
    for i in range(chainDim)
]
for mh in mh_coll:
    mh.setLikelihood(conditional, y_obs, linkFunction, p)
sampler = ot.Gibbs(mh_coll)
sampler.setThinning(4)
sampler.setBurnIn(2000)

# get a realization
realization = sampler.getRealization()
print('y1=', realization)

# try to generate a sample
sampleSize = 1000
sample = sampler.getSample(sampleSize)
Exemplo n.º 2
0
#

# %%

proposal = []
proposal.append(ot.Normal(0., 0.1 * np.sqrt(a_beta / b_beta**2)))
proposal.append(ot.Normal(0., 0.1 * (alpha_max - alpha_min)))
proposal = ot.ComposedDistribution(proposal)

# %%
# Sample from the posterior distribution
# --------------------------------------

# %%

sampler = ot.RandomWalkMetropolisHastings(prior, initialState, proposal)
sampler.setLikelihood(conditional, x)
sampleSize = 10000
sample = sampler.getSample(sampleSize)
# compute acceptance rate
print("Acceptance rate: %s" % (sampler.getAcceptanceRate()))

# %%
# Plot prior to posterior marginal plots.
#

# %%
kernel = ot.KernelSmoothing()
posterior = kernel.build(sample)
grid = ot.GridLayout(1, 2)
grid.setTitle('Bayesian inference')
Exemplo n.º 3
0
# ------------------------------------

# %%
# Creation of a single component random walk Metropolis-Hastings (RWMH) sampler.
# This involves a combination of the RWMH and the Gibbs algorithms.

# %%
initialState = thetaPriorMean

# %%
# We create a :class:`~openturns.RandomWalkMetropolisHastings` sampler for each component.
# Each sampler must be aware of the joint prior distribution.
# We also use the same proposal distribution, but this is not mandatory.

mh_coll = [
    ot.RandomWalkMetropolisHastings(prior, initialState, proposal, [i])
    for i in range(paramDim)
]

# %%
# Each sampler must be made aware of the likelihood.
# Otherwise we would sample from the prior!

for mh in mh_coll:
    mh.setLikelihood(conditional, y_obs, linkFunction, x_obs)

# %%
# Finally, the :class:`~openturns.Gibbs` algorithm is constructed from all Metropolis-Hastings samplers.

sampler = ot.Gibbs(mh_coll)
Exemplo n.º 4
0
    Q0[i, i] = 1.0 / Q0_inv[i, i]
print('Q0=', Q0)

mu0 = [0.0] * chainDim
prior = ot.Normal(mu0, Q0_inv)  # x0 ~ N(mu0, sigma0)
print('x~', prior)

# start from te mean x0=(0.,0.,0.)
print('x0=', mu0)

# conditional distribution y~N(z, 1.0)
conditional = ot.Normal()
print('y~', conditional)

# create a gibbs sampler
mh_coll = [ot.RandomWalkMetropolisHastings(prior, mu0, instrumental, [i]) for i in range(chainDim)]
for mh in mh_coll: mh.setLikelihood(conditional, y_obs, linkFunction, p)
sampler = ot.Gibbs(mh_coll)
sampler.setThinning(4)
sampler.setBurnIn(2000)

# get a realization
realization = sampler.getRealization()
print('y1=', realization)

# try to generate a sample
sampleSize = 1000
sample = sampler.getSample(sampleSize)

x_mu = sample.computeMean()
x_sigma = sample.computeStandardDeviation()
# %%
mymcmc.computeLogLikelihood(parameterPriorMean)

# %%
# Test the Metropolis-Hastings sampler
# ------------------------------------

# %%
# - Creation of the Random Walk Metropolis-Hastings (RWMH) sampler.

# %%
initialState = parameterPriorMean

# %%
RWMHsampler = ot.RandomWalkMetropolisHastings(prior, conditional, model, Qobs,
                                              Hobs, initialState, proposal)

# %%
# Tuning of the RWMH algorithm.

# %%
# Strategy of calibration for the random walk (trivial example: default).

# %%
strategy = ot.CalibrationStrategyCollection(paramDim)
RWMHsampler.setCalibrationStrategyPerComponent(strategy)

# %%
# Other parameters.

# %%
conditional = ot.Normal()

# %%
# The proposed steps for :math:`K_s` :math:`Z_v` and :math:`Z_m`
# will all follow uniform distributions,
# but with different supports.

# %%
proposal = [ot.Uniform(-5., 5.), ot.Uniform(-1., 1.), ot.Uniform(-1., 1.)]

# %%
# Build a Gibbs sampler
# ---------------------

initialState = parameterPriorMean
mh_coll = [ot.RandomWalkMetropolisHastings(
    prior, initialState, proposal[i], [i]) for i in range(paramDim)]
for mh in mh_coll:
    mh.setLikelihood(conditional, Hobs, linkFunction, Qobs)
sampler = ot.Gibbs(mh_coll)

# %%
# Tuning of the Gibbs algorithm.

# %%
sampler.setThinning(1)
sampler.setBurnIn(200)

# %%
# Generate a sample from the posterior distribution of :math:`\vect \theta`.

# %%
# distribution of the sample mean
for i in range(len(sigma0s)):

    sigma0 = sigma0s[i]
    mean_prior = ot.Normal(mu0, sigma0)
    std_prior = ot.Dirac(2.0)  # standard dev is known
    prior = ot.ComposedDistribution([mean_prior, std_prior])

    # choose the initial state within the prior
    initialState = prior.getRealization()

    # conditional distribution
    conditional = ot.Normal()

    # create a metropolis-hastings sampler
    sampler = ot.RandomWalkMetropolisHastings(
        prior, conditional, data, initialState, proposalColl)
    sampler.setVerbose(True)
    sampler.setThinning(2)
    sampler.setBurnIn(500)
    sampler.setCalibrationStrategyPerComponent(calibrationColl)
    realization = sampler.getRealization()

    sigmay = ot.ConditionalDistribution(
        ot.Normal(), prior).getStandardDeviation()[0]
    w = size * sigma0 ** 2. / (size * sigma0 ** 2. + sigmay ** 2.0)

    print("prior variance= %.12g" % (sigma0 ** 2.))
    print("  realization=", realization)

    print("  w= %.12g" % w)
#! /usr/bin/env python

import openturns as ot
import openturns.testing as ott

ot.TESTPREAMBLE()

# dummy run without likelihood
mu = 5000.0
prior = ot.Normal(mu, 1.0)
initialState = [0.0]
instrumental = ot.Normal(0.0, 5.0)
sampler = ot.RandomWalkMetropolisHastings(prior, initialState, instrumental)
sampler.setBurnIn(1000)
s1 = sampler.getSample(50)
ott.assert_almost_equal(s1.computeMean()[0], mu, 1e-2, 1e3)

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

data.setDescription(['Temp. (°F)', 'Failure'])
print(data)

fun = ot.SymbolicFunction(
    ["alpha", "beta", "x"],
    ["exp(alpha + beta * x) / (1 + exp(alpha + beta * x))"])
linkFunction = ot.ParametricFunction(fun, [2], [0.0])
# %%
# Even with very few sampling points (500),
# independent Metropolis-Hastings
# (with a judiciously chosen instrumental distribution)
# manages to capture the main features of the target density.

# %%
# Random walk Metropolis-Hastings
# -------------------------------
#
# Let us use a normal instrumental distribution.

instrumentalDistribution = ot.Normal(0.0, 2.5)
randomwalkMH = ot.RandomWalkMetropolisHastings(log_density, support,
                                               initialState,
                                               instrumentalDistribution, [0])

# %%
# Get a sample
sample = randomwalkMH.getSample(sampleSize)

# %%
# Plot the PDF of the sample to compare it to the target density

kernel = ot.KernelSmoothing()
posterior = kernel.build(sample)
graph = ot.Graph(
    "Random walk Metropolis-Hastings sample: {} points".format(sampleSize), "",
    "", True, "topright")
graph.setBoundingBox(
Exemplo n.º 10
0
# if large, the the posterior distribution is equivalent to the
# distribution of the sample mean
for i in range(len(sigma0s)):

    sigma0 = sigma0s[i]
    mean_prior = ot.Normal(mu0, sigma0)
    std_prior = ot.Dirac(2.0)  # standard dev is known
    prior = ot.ComposedDistribution([mean_prior, std_prior])
    # choose the initial state within the prior
    initialState = prior.getRealization()

    # conditional distribution
    conditional = ot.Normal()

    # create a Gibbs sampler
    mean_sampler = ot.RandomWalkMetropolisHastings(prior, initialState,
                                                   mean_instrumental, [0])
    mean_sampler.setLikelihood(conditional, data)
    std_sampler = ot.RandomWalkMetropolisHastings(prior, initialState,
                                                  std_instrumental, [1])
    std_sampler.setLikelihood(conditional, data)
    sampler = ot.Gibbs([mean_sampler, std_sampler])
    sampler.setThinning(2)
    sampler.setBurnIn(500)
    realization = sampler.getRealization()

    sigmay = ot.ConditionalDistribution(ot.Normal(),
                                        prior).getStandardDeviation()[0]
    w = size * sigma0**2. / (size * sigma0**2. + sigmay**2.0)

    print("prior variance= %.12g" % (sigma0**2.))
    print("  realization=", realization)