# ed.MetropolisHastings

## Class MetropolisHastings

Inherits From: MonteCarlo

### Aliases:

• Class ed.MetropolisHastings
• Class ed.inferences.MetropolisHastings

Metropolis-Hastings (Hastings, 1970; Metropolis, Rosenbluth, Rosenbluth, Teller, & Teller, 1953).

In conditional inference, we infer $$z$$ in $$p(z, \beta \mid x)$$ while fixing inference over $$\beta$$ using another distribution $$q(\beta)$$. To calculate the acceptance ratio, MetropolisHastings uses an estimate of the marginal density,

$$p(x, z) = \mathbb{E}_{q(\beta)} [ p(x, z, \beta) ] \approx p(x, z, \beta^*)$$

leveraging a single Monte Carlo sample, where $$\beta^* \sim q(\beta)$$. This is unbiased (and therefore asymptotically exact as a pseudo-marginal method) if $$q(\beta) = p(\beta \mid x)$$.

MetropolisHastings assumes the proposal distribution has the same support as the prior. The auto_transform attribute in the method initialize() is not applicable.

#### Examples

mu = Normal(loc=0.0, scale=1.0)
x = Normal(loc=mu, scale=1.0, sample_shape=10)

qmu = Empirical(tf.Variable(tf.zeros(500)))
proposal_mu = Normal(loc=mu, scale=0.5)
inference = ed.MetropolisHastings({mu: qmu}, {mu: proposal_mu},
data={x: np.zeros(10, dtype=np.float32)})

## Methods

### init

__init__(
latent_vars,
proposal_vars,
data=None
)

Create an inference algorithm.

#### Args:

• proposal_vars: dict of RandomVariable to RandomVariable. Collection of random variables to perform inference on; each is binded to a proposal distribution $$g(z' \mid z)$$.

### build_update

build_update()

Draw sample from proposal conditional on last sample. Then accept or reject the sample based on the ratio,

$$\text{ratio} = \log p(x, z^{\text{new}}) - \log p(x, z^{\text{old}}) - \log g(z^{\text{new}} \mid z^{\text{old}}) + \log g(z^{\text{old}} \mid z^{\text{new}})$$

#### Notes

The updates assume each Empirical random variable is directly parameterized by tf.Variables.

### finalize

finalize()

Function to call after convergence.

### initialize

initialize(
*args,
**kwargs
)
print_progress(info_dict)

Print progress to output.

### run

run(
variables=None,
use_coordinator=True,
*args,
**kwargs
)

A simple wrapper to run inference.

1. Initialize algorithm via initialize.
2. (Optional) Build a TensorFlow summary writer for TensorBoard.
3. (Optional) Initialize TensorFlow variables.
4. (Optional) Start queue runners.
5. Run update for self.n_iter iterations.
6. While running, print_progress.
7. Finalize algorithm via finalize.
8. (Optional) Stop queue runners.

To customize the way inference is run, run these steps individually.

#### Args:

• variables: list. A list of TensorFlow variables to initialize during inference. Default is to initialize all variables (this includes reinitializing variables that were already initialized). To avoid initializing any variables, pass in an empty list.
• use_coordinator: bool. Whether to start and stop queue runners during inference using a TensorFlow coordinator. For example, queue runners are necessary for batch training with file readers. *args, **kwargs: Passed into initialize.

### update

update(feed_dict=None)

Run one iteration of sampling.

#### Args:

• feed_dict: dict. Feed dictionary for a TensorFlow session run. It is used to feed placeholders that are not fed during initialization.

#### Returns:

dict. Dictionary of algorithm-specific information. In this case, the acceptance rate of samples since (and including) this iteration.

#### Notes

We run the increment of t separately from other ops. Whether the others op run with the t before incrementing or after incrementing depends on which is run faster in the TensorFlow graph. Running it separately forces a consistent behavior.

Hastings, W. K. (1970). Monte Carlo sampling methods using Markov chains and their applications. Biometrika, 57(1), 97–109.

Metropolis, N., Rosenbluth, A. W., Rosenbluth, M. N., Teller, A. H., & Teller, E. (1953). Equation of state calculations by fast computing machines. The Journal of Chemical Physics, 21(6), 1087–1092.