Repository: scipy/scipy
File: scipy/stats/tests/test_stats.py (Python)

Code before:
                            expected_counts[xp.newaxis, :])).T

        table5 = np.asarray([
            # lambda, statistic
            -10.0, 72.2e3,
            -5.0, 28.9e1,
            -3.0, 65.6,
            -2.0, 40.6,
            -1.5, 34.0,
            -1.0, 29.5,
            -0.5, 26.5,
            0.0, 24.6,
            0.5, 23.4,
            0.67, 23.1,
            1.0, 22.7,
            1.5, 22.6,
            2.0, 22.9,
            3.0, 24.8,
            5.0, 35.5,
            10.0, 21.4e1,
            ])
        table5 = np.reshape(table5, (-1, 2))

        for i in range(table5.shape[0]):
            lambda_, expected_stat = float(table5[i, 0]), float(table5[i, 1])
            stat, p = stats.power_divergence(table4[:,0], table4[:,1], lambda_=lambda_)
            xp_assert_close(stat, xp.asarray(expected_stat), rtol=5e-3)


@make_xp_test_case(stats.chisquare)
class TestChisquare:
    @skip_xp_backends(eager_only=True)
    def test_chisquare_12282a(self, xp):
        # Currently `chisquare` is implemented via power_divergence
        # in case that ever changes, perform a basic test like
        # test_power_divergence_gh_12282
        with assert_raises(ValueError, match='For each axis slice...'):
            f_obs = xp.asarray([10., 20.])
            f_exp = xp.asarray([30., 60.])
            stats.chisquare(f_obs, f_exp=f_exp)

    def test_chisquare_12282b(self, xp):
        # Check that users can now disable the sum check tested in
        # test_chisquare_12282a. Also, confirm that statistic and p-value
        # are as expected.
        rng = np.random.default_rng(3843874358728234)
        n = 10
        lam = rng.uniform(1000, 2000, size=n)
        x = rng.poisson(lam)
        lam = xp.asarray(lam)
        x = xp.asarray(x, dtype=lam.dtype)

Review comment (suggestion):
```suggestion
            stat, p = stats.power_divergence(table4[:, 0], table4[:, 1], 
                                             lambda_=lambda_)
```

Code after:
        # J. R. Statist. Soc. B (1984), Vol 46, No. 3, pp. 440-464.
        # This tests the calculation for several values of lambda.

        # Table 4 data recalculated for greater precision according to:
        # Shelby J. Haberman, Analysis of Qualitative Data: Volume 1
        # Introductory Topics, Academic Press, New York, USA (1978).
        obs = xp.asarray([15., 11., 14., 17., 5., 11., 10., 4., 8.,
                          10., 7., 9., 11., 3., 6., 1., 1., 4.])
        beta = -0.083769  # Haberman (1978), p. 15
        i = xp.arange(1., obs.shape[0] + 1.)
        alpha = xp.log(xp.sum(obs) / xp.sum(xp.exp(beta*i)))
        expected_counts = xp.exp(alpha + beta*i)

        # `table4` holds just the second and third columns from Table 4.
        table4 = xp.concat((obs[xp.newaxis, :],
                            expected_counts[xp.newaxis, :])).T

        table5 = np.asarray([
            # lambda, statistic
            -10.0, 72.2e3,
            -5.0, 28.9e1,
            -3.0, 65.6,
            -2.0, 40.6,
            -1.5, 34.0,
            -1.0, 29.5,
            -0.5, 26.5,
            0.0, 24.6,
            0.5, 23.4,
            0.67, 23.1,
            1.0, 22.7,
            1.5, 22.6,
            2.0, 22.9,
            3.0, 24.8,
            5.0, 35.5,
            10.0, 21.4e1,
            ])
        table5 = np.reshape(table5, (-1, 2))

        for i in range(table5.shape[0]):
            lambda_, expected_stat = float(table5[i, 0]), float(table5[i, 1])
            stat, p = stats.power_divergence(table4[:, 0], table4[:, 1],
                                             lambda_=lambda_)
            xp_assert_close(stat, xp.asarray(expected_stat), rtol=5e-3)


@make_xp_test_case(stats.chisquare)
class TestChisquare:
    def test_chisquare_12282a(self, xp):
        # Currently `chisquare` is implemented via power_divergence
        # in case that ever changes, perform a basic test like
        # test_power_divergence_gh_12282