{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "c85bd64c",
            "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": "eecf8c1f",
            "cell_type": "markdown",
            "source": "Suppose we wish to test the null hypothesis that a sample is distributed\naccording to the standard normal.\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\nWhen testing uniformly distributed data, we would expect the\nnull hypothesis to be rejected.\n",
            "metadata": {}
        },
        {
            "id": "75af7a0c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import stats\nrng = np.random.default_rng()\nstats.kstest(stats.uniform.rvs(size=100, random_state=rng),\n             stats.norm.cdf)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "KstestResult(statistic=0.5001899973268688,\n             pvalue=1.1616392184763533e-23,\n             statistic_location=0.00047625268963724654,\n             statistic_sign=-1)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "77854fe2",
            "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\nare *not* distributed according to the standard normal.\n\nWhen testing random variates from the standard normal distribution, we\nexpect the data to be consistent with the null hypothesis most of the time.\n",
            "metadata": {}
        },
        {
            "id": "14d94977",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = stats.norm.rvs(size=100, random_state=rng)\nstats.kstest(x, stats.norm.cdf)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "KstestResult(statistic=0.05345882212970396,\n             pvalue=0.9227159037744717,\n             statistic_location=-1.2451343873745018,\n             statistic_sign=1)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "df402af2",
            "cell_type": "markdown",
            "source": "As expected, the p-value of 0.92 is not below our threshold of 0.05, so\nwe cannot reject the null hypothesis.\n\nSuppose, however, that the random variates are distributed according to\na normal distribution that is shifted toward greater values. In this case,\nthe cumulative density function (CDF) of the underlying distribution tends\nto be *less* than the CDF of the standard normal. Therefore, we would\nexpect the null hypothesis to be rejected with ``alternative='less'``:\n",
            "metadata": {}
        },
        {
            "id": "0277c201",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = stats.norm.rvs(size=100, loc=0.5, random_state=rng)\nstats.kstest(x, stats.norm.cdf, alternative='less')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "KstestResult(statistic=0.17482387821055168,\n             pvalue=0.001913921057766743,\n             statistic_location=0.3713830565352756,\n             statistic_sign=-1)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c3dd4642",
            "cell_type": "markdown",
            "source": "and indeed, with p-value smaller than our threshold, we reject the null\nhypothesis in favor of the alternative.\n\nFor convenience, the previous test can be performed using the name of the\ndistribution as the second argument.\n",
            "metadata": {}
        },
        {
            "id": "5ee1a185",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "stats.kstest(x, \"norm\", alternative='less')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "KstestResult(statistic=0.17482387821055168,\n             pvalue=0.001913921057766743,\n             statistic_location=0.3713830565352756,\n             statistic_sign=-1)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "cdc996a5",
            "cell_type": "markdown",
            "source": "The examples above have all been one-sample tests identical to those\nperformed by `ks_1samp`. Note that `kstest` can also perform two-sample\ntests identical to those performed by `ks_2samp`. For example, when two\nsamples are drawn from the same distribution, we expect the data to be\nconsistent with the null hypothesis most of the time.\n",
            "metadata": {}
        },
        {
            "id": "094fbabb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "sample1 = stats.laplace.rvs(size=105, random_state=rng)\nsample2 = stats.laplace.rvs(size=95, random_state=rng)\nstats.kstest(sample1, sample2)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "KstestResult(statistic=0.11779448621553884,\n             pvalue=0.4494256912629795,\n             statistic_location=0.6138814275424155,\n             statistic_sign=1)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "66ca471c",
            "cell_type": "markdown",
            "source": "As expected, the p-value of 0.45 is not below our threshold of 0.05, so\nwe cannot reject the null hypothesis.",
            "metadata": {}
        }
    ]
}