{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "3a7f693f",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "8f029c89",
            "cell_type": "markdown",
            "source": "An example use of Barnard's test is presented in [2].\n\n    Consider the following example of a vaccine efficacy study\n    (Chan, 1998). In a randomized clinical trial of 30 subjects, 15 were\n    inoculated with a recombinant DNA influenza vaccine and the 15 were\n    inoculated with a placebo. Twelve of the 15 subjects in the placebo\n    group (80%) eventually became infected with influenza whereas for the\n    vaccine group, only 7 of the 15 subjects (47%) became infected. The\n    data are tabulated as a 2 x 2 table\n```\n\nVaccine  Placebo\nYes     7        12\nNo      8        3\n\n```\nWhen working with statistical hypothesis testing, we usually use a\nthreshold probability or significance level upon which we decide\nto reject the null hypothesis $H_0$. Suppose we choose the common\nsignificance level of 5%.\n\nOur alternative hypothesis is that the vaccine will lower the chance of\nbecoming infected with the virus; that is, the probability $p_1$ of\ncatching the virus with the vaccine will be *less than* the probability\n$p_2$ of catching the virus without the vaccine.  Therefore, we call\n`barnard_exact` with the ``alternative=\"less\"`` option:\n",
            "metadata": {}
        },
        {
            "id": "473b5254",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import scipy.stats as stats\nres = stats.barnard_exact([[7, 12], [8, 3]], alternative=\"less\")\nres.statistic",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "-1.894"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f7dbb1ee",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.pvalue",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.03407"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "042a6c2f",
            "cell_type": "markdown",
            "source": "Under the null hypothesis that the vaccine will not lower the chance of\nbecoming infected, the probability of obtaining test results at least as\nextreme as the observed data is approximately 3.4%. Since this p-value is\nless than our chosen significance level, we have evidence to reject\n$H_0$ in favor of the alternative.\n\nSuppose we had used Fisher's exact test instead:\n",
            "metadata": {}
        },
        {
            "id": "98652233",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "_, pvalue = stats.fisher_exact([[7, 12], [8, 3]], alternative=\"less\")\npvalue",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.0640"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "b60e2d53",
            "cell_type": "markdown",
            "source": "With the same threshold significance of 5%, we would not have been able\nto reject the null hypothesis in favor of the alternative. As stated in\n[2], Barnard's test is uniformly more powerful than Fisher's exact test\nbecause Barnard's test does not condition on any margin. Fisher's test\nshould only be used when both sets of marginals are fixed.",
            "metadata": {}
        }
    ]
}