{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "832b3753",
            "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": "22d4f34a",
            "cell_type": "markdown",
            "source": "A two-way example (2 x 3):\n",
            "metadata": {}
        },
        {
            "id": "47ec88c2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.stats import chi2_contingency\nobs = np.array([[10, 10, 20], [20, 20, 20]])\nres = chi2_contingency(obs)\nres.statistic",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "2.7777777777777777"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e5d6da62",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.pvalue",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.24935220877729619"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "99979c9e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.dof",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "2"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "3ca4d7c7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.expected_freq",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 12.,  12.,  16.],\n       [ 18.,  18.,  24.]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c01339bb",
            "cell_type": "markdown",
            "source": "Perform the test using the log-likelihood ratio (i.e. the \"G-test\")\ninstead of Pearson's chi-squared statistic.\n",
            "metadata": {}
        },
        {
            "id": "62a3b2b4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = chi2_contingency(obs, lambda_=\"log-likelihood\")\nres.statistic",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "2.7688587616781319"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "89f4a803",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.pvalue",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.25046668010954165"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "9b824c17",
            "cell_type": "markdown",
            "source": "A four-way example (2 x 2 x 2 x 2):\n",
            "metadata": {}
        },
        {
            "id": "6dcb939a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "obs = np.array(\n    [[[[12, 17],\n       [11, 16]],\n      [[11, 12],\n       [15, 16]]],\n     [[[23, 15],\n       [30, 22]],\n      [[14, 17],\n       [15, 16]]]])\nres = chi2_contingency(obs)\nres.statistic",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "8.7584514426741897"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "266227d2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.pvalue",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.64417725029295503"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2bf3d600",
            "cell_type": "markdown",
            "source": "When the sum of the elements in a two-way table is small, the p-value\nproduced by the default asymptotic approximation may be inaccurate.\nConsider passing a `PermutationMethod` or `MonteCarloMethod` as the\n`method` parameter with `correction=False`.\n",
            "metadata": {}
        },
        {
            "id": "48b8c988",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import PermutationMethod\nobs = np.asarray([[12, 3],\n                  [17, 16]])\nres = chi2_contingency(obs, correction=False)\nref = chi2_contingency(obs, correction=False, method=PermutationMethod())\nres.pvalue, ref.pvalue",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(0.0614122539870913, 0.1074)  # may vary"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ea002794",
            "cell_type": "markdown",
            "source": "For a more detailed example, see :ref:`hypothesis_chi2_contingency`.",
            "metadata": {}
        }
    ]
}