From f059ceb34ee9d266c27c471a2fa982c4d7ebf60e Mon Sep 17 00:00:00 2001 From: Till Hoffmann Date: Fri, 9 Feb 2024 18:34:14 -0500 Subject: [PATCH] Add `inits` and explicit gradient test. --- cmdstanpy/model.py | 4 +++- test/test_model.py | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cmdstanpy/model.py b/cmdstanpy/model.py index 3a2286b2..62e1e48b 100644 --- a/cmdstanpy/model.py +++ b/cmdstanpy/model.py @@ -2221,6 +2221,8 @@ def diagnose( * "model": Gradients evaluated using autodiff. * "finite_diff": Gradients evaluated using finite differences. * "error": Delta between autodiff and finite difference gradients. + + Gradients are evaluated in the unconstrained space. """ with temp_single_json(data) as _data, \ @@ -2237,7 +2239,7 @@ def diagnose( if _data is not None: cmd += ["data", f"file={_data}"] if _inits is not None: - cmd.append(f"inits={_inits}") + cmd.append(f"init={_inits}") output_dir = tempfile.mkdtemp(prefix=self.name, dir=_TMPDIR) diff --git a/test/test_model.py b/test/test_model.py index 2b589c95..9eadb036 100644 --- a/test/test_model.py +++ b/test/test_model.py @@ -613,6 +613,11 @@ def test_diagnose(): "error", } + # Check gradients against the same value as in `log_prob`. + inits = {"theta": 0.34903938392023830482} + gradients = model.diagnose(data=BERN_DATA, inits=inits) + np.testing.assert_allclose(gradients.model.iloc[0], -1.18847) + # Simulate bad gradients by using large finite difference. with pytest.raises(RuntimeError, match="may exceed the error threshold"): model.diagnose(data=BERN_DATA, epsilon=3)