{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "ecfa060b",
            "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": "4e525962",
            "cell_type": "markdown",
            "source": "Show the probability of a gap at least as big as 0, 0.5 and 1.0.\n",
            "metadata": {}
        },
        {
            "id": "39e86b65",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.special import kolmogorov\nfrom scipy.stats import kstwobign\nkolmogorov([0, 0.5, 1.0])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 1.        ,  0.96394524,  0.26999967])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "fa468c62",
            "cell_type": "markdown",
            "source": "Compare a sample of size 1000 drawn from a Laplace(0, 1) distribution against\nthe target distribution, a Normal(0, 1) distribution.\n",
            "metadata": {}
        },
        {
            "id": "cfcb45fc",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import norm, laplace\nrng = np.random.default_rng()\nn = 1000\nlap01 = laplace(0, 1)\nx = np.sort(lap01.rvs(n, random_state=rng))\nnp.mean(x), np.std(x)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(-0.05841730131499543, 1.3968109101997568)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c393134a",
            "cell_type": "markdown",
            "source": "Construct the Empirical CDF and the K-S statistic Dn.\n",
            "metadata": {}
        },
        {
            "id": "6a988f83",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "target = norm(0,1)  # Normal mean 0, stddev 1\ncdfs = target.cdf(x)\necdfs = np.arange(n+1, dtype=float)/n\ngaps = np.column_stack([cdfs - ecdfs[:n], ecdfs[1:] - cdfs])\nDn = np.max(gaps)\nKn = np.sqrt(n) * Dn\nprint('Dn=%f, sqrt(n)*Dn=%f' % (Dn, Kn))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Dn=0.043363, sqrt(n)*Dn=1.371265"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "33ace761",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "print(chr(10).join(['For a sample of size n drawn from a N(0, 1) distribution:',\n  ' the approximate Kolmogorov probability that sqrt(n)*Dn>=%f is %f' %\n   (Kn, kolmogorov(Kn)),\n  ' the approximate Kolmogorov probability that sqrt(n)*Dn<=%f is %f' %\n   (Kn, kstwobign.cdf(Kn))]))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "For a sample of size n drawn from a N(0, 1) distribution:\n the approximate Kolmogorov probability that sqrt(n)*Dn>=1.371265 is 0.046533\n the approximate Kolmogorov probability that sqrt(n)*Dn<=1.371265 is 0.953467"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2a2397b0",
            "cell_type": "markdown",
            "source": "Plot the Empirical CDF against the target N(0, 1) CDF.\n",
            "metadata": {}
        },
        {
            "id": "e3c1e7dd",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nplt.step(np.concatenate([[-3], x]), ecdfs, where='post', label='Empirical CDF')\nx3 = np.linspace(-3, 3, 100)\nplt.plot(x3, target.cdf(x3), label='CDF for N(0, 1)')\nplt.ylim([0, 1]); plt.grid(True); plt.legend();\n# Add vertical lines marking Dn+ and Dn-\niminus, iplus = np.argmax(gaps, axis=0)\nplt.vlines([x[iminus]], ecdfs[iminus], cdfs[iminus],\n           color='r', linestyle='dashed', lw=4)\nplt.vlines([x[iplus]], cdfs[iplus], ecdfs[iplus+1],\n           color='r', linestyle='dashed', lw=4)\nplt.show()",
            "outputs": []
        }
    ]
}