コード例 #1
0
ファイル: test_student_t.py プロジェクト: navass11/Copulas
    def test_fit_sample(self):
        model = StudentTUnivariate()
        model.fit(self.data)

        np.testing.assert_allclose(model._params['df'], 3.0, rtol=0.1)
        np.testing.assert_allclose(model._params['loc'], 1.0, atol=0.1)
        np.testing.assert_allclose(model._params['scale'], 0.5, atol=0.1)

        sampled_data = model.sample(50)

        assert isinstance(sampled_data, np.ndarray)
        assert sampled_data.shape == (50, )
コード例 #2
0
    def test_cdf(self):
        model = StudentTUnivariate()
        model.fit(self.data)

        sampled_data = model.sample(50)

        # Test 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()
コード例 #3
0
    def test_to_dict_constant(self):
        model = StudentTUnivariate()
        model.fit(self.constant)

        params = model.to_dict()

        df = params.pop('df')
        assert np.isclose(df, 152801, atol=1)

        assert params == {
            'type': 'copulas.univariate.student_t.StudentTUnivariate',
            'loc': 5,
            'scale': 0,
        }
コード例 #4
0
    def test_to_dict_from_dict(self):
        model = StudentTUnivariate()
        model.fit(self.data)

        sampled_data = model.sample(50)

        params = model.to_dict()
        model2 = StudentTUnivariate.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))
コード例 #5
0
    def test_to_dict_from_dict_constant(self):
        model = StudentTUnivariate()
        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 = StudentTUnivariate.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))
コード例 #6
0
    def test_save_load(self):
        model = StudentTUnivariate()
        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 = StudentTUnivariate.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))