def test_to_dict_from_dict(self): model = BetaUnivariate() model.fit(self.data) sampled_data = model.sample(50) params = model.to_dict() model2 = BetaUnivariate.from_dict(params) pdf = model.probability_density(sampled_data) pdf2 = model2.probability_density(sampled_data) assert np.all(np.isclose(pdf, pdf2, atol=0.01)) cdf = model.cumulative_distribution(sampled_data) cdf2 = model2.cumulative_distribution(sampled_data) assert np.all(np.isclose(cdf, cdf2, atol=0.01))
def test_to_dict_from_dict_constant(self): model = BetaUnivariate() model.fit(self.constant) sampled_data = model.sample(50) pdf = model.probability_density(sampled_data) cdf = model.cumulative_distribution(sampled_data) params = model.to_dict() model2 = BetaUnivariate.from_dict(params) np.testing.assert_equal(np.full(50, 5), sampled_data) np.testing.assert_equal(np.full(50, 5), model2.sample(50)) np.testing.assert_equal(np.full(50, 1), pdf) np.testing.assert_equal(np.full(50, 1), model2.probability_density(sampled_data)) np.testing.assert_equal(np.full(50, 1), cdf) np.testing.assert_equal(np.full(50, 1), model2.cumulative_distribution(sampled_data))
def test_valid_serialization_fit_model(self): """For a fitted model to_dict and from_dict are opposites.""" instance = BetaUnivariate() instance.fit(np.array([1, 2, 3, 2, 1])) result = BetaUnivariate.from_dict(instance.to_dict()) assert instance.to_dict() == result.to_dict()
def test_valid_serialization_unfit_model(self): """For a unfitted model to_dict and from_dict are opposites.""" instance = BetaUnivariate() result = BetaUnivariate.from_dict(instance.to_dict()) assert instance.to_dict() == result.to_dict()