{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "caec31d0",
            "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": "0f7f423a",
            "cell_type": "markdown",
            "source": "Compute the negative binomial cumulative distribution function for an\nexemplary parameter set.\n",
            "metadata": {}
        },
        {
            "id": "e594bfc8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.special import nbdtr, nbdtrik\nk, n, p = 5, 2, 0.5\ncdf_value = nbdtr(k, n, p)\ncdf_value",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.9375"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "6ab0a453",
            "cell_type": "markdown",
            "source": "Verify that `nbdtrik` recovers the original value for `k`.\n",
            "metadata": {}
        },
        {
            "id": "fb4b7b88",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "nbdtrik(cdf_value, n, p)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "5.0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "1266f425",
            "cell_type": "markdown",
            "source": "Plot the function for different parameter sets.\n",
            "metadata": {}
        },
        {
            "id": "b6b29ac2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\np_parameters = [0.2, 0.5, 0.7, 0.5]\nn_parameters = [30, 30, 30, 80]\nlinestyles = ['solid', 'dashed', 'dotted', 'dashdot']\nparameters_list = list(zip(p_parameters, n_parameters, linestyles))\ncdf_vals = np.linspace(0, 1, 1000)\nfig, ax = plt.subplots(figsize=(8, 8))\nfor parameter_set in parameters_list:\n    p, n, style = parameter_set\n    nbdtrik_vals = nbdtrik(cdf_vals, n, p)\n    ax.plot(cdf_vals, nbdtrik_vals, label=rf\"$n={n},\\ p={p}$\",\n            ls=style)\nax.legend()\nax.set_ylabel(\"$k$\")\nax.set_xlabel(\"$CDF$\")\nax.set_title(\"Negative binomial percentile function\")\nplt.show()",
            "outputs": []
        },
        {
            "id": "465e036e",
            "cell_type": "markdown",
            "source": "The negative binomial distribution is also available as\n`scipy.stats.nbinom`. The percentile function  method ``ppf``\nreturns the result of `nbdtrik` rounded up to integers:\n",
            "metadata": {}
        },
        {
            "id": "7d962e43",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import nbinom\nq, n, p = 0.6, 5, 0.5\nnbinom.ppf(q, n, p), nbdtrik(q, n, p)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(5.0, 4.800428460273882)"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}