class_docstr: Superclass to implement Hamiltonian/hybrid monte carlo.
docstr: Set up Hamiltonian samplers with common structures.
  
  Parameters
  ----------
  vars: list, default=None
      List of PyTensor variables. If None, all continuous RVs from the
      model are included.
  scaling: array_like, ndim={1,2}
      Scaling for momentum distribution. 1d arrays interpreted matrix
      diagonal.
  step_scale: float, default=0.25
      Size of steps to take, automatically scaled down by 1/n**(1/4),
      where n is the dimensionality of the parameter space
  is_cov: bool, default=False
      Treat scaling as a covariance matrix/vector if True, else treat
      it as a precision matrix/vector
  model: pymc.Model
  blocked: bool, default=True
  potential: Potential, optional
      An object that represents the Hamiltonian with methods `velocity`,
      `energy`, and `random` methods.
  **pytensor_kwargs: passed to PyTensor functions
docstr_tok: ["Set","up","Hamiltonian","samplers","with","common","structures",".","Parameters","--","--","--","--","--","vars",":","list",",","default=None","List","of","PyTensor","variables",".","If","None",",","all","continuous","RVs","from","the","model","are","included",".","scaling",":","array_like",",","ndim=","{","1,2","}","Scaling","for","momentum","distribution",".","1d","arrays","interpreted","matrix","diagonal",".","step_scale",":","float",",","default=0.25","Size","of","steps","to","take",",","automatically","scaled","down","by","1\/n","*","*","(","1\/4",")",",","where","n","is","the","dimensionality","of","the","parameter","space","is_cov",":","bool",",","default=False","Treat","scaling","as","a","covariance","matrix\/vector","if","True",",","else","treat","it","as","a","precision","matrix\/vector","model",":","pymc.Model","blocked",":","bool",",","default=True","potential",":","Potential",",","optional","An","object","that","represents","the","Hamiltonian","with","methods","`","velocity","`",",","`","energy","`",",","and","`","random","`","methods",".","*","*","pytensor_kwargs",":","passed","to","PyTensor","functions"]
code: def __init__(
      self,
      vars=None,
      scaling=None,
      step_scale=0.25,
      is_cov=False,
      model=None,
      blocked=True,
      potential=None,
      dtype=None,
      Emax=1000,
      target_accept=0.8,
      gamma=0.05,
      k=0.75,
      t0=10,
      adapt_step_size=True,
      step_rand=None,
      **pytensor_kwargs,
  ):
      """Set up Hamiltonian samplers with common structures.
  
      Parameters
      ----------
      vars: list, default=None
          List of PyTensor variables. If None, all continuous RVs from the
          model are included.
      scaling: array_like, ndim={1,2}
          Scaling for momentum distribution. 1d arrays interpreted matrix
          diagonal.
      step_scale: float, default=0.25
          Size of steps to take, automatically scaled down by 1/n**(1/4),
          where n is the dimensionality of the parameter space
      is_cov: bool, default=False
          Treat scaling as a covariance matrix/vector if True, else treat
          it as a precision matrix/vector
      model: pymc.Model
      blocked: bool, default=True
      potential: Potential, optional
          An object that represents the Hamiltonian with methods `velocity`,
          `energy`, and `random` methods.
      **pytensor_kwargs: passed to PyTensor functions
      """
      self._model = modelcontext(model)
  
      if vars is None:
          vars = self._model.continuous_value_vars
      else:
          vars = get_value_vars_from_user_vars(vars, self._model)
      super().__init__(
          vars,
          blocked=blocked,
          model=self._model,
          dtype=dtype,
          **pytensor_kwargs,
      )
  
      self.adapt_step_size = adapt_step_size
      self.Emax = Emax
      self.iter_count = 0
  
      # We're using the initial/test point to determine the (initial) step
      # size.
      # XXX: If the dimensions of these terms change, the step size
      # dimension-scaling should change as well, no?
      test_point = self._model.initial_point()
  
      nuts_vars = [test_point[v.name] for v in vars]
      size = sum(v.size for v in nuts_vars)
  
      self.step_size = step_scale / (size**0.25)
      self.step_adapt = step_sizes.DualAverageAdaptation(
          self.step_size, target_accept, gamma, k, t0
      )
      self.target_accept = target_accept
      self.tune = True
  
      if scaling is None and potential is None:
          mean = floatX(np.zeros(size))
          var = floatX(np.ones(size))
          potential = QuadPotentialDiagAdapt(size, mean, var, 10)
  
      if isinstance(scaling, dict):
          point = Point(scaling, model=self._model)
          scaling = guess_scaling(point, model=self._model, vars=vars)
  
      if scaling is not None and potential is not None:
          raise ValueError(
              "Can not specify both potential and scaling."
          )
  
      if potential is not None:
          self.potential = potential
      else:
          self.potential = quad_potential(scaling, is_cov)
  
      self.integrator = integration.CpuLeapfrogIntegrator(
          self.potential, self._logp_dlogp_func
      )
  
      self._step_rand = step_rand
      self._num_divs_sample = 0
  
imports: from __future__ import annotations
  import logging
  import time
  from abc import abstractmethod
  from typing import Any, NamedTuple
  import numpy
  from pymc.blocking import DictToArrayBijection, RaveledVars, StatsType
  from pymc.exceptions import SamplingError
  from pymc.model import Point, modelcontext
  from pymc.pytensorf import floatX
  from pymc.stats.convergence import SamplerWarning, WarningType
  from pymc.step_methods import step_sizes
  from pymc.step_methods.arraystep import GradientSharedStep
  from pymc.step_methods.hmc import integration
  from pymc.step_methods.hmc.integration import IntegrationError, State
  from pymc.step_methods.hmc.quadpotential import QuadPotentialDiagAdapt, quad_potential
  from pymc.tuning import guess_scaling
  from pymc.util import get_value_vars_from_user_vars
label_desc: Use image node_id 1 to create a new BaseHMC object from inherited base classes: GradientSharedStep with example: obj = BaseHMC(vars, scaling, step_scale, is_cov, model, blocked, potential, dtype, Emax, target_accept, gamma, k, t0, adapt_step_size, step_rand)