Skip to content

Commit f059ceb

Browse files
committed
Add inits and explicit gradient test.
1 parent 48b5a7e commit f059ceb

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

cmdstanpy/model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,8 @@ def diagnose(
22212221
* "model": Gradients evaluated using autodiff.
22222222
* "finite_diff": Gradients evaluated using finite differences.
22232223
* "error": Delta between autodiff and finite difference gradients.
2224+
2225+
Gradients are evaluated in the unconstrained space.
22242226
"""
22252227

22262228
with temp_single_json(data) as _data, \
@@ -2237,7 +2239,7 @@ def diagnose(
22372239
if _data is not None:
22382240
cmd += ["data", f"file={_data}"]
22392241
if _inits is not None:
2240-
cmd.append(f"inits={_inits}")
2242+
cmd.append(f"init={_inits}")
22412243

22422244
output_dir = tempfile.mkdtemp(prefix=self.name, dir=_TMPDIR)
22432245

test/test_model.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,11 @@ def test_diagnose():
613613
"error",
614614
}
615615

616+
# Check gradients against the same value as in `log_prob`.
617+
inits = {"theta": 0.34903938392023830482}
618+
gradients = model.diagnose(data=BERN_DATA, inits=inits)
619+
np.testing.assert_allclose(gradients.model.iloc[0], -1.18847)
620+
616621
# Simulate bad gradients by using large finite difference.
617622
with pytest.raises(RuntimeError, match="may exceed the error threshold"):
618623
model.diagnose(data=BERN_DATA, epsilon=3)

0 commit comments

Comments
 (0)