{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "d6987853",
            "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": "d97c9c6a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import stats\nfrom scipy.stats.sampling import FastGeneratorInversion",
            "outputs": []
        },
        {
            "id": "7d599e5b",
            "cell_type": "markdown",
            "source": "Let's start with a simple example to illustrate the main features:\n",
            "metadata": {}
        },
        {
            "id": "fd614b6f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "gamma_frozen = stats.gamma(1.5)\ngamma_dist = FastGeneratorInversion(gamma_frozen)\nr = gamma_dist.rvs(size=1000)",
            "outputs": []
        },
        {
            "id": "dab59d70",
            "cell_type": "markdown",
            "source": "The mean should be approximately equal to the shape parameter 1.5:\n",
            "metadata": {}
        },
        {
            "id": "43d33d8a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r.mean()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "1.52423591130436  # may vary"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "df1ab867",
            "cell_type": "markdown",
            "source": "Similarly, we can draw a sample based on quasi-random numbers:\n",
            "metadata": {}
        },
        {
            "id": "b7247c5e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r = gamma_dist.qrvs(size=1000)\nr.mean()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "1.4996639255942914  # may vary"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f65fdb52",
            "cell_type": "markdown",
            "source": "Compare the PPF against approximation `ppf`.\n",
            "metadata": {}
        },
        {
            "id": "df1ad800",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "q = [0.001, 0.2, 0.5, 0.8, 0.999]\nnp.max(np.abs(gamma_frozen.ppf(q) - gamma_dist.ppf(q)))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "4.313394796895409e-08"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ce8ebdc1",
            "cell_type": "markdown",
            "source": "To confirm that the numerical inversion is accurate, we evaluate the\napproximation error (u-error), which should be below 1e-10 (for more\ndetails, refer to the documentation of `evaluate_error`):\n",
            "metadata": {}
        },
        {
            "id": "170cc835",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "gamma_dist.evaluate_error()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(7.446320551265581e-11, nan)  # may vary"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "49983c02",
            "cell_type": "markdown",
            "source": "Note that the location and scale can be changed without instantiating a\nnew generator:\n",
            "metadata": {}
        },
        {
            "id": "52976fa8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "gamma_dist.loc = 2\ngamma_dist.scale = 3\nr = gamma_dist.rvs(size=1000)",
            "outputs": []
        },
        {
            "id": "8cb1c9ab",
            "cell_type": "markdown",
            "source": "The mean should be approximately 2 + 3*1.5 = 6.5.\n",
            "metadata": {}
        },
        {
            "id": "8cb67df1",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r.mean()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "6.399549295242894  # may vary"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "21ffc110",
            "cell_type": "markdown",
            "source": "Let us also illustrate how truncation can be applied:\n",
            "metadata": {}
        },
        {
            "id": "b74d1094",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "trunc_norm = FastGeneratorInversion(stats.norm(), domain=(3, 4))\nr = trunc_norm.rvs(size=1000)\n3 < r.min() < r.max() < 4",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "61d0ffc2",
            "cell_type": "markdown",
            "source": "Check the mean:\n",
            "metadata": {}
        },
        {
            "id": "ea54f898",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r.mean()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "3.250433367078603  # may vary"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "476d201c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "stats.norm.expect(lb=3, ub=4, conditional=True)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "3.260454285589997"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d591201e",
            "cell_type": "markdown",
            "source": "In this particular, case, `scipy.stats.truncnorm` could also be used to\ngenerate truncated normal random variates.",
            "metadata": {}
        }
    ]
}