TuringLang / AdvancedHMC.jl

Robust, modular and efficient implementation of advanced Hamiltonian Monte Carlo algorithms
https://turinglang.org/AdvancedHMC.jl/
MIT License
228 stars 39 forks source link

Accessing samples? #365

Open rkube opened 5 months ago

rkube commented 5 months ago

Hi, Going through the documentation I'm wondering how to access the samples from HMC. In the first example the samples are just a vector:

julia> samples, stats = sample(hamiltonian, kernel, initial_θ, n_samples, adaptor, n_adapts; progress=true)
julia> samples
100000-element Vector{Vector{Float64}}:

In the AbstractMCMC example the samples are a long vector of some complicated type and I had to dig around to get the actual samples:

julia> samples = AbstractMCMC.sample(
      model,
      sampler,
      n_adapts + n_samples;
      nadapts = n_adapts,
      initial_params = initial_θ,
  )
julia> samples
julia> samples
3000-element Vector{AdvancedHMC.Transition{AdvancedHMC.PhasePoint{Vector{Float64}, AdvancedHMC.DualValue{Float64, Vector{Float64}}}, @NamedTuple{n_steps::Int64, is_accept::Bool, acceptance_rate::Float64, log_density::Float64, hamiltonian_energy::Float64, hamiltonian_energy_error::Float64, max_hamiltonian_energy_error::Float64, tree_depth::Int64, numerical_error::Bool, step_size::Float64, nom_step_size::Float64, is_adapt::Bool}}}:
[...]
julia>reduce(vcat, [s.z.θ for s ∈ samples])
3000-element Vector{Float64}:
 -0.21417892820474493
[...]

Am I missing something or is this the correct way of getting to the samples?