Skip to content

Commit 5d60161

Browse files
authored
Merge pull request #855 from stan-dev/fix/optimize-tests-2.39
Fix optimization tests for new cmdstan
2 parents 39f4cbc + f08c698 commit 5d60161

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

test/test_generate_quantities.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ def test_from_optimization() -> None:
565565
assert os.path.exists(csv_file)
566566

567567
assert bern_gqs.draws().shape == (1, 1, 10)
568-
assert bern_gqs.draws(inc_sample=True).shape == (1, 1, 12)
568+
assert bern_gqs.draws(inc_sample=True).shape == (1, 1, 13)
569569

570570
# draws_pd()
571571
assert bern_gqs.draws_pd().shape == (1, 13)
@@ -611,18 +611,18 @@ def test_opt_save_iterations(caplog: pytest.LogCaptureFixture) -> None:
611611
assert bern_gqs.draws(inc_warmup=True, inc_sample=True).shape == (
612612
iters,
613613
1,
614-
12,
614+
13,
615615
)
616616

617617
assert bern_gqs.draws(concat_chains=True).shape == (1, 10)
618-
assert bern_gqs.draws(concat_chains=True, inc_sample=True).shape == (1, 12)
618+
assert bern_gqs.draws(concat_chains=True, inc_sample=True).shape == (1, 13)
619619
assert bern_gqs.draws(concat_chains=True, inc_warmup=True).shape == (
620620
iters,
621621
10,
622622
)
623623
assert bern_gqs.draws(
624624
concat_chains=True, inc_warmup=True, inc_sample=True
625-
).shape == (iters, 12)
625+
).shape == (iters, 13)
626626

627627
# stan_variable
628628
theta = bern_gqs.stan_variable(var='theta')

test/test_optimize.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ def test_rosenbrock(caplog: pytest.LogCaptureFixture) -> None:
9595
assert 'CmdStanMLE: model=rosenbrock' in repr(mle)
9696
assert 'method=optimize' in repr(mle)
9797
assert mle.converged
98-
assert mle.column_names == ('lp__', 'x', 'y')
98+
assert mle.column_names == ('lp__', 'converged__', 'x', 'y')
9999
np.testing.assert_almost_equal(mle.stan_variable('x'), 1, decimal=3)
100100
np.testing.assert_almost_equal(mle.stan_variable('y'), 1, decimal=3)
101101
np.testing.assert_almost_equal(
102102
mle.optimized_params_pd['x'][0], 1, decimal=3
103103
)
104-
np.testing.assert_almost_equal(mle.optimized_params_np[1], 1, decimal=3)
104+
np.testing.assert_almost_equal(mle.optimized_params_np[2], 1, decimal=3)
105105
np.testing.assert_almost_equal(mle.optimized_params_dict['x'], 1, decimal=3)
106106
with caplog.at_level(logging.WARNING):
107107
assert mle.optimized_iterations_np is None
@@ -131,8 +131,8 @@ def test_rosenbrock(caplog: pytest.LogCaptureFixture) -> None:
131131
np.testing.assert_almost_equal(mle.stan_variable('x'), 1, decimal=3)
132132
np.testing.assert_almost_equal(mle.stan_variable('y'), 1, decimal=3)
133133

134-
assert mle.optimized_params_np.shape == (3,)
135-
np.testing.assert_almost_equal(mle.optimized_params_np[1], 1, decimal=3)
134+
assert mle.optimized_params_np.shape == (4,)
135+
np.testing.assert_almost_equal(mle.optimized_params_np[2], 1, decimal=3)
136136
np.testing.assert_almost_equal(
137137
mle.optimized_params_pd['x'][0], 1, decimal=3
138138
)
@@ -143,7 +143,7 @@ def test_rosenbrock(caplog: pytest.LogCaptureFixture) -> None:
143143
mle.optimized_iterations_np[0, 1]
144144
!= mle.optimized_iterations_np[last_iter, 1]
145145
)
146-
for i in range(3):
146+
for i in range(4):
147147
assert (
148148
mle.optimized_params_np[i]
149149
== mle.optimized_iterations_np[last_iter, i]
@@ -162,7 +162,7 @@ def test_eight_schools(caplog: pytest.LogCaptureFixture) -> None:
162162
assert 'method=optimize' in repr(mle)
163163
assert not mle.converged
164164
with caplog.at_level(logging.WARNING):
165-
assert mle.optimized_params_pd.shape == (1, 11)
165+
assert mle.optimized_params_pd.shape == (1, 12)
166166
check_present(
167167
caplog,
168168
(
@@ -294,15 +294,15 @@ def test_optimize_good() -> None:
294294
# test numpy output
295295
assert isinstance(mle.optimized_params_np, np.ndarray)
296296
np.testing.assert_almost_equal(mle.optimized_params_np[0], -5, decimal=2)
297-
np.testing.assert_almost_equal(mle.optimized_params_np[1], 0.2, decimal=3)
297+
np.testing.assert_almost_equal(mle.optimized_params_np[2], 0.2, decimal=3)
298298

299299
# test pandas output
300300
assert mle.optimized_params_np[0] == mle.optimized_params_pd['lp__'][0]
301-
assert mle.optimized_params_np[1] == mle.optimized_params_pd['theta'][0]
301+
assert mle.optimized_params_np[2] == mle.optimized_params_pd['theta'][0]
302302

303303
# test dict output
304304
assert mle.optimized_params_np[0] == mle.optimized_params_dict['lp__']
305-
assert mle.optimized_params_np[1] == mle.optimized_params_dict['theta']
305+
assert mle.optimized_params_np[2] == mle.optimized_params_dict['theta']
306306

307307

308308
def test_negative_parameter_values() -> None:
@@ -530,14 +530,14 @@ def test_optimize_good_dict() -> None:
530530
)
531531
# test numpy output
532532
np.testing.assert_almost_equal(mle.optimized_params_np[0], -5, decimal=2)
533-
np.testing.assert_almost_equal(mle.optimized_params_np[1], 0.2, decimal=3)
533+
np.testing.assert_almost_equal(mle.optimized_params_np[2], 0.2, decimal=3)
534534

535535

536536
def test_optimize_rosenbrock() -> None:
537537
stan = os.path.join(DATAFILES_PATH, 'optimize', 'rosenbrock.stan')
538538
rose_model = CmdStanModel(stan_file=stan)
539539
mle = rose_model.optimize(seed=1239812093, inits=None, algorithm='BFGS')
540-
assert mle.column_names == ('lp__', 'x', 'y')
540+
assert mle.column_names == ('lp__', 'converged__', 'x', 'y')
541541
np.testing.assert_almost_equal(mle.optimized_params_dict['x'], 1, decimal=3)
542542
np.testing.assert_almost_equal(mle.optimized_params_dict['y'], 1, decimal=3)
543543

@@ -546,7 +546,7 @@ def test_optimize_no_data() -> None:
546546
stan = os.path.join(DATAFILES_PATH, 'optimize', 'no_data.stan')
547547
rose_model = CmdStanModel(stan_file=stan)
548548
mle = rose_model.optimize(seed=1239812093)
549-
assert mle.column_names == ('lp__', 'a')
549+
assert mle.column_names == ('lp__', 'converged__', 'a')
550550
np.testing.assert_almost_equal(mle.optimized_params_dict['a'], 0, decimal=3)
551551

552552

@@ -599,7 +599,7 @@ def test_exe_only() -> None:
599599
jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
600600
mle = bern2_model.optimize(data=jdata)
601601
assert mle.optimized_params_np[0] == mle.optimized_params_dict['lp__']
602-
assert mle.optimized_params_np[1] == mle.optimized_params_dict['theta']
602+
assert mle.optimized_params_np[2] == mle.optimized_params_dict['theta']
603603

604604

605605
def test_complex_output() -> None:

0 commit comments

Comments
 (0)