{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "9433af60",
            "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": "91983928",
            "cell_type": "markdown",
            "source": "Suppose we wish to test the null hypothesis that two samples were drawn\nfrom the same distribution.\nWe choose a confidence level of 95%; that is, we will reject the null\nhypothesis in favor of the alternative if the p-value is less than 0.05.\n\nIf the first sample were drawn from a uniform distribution and the second\nwere drawn from the standard normal, we would expect the null hypothesis\nto be rejected.\n",
            "metadata": {}
        },
        {
            "id": "0fb60cd0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import stats\nrng = np.random.default_rng()\nsample1 = stats.uniform.rvs(size=100, random_state=rng)\nsample2 = stats.norm.rvs(size=110, random_state=rng)\nstats.ks_2samp(sample1, sample2)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "KstestResult(statistic=0.5454545454545454,\n             pvalue=7.37417839555191e-15,\n             statistic_location=-0.014071496412861274,\n             statistic_sign=-1)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c1b451e0",
            "cell_type": "markdown",
            "source": "Indeed, the p-value is lower than our threshold of 0.05, so we reject the\nnull hypothesis in favor of the default \"two-sided\" alternative: the data\nwere *not* drawn from the same distribution.\n\nWhen both samples are drawn from the same distribution, we expect the data\nto be consistent with the null hypothesis most of the time.\n",
            "metadata": {}
        },
        {
            "id": "fd0ac82a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "sample1 = stats.norm.rvs(size=105, random_state=rng)\nsample2 = stats.norm.rvs(size=95, random_state=rng)\nstats.ks_2samp(sample1, sample2)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "KstestResult(statistic=0.10927318295739348,\n             pvalue=0.5438289009927495,\n             statistic_location=-0.1670157701848795,\n             statistic_sign=-1)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "fc2b1077",
            "cell_type": "markdown",
            "source": "As expected, the p-value of 0.54 is not below our threshold of 0.05, so\nwe cannot reject the null hypothesis.\n\nSuppose, however, that the first sample were drawn from\na normal distribution shifted toward greater values. In this case,\nthe cumulative density function (CDF) of the underlying distribution tends\nto be *less* than the CDF underlying the second sample. Therefore, we would\nexpect the null hypothesis to be rejected with ``alternative='less'``:\n",
            "metadata": {}
        },
        {
            "id": "2a805a3d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "sample1 = stats.norm.rvs(size=105, loc=0.5, random_state=rng)\nstats.ks_2samp(sample1, sample2, alternative='less')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "KstestResult(statistic=0.4055137844611529,\n             pvalue=3.5474563068855554e-08,\n             statistic_location=-0.13249370614972575,\n             statistic_sign=-1)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "22817999",
            "cell_type": "markdown",
            "source": "and indeed, with p-value smaller than our threshold, we reject the null\nhypothesis in favor of the alternative.",
            "metadata": {}
        }
    ]
}