`HMC`

Inherits From: `MonteCarlo`

- Class
`ed.HMC`

- Class
`ed.inferences.HMC`

Defined in `edward/inferences/hmc.py`

.

Hamiltonian Monte Carlo, also known as hybrid Monte Carlo (Duane, Kennedy, Pendleton, & Roweth, 1987; Neal, 2011).

In conditional inference, we infer \(z\) in \(p(z, \beta \mid x)\) while fixing inference over \(\beta\) using another distribution \(q(\beta)\). `HMC`

substitutes the model’s log marginal density

\(\log p(x, z) = \log \mathbb{E}_{q(\beta)} [ p(x, z, \beta) ] \approx \log 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)\).

```
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)))
inference = ed.HMC({mu: qmu}, {x: np.zeros(10, dtype=np.float32)})
```

**init**

```
__init__(
*args,
**kwargs
)
```

`build_update`

`build_update()`

Simulate Hamiltonian dynamics using a numerical integrator. Correct for the integrator’s discretization error using an acceptance ratio.

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

s.

`finalize`

`finalize()`

Function to call after convergence.

`initialize`

```
initialize(
step_size=0.25,
n_steps=2,
*args,
**kwargs
)
```

Initialize inference algorithm. It initializes hyperparameters and builds ops for the algorithm’s computation graph.

: float. Step size of numerical integrator.`step_size`

: int. Number of steps of numerical integrator.`n_steps`

`print_progress`

`print_progress(info_dict)`

Print progress to output.

`run`

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

A simple wrapper to run inference.

- Initialize algorithm via
`initialize`

. - (Optional) Build a TensorFlow summary writer for TensorBoard.
- (Optional) Initialize TensorFlow variables.
- (Optional) Start queue runners.
- Run
`update`

for`self.n_iter`

iterations. - While running,
`print_progress`

. - Finalize algorithm via
`finalize`

. - (Optional) Stop queue runners.

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

: 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.`variables`

: 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`use_coordinator`

`initialize`

.

`update`

`update(feed_dict=None)`

Run one iteration of sampling.

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

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

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.

Duane, S., Kennedy, A. D., Pendleton, B. J., & Roweth, D. (1987). Hybrid Monte Carlo. *Physics Letters B*, *195*(2), 216–222.

Neal, R. M. (2011). MCMC using Hamiltonian dynamics. *Handbook of Markov Chain Monte Carlo*.