{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "983878fd",
            "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": "448793a1",
            "cell_type": "markdown",
            "source": "When only the mandatory `f_obs` argument is given, it is assumed that the\nexpected frequencies are uniform and given by the mean of the observed\nfrequencies:\n",
            "metadata": {}
        },
        {
            "id": "ea6c4015",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.stats import chisquare\nchisquare([16, 18, 16, 14, 12, 12])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Power_divergenceResult(statistic=2.0, pvalue=0.84914503608460956)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f66461a8",
            "cell_type": "markdown",
            "source": "The optional `f_exp` argument gives the expected frequencies.\n",
            "metadata": {}
        },
        {
            "id": "95755848",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "chisquare([16, 18, 16, 14, 12, 12], f_exp=[16, 16, 16, 16, 16, 8])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Power_divergenceResult(statistic=3.5, pvalue=0.62338762774958223)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "87b97cd2",
            "cell_type": "markdown",
            "source": "When `f_obs` is 2-D, by default the test is applied to each column.\n",
            "metadata": {}
        },
        {
            "id": "e010a62d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "obs = np.array([[16, 18, 16, 14, 12, 12], [32, 24, 16, 28, 20, 24]]).T\nobs.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(6, 2)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ced0ef52",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "chisquare(obs)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Power_divergenceResult(statistic=array([2.        , 6.66666667]), pvalue=array([0.84914504, 0.24663415]))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "3220b20c",
            "cell_type": "markdown",
            "source": "By setting ``axis=None``, the test is applied to all data in the array,\nwhich is equivalent to applying the test to the flattened array.\n",
            "metadata": {}
        },
        {
            "id": "e03688f6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "chisquare(obs, axis=None)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Power_divergenceResult(statistic=23.31034482758621, pvalue=0.015975692534127565)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ab7c9767",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "chisquare(obs.ravel())",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Power_divergenceResult(statistic=23.310344827586206, pvalue=0.01597569253412758)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "78f7e099",
            "cell_type": "markdown",
            "source": "`ddof` is the change to make to the default degrees of freedom.\n",
            "metadata": {}
        },
        {
            "id": "7959cf4f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "chisquare([16, 18, 16, 14, 12, 12], ddof=1)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Power_divergenceResult(statistic=2.0, pvalue=0.7357588823428847)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e9081b2f",
            "cell_type": "markdown",
            "source": "The calculation of the p-values is done by broadcasting the\nchi-squared statistic with `ddof`.\n",
            "metadata": {}
        },
        {
            "id": "b0998b91",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "chisquare([16, 18, 16, 14, 12, 12], ddof=[0, 1, 2])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Power_divergenceResult(statistic=2.0, pvalue=array([0.84914504, 0.73575888, 0.5724067 ]))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "73ff47e2",
            "cell_type": "markdown",
            "source": "`f_obs` and `f_exp` are also broadcast.  In the following, `f_obs` has\nshape (6,) and `f_exp` has shape (2, 6), so the result of broadcasting\n`f_obs` and `f_exp` has shape (2, 6).  To compute the desired chi-squared\nstatistics, we use ``axis=1``:\n",
            "metadata": {}
        },
        {
            "id": "927d4e1c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "chisquare([16, 18, 16, 14, 12, 12],\n          f_exp=[[16, 16, 16, 16, 16, 8], [8, 20, 20, 16, 12, 12]],\n          axis=1)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Power_divergenceResult(statistic=array([3.5 , 9.25]), pvalue=array([0.62338763, 0.09949846]))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c10e07bf",
            "cell_type": "markdown",
            "source": "For a more detailed example, see :ref:`hypothesis_chisquare`.",
            "metadata": {}
        }
    ]
}