{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "ab9ae827",
            "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": "aff09628",
            "cell_type": "markdown",
            "source": "Create a scipy.stats distribution from a numpy histogram\n",
            "metadata": {}
        },
        {
            "id": "0b7422d0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import scipy.stats\nimport numpy as np\ndata = scipy.stats.norm.rvs(size=100000, loc=0, scale=1.5,\n                            random_state=123)\nhist = np.histogram(data, bins=100)\nhist_dist = scipy.stats.rv_histogram(hist, density=False)",
            "outputs": []
        },
        {
            "id": "0ebb3675",
            "cell_type": "markdown",
            "source": "Behaves like an ordinary scipy rv_continuous distribution\n",
            "metadata": {}
        },
        {
            "id": "25abef0d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "hist_dist.pdf(1.0)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.20538577847618705"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "119b6d4c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "hist_dist.cdf(2.0)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.90818568543056499"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "06665258",
            "cell_type": "markdown",
            "source": "PDF is zero above (below) the highest (lowest) bin of the histogram,\ndefined by the max (min) of the original dataset\n",
            "metadata": {}
        },
        {
            "id": "c014bdf8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "hist_dist.pdf(np.max(data))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a47ab994",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "hist_dist.cdf(np.max(data))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "1.0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f7b14777",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "hist_dist.pdf(np.min(data))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "7.7591907244498314e-05"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f223d4a5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "hist_dist.cdf(np.min(data))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a383d3c7",
            "cell_type": "markdown",
            "source": "PDF and CDF follow the histogram\n",
            "metadata": {}
        },
        {
            "id": "573713f3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nX = np.linspace(-5.0, 5.0, 100)\nfig, ax = plt.subplots()\nax.set_title(\"PDF from Template\")\nax.hist(data, density=True, bins=100)\nax.plot(X, hist_dist.pdf(X), label='PDF')\nax.plot(X, hist_dist.cdf(X), label='CDF')\nax.legend()\nfig.show()",
            "outputs": []
        }
    ]
}