code: def run_adaptive_nuts_on_target(
      target: gym.targets.Model,
      init_step_size: jnp.ndarray,
      num_adaptation_steps: int,
      num_results: int,
      seed: jax.Array,
      num_mala_steps: int = 100,
      rvar_smoothing: int = 0,
      num_chains: Optional[int] = None,
      init_x: Optional[jnp.ndarray] = None,
      ground_truth: Optional[Dict[str, Dict[str, jnp.ndarray]]] = None,
      save_warmup: bool = True,
      **anuts_kwargs: Any,
  ) -> Tuple[Dict[str, Any], Dict[str, Any]]:
      """Does a run of adaptive MCMC.
  
      Args:
        target: Target model.
        init_step_size: Initial scalar step size.
        num_adaptation_steps: Number of adaptation steps.
        num_results: Number of post-adaptation results.
        seed: Random seed.
        num_mala_steps: Number of initial steps to be MALA.
        rvar_smoothing: Smoothing points.
        num_chains: Number of chains. Optional if `init_x` is specified.
        init_x: Initial constrained chain position. Optional if `num_chains` is
          specified.
        ground_truth: Ground truth means.
        save_warmup: Whether to save the warmup iterations.
        **anuts_kwargs: Extra kwargs to pass to Adaptive MCMC.
  
      Returns:
         A tuple of final and traced results.
      """
      if init_x is None:
          init_x = get_init_x(target, num_chains)
      init_z = target.default_event_space_bijector.inverse(init_x)
  
      def target_log_prob_fn(x):
          return target.unnormalized_log_prob(x), ()
  
      target_log_prob_fn = fun_mc.transform_log_prob_fn(
          target_log_prob_fn, target.default_event_space_bijector
      )
  
      def kernel(anuts_state, seed):
          seed, mcmc_seed = jax.random.split(seed)
  
          anuts_state, anuts_extra = adaptive_nuts_step(
              anuts_state,
              target_log_prob_fn=target_log_prob_fn,
              num_mala_steps=num_mala_steps,
              num_adaptation_steps=num_adaptation_steps,
              seed=mcmc_seed,
              rvar_smoothing=rvar_smoothing,
              **anuts_kwargs,
          )
          traced = {
              "state": anuts_state.nuts_state[0],
              "log_accept_ratio": anuts_extra.log_accept_ratio,
              "scalar_step_size": anuts_extra.scalar_step_size,
              "vector_step_size": anuts_extra.vector_step_size,
              "num_integrator_steps": anuts_extra.num_integrator_steps,
              "mean_num_integrator_steps": anuts_extra.mean_num_integrator_steps,
              "extra": anuts_extra.extra,
          }
  
          return (anuts_state, seed), traced
  
      anuts_state = adaptive_nuts_init(
          init_z,
          target_log_prob_fn,
          init_step_size=init_step_size,
          rvar_smoothing=rvar_smoothing,
      )
  
      if save_warmup:
          _, trace = fun_mc.trace(
              (anuts_state, seed),
              kernel,
              num_results + num_adaptation_steps,
          )
      else:
          state, _ = fun_mc.trace(
              (anuts_state, seed),
              kernel,
              num_adaptation_steps,
              trace_fn=lambda *_: (),
          )
          _, trace = fun_mc.trace(state, kernel, num_results)
  
      warmed_up_steps = int(num_results * 0.8)
      warmed_up_state = trace["state"][-warmed_up_steps:]
  
      if ground_truth is None:
          cov = jnp.cov(
              warmed_up_state.reshape([-1, warmed_up_state.shape[-1]]),
              rowvar=False,
          )
          principal = jnp.linalg.eigh(cov)[1][:, -1]
          mean = warmed_up_state.mean((0, 1))
          bias = {}
      else:
          principal = ground_truth["principal"]
          mean = ground_truth["stats"]["m1"]["mean"]
          if save_warmup:
              bias = {
                  "bias": compute_bias(
                      trace["state"], ground_truth, mean, principal
                  )  # pytype: disable=wrong-arg-types  # jax-ndarray
              }
          else:
              bias = {}
  
      final = {
          "stats": compute_stats(
              state=warmed_up_state,
              num_grads=trace["num_integrator_steps"][
                  -warmed_up_steps:
              ].sum(),
              mean=mean,
              principal=principal,
          ),
          "final_x": target.default_event_space_bijector(
              trace["state"][-1]
          ),
          **bias,
      }
      return trace, final
  
imports: import functools
  import os
  from typing import Any, Callable, Dict, Optional, Mapping, NamedTuple, Tuple, Union
  from etils import epath
  import gin
  import immutabledict
  import jax
  import jax.numpy
  import numpy
  from discussion.adaptive_malt import utils
  from fun_mc import using_jax
  from inference_gym import using_jax
  import tensorflow_probability.substrates.jax
label_desc: Use image node_id 18 for calling a global function with example usage: run_adaptive_nuts_on_target(target, init_step_size, num_adaptation_steps, num_results, seed, num_mala_steps, rvar_smoothing, num_chains, init_x, ground_truth, save_warmup) and returns: trace,  final, target,  , anuts_state,  seed,  traced