{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "0570cee6",
            "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": "0843b3d4",
            "cell_type": "markdown",
            "source": "Compute the function for ``k=10`` and ``n=5`` at ``p=0.5``.\n",
            "metadata": {}
        },
        {
            "id": "7f34622b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.special import nbdtr\nnbdtr(10, 5, 0.5)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.940765380859375"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a9091011",
            "cell_type": "markdown",
            "source": "Compute the function for ``n=10`` and ``p=0.5`` at several points by\nproviding a NumPy array or list for `k`.\n",
            "metadata": {}
        },
        {
            "id": "d198cfcd",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "nbdtr([5, 10, 15], 10, 0.5)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.15087891, 0.58809853, 0.88523853])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "6368ae0f",
            "cell_type": "markdown",
            "source": "Plot the function for four different parameter sets.\n",
            "metadata": {}
        },
        {
            "id": "b2628cd5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nk = np.arange(130)\nn_parameters = [20, 20, 20, 80]\np_parameters = [0.2, 0.5, 0.8, 0.5]\nlinestyles = ['solid', 'dashed', 'dotted', 'dashdot']\nparameters_list = list(zip(p_parameters, n_parameters,\n                           linestyles))\nfig, ax = plt.subplots(figsize=(8, 8))\nfor parameter_set in parameters_list:\n    p, n, style = parameter_set\n    nbdtr_vals = nbdtr(k, n, p)\n    ax.plot(k, nbdtr_vals, label=rf\"$n={n},\\, p={p}$\",\n            ls=style)\nax.legend()\nax.set_xlabel(\"$k$\")\nax.set_title(\"Negative binomial cumulative distribution function\")\nplt.show()",
            "outputs": []
        },
        {
            "id": "b5fe1d00",
            "cell_type": "markdown",
            "source": "The negative binomial distribution is also available as\n`scipy.stats.nbinom`. Using `nbdtr` directly can be much faster than\ncalling the ``cdf`` method of `scipy.stats.nbinom`, especially for small\narrays or individual values. To get the same results one must use the\nfollowing parametrization: ``nbinom(n, p).cdf(k)=nbdtr(k, n, p)``.\n",
            "metadata": {}
        },
        {
            "id": "0d0f0a4c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import nbinom\nk, n, p = 5, 3, 0.5\nnbdtr_res = nbdtr(k, n, p)  # this will often be faster than below\nstats_res = nbinom(n, p).cdf(k)\nstats_res, nbdtr_res  # test that results are equal",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(0.85546875, 0.85546875)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "5d874db9",
            "cell_type": "markdown",
            "source": "`nbdtr` can evaluate different parameter sets by providing arrays with\nshapes compatible for broadcasting for `k`, `n` and `p`. Here we compute\nthe function for three different `k` at four locations `p`, resulting in\na 3x4 array.\n",
            "metadata": {}
        },
        {
            "id": "bfab3a72",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "k = np.array([[5], [10], [15]])\np = np.array([0.3, 0.5, 0.7, 0.9])\nk.shape, p.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "((3, 1), (4,))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d912a15d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "nbdtr(k, 5, p)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0.15026833, 0.62304687, 0.95265101, 0.9998531 ],\n       [0.48450894, 0.94076538, 0.99932777, 0.99999999],\n       [0.76249222, 0.99409103, 0.99999445, 1.        ]])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}