{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "3febf034",
            "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": "371bcd9b",
            "cell_type": "markdown",
            "source": "(See `chisquare` for more examples.)\n\nWhen just `f_obs` is given, it is assumed that the expected frequencies\nare uniform and given by the mean of the observed frequencies.  Here we\nperform a G-test (i.e. use the log-likelihood ratio statistic):\n",
            "metadata": {}
        },
        {
            "id": "bc2e8e88",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.stats import power_divergence\npower_divergence([16, 18, 16, 14, 12, 12], lambda_='log-likelihood')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(2.006573162632538, 0.84823476779463769)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "6ff2b719",
            "cell_type": "markdown",
            "source": "The expected frequencies can be given with the `f_exp` argument:\n",
            "metadata": {}
        },
        {
            "id": "aedfde62",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "power_divergence([16, 18, 16, 14, 12, 12],\n                 f_exp=[16, 16, 16, 16, 16, 8],\n                 lambda_='log-likelihood')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(3.3281031458963746, 0.6495419288047497)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "89745312",
            "cell_type": "markdown",
            "source": "When `f_obs` is 2-D, by default the test is applied to each column.\n",
            "metadata": {}
        },
        {
            "id": "60f6cb73",
            "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": "3d83f8fe",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "power_divergence(obs, lambda_=\"log-likelihood\")",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(array([ 2.00657316,  6.77634498]), array([ 0.84823477,  0.23781225]))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "3028a107",
            "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": "1fcc2f04",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "power_divergence(obs, axis=None)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(23.31034482758621, 0.015975692534127565)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c4fdc307",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "power_divergence(obs.ravel())",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(23.31034482758621, 0.015975692534127565)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f4c88663",
            "cell_type": "markdown",
            "source": "`ddof` is the change to make to the default degrees of freedom.\n",
            "metadata": {}
        },
        {
            "id": "9615dbb2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "power_divergence([16, 18, 16, 14, 12, 12], ddof=1)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(2.0, 0.73575888234288467)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "109f54a8",
            "cell_type": "markdown",
            "source": "The calculation of the p-values is done by broadcasting the\ntest statistic with `ddof`.\n",
            "metadata": {}
        },
        {
            "id": "690845b3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "power_divergence([16, 18, 16, 14, 12, 12], ddof=[0,1,2])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(2.0, array([ 0.84914504,  0.73575888,  0.5724067 ]))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e278bf6f",
            "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 must use ``axis=1``:\n",
            "metadata": {}
        },
        {
            "id": "73547b33",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "power_divergence([16, 18, 16, 14, 12, 12],\n                 f_exp=[[16, 16, 16, 16, 16, 8],\n                        [8, 20, 20, 16, 12, 12]],\n                 axis=1)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(array([ 3.5 ,  9.25]), array([ 0.62338763,  0.09949846]))"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}