{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "24786744",
            "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": "a56dd324",
            "cell_type": "markdown",
            "source": "In [4], the differences in height between cross- and self-fertilized\ncorn plants is given as follows:\n",
            "metadata": {}
        },
        {
            "id": "31957dc2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "d = [6, 8, 14, 16, 23, 24, 28, 29, 41, -48, 49, 56, 60, -67, 75]",
            "outputs": []
        },
        {
            "id": "378d6f07",
            "cell_type": "markdown",
            "source": "Cross-fertilized plants appear to be higher. To test the null\nhypothesis that there is no height difference, we can apply the\ntwo-sided test:\n",
            "metadata": {}
        },
        {
            "id": "dc19d345",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import wilcoxon\nres = wilcoxon(d)\nres.statistic, res.pvalue",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(24.0, 0.041259765625)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a0e37de1",
            "cell_type": "markdown",
            "source": "Hence, we would reject the null hypothesis at a confidence level of 5%,\nconcluding that there is a difference in height between the groups.\nTo confirm that the median of the differences can be assumed to be\npositive, we use:\n",
            "metadata": {}
        },
        {
            "id": "321fe17b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = wilcoxon(d, alternative='greater')\nres.statistic, res.pvalue",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(96.0, 0.0206298828125)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "edf7c52f",
            "cell_type": "markdown",
            "source": "This shows that the null hypothesis that the median is negative can be\nrejected at a confidence level of 5% in favor of the alternative that\nthe median is greater than zero. The p-values above are exact. Using the\nnormal approximation gives very similar values:\n",
            "metadata": {}
        },
        {
            "id": "747c3613",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = wilcoxon(d, method='asymptotic')\nres.statistic, res.pvalue",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(24.0, 0.04088813291185591)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2507ed2c",
            "cell_type": "markdown",
            "source": "Note that the statistic changed to 96 in the one-sided case (the sum\nof ranks of positive differences) whereas it is 24 in the two-sided\ncase (the minimum of sum of ranks above and below zero).\n\nIn the example above, the differences in height between paired plants are\nprovided to `wilcoxon` directly. Alternatively, `wilcoxon` accepts two\nsamples of equal length, calculates the differences between paired\nelements, then performs the test. Consider the samples ``x`` and ``y``:\n",
            "metadata": {}
        },
        {
            "id": "eec51a61",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nx = np.array([0.5, 0.825, 0.375, 0.5])\ny = np.array([0.525, 0.775, 0.325, 0.55])\nres = wilcoxon(x, y, alternative='greater')\nres",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "WilcoxonResult(statistic=5.0, pvalue=0.5625)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ee85d880",
            "cell_type": "markdown",
            "source": "Note that had we calculated the differences by hand, the test would have\nproduced different results:\n",
            "metadata": {}
        },
        {
            "id": "ccad6dc2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "d = [-0.025, 0.05, 0.05, -0.05]\nref = wilcoxon(d, alternative='greater')\nref",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "WilcoxonResult(statistic=6.0, pvalue=0.5)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "340eea9f",
            "cell_type": "markdown",
            "source": "The substantial difference is due to roundoff error in the results of\n``x-y``:\n",
            "metadata": {}
        },
        {
            "id": "d7635b65",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "d - (x-y)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([2.08166817e-17, 6.93889390e-17, 1.38777878e-17, 4.16333634e-17])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "39872886",
            "cell_type": "markdown",
            "source": "Even though we expected all the elements of ``(x-y)[1:]`` to have the same\nmagnitude ``0.05``, they have slightly different magnitudes in practice,\nand therefore are assigned different ranks in the test. Before performing\nthe test, consider calculating ``d`` and adjusting it as necessary to\nensure that theoretically identically values are not numerically distinct.\nFor example:\n",
            "metadata": {}
        },
        {
            "id": "5e8708b6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "d2 = np.around(x - y, decimals=3)\nwilcoxon(d2, alternative='greater')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "WilcoxonResult(statistic=6.0, pvalue=0.5)"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}