{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "9066da7d",
            "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": "eda91f9a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import stats",
            "outputs": []
        },
        {
            "id": "8459fc07",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats.sampling import RatioUniforms\nrng = np.random.default_rng()",
            "outputs": []
        },
        {
            "id": "87c9672b",
            "cell_type": "markdown",
            "source": "Simulate normally distributed random variables. It is easy to compute the\nbounding rectangle explicitly in that case. For simplicity, we drop the\nnormalization factor of the density.\n",
            "metadata": {}
        },
        {
            "id": "b8524073",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "f = lambda x: np.exp(-x**2 / 2)\nv = np.sqrt(f(np.sqrt(2))) * np.sqrt(2)\numax = np.sqrt(f(0))\ngen = RatioUniforms(f, umax=umax, vmin=-v, vmax=v, random_state=rng)\nr = gen.rvs(size=2500)",
            "outputs": []
        },
        {
            "id": "1f2d182c",
            "cell_type": "markdown",
            "source": "The K-S test confirms that the random variates are indeed normally\ndistributed (normality is not rejected at 5% significance level):\n",
            "metadata": {}
        },
        {
            "id": "5761b69d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "stats.kstest(r, 'norm')[1]",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.250634764150542"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d52c59cb",
            "cell_type": "markdown",
            "source": "The exponential distribution provides another example where the bounding\nrectangle can be determined explicitly.\n",
            "metadata": {}
        },
        {
            "id": "1d6873ea",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "gen = RatioUniforms(lambda x: np.exp(-x), umax=1, vmin=0,\n                    vmax=2*np.exp(-1), random_state=rng)\nr = gen.rvs(1000)\nstats.kstest(r, 'expon')[1]",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.21121052054580314"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}