def test_find_constrained_prior_error_too_large(distribution, lower, upper, init_guess, fixed_params): with pytest.warns(UserWarning, match="instead of the requested 95%"): pm.find_constrained_prior( distribution, lower=lower, upper=upper, mass=0.95, init_guess=init_guess, fixed_params=fixed_params, )
def test_find_constrained_prior_input_errors(): # missing param with pytest.raises(TypeError, match="required positional argument"): pm.find_constrained_prior( pm.StudentT, lower=0.1, upper=0.4, mass=0.95, init_guess={ "mu": 170, "sigma": 3 }, ) # mass too high with pytest.raises(AssertionError, match="has to be between 0.01 and 0.99"): pm.find_constrained_prior( pm.StudentT, lower=0.1, upper=0.4, mass=0.995, init_guess={ "mu": 170, "sigma": 3 }, fixed_params={"nu": 7}, ) # mass too low with pytest.raises(AssertionError, match="has to be between 0.01 and 0.99"): pm.find_constrained_prior( pm.StudentT, lower=0.1, upper=0.4, mass=0.005, init_guess={ "mu": 170, "sigma": 3 }, fixed_params={"nu": 7}, ) # non-scalar params with pytest.raises(NotImplementedError, match="does not work with non-scalar parameters yet"): pm.find_constrained_prior( pm.MvNormal, lower=0, upper=1, mass=0.95, init_guess={ "mu": 5, "cov": np.asarray([[1, 0.2], [0.2, 1]]) }, )
def test_find_constrained_prior(distribution, lower, upper, init_guess, fixed_params, mass): with pytest.warns(None) as record: opt_params = pm.find_constrained_prior( distribution, lower=lower, upper=upper, mass=mass, init_guess=init_guess, fixed_params=fixed_params, ) assert len(record) == 0 opt_distribution = distribution.dist(**opt_params) mass_in_interval = ( pm.math.exp(pm.logcdf(opt_distribution, upper)) - pm.math.exp(pm.logcdf(opt_distribution, lower))).eval() assert np.abs(mass_in_interval - mass) <= 1e-5