{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "7b564c49",
            "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": "9ae246cd",
            "cell_type": "markdown",
            "source": "We follow the example from [4]: nine randomly sampled young adults were\ndiagnosed with type II diabetes at the ages below.\n",
            "metadata": {}
        },
        {
            "id": "d675c541",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "males = [19, 22, 16, 29, 24]\nfemales = [20, 11, 17, 12]",
            "outputs": []
        },
        {
            "id": "2eb61a7b",
            "cell_type": "markdown",
            "source": "We use the Mann-Whitney U test to assess whether there is a statistically\nsignificant difference in the diagnosis age of males and females.\nThe null hypothesis is that the distribution of male diagnosis ages is\nthe same as the distribution of female diagnosis ages. We decide\nthat a confidence level of 95% is required to reject the null hypothesis\nin favor of the alternative that the distributions are different.\nSince the number of samples is very small and there are no ties in the\ndata, we can compare the observed test statistic against the *exact*\ndistribution of the test statistic under the null hypothesis.\n",
            "metadata": {}
        },
        {
            "id": "abd10dea",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import mannwhitneyu\nU1, p = mannwhitneyu(males, females, method=\"exact\")\nprint(U1)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "17.0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "9f52e96a",
            "cell_type": "markdown",
            "source": "`mannwhitneyu` always reports the statistic associated with the first\nsample, which, in this case, is males. This agrees with $U_M = 17$\nreported in [4]. The statistic associated with the second statistic\ncan be calculated:\n",
            "metadata": {}
        },
        {
            "id": "28e7a554",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "nx, ny = len(males), len(females)\nU2 = nx*ny - U1\nprint(U2)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "3.0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "17dbdfd8",
            "cell_type": "markdown",
            "source": "This agrees with $U_F = 3$ reported in [4]. The two-sided\n*p*-value can be calculated from either statistic, and the value produced\nby `mannwhitneyu` agrees with $p = 0.11$ reported in [4].\n",
            "metadata": {}
        },
        {
            "id": "02336e93",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "print(p)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.1111111111111111"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "42b269a4",
            "cell_type": "markdown",
            "source": "The exact distribution of the test statistic is asymptotically normal, so\nthe example continues by comparing the exact *p*-value against the\n*p*-value produced using the normal approximation.\n",
            "metadata": {}
        },
        {
            "id": "1d8cc625",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "_, pnorm = mannwhitneyu(males, females, method=\"asymptotic\")\nprint(pnorm)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.11134688653314041"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "56934d8b",
            "cell_type": "markdown",
            "source": "Here `mannwhitneyu`'s reported *p*-value appears to conflict with the\nvalue $p = 0.09$ given in [4]. The reason is that [4]\ndoes not apply the continuity correction performed by `mannwhitneyu`;\n`mannwhitneyu` reduces the distance between the test statistic and the\nmean $\\mu = n_x n_y / 2$ by 0.5 to correct for the fact that the\ndiscrete statistic is being compared against a continuous distribution.\nHere, the $U$ statistic used is less than the mean, so we reduce\nthe distance by adding 0.5 in the numerator.\n",
            "metadata": {}
        },
        {
            "id": "39c2a27f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.stats import norm\nU = min(U1, U2)\nN = nx + ny\nz = (U - nx*ny/2 + 0.5) / np.sqrt(nx*ny * (N + 1)/ 12)\np = 2 * norm.cdf(z)  # use CDF to get p-value from smaller statistic\nprint(p)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.11134688653314041"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "643d13a8",
            "cell_type": "markdown",
            "source": "If desired, we can disable the continuity correction to get a result\nthat agrees with that reported in [4].\n",
            "metadata": {}
        },
        {
            "id": "8b130130",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "_, pnorm = mannwhitneyu(males, females, use_continuity=False,\n                        method=\"asymptotic\")\nprint(pnorm)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.0864107329737"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "18f46d4b",
            "cell_type": "markdown",
            "source": "Regardless of whether we perform an exact or asymptotic test, the\nprobability of the test statistic being as extreme or more extreme by\nchance exceeds 5%, so we do not consider the results statistically\nsignificant.\n\nSuppose that, before seeing the data, we had hypothesized that females\nwould tend to be diagnosed at a younger age than males.\nIn that case, it would be natural to provide the female ages as the\nfirst input, and we would have performed a one-sided test using\n``alternative = 'less'``: females are diagnosed at an age that is\nstochastically less than that of males.\n",
            "metadata": {}
        },
        {
            "id": "727d2781",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = mannwhitneyu(females, males, alternative=\"less\", method=\"exact\")\nprint(res)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "MannwhitneyuResult(statistic=3.0, pvalue=0.05555555555555555)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "48474ca0",
            "cell_type": "markdown",
            "source": "Again, the probability of getting a sufficiently low value of the\ntest statistic by chance under the null hypothesis is greater than 5%,\nso we do not reject the null hypothesis in favor of our alternative.\n\nIf it is reasonable to assume that the means of samples from the\npopulations are normally distributed, we could have used a t-test to\nperform the analysis.\n",
            "metadata": {}
        },
        {
            "id": "e79d3851",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import ttest_ind\nres = ttest_ind(females, males, alternative=\"less\")\nprint(res)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "TtestResult(statistic=-2.239334696520584,\n            pvalue=0.030068441095757924,\n            df=7.0)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "71ce398f",
            "cell_type": "markdown",
            "source": "Under this assumption, the *p*-value would be low enough to reject the\nnull hypothesis in favor of the alternative.",
            "metadata": {}
        }
    ]
}