class_docstr: Hamiltonian Monte Carlo, also known as hybrid Monte Carlo
  [@duane1987hybrid; @neal2011mcmc].
  
  #### Notes
  
  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)$.
  
  #### Examples
  
  ```python
  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)})
  ```
class_docstr_tok: ["Hamiltonian","Monte","Carlo",",","also","known","as","hybrid","Monte","Carlo","[","@","duane1987hybrid",";","@","neal2011mcmc","]",".","#","#","#","#","Notes","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",")","$",".","#","#","#","#","Examples","``","`","python","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",")","}",")","``","`"]
docstr: Simulate Hamiltonian dynamics using a numerical integrator.
  Correct for the integrator's discretization error using an
  acceptance ratio.
  
  #### Notes
  
  The updates assume each Empirical random variable is directly
  parameterized by `tf.Variable`s.
docstr_tok: ["Simulate","Hamiltonian","dynamics","using","a","numerical","integrator",".","Correct","for","the","integrator","'s","discretization","error","using","an","acceptance","ratio",".","#","#","#","#","Notes","The","updates","assume","each","Empirical","random","variable","is","directly","parameterized","by","`","tf.Variable","`","s","."]
code: def build_update(self):
      """Simulate Hamiltonian dynamics using a numerical integrator.
      Correct for the integrator's discretization error using an
      acceptance ratio.
  
      #### Notes
  
      The updates assume each Empirical random variable is directly
      parameterized by `tf.Variable`s.
      """
  
      # Gather the initial state, transformed to unconstrained space.
      try:
          self.latent_vars_unconstrained
      except:
          raise ValueError(
              "This implementation of HMC requires that all "
              "variables have unconstrained support. Please "
              "initialize with auto_transform=True to ensure "
              "this. (if your variables already have unconstrained "
              "support then doing this is a no-op)."
          )
      old_sample = {
          z_unconstrained: tf.gather(
              qz_unconstrained.params, tf.maximum(self.t - 1, 0)
          )
          for z_unconstrained, qz_unconstrained in six.iteritems(
              self.latent_vars_unconstrained
          )
      }
      old_sample = OrderedDict(old_sample)
  
      # Sample momentum.
      old_r_sample = OrderedDict()
      for z, qz in six.iteritems(self.latent_vars_unconstrained):
          event_shape = qz.event_shape
          old_r_sample[z] = tf.random_normal(
              event_shape, dtype=qz.dtype
          )
  
      # Simulate Hamiltonian dynamics.
      new_sample, new_r_sample = leapfrog(
          old_sample,
          old_r_sample,
          self.step_size,
          self._log_joint_unconstrained,
          self.n_steps,
      )
  
      # Calculate acceptance ratio.
      ratio = tf.reduce_sum(
          [
              0.5 * tf.reduce_sum(tf.square(r))
              for r in six.itervalues(old_r_sample)
          ]
      )
      ratio -= tf.reduce_sum(
          [
              0.5 * tf.reduce_sum(tf.square(r))
              for r in six.itervalues(new_r_sample)
          ]
      )
      ratio += self._log_joint_unconstrained(new_sample)
      ratio -= self._log_joint_unconstrained(old_sample)
  
      # Accept or reject sample.
      u = tf.random_uniform([], dtype=ratio.dtype)
      accept = tf.log(u) < ratio
      sample_values = tf.cond(
          accept,
          lambda: list(six.itervalues(new_sample)),
          lambda: list(six.itervalues(old_sample)),
      )
      if not isinstance(sample_values, list):
          # `tf.cond` returns tf.Tensor if output is a list of size 1.
          sample_values = [sample_values]
  
      sample = {
          z_unconstrained: sample_value
          for z_unconstrained, sample_value in zip(
              six.iterkeys(new_sample), sample_values
          )
      }
  
      # Update Empirical random variables.
      assign_ops = []
      for z_unconstrained, qz_unconstrained in six.iteritems(
          self.latent_vars_unconstrained
      ):
          variable = qz_unconstrained.get_variables()[0]
          assign_ops.append(
              tf.scatter_update(
                  variable, self.t, sample[z_unconstrained]
              )
          )
  
      # Increment n_accept (if accepted).
      assign_ops.append(
          self.n_accept.assign_add(tf.where(accept, 1, 0))
      )
      return tf.group(*assign_ops)
  
file_path: edward/edward/inferences/hmc.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import six
  import tensorflow
  from collections import OrderedDict
  from edward.inferences.monte_carlo import MonteCarlo
  from edward.models import RandomVariable
  from edward.util import copy
label_desc: Use image node_id 3 for calling the HMC obj's underlying member method code with example usage: obj.build_update() and returns: tf