def test_chi2_moments(self):
        # construct the expansion for \chi^2
        N, df = 6, 15
        cum = [_chi2_cumulant(n+1, df) for n in range(N)]
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", RuntimeWarning)
            ne = ExpandedNormal(cum, name='edgw_chi2')

        # compare the moments
        assert_allclose([_chi2_moment(n, df) for n in range(N)],
                        [ne.moment(n) for n in range(N)])

        # compare the pdf [fragile!]
        # this one is actually not a very good test: there is, strictly
        # speaking, no guarantee that the pdfs match point-by-point
        # m, s = df, np.sqrt(df)
        # x = np.linspace(m - s, m + s, 10)
        # assert_allclose(ne.pdf(x), stats.chi2.pdf(x, df),
        #        atol=1e-4, rtol=1e-5)

        # pdf-cdf roundtrip
        check_pdf(ne, arg=(), msg='')

        # cdf-ppf roundtrip
        check_cdf_ppf(ne, arg=(), msg='')

        # cdf + sf == 1
        check_cdf_sf(ne, arg=(), msg='')

        # generate rvs & run a KS test
        np.random.seed(765456)
        rvs = ne.rvs(size=500)
        check_distribution_rvs(ne, args=(), alpha=0.01, rvs=rvs)
    def test_chi2_moments(self):
        # construct the expansion for \chi^2
        N, df = 6, 15
        cum = [_chi2_cumulant(n + 1, df) for n in range(N)]
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", RuntimeWarning)
            ne = ExpandedNormal(cum, name='edgw_chi2')

        # compare the moments
        assert_allclose([_chi2_moment(n, df) for n in range(N)],
                        [ne.moment(n) for n in range(N)])

        # compare the pdf [fragile!]
        # this one is actually not a very good test: there is, strictly
        # speaking, no guarantee that the pdfs match point-by-point
        # m, s = df, np.sqrt(df)
        # x = np.linspace(m - s, m + s, 10)
        # assert_allclose(ne.pdf(x), stats.chi2.pdf(x, df),
        #        atol=1e-4, rtol=1e-5)

        # pdf-cdf roundtrip
        check_pdf(ne, arg=(), msg='')

        # cdf-ppf roundtrip
        check_cdf_ppf(ne, arg=(), msg='')

        # cdf + sf == 1
        check_cdf_sf(ne, arg=(), msg='')

        # generate rvs & run a KS test
        np.random.seed(765456)
        rvs = ne.rvs(size=500)
        check_distribution_rvs(ne, args=(), alpha=0.01, rvs=rvs)
    def test_coefficients(self):
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', UserWarning)
            # 3rd order in n**(1/2)
            ne3 = ExpandedNormal([0., 1., 1.])
            assert_allclose(ne3._coef, [1., 0., 0., 1./6])

            # 4th order in n**(1/2)
            ne4 = ExpandedNormal([0., 1., 1., 1.])
            assert_allclose(ne4._coef, [1., 0., 0., 1./6, 1./24, 0., 1./72])

            # 5th order
            ne5 = ExpandedNormal([0., 1., 1., 1., 1.])
            assert_allclose(ne5._coef, [1., 0., 0., 1./6, 1./24, 1./120,
                    1./72, 1./144, 0., 1./1296])

            # adding trailing zeroes increases the order
            ne33 = ExpandedNormal([0., 1., 1., 0.])
            assert_allclose(ne33._coef, [1., 0., 0., 1./6, 0., 0., 1./72])
 def test_normal(self):
     # with two cumulants, it's just a gaussian
     ne2 = ExpandedNormal([3, 4])
     x = np.linspace(-2., 2., 100)
     assert_allclose(ne2.pdf(x), stats.norm.pdf(x, loc=3, scale=2))
 def test_pdf_no_roots(self):
     with warnings.catch_warnings():
         warnings.simplefilter("error", RuntimeWarning)
         ne = ExpandedNormal([0, 1])
         ne = ExpandedNormal([0, 1, 0.1, 0.1])
 def test_normal(self):
     # with two cumulants, it's just a gaussian
     ne2 = ExpandedNormal([3, 4])
     x = np.linspace(-2., 2., 100)
     assert_allclose(ne2.pdf(x), stats.norm.pdf(x, loc=3, scale=2))