class_docstr: Class for performing sampling in Continuous model
  using Hamiltonian Monte Carlo with dual averaging for
  adaptaion of parameter stepsize.
  
  Parameters
  ----------
  model: An instance pgmpy.models
      Model from which sampling has to be done
  
  grad_log_pdf: A subclass of pgmpy.inference.continuous.GradientLogPDF
      Class to compute the log and gradient log of distribution
  
  simulate_dynamics: A subclass of pgmpy.inference.continuous.BaseSimulateHamiltonianDynamics
      Class to propose future states of position and momentum in time by simulating
      HamiltonianDynamics
  
  delta: float (in between 0 and 1), defaults to 0.65
      The target HMC acceptance probability
  
  Example
  -------
  >>> from pgmpy.sampling import HamiltonianMCDA as HMCda, LeapFrog, GradLogPDFGaussian as GLPG
  >>> from pgmpy.factors.continuous import GaussianDistribution as JGD
  >>> import numpy as np
  >>> mean = np.array([1, 2, 3])
  >>> covariance = np.array([[2, 0.4, 0.5], [0.4, 3, 0.6], [0.5, 0.6, 4]])
  >>> model = JGD(['x', 'y', 'z'], mean, covariance)
  >>> sampler = HMCda(model=model, grad_log_pdf=GLPG)
  >>> samples = sampler.sample(np.array([0, 0, 0]), num_adapt=10000, num_samples = 10000, trajectory_length=7,
  ...                          return_type='recarray')
  >>> samples_array = np.array([samples[var_name] for var_name in model.variables])
  >>> np.cov(samples_array)
  array([[ 1.83023816,  0.40449162,  0.51200707],
         [ 0.40449162,  2.85863596,  0.76747343],
         [ 0.51200707,  0.76747343,  3.87020982]])
  >>> sampler.acceptance_rate
  0.9929
  
  References
  -----------
  Matthew D. Hoffman, Andrew Gelman, The No-U-Turn Sampler: Adaptively
  Setting Path Lengths in Hamiltonian Monte Carlo. Journal of
  Machine Learning Research 15 (2014) 1351-1381
  Algorithm 5 : Hamiltonian Monte Carlo with dual averaging
class_docstr_tok: ["Class","for","performing","sampling","in","Continuous","model","using","Hamiltonian","Monte","Carlo","with","dual","averaging","for","adaptaion","of","parameter","stepsize",".","Parameters","--","--","--","--","--","model",":","An","instance","pgmpy.models","Model","from","which","sampling","has","to","be","done","grad_log_pdf",":","A","subclass","of","pgmpy.inference.continuous.GradientLogPDF","Class","to","compute","the","log","and","gradient","log","of","distribution","simulate_dynamics",":","A","subclass","of","pgmpy.inference.continuous.BaseSimulateHamiltonianDynamics","Class","to","propose","future","states","of","position","and","momentum","in","time","by","simulating","HamiltonianDynamics","delta",":","float","(","in","between","0","and","1",")",",","defaults","to","0.65","The","target","HMC","acceptance","probability","Example","--","--","--","-",">",">",">","from","pgmpy.sampling","import","HamiltonianMCDA","as","HMCda",",","LeapFrog",",","GradLogPDFGaussian","as","GLPG",">",">",">","from","pgmpy.factors.continuous","import","GaussianDistribution","as","JGD",">",">",">","import","numpy","as","np",">",">",">","mean","=","np.array","(","[","1",",","2",",","3","]",")",">",">",">","covariance","=","np.array","(","[","[","2",",","0.4",",","0.5","]",",","[","0.4",",","3",",","0.6","]",",","[","0.5",",","0.6",",","4","]","]",")",">",">",">","model","=","JGD","(","[","'","x","'",",","'","y","'",",","'","z","'","]",",","mean",",","covariance",")",">",">",">","sampler","=","HMCda","(","model=model",",","grad_log_pdf=GLPG",")",">",">",">","samples","=","sampler.sample","(","np.array","(","[","0",",","0",",","0","]",")",",","num_adapt=10000",",","num_samples","=","10000",",","trajectory_length=7",",","...","return_type='recarray","'",")",">",">",">","samples_array","=","np.array","(","[","samples","[","var_name","]","for","var_name","in","model.variables","]",")",">",">",">","np.cov","(","samples_array",")","array","(","[","[","1.83023816",",","0.40449162",",","0.51200707","]",",","[","0.40449162",",","2.85863596",",","0.76747343","]",",","[","0.51200707",",","0.76747343",",","3.87020982","]","]",")",">",">",">","sampler.acceptance_rate","0.9929","References","--","--","--","--","--","-","Matthew","D.","Hoffman",",","Andrew","Gelman",",","The","No-U-Turn","Sampler",":","Adaptively","Setting","Path","Lengths","in","Hamiltonian","Monte","Carlo",".","Journal","of","Machine","Learning","Research","15","(","2014",")","1351-1381","Algorithm","5",":","Hamiltonian","Monte","Carlo","with","dual","averaging"]
docstr: Method returns a generator type object whose each iteration yields a sample
  using Hamiltonian Monte Carlo
  
  Parameters
  ----------
  initial_pos: A 1d array like object
      Vector representing values of parameter position, the starting
      state in markov chain.
  
  num_adapt: int
      The number of iterations to run the adaptation of stepsize
  
  num_samples: int
      Number of samples to be generated
  
  trajectory_length: int or float
      Target trajectory length, stepsize * number of steps(L),
      where L is the number of steps taken to propose new values of position and momentum
      per HMC iteration and stepsize is step size.
  
  stepsize: float , defaults to None
      The stepsize for proposing new values of position and momentum in simulate_dynamics
      If None, then will be chosen suitably
  
  Returns
  -------
  genrator: yielding a numpy.array type object for a sample
  
  Examples
  --------
  >>> from pgmpy.sampling import HamiltonianMCDA as HMCda, GradLogPDFGaussian as GLPG, LeapFrog
  >>> from pgmpy.factors.continuous import GaussianDistribution as JGD
  >>> import numpy as np
  >>> mean = np.array([1, 1])
  >>> covariance = np.array([[1, 0.7], [0.7, 3]])
  >>> model = JGD(['x', 'y'], mean, covariance)
  >>> sampler = HMCda(model=model, grad_log_pdf=GLPG, simulate_dynamics=LeapFrog)
  >>> gen_samples = sampler.generate_sample(np.array([1, 1]), num_adapt=10000,
  ...                                       num_samples = 10000, trajectory_length=2, stepsize=None)
  >>> samples_array = np.array([sample for sample in gen_samples])
  >>> np.cov(samples_array.T)
  array([[ 0.98432155,  0.69517394],
         [ 0.69517394,  2.95449533]])
docstr_tok: ["Method","returns","a","generator","type","object","whose","each","iteration","yields","a","sample","using","Hamiltonian","Monte","Carlo","Parameters","--","--","--","--","--","initial_pos",":","A","1d","array","like","object","Vector","representing","values","of","parameter","position",",","the","starting","state","in","markov","chain",".","num_adapt",":","int","The","number","of","iterations","to","run","the","adaptation","of","stepsize","num_samples",":","int","Number","of","samples","to","be","generated","trajectory_length",":","int","or","float","Target","trajectory","length",",","stepsize","*","number","of","steps","(","L",")",",","where","L","is","the","number","of","steps","taken","to","propose","new","values","of","position","and","momentum","per","HMC","iteration","and","stepsize","is","step","size",".","stepsize",":","float",",","defaults","to","None","The","stepsize","for","proposing","new","values","of","position","and","momentum","in","simulate_dynamics","If","None",",","then","will","be","chosen","suitably","Returns","--","--","--","-","genrator",":","yielding","a","numpy.array","type","object","for","a","sample","Examples","--","--","--","--",">",">",">","from","pgmpy.sampling","import","HamiltonianMCDA","as","HMCda",",","GradLogPDFGaussian","as","GLPG",",","LeapFrog",">",">",">","from","pgmpy.factors.continuous","import","GaussianDistribution","as","JGD",">",">",">","import","numpy","as","np",">",">",">","mean","=","np.array","(","[","1",",","1","]",")",">",">",">","covariance","=","np.array","(","[","[","1",",","0.7","]",",","[","0.7",",","3","]","]",")",">",">",">","model","=","JGD","(","[","'","x","'",",","'","y","'","]",",","mean",",","covariance",")",">",">",">","sampler","=","HMCda","(","model=model",",","grad_log_pdf=GLPG",",","simulate_dynamics=LeapFrog",")",">",">",">","gen_samples","=","sampler.generate_sample","(","np.array","(","[","1",",","1","]",")",",","num_adapt=10000",",","...","num_samples","=","10000",",","trajectory_length=2",",","stepsize=None",")",">",">",">","samples_array","=","np.array","(","[","sample","for","sample","in","gen_samples","]",")",">",">",">","np.cov","(","samples_array.T",")","array","(","[","[","0.98432155",",","0.69517394","]",",","[","0.69517394",",","2.95449533","]","]",")"]
code: def generate_sample(
      self,
      initial_pos,
      num_adapt,
      num_samples,
      trajectory_length,
      stepsize=None,
  ):
      """
      Method returns a generator type object whose each iteration yields a sample
      using Hamiltonian Monte Carlo
  
      Parameters
      ----------
      initial_pos: A 1d array like object
          Vector representing values of parameter position, the starting
          state in markov chain.
  
      num_adapt: int
          The number of iterations to run the adaptation of stepsize
  
      num_samples: int
          Number of samples to be generated
  
      trajectory_length: int or float
          Target trajectory length, stepsize * number of steps(L),
          where L is the number of steps taken to propose new values of position and momentum
          per HMC iteration and stepsize is step size.
  
      stepsize: float , defaults to None
          The stepsize for proposing new values of position and momentum in simulate_dynamics
          If None, then will be chosen suitably
  
      Returns
      -------
      genrator: yielding a numpy.array type object for a sample
  
      Examples
      --------
      >>> from pgmpy.sampling import HamiltonianMCDA as HMCda, GradLogPDFGaussian as GLPG, LeapFrog
      >>> from pgmpy.factors.continuous import GaussianDistribution as JGD
      >>> import numpy as np
      >>> mean = np.array([1, 1])
      >>> covariance = np.array([[1, 0.7], [0.7, 3]])
      >>> model = JGD(['x', 'y'], mean, covariance)
      >>> sampler = HMCda(model=model, grad_log_pdf=GLPG, simulate_dynamics=LeapFrog)
      >>> gen_samples = sampler.generate_sample(np.array([1, 1]), num_adapt=10000,
      ...                                       num_samples = 10000, trajectory_length=2, stepsize=None)
      >>> samples_array = np.array([sample for sample in gen_samples])
      >>> np.cov(samples_array.T)
      array([[ 0.98432155,  0.69517394],
             [ 0.69517394,  2.95449533]])
      """
      self.accepted_proposals = 0
      initial_pos = _check_1d_array_object(initial_pos, "initial_pos")
      _check_length_equal(
          initial_pos,
          self.model.variables,
          "initial_pos",
          "model.variables",
      )
  
      if stepsize is None:
          stepsize = self._find_reasonable_stepsize(initial_pos)
  
      if (
          num_adapt <= 1
      ):  # return sample generated using Simple HMC algorithm
          for sample in HamiltonianMC.generate_sample(
              self,
              initial_pos,
              num_samples,
              trajectory_length,
              stepsize,
          ):
              yield sample
          return
      mu = np.log(10.0 * stepsize)
  
      stepsize_bar = 1.0
      h_bar = 0.0
  
      position_m = initial_pos.copy()
      num_adapt += 1
  
      for i in range(1, num_samples + 1):
          position_m, alpha = self._sample(
              position_m, trajectory_length, stepsize
          )
  
          if i <= num_adapt:
              stepsize, stepsize_bar, h_bar = self._adapt_params(
                  stepsize, stepsize_bar, h_bar, mu, i, alpha
              )
          else:
              stepsize = stepsize_bar
  
          yield position_m
  
      self.acceptance_rate = self.accepted_proposals / num_samples
  
file_path: pgmpy/pgmpy/sampling/HMC.py
imports: from math import sqrt
  import numpy
  from tqdm.auto import tqdm
  from pgmpy.sampling import BaseGradLogPDF, BaseSimulateHamiltonianDynamics, LeapFrog, _return_samples
  from pgmpy.utils import _check_1d_array_object, _check_length_equal
label_desc: Use image node_id 4 for calling the HamiltonianMCDA obj's underlying member method code with example usage: obj.generate_sample(initial_pos, num_adapt, num_samples, trajectory_length, stepsize) without return types