Skip to content
84 changes: 84 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Overview

AdvancedHMC.jl is a Julia implementation of advanced Hamiltonian Monte Carlo (HMC) algorithms. It serves as a backend for Turing.jl and can be used directly for flexible MCMC sampling.

## Common Commands

### Testing
```bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
```bash
```bash

# Run all core tests
julia --project -e 'using Pkg; Pkg.test()'

# Run specific test group
AHMC_TEST_GROUP=AdvancedHMC julia --project test/runtests.jl

# Run experimental/research tests
AHMC_TEST_GROUP=Experimental julia --project test/runtests.jl

# Run downstream Turing.jl integration tests
AHMC_TEST_GROUP=Downstream julia --project test/runtests.jl
```

Tests use ReTest.jl. Test files are in `test/` and cover: metric, hamiltonian, integrator, trajectory, adaptation, sampler, abstractmcmc, mcmcchains, constructors.

### Code Formatting
```bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
```bash
```bash

julia -e 'using JuliaFormatter; format(".")'
```
Uses Blue style (see `.JuliaFormatter.toml`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
Uses Blue style (see `.JuliaFormatter.toml`).
Uses Blue style (see `.JuliaFormatter.toml`).


## Architecture

The library uses a modular, composable design where sampling is built from independent components:

```
Sampler (NUTS/HMC/HMCDA)
Hamiltonian (metric + kinetic energy + log density)
Integrator (Leapfrog variants)
Trajectory (termination criteria + sampling method)
Adaptation (step size + mass matrix)
```

### Key Source Files

- `src/metric.jl` - Metric types: `UnitEuclideanMetric`, `DiagEuclideanMetric`, `DenseEuclideanMetric`
- `src/hamiltonian.jl` - `Hamiltonian`, `PhasePoint`, `DualValue` (caches value+gradient)
- `src/integrator.jl` - `Leapfrog`, `JitteredLeapfrog`, `TemperedLeapfrog`
- `src/trajectory.jl` - Termination criteria (`ClassicNoUTurn`, `GeneralisedNoUTurn`, `FixedNSteps`) and trajectory samplers (`EndPointTS`, `SliceTS`, `MultinomialTS`)
- `src/adaptation/` - Step size (`NesterovDualAveraging`), mass matrix (`WelfordVar`, `WelfordCov`, `NutpieVar`), composite (`StanHMCAdaptor`, `NaiveHMCAdaptor`)
- `src/constructors.jl` - Convenience constructors: `NUTS`, `HMC`, `HMCDA`
- `src/abstractmcmc.jl` - AbstractMCMC.jl interface implementation
- `src/riemannian/` - Riemannian manifold support with `GeneralizedLeapfrog`
Comment on lines +52 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
- `src/metric.jl` - Metric types: `UnitEuclideanMetric`, `DiagEuclideanMetric`, `DenseEuclideanMetric`
- `src/hamiltonian.jl` - `Hamiltonian`, `PhasePoint`, `DualValue` (caches value+gradient)
- `src/integrator.jl` - `Leapfrog`, `JitteredLeapfrog`, `TemperedLeapfrog`
- `src/trajectory.jl` - Termination criteria (`ClassicNoUTurn`, `GeneralisedNoUTurn`, `FixedNSteps`) and trajectory samplers (`EndPointTS`, `SliceTS`, `MultinomialTS`)
- `src/adaptation/` - Step size (`NesterovDualAveraging`), mass matrix (`WelfordVar`, `WelfordCov`, `NutpieVar`), composite (`StanHMCAdaptor`, `NaiveHMCAdaptor`)
- `src/constructors.jl` - Convenience constructors: `NUTS`, `HMC`, `HMCDA`
- `src/abstractmcmc.jl` - AbstractMCMC.jl interface implementation
- `src/riemannian/` - Riemannian manifold support with `GeneralizedLeapfrog`
- `src/metric.jl` - Metric types: `UnitEuclideanMetric`, `DiagEuclideanMetric`, `DenseEuclideanMetric`
- `src/hamiltonian.jl` - `Hamiltonian`, `PhasePoint`, `DualValue` (caches value+gradient)
- `src/integrator.jl` - `Leapfrog`, `JitteredLeapfrog`, `TemperedLeapfrog`
- `src/trajectory.jl` - Termination criteria (`ClassicNoUTurn`, `GeneralisedNoUTurn`, `FixedNSteps`) and trajectory samplers (`EndPointTS`, `SliceTS`, `MultinomialTS`)
- `src/adaptation/` - Step size (`NesterovDualAveraging`), mass matrix (`WelfordVar`, `WelfordCov`, `NutpieVar`), composite (`StanHMCAdaptor`, `NaiveHMCAdaptor`)
- `src/constructors.jl` - Convenience constructors: `NUTS`, `HMC`, `HMCDA`
- `src/abstractmcmc.jl` - AbstractMCMC.jl interface implementation
- `src/riemannian/` - Riemannian manifold support with `GeneralizedLeapfrog`


### Extensions (ext/)

Optional features loaded when dependencies are available:
- `AdvancedHMCOrdinaryDiffEqExt` - DiffEqIntegrator support
- `AdvancedHMCMCMCChainsExt` - MCMCChains.jl integration
- `AdvancedHMCCUDAExt` - CUDA array support
- `AdvancedHMCComponentArraysExt` - ComponentArrays support
- `AdvancedHMCADTypesExt` - ADTypes.jl integration
Comment on lines +64 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
- `AdvancedHMCOrdinaryDiffEqExt` - DiffEqIntegrator support
- `AdvancedHMCMCMCChainsExt` - MCMCChains.jl integration
- `AdvancedHMCCUDAExt` - CUDA array support
- `AdvancedHMCComponentArraysExt` - ComponentArrays support
- `AdvancedHMCADTypesExt` - ADTypes.jl integration
- `AdvancedHMCOrdinaryDiffEqExt` - DiffEqIntegrator support
- `AdvancedHMCMCMCChainsExt` - MCMCChains.jl integration
- `AdvancedHMCCUDAExt` - CUDA array support
- `AdvancedHMCComponentArraysExt` - ComponentArrays support
- `AdvancedHMCADTypesExt` - ADTypes.jl integration


## Key Interfaces

Works with:
- `LogDensityProblems.jl` - Define target distributions
- `LogDensityProblemsAD.jl` - Automatic differentiation
- `AbstractMCMC.jl` - MCMC sampling interface
Comment on lines +73 to +75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
- `LogDensityProblems.jl` - Define target distributions
- `LogDensityProblemsAD.jl` - Automatic differentiation
- `AbstractMCMC.jl` - MCMC sampling interface
- `LogDensityProblems.jl` - Define target distributions
- `LogDensityProblemsAD.jl` - Automatic differentiation
- `AbstractMCMC.jl` - MCMC sampling interface


## Code Conventions

Mathematical notation in comments:
- `ℓπ` - log density of target distribution
- `θ` - position/parameters
- `r` - momentum
- `z` - phase point (θ, r)
- `∂ℓπ∂θ` - gradient of log density
Comment on lines +80 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
- `ℓπ` - log density of target distribution
- `θ` - position/parameters
- `r` - momentum
- `z` - phase point (θ, r)
- `∂ℓπ∂θ` - gradient of log density
- `ℓπ` - log density of target distribution
- `θ` - position/parameters
- `r` - momentum
- `z` - phase point (θ, r)
- `∂ℓπ∂θ` - gradient of log density

Loading
Loading