Fix #281: Fix sampler failure when n_samples = 1#489
Draft
Conversation
sample() with n_samples=1 crashed in the verbose end-of-sampling report because EBFMI() called mean() over an empty range. Guard with a length(stats) > 1 check and return NaN when undefined. Add regression test.
| average_acceptance_rate = mean(map(s -> s.acceptance_rate, stats)) | ||
| if θ isa AbstractVector | ||
| n_chains = 1 | ||
| EBFMI_est = length(stats) > 1 ? EBFMI(map(s -> s.hamiltonian_energy, stats)) : NaN |
Contributor
There was a problem hiding this comment.
[JuliaFormatter] reported by reviewdog 🐶
Suggested change
| EBFMI_est = length(stats) > 1 ? EBFMI(map(s -> s.hamiltonian_energy, stats)) : NaN | |
| EBFMI_est = | |
| length(stats) > 1 ? EBFMI(map(s -> s.hamiltonian_energy, stats)) : NaN |
| EBFMI_est = length(stats) > 1 ? EBFMI(map(s -> s.hamiltonian_energy, stats)) : NaN | ||
| else | ||
| n_chains = size(θ, 2) | ||
| EBFMI_est = length(stats) > 1 ? EBFMI(map(s -> s.hamiltonian_energy, stats)) : fill(NaN, n_chains) |
Contributor
There was a problem hiding this comment.
[JuliaFormatter] reported by reviewdog 🐶
Suggested change
| EBFMI_est = length(stats) > 1 ? EBFMI(map(s -> s.hamiltonian_energy, stats)) : fill(NaN, n_chains) | |
| EBFMI_est = if length(stats) > 1 | |
| EBFMI(map(s -> s.hamiltonian_energy, stats)) | |
| else | |
| fill(NaN, n_chains) | |
| end |
Contributor
|
AdvancedHMC.jl documentation for PR #489 is available at: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #489 +/- ##
==========================================
+ Coverage 77.71% 78.04% +0.33%
==========================================
Files 21 21
Lines 1270 1271 +1
==========================================
+ Hits 987 992 +5
+ Misses 283 279 -4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
n_samples = 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I'll check the changes before marking it as ready for review.
Human content above
Claude content below
Fix sampler failure when
n_samples = 1Problem
Calling
sample(h, κ, θ, 1)crashes in the verbose end-of-sampling report. TheEBFMIfunction (src/diagnosis.jl) computes:With 1 sample,
length(Es) = 1, so it evaluatesmean(f, 1:0)— iterating over an empty range. Julia'sStatistics.meanover an empty collection throws an error.MWE
See
281-n-samples-1.jl. Callssample(h, κ, θ, 1; verbose=true)and checks that it completes without error. Fails onmainwithArgumentError: mean of empty collectioninside the EBFMI computation.Change
2 files changed:
src/sampler.jl— moveEBFMI_estcomputation inside theif θ isa AbstractVector/elsebranches; guard withlength(stats) > 1 ? EBFMI(...) : NaNtest/sampler.jl— add@testset "n_samples=1 does not crash"that callssamplewithn_samples=1, verbose=trueand asserts the result has length 1Notes
verbose=trueby default, so this crash affects all users callingsample(h, κ, θ, 1)without explicitly passingverbose=falsen_adaptsdefaults tomin(div(1, 10), 1000) = 0, so adaptation is correctly skipped — the only broken part was the end-of-sampling logNaNis the correct sentinel for undefinedMWE
281-n-samples-1.jlmain output:
worktree output:
Fixes #281