{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "42b4970c",
            "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": "a789cec2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import stats\nfrom scipy.stats.sampling import FastGeneratorInversion",
            "outputs": []
        },
        {
            "id": "94e941c4",
            "cell_type": "markdown",
            "source": "Create an object for the normal distribution:\n",
            "metadata": {}
        },
        {
            "id": "c351d09d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "d_norm_frozen = stats.norm()\nd_norm = FastGeneratorInversion(d_norm_frozen)",
            "outputs": []
        },
        {
            "id": "7f7c74e4",
            "cell_type": "markdown",
            "source": "To confirm that the numerical inversion is accurate, we evaluate the\napproximation error (u-error and x-error).\n",
            "metadata": {}
        },
        {
            "id": "476b5a8c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "u_error, x_error = d_norm.evaluate_error(x_error=True)",
            "outputs": []
        },
        {
            "id": "70b4dcb2",
            "cell_type": "markdown",
            "source": "The u-error should be below 1e-10:\n",
            "metadata": {}
        },
        {
            "id": "8883c648",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "u_error",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "8.785783212061915e-11  # may vary"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "009ec4f6",
            "cell_type": "markdown",
            "source": "Compare the PPF against approximation `ppf`:\n",
            "metadata": {}
        },
        {
            "id": "e3f1e0f7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "q = [0.001, 0.2, 0.4, 0.6, 0.8, 0.999]\ndiff = np.abs(d_norm_frozen.ppf(q) - d_norm.ppf(q))\nx_error_abs = np.max(diff)\nx_error_abs",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "1.2937954707581412e-08"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "36f0106b",
            "cell_type": "markdown",
            "source": "This is the absolute x-error evaluated at the points q. The relative\nerror is given by\n",
            "metadata": {}
        },
        {
            "id": "5539e753",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x_error_rel = np.max(diff / np.abs(d_norm_frozen.ppf(q)))\nx_error_rel",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "4.186725600453555e-09"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "846b2f7d",
            "cell_type": "markdown",
            "source": "The x_error computed above is derived in a very similar way over a\nmuch larger set of random values q. At each value q[i], the minimum\nof the relative and absolute error is taken. The final value is then\nderived as the maximum of these values. In our example, we get the\nfollowing value:\n",
            "metadata": {}
        },
        {
            "id": "9a4da842",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x_error",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "4.507068014335139e-07  # may vary"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}