{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "ac0011e4",
            "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": "f3e7d65c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.stats import ansari\nrng = np.random.default_rng()",
            "outputs": []
        },
        {
            "id": "e7f6301a",
            "cell_type": "markdown",
            "source": "For these examples, we'll create three random data sets.  The first\ntwo, with sizes 35 and 25, are drawn from a normal distribution with\nmean 0 and standard deviation 2.  The third data set has size 25 and\nis drawn from a normal distribution with standard deviation 1.25.\n",
            "metadata": {}
        },
        {
            "id": "e0d59431",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x1 = rng.normal(loc=0, scale=2, size=35)\nx2 = rng.normal(loc=0, scale=2, size=25)\nx3 = rng.normal(loc=0, scale=1.25, size=25)",
            "outputs": []
        },
        {
            "id": "576460e6",
            "cell_type": "markdown",
            "source": "First we apply `ansari` to `x1` and `x2`.  These samples are drawn\nfrom the same distribution, so we expect the Ansari-Bradley test\nshould not lead us to conclude that the scales of the distributions\nare different.\n",
            "metadata": {}
        },
        {
            "id": "110fb16b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ansari(x1, x2)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "AnsariResult(statistic=541.0, pvalue=0.9762532927399098)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "99a43c14",
            "cell_type": "markdown",
            "source": "With a p-value close to 1, we cannot conclude that there is a\nsignificant difference in the scales (as expected).\n\nNow apply the test to `x1` and `x3`:\n",
            "metadata": {}
        },
        {
            "id": "be7c8b21",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ansari(x1, x3)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "AnsariResult(statistic=425.0, pvalue=0.0003087020407974518)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "cf174e68",
            "cell_type": "markdown",
            "source": "The probability of observing such an extreme value of the statistic\nunder the null hypothesis of equal scales is only 0.03087%. We take this\nas evidence against the null hypothesis in favor of the alternative:\nthe scales of the distributions from which the samples were drawn\nare not equal.\n\nWe can use the `alternative` parameter to perform a one-tailed test.\nIn the above example, the scale of `x1` is greater than `x3` and so\nthe ratio of scales of `x1` and `x3` is greater than 1. This means\nthat the p-value when ``alternative='greater'`` should be near 0 and\nhence we should be able to reject the null hypothesis:\n",
            "metadata": {}
        },
        {
            "id": "2a433f01",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ansari(x1, x3, alternative='greater')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "AnsariResult(statistic=425.0, pvalue=0.0001543510203987259)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2db2a486",
            "cell_type": "markdown",
            "source": "As we can see, the p-value is indeed quite low. Use of\n``alternative='less'`` should thus yield a large p-value:\n",
            "metadata": {}
        },
        {
            "id": "cf4210cd",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ansari(x1, x3, alternative='less')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "AnsariResult(statistic=425.0, pvalue=0.9998643258449039)"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}