Skip to content

Fix error in warn_treedepth when using multiple NUTS samplers #7182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pymc/stats/convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def warn_treedepth(idata: arviz.InferenceData) -> list[SamplerWarning]:

warnings = []
for c in rmtd.chain:
if sum(rmtd.sel(chain=c)) / rmtd.sizes["draw"] > 0.05:
if (rmtd.sel(chain=c).mean("draw") > 0.05).any():
warnings.append(
SamplerWarning(
WarningType.TREEDEPTH,
Expand Down
2 changes: 1 addition & 1 deletion pymc/step_methods/hmc/nuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def _hamiltonian_step(self, start, p0, step_size):

if divergence_info or turning:
break
else:
else: # no-break
reached_max_treedepth = not self.tune

stats = tree.stats()
Expand Down
16 changes: 16 additions & 0 deletions tests/stats/test_convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ def test_warn_treedepth():
assert "Chain 1 reached the maximum tree depth" in warns[0].message


def test_warn_treedepth_multiple_samplers():
"""Check we handle cases when sampling with multiple NUTS samplers, each of which reports max_treedepth."""
max_treedepth = np.zeros((3, 2, 2), dtype=bool)
max_treedepth[0, 0, 0] = True
max_treedepth[2, 1, 1] = True
idata = arviz.from_dict(
sample_stats={
"reached_max_treedepth": max_treedepth,
}
)
warns = convergence.warn_treedepth(idata)
assert len(warns) == 2
assert "Chain 0 reached the maximum tree depth" in warns[0].message
assert "Chain 2 reached the maximum tree depth" in warns[1].message


def test_log_warning_stats(caplog):
s1 = dict(warning="Temperature too low!")
s2 = dict(warning="Temperature too high!")
Expand Down