{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "27cad83e",
            "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": "088d43ba",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.stats import truncnorm\nimport matplotlib.pyplot as plt\nfig, ax = plt.subplots(1, 1)",
            "outputs": []
        },
        {
            "id": "08ad1ac1",
            "cell_type": "markdown",
            "source": "Get the support:\n",
            "metadata": {}
        },
        {
            "id": "22c66abc",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a, b = 0.1, 2\nlb, ub = truncnorm.support(a, b)",
            "outputs": []
        },
        {
            "id": "52933c4b",
            "cell_type": "markdown",
            "source": "Calculate the first four moments:\n",
            "metadata": {}
        },
        {
            "id": "72cc3c2c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "mean, var, skew, kurt = truncnorm.stats(a, b, moments='mvsk')",
            "outputs": []
        },
        {
            "id": "473a5b1c",
            "cell_type": "markdown",
            "source": "Display the probability density function (``pdf``):\n",
            "metadata": {}
        },
        {
            "id": "11db54d6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = np.linspace(truncnorm.ppf(0.01, a, b),\n                truncnorm.ppf(0.99, a, b), 100)\nax.plot(x, truncnorm.pdf(x, a, b),\n       'r-', lw=5, alpha=0.6, label='truncnorm pdf')",
            "outputs": []
        },
        {
            "id": "1071b355",
            "cell_type": "markdown",
            "source": "Alternatively, the distribution object can be called (as a function)\nto fix the shape, location and scale parameters. This returns a \"frozen\"\nRV object holding the given parameters fixed.\n\nFreeze the distribution and display the frozen ``pdf``:\n",
            "metadata": {}
        },
        {
            "id": "9663b729",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "rv = truncnorm(a, b)\nax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')",
            "outputs": []
        },
        {
            "id": "bd36cfd0",
            "cell_type": "markdown",
            "source": "Check accuracy of ``cdf`` and ``ppf``:\n",
            "metadata": {}
        },
        {
            "id": "1a04f295",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "vals = truncnorm.ppf([0.001, 0.5, 0.999], a, b)\nnp.allclose([0.001, 0.5, 0.999], truncnorm.cdf(vals, a, b))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "6278c792",
            "cell_type": "markdown",
            "source": "Generate random numbers:\n",
            "metadata": {}
        },
        {
            "id": "ebfdfe1a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r = truncnorm.rvs(a, b, size=1000)",
            "outputs": []
        },
        {
            "id": "ca8035f9",
            "cell_type": "markdown",
            "source": "And compare the histogram:\n",
            "metadata": {}
        },
        {
            "id": "778c2140",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ax.hist(r, density=True, bins='auto', histtype='stepfilled', alpha=0.2)\nax.set_xlim([x[0], x[-1]])\nax.legend(loc='best', frameon=False)\nplt.show()",
            "outputs": []
        },
        {
            "id": "d986df9a",
            "cell_type": "markdown",
            "source": "In the examples above, ``loc=0`` and ``scale=1``, so the plot is truncated\nat ``a`` on the left and ``b`` on the right. However, suppose we were to\nproduce the same histogram with ``loc = 1`` and ``scale=0.5``.\n",
            "metadata": {}
        },
        {
            "id": "e2ea0847",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "loc, scale = 1, 0.5\nrv = truncnorm(a, b, loc=loc, scale=scale)\nx = np.linspace(truncnorm.ppf(0.01, a, b),\n                truncnorm.ppf(0.99, a, b), 100)\nr = rv.rvs(size=1000)",
            "outputs": []
        },
        {
            "id": "20ec1a5e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fig, ax = plt.subplots(1, 1)\nax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')\nax.hist(r, density=True, bins='auto', histtype='stepfilled', alpha=0.2)\nax.set_xlim(a, b)\nax.legend(loc='best', frameon=False)\nplt.show()",
            "outputs": []
        },
        {
            "id": "1f4e9f7e",
            "cell_type": "markdown",
            "source": "Note that the distribution is no longer appears to be truncated at\nabscissae ``a`` and ``b``. That is because the *standard* normal\ndistribution is first truncated at ``a`` and ``b``, *then* the resulting\ndistribution is scaled by ``scale`` and shifted by ``loc``. If we instead\nwant the shifted and scaled distribution to be truncated at ``a`` and\n``b``, we need to transform these values before passing them as the\ndistribution parameters.\n",
            "metadata": {}
        },
        {
            "id": "847305db",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a_transformed, b_transformed = (a - loc) / scale, (b - loc) / scale\nrv = truncnorm(a_transformed, b_transformed, loc=loc, scale=scale)\nx = np.linspace(truncnorm.ppf(0.01, a, b),\n                truncnorm.ppf(0.99, a, b), 100)\nr = rv.rvs(size=10000)",
            "outputs": []
        },
        {
            "id": "3376be75",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fig, ax = plt.subplots(1, 1)\nax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')\nax.hist(r, density=True, bins='auto', histtype='stepfilled', alpha=0.2)\nax.set_xlim(a-0.1, b+0.1)\nax.legend(loc='best', frameon=False)\nplt.show()",
            "outputs": []
        }
    ]
}