Skip to content

Fix warn_treedepth looking at the wrong stat #6591

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 2 commits into from
Mar 12, 2023
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
10 changes: 5 additions & 5 deletions pymc/stats/convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,17 @@ def warn_treedepth(idata: arviz.InferenceData) -> List[SamplerWarning]:
if sampler_stats is None:
return []

treedepth = sampler_stats.get("tree_depth", None)
if treedepth is None:
rmtd = sampler_stats.get("reached_max_treedepth", None)
if rmtd is None:
return []

warnings = []
for c in treedepth.chain:
if sum(treedepth.sel(chain=c)) / treedepth.sizes["draw"] > 0.05:
for c in rmtd.chain:
if sum(rmtd.sel(chain=c)) / rmtd.sizes["draw"] > 0.05:
warnings.append(
SamplerWarning(
WarningType.TREEDEPTH,
f"Chain {c} reached the maximum tree depth."
f"Chain {int(c)} reached the maximum tree depth."
" Increase `max_treedepth`, increase `target_accept` or reparameterize.",
"warn",
)
Expand Down
11 changes: 11 additions & 0 deletions tests/stats/test_convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ def test_warn_divergences():
assert "2 divergences after tuning" in warns[0].message


def test_warn_treedepth():
idata = arviz.from_dict(
sample_stats={
"reached_max_treedepth": np.array([[0, 0, 0], [0, 1, 0]]).astype(bool),
}
)
warns = convergence.warn_treedepth(idata)
assert len(warns) == 1
assert "Chain 1 reached the maximum tree depth" in warns[0].message


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