bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / stats / _stats_py / chisquare
function
scipy.stats._stats_py:chisquare
source: build-install/usr/lib/python3.14/site-packages/scipy/stats/_stats_py.py:7337
Signature
def chisquare(f_obs, f_exp = None, ddof = 0, axis = 0, *, sum_check = True, nan_policy = propagate, keepdims = False)Summary
Perform Pearson's chi-squared test.
Extended Summary
Pearson's chi-squared test [1] is a goodness-of-fit test for a multinomial distribution with given probabilities; that is, it assesses the null hypothesis that the observed frequencies (counts) are obtained by independent sampling of N observations from a categorical distribution with given expected frequencies.
Parameters
f_obs: array_likeObserved frequencies in each category.
f_exp: array_like, optionalExpected frequencies in each category. By default, the categories are assumed to be equally likely.
ddof: int, optional"Delta degrees of freedom": adjustment to the degrees of freedom for the p-value. The p-value is computed using a chi-squared distribution with
k - 1 - ddofdegrees of freedom, wherekis the number of categories. The default value ofddofis 0.axis: int or None, default: 0If an int, the axis of the input along which to compute the statistic. The statistic of each axis-slice (e.g. row) of the input will appear in a corresponding element of the output. If
None, the input will be raveled before computing the statistic.sum_check: bool, optionalWhether to perform a check that
sum(f_obs) - sum(f_exp) == 0. If True, (default) raise an error (or, for lazy backends, return NaN) when the relative difference exceeds the square root of the precision of the data type. See Notes for rationale and possible exceptions.nan_policy: {'propagate', 'omit', 'raise'}Defines how to handle input NaNs.
propagate: if a NaN is present in the axis slice (e.g. row) along which the statistic is computed, the corresponding entry of the output will be NaN.omit: NaNs will be omitted when performing the calculation. If insufficient data remains in the axis slice along which the statistic is computed, the corresponding entry of the output will be NaN.raise: if a NaN is present, aValueErrorwill be raised.
nan_policy='raise'andnan_policy='omit'are not supported by lazy backends, andnan_policy='omit'is not compatible with MArray input. For multidimensional input,nan_policy='omit'is supported only by the NumPy backend. Instead, mask NaN values using MArray; and use the defaultnan_policy. See Notes for support information.keepdims: bool, default: FalseIf this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
Returns
: res: Power_divergenceResultAn object containing attributes:
statistic
The chi-squared test statistic. The value is a float if
axisis None orf_obsandf_expare 1-D.pvalue
The p-value of the test. The value is a float if
ddofand the result attributestatisticare scalars.
Notes
This test is invalid when the observed or expected frequencies in each category are too small. A typical rule is that all of the observed and expected frequencies should be at least 5. According to [2], the total number of observations is recommended to be greater than 13, otherwise exact tests (such as Barnard's Exact test) should be used because they do not overreject.
The default degrees of freedom, k-1, are for the case when no parameters of the distribution are estimated. If p parameters are estimated by efficient maximum likelihood then the correct degrees of freedom are k-1-p. If the parameters are estimated in a different way, then the dof can be between k-1-p and k-1. However, it is also possible that the asymptotic distribution is not chi-square, in which case this test is not appropriate.
For Pearson's chi-squared test, the total observed and expected counts must match for the p-value to accurately reflect the probability of observing such an extreme value of the statistic under the null hypothesis. This function may be used to perform other statistical tests that do not require the total counts to be equal. For instance, to test the null hypothesis that f_obs[i] is Poisson-distributed with expectation f_exp[i], set ddof=-1 and sum_check=False. This test follows from the fact that a Poisson random variable with mean and variance f_exp[i] is approximately normal with the same mean and variance; the chi-squared statistic standardizes, squares, and sums the observations; and the sum of n squared standard normal variables follows the chi-squared distribution with n degrees of freedom.
Beginning in SciPy 1.9, np.matrix inputs (not recommended for new code) are converted to np.ndarray before the calculation is performed. In this case, the output will be a scalar or np.ndarray of appropriate shape rather than a 2D np.matrix. Similarly, while masked elements of masked arrays are ignored, the output will be a scalar or np.ndarray rather than a masked array with mask=False.
Examples
When only the mandatory `f_obs` argument is given, it is assumed that the expected frequencies are uniform and given by the mean of the observed frequencies:import numpy as np from scipy.stats import chisquare✓
chisquare([16, 18, 16, 14, 12, 12])
✗Power_divergenceResult(statistic=np.float64(2.0), pvalue=np.float64(0.8491450360846096))The optional `f_exp` argument gives the expected frequencies.
chisquare([16, 18, 16, 14, 12, 12], f_exp=[16, 16, 16, 16, 16, 8])
✗Power_divergenceResult(statistic=np.float64(3.5), pvalue=np.float64(0.6233876277495822))When `f_obs` is 2-D, by default the test is applied to each column.
obs = np.array([[16, 18, 16, 14, 12, 12], [32, 24, 16, 28, 20, 24]]).T obs.shape✓
(6, 2)
chisquare(obs)
✗Power_divergenceResult(statistic=array([2. , 6.66667]), pvalue=array([0.84915, 0.24663]))By setting ``axis=None``, the test is applied to all data in the array, which is equivalent to applying the test to the flattened array.
chisquare(obs, axis=None) chisquare(obs.ravel())✗
Power_divergenceResult(statistic=np.float64(23.310344827586206), pvalue=np.float64(0.01597569253412758))`ddof` is the change to make to the default degrees of freedom.
chisquare([16, 18, 16, 14, 12, 12], ddof=1)
✗Power_divergenceResult(statistic=np.float64(2.0), pvalue=np.float64(0.7357588823428847))The calculation of the p-values is done by broadcasting the chi-squared statistic with `ddof`.
chisquare([16, 18, 16, 14, 12, 12], ddof=[0, 1, 2])
✗Power_divergenceResult(statistic=np.float64(2.0), pvalue=array([0.84915, 0.73576, 0.57241]))`f_obs` and `f_exp` are also broadcast. In the following, `f_obs` has shape (6,) and `f_exp` has shape (2, 6), so the result of broadcasting `f_obs` and `f_exp` has shape (2, 6). To compute the desired chi-squared statistics, we use ``axis=1``:
chisquare([16, 18, 16, 14, 12, 12], f_exp=[[16, 16, 16, 16, 16, 8], [8, 20, 20, 16, 12, 12]], axis=1)✗
Power_divergenceResult(statistic=array([3.5 , 9.25]), pvalue=array([0.62339, 0.0995 ]))For a more detailed example, see :ref:`hypothesis_chisquare`.
See also
- hypothesis_chisquare
Extended example
- scipy.stats.barnard_exact
An unconditional exact test. An alternative to chi-squared test for small sample sizes.
- scipy.stats.fisher_exact
Fisher exact test on a 2x2 contingency table.
- scipy.stats.power_divergence
Aliases
scipy.stats.chisquare