NUTS

Struct NUTS 

Source
pub struct NUTS<T, B, GTarget>{ /* private fields */ }
Expand description

No-U-Turn Sampler (NUTS).

Encapsulates multiple independent Markov chains using the NUTS algorithm. Utilizes dual-averaging step size adaptation and dynamic trajectory lengths to efficiently explore complex posterior geometries. Chains are executed concurrently via Rayon, each evolving independently.

§Type Parameters

  • T: Floating-point type for numerical calculations.
  • B: Autodiff backend from the burn crate.
  • GTarget: Target distribution type implementing the GradientTarget trait.

Implementations§

Source§

impl<T, B, GTarget> NUTS<T, B, GTarget>

Source

pub fn new( target: GTarget, initial_positions: Vec<Vec<T>>, target_accept_p: T, ) -> Self

Creates a new NUTS sampler with the given target distribution and initial state for each chain.

§Parameters
  • target: The target distribution implementing GradientTarget.
  • initial_positions: A vector of initial positions for each chain, shape [n_chains, D].
  • target_accept_p: Desired average acceptance probability for the dual-averaging adaptation. Try values between 0.6 and 0.95.
§Returns

A newly initialized NUTS instance.

§Example
type B = Autodiff<NdArray>;

// Create a 2D Gaussian with mean [0,0] and identity covariance
let gauss = DiffableGaussian2D::new([0.0_f64, 0.0], [[1.0, 0.0], [0.0, 1.0]]);

// Initialize 3 chains in 2D at different starting points
let init_positions = vec![
    vec![-1.0, -1.0],
    vec![ 0.0,  0.0],
    vec![ 1.0,  1.0],
];

// Build the sampler targeting 85% acceptance probability
let sampler: NUTS<f64, B, _> = NUTS::new(gauss, init_positions, 0.85);
Source

pub fn run(&mut self, n_collect: usize, n_discard: usize) -> Tensor<B, 3>

Runs all chains for a total of n_collect + n_discard steps and collects samples.

First discards n_discard warm-up steps for each chain (during which adaptation occurs), then collects n_collect samples per chain.

§Parameters
  • n_collect: Number of samples to collect after warm-up per chain.
  • n_discard: Number of warm-up (burn-in) steps to discard per chain.
§Returns

A 3D tensor of shape [n_chains, n_collect, D] containing the collected samples.

§Example
type B = Autodiff<NdArray>;

// As above, construct the sampler
let gauss = DiffableGaussian2D::new([0.0_f32, 0.0], [[1.0,0.0],[0.0,1.0]]);
let mut sampler = NUTS::new(gauss, init::<f32>(2, 2), 0.8);

// Discard 50 warm-up steps, then collect 150 observations per chain
let sample: Tensor<B, 3> = sampler.run(150, 50);

// sample.dims() == [2 chains, 150 observations, 2 dimensions]
assert_eq!(sample.dims(), [2, 150, 2]);
Source

pub fn run_progress( &mut self, n_collect: usize, n_discard: usize, ) -> Result<(Tensor<B, 3>, RunStats), Box<dyn Error>>

Run with live progress bars and collect summary stats.

Spawns a background thread to render per-chain and global bars, then returns (samples, RunStats) when done.

§Example
use burn::backend::{Autodiff, NdArray};
use mini_mcmc::distributions::Rosenbrock2D;
use mini_mcmc::nuts::NUTS;
use mini_mcmc::core::init;

type B = Autodiff<NdArray>;

let target = Rosenbrock2D { a: 1.0, b: 100.0 };
let init   = init::<f64>(4, 2);    // 4 chains in 2D
let mut sampler = NUTS::<f64, B, Rosenbrock2D<f64>>::new(target, init, 0.9);
let (samples, stats) = sampler.run_progress(100, 20).unwrap();

You can swap in any other GradientTarget just as easily.

Source

pub fn set_seed(self, seed: u64) -> Self

Sets a new random seed for all chains to ensure reproducibility.

§Parameters
  • seed: Base seed value. Each chain will derive its own seed for independence.
§Returns

self with the RNGs re-seeded.

Trait Implementations§

Source§

impl<T, B, GTarget> Clone for NUTS<T, B, GTarget>

Source§

fn clone(&self) -> NUTS<T, B, GTarget>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, B, GTarget> Debug for NUTS<T, B, GTarget>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, B, GTarget> Freeze for NUTS<T, B, GTarget>

§

impl<T, B, GTarget> RefUnwindSafe for NUTS<T, B, GTarget>

§

impl<T, B, GTarget> Send for NUTS<T, B, GTarget>
where GTarget: Send,

§

impl<T, B, GTarget> Sync for NUTS<T, B, GTarget>

§

impl<T, B, GTarget> Unpin for NUTS<T, B, GTarget>

§

impl<T, B, GTarget> UnwindSafe for NUTS<T, B, GTarget>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V