{ } Raw JSON

bundles / scipy latest / scipy / stats / _hypotests / barnard_exact

function

scipy.stats._hypotests:barnard_exact

source: /scipy/stats/_hypotests.py :987

Signature

def   barnard_exact ( table alternative = two-sided pooled = True n = 32 )

Summary

Perform a Barnard exact test on a 2x2 contingency table.

Parameters

table : array_like of ints

A 2x2 contingency table. Elements should be non-negative integers.

alternative : {'two-sided', 'less', 'greater'}, optional

Defines the null and alternative hypotheses. Default is 'two-sided'. Please see explanations in the Notes section below.

pooled : bool, optional

Whether to compute score statistic with pooled variance (as in Student's t-test, for example) or unpooled variance (as in Welch's t-test). Default is True.

n : int, optional

Number of sampling points used in the construction of the sampling method. Note that this argument will automatically be converted to the next higher power of 2 since scipy.stats.qmc.Sobol is used to select sample points. Default is 32. Must be positive. In most cases, 32 points is enough to reach good precision. More points comes at performance cost.

Returns

ber : BarnardExactResult

A result object with the following attributes.

statistic

statistic

pvalue

pvalue

Notes

Barnard's test is an exact test used in the analysis of contingency tables. It examines the association of two categorical variables, and is a more powerful alternative than Fisher's exact test for 2x2 contingency tables.

Let's define a 2x2 matrix representing the observed sample, where each column stores the binomial experiment, as in the example below. Let's also define the theoretical binomial probabilities for and . When using Barnard exact test, we can assert three different null hypotheses :

  • versus , with alternative = "less"

  • versus , with alternative = "greater"

  • versus , with alternative = "two-sided" (default one)

In order to compute Barnard's exact test, we are using the Wald statistic [3] with pooled or unpooled variance. Under the default assumption that both variances are equal (pooled = True), the statistic is computed as:

with and the estimator of and , the latter being the combined probability, given the assumption that .

If this assumption is invalid (pooled = False), the statistic is:

The p-value is then computed as:

where the sum is over all 2x2 contingency tables such that: * when alternative = "less", * when alternative = "greater", or * when alternative = "two-sided". Above, are the sum of the columns 1 and 2, and the total (sum of the 4 sample's element).

The returned p-value is the maximum p-value taken over the nuisance parameter , where .

This function's complexity is , where n is the number of sample points.

Array API Standard Support

barnard_exact has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

====================  ====================  ====================
Library               CPU                   GPU
====================  ====================  ====================
NumPy                 ✅                     n/a                 
CuPy                  n/a                   ⛔                   
PyTorch               ⛔                     ⛔                   
JAX                   ⛔                     ⛔                   
Dask                  ⛔                     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

An example use of Barnard's test is presented in [2]_. Consider the following example of a vaccine efficacy study (Chan, 1998). In a randomized clinical trial of 30 subjects, 15 were inoculated with a recombinant DNA influenza vaccine and the 15 were inoculated with a placebo. Twelve of the 15 subjects in the placebo group (80%) eventually became infected with influenza whereas for the vaccine group, only 7 of the 15 subjects (47%) became infected. The data are tabulated as a 2 x 2 table:: Vaccine Placebo Yes 7 12 No 8 3 When working with statistical hypothesis testing, we usually use a threshold probability or significance level upon which we decide to reject the null hypothesis :math:`H_0`. Suppose we choose the common significance level of 5%. Our alternative hypothesis is that the vaccine will lower the chance of becoming infected with the virus; that is, the probability :math:`p_1` of catching the virus with the vaccine will be *less than* the probability :math:`p_2` of catching the virus without the vaccine. Therefore, we call `barnard_exact` with the ``alternative="less"`` option:
import scipy.stats as stats
res = stats.barnard_exact([[7, 12], [8, 3]], alternative="less")
res.statistic
res.pvalue
Under the null hypothesis that the vaccine will not lower the chance of becoming infected, the probability of obtaining test results at least as extreme as the observed data is approximately 3.4%. Since this p-value is less than our chosen significance level, we have evidence to reject :math:`H_0` in favor of the alternative. Suppose we had used Fisher's exact test instead:
_, pvalue = stats.fisher_exact([[7, 12], [8, 3]], alternative="less")
pvalue
With the same threshold significance of 5%, we would not have been able to reject the null hypothesis in favor of the alternative. As stated in [2]_, Barnard's test is uniformly more powerful than Fisher's exact test because Barnard's test does not condition on any margin. Fisher's test should only be used when both sets of marginals are fixed.

See also

boschloo_exact

Boschloo's exact test on a 2x2 contingency table, which is an uniformly more powerful alternative to Fisher's exact test.

chi2_contingency

Chi-square test of independence of variables in a contingency table.

fisher_exact

Fisher exact test on a 2x2 contingency table.

Aliases

  • scipy.stats.barnard_exact

Referenced by

This package