Example #1
0
 def test_test_fit_equal_values(self):
     """If it's fit with constant data, contant_value is set."""
     instance = BetaUnivariate()
     instance.fit(np.array([5, 5, 5, 5, 5, 5]))
     assert instance.a is None
     assert instance.b is None
     assert instance.constant_value == 5
Example #2
0
    def test__fit(self):
        distribution = BetaUnivariate()

        data = beta.rvs(size=10000, a=1, b=1, loc=1, scale=1)
        distribution._fit(data)

        expected = {'loc': 1, 'scale': 1, 'a': 1, 'b': 1}
        for key, value in distribution._params.items():
            np.testing.assert_allclose(value, expected[key], atol=0.3)
Example #3
0
    def test__fit_constant(self):
        distribution = BetaUnivariate()

        distribution._fit_constant(np.array([1, 1, 1, 1]))

        assert distribution._params == {
            'a': 1,
            'b': 1,
            'loc': 1,
            'scale': 0
        }
Example #4
0
    def test__extract_constant(self):
        distribution = BetaUnivariate()
        distribution._params = {
            'loc': 1,
            'scale': 1,
            'a': 1,
            'b': 1
        }

        constant = distribution._extract_constant()

        assert 1 == constant
Example #5
0
    def test_fit_sample(self):
        model = BetaUnivariate()
        model.fit(self.data)

        np.testing.assert_allclose(model._params['loc'], 1.0, atol=0.2)
        np.testing.assert_allclose(model._params['scale'], 1.0, atol=0.2)
        np.testing.assert_allclose(model._params['a'], 1.0, atol=0.2)
        np.testing.assert_allclose(model._params['b'], 1.0, atol=0.2)

        sampled_data = model.sample(50)

        assert isinstance(sampled_data, np.ndarray)
        assert sampled_data.shape == (50, )
Example #6
0
    def test_to_dict_constant(self):
        model = BetaUnivariate()
        model.fit(self.constant)

        params = model.to_dict()

        assert params == {
            'type': 'copulas.univariate.beta.BetaUnivariate',
            'loc': 5,
            'scale': 0,
            'a': 1,
            'b': 1
        }
Example #7
0
    def test_fit(self):
        """On fit, stats from fit data are set in the model."""

        # Generate data with known parameters
        np.random.seed(42)
        a, b, loc, scale = 1.0, 1.0, 10.0, 11.0
        data = beta.rvs(a, b, loc, scale, size=100000)

        # Fit the model and check parameters
        copula = BetaUnivariate()
        copula.fit(data)
        self.assertAlmostEqual(copula.a, a, places=1)
        self.assertAlmostEqual(copula.b, b, places=1)
        self.assertAlmostEqual(copula.loc, loc, places=1)
        self.assertAlmostEqual(copula.scale, scale, places=1)
Example #8
0
 def test___init__(self):
     """On init, default values are set on instance."""
     copula = BetaUnivariate()
     assert copula.a is None
     assert copula.b is None
     assert copula.loc is None
     assert copula.scale is None
Example #9
0
    def test_save_load(self):
        model = BetaUnivariate()
        model.fit(self.data)

        sampled_data = model.sample(50)

        path_to_model = os.path.join(self.test_dir.name, "model.pkl")
        model.save(path_to_model)
        model2 = BetaUnivariate.load(path_to_model)

        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))
Example #10
0
    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))
Example #11
0
    def test_fit_sample_distribution_dict_multiple(self):
        data = sample_trivariate_xyz()
        model = GaussianMultivariate(
            distribution={
                'x': Univariate(parametric=ParametricType.PARAMETRIC),
                'y': BetaUnivariate(),
                'z': GaussianKDE()
            })
        model.fit(data)

        sampled_data = model.sample(10)
        assert sampled_data.shape == (10, 3)
Example #12
0
    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))
Example #13
0
    def test_pdf(self):
        model = BetaUnivariate()
        model.fit(self.data)

        sampled_data = model.sample(50)

        # Test PDF
        pdf = model.probability_density(sampled_data)
        assert (0 < pdf).all()
Example #14
0
    def test_cdf(self):
        model = BetaUnivariate()
        model.fit(self.data)

        sampled_data = model.sample(50)

        # Test the CDF
        cdf = model.cumulative_distribution(sampled_data)
        assert (0 <= cdf).all() and (cdf <= 1).all()

        # Test CDF increasing function
        sorted_data = sorted(sampled_data)
        cdf = model.cumulative_distribution(sorted_data)
        assert (np.diff(cdf) >= 0).all()
Example #15
0
    def test_fit_sample_constant(self):
        model = BetaUnivariate()
        model.fit(self.constant)

        sampled_data = model.sample(50)

        assert isinstance(sampled_data, np.ndarray)
        assert sampled_data.shape == (50, )

        assert model._constant_value == 5
        np.testing.assert_equal(np.full(50, 5), model.sample(50))
Example #16
0
    def test__is_constant_false(self):
        distribution = BetaUnivariate()

        distribution.fit(np.array([1, 2, 3, 4]))

        assert not distribution._is_constant()
Example #17
0
    def test__is_constant_true(self):
        distribution = BetaUnivariate()

        distribution.fit(np.array([1, 1, 1, 1]))

        assert distribution._is_constant()
Example #18
0
 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()
Example #19
0
 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()