{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "c6dec618",
            "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": "f5fd3517",
            "cell_type": "markdown",
            "source": "`nbdtri` is the inverse of `nbdtr` with respect to `p`.\nUp to floating point errors the following holds:\n``nbdtri(k, n, nbdtr(k, n, p))=p``.\n",
            "metadata": {}
        },
        {
            "id": "e932fed3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.special import nbdtri, nbdtr\nk, n, y = 5, 10, 0.2\ncdf_val = nbdtr(k, n, y)\nnbdtri(k, n, cdf_val)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.20000000000000004"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "7dc38104",
            "cell_type": "markdown",
            "source": "Compute the function for ``k=10`` and ``n=5`` at several points by\nproviding a NumPy array or list for `y`.\n",
            "metadata": {}
        },
        {
            "id": "f74eeda9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "y = np.array([0.1, 0.4, 0.8])\nnbdtri(3, 5, y)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.34462319, 0.51653095, 0.69677416])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e17c21da",
            "cell_type": "markdown",
            "source": "Plot the function for three different parameter sets.\n",
            "metadata": {}
        },
        {
            "id": "8e90c257",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nn_parameters = [5, 20, 30, 30]\nk_parameters = [20, 20, 60, 80]\nlinestyles = ['solid', 'dashed', 'dotted', 'dashdot']\nparameters_list = list(zip(n_parameters, k_parameters, linestyles))\ncdf_vals = np.linspace(0, 1, 1000)\nfig, ax = plt.subplots(figsize=(8, 8))\nfor parameter_set in parameters_list:\n    n, k, style = parameter_set\n    nbdtri_vals = nbdtri(k, n, cdf_vals)\n    ax.plot(cdf_vals, nbdtri_vals, label=rf\"$k={k},\\ n={n}$\",\n            ls=style)\nax.legend()\nax.set_ylabel(\"$p$\")\nax.set_xlabel(\"$CDF$\")\ntitle = \"nbdtri: inverse of negative binomial CDF with respect to $p$\"\nax.set_title(title)\nplt.show()",
            "outputs": []
        },
        {
            "id": "04c60561",
            "cell_type": "markdown",
            "source": "`nbdtri` 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": "ee6686b6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "k = np.array([[5], [10], [15]])\ny = np.array([0.3, 0.5, 0.7, 0.9])\nk.shape, y.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "((3, 1), (4,))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "1a01b8d0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "nbdtri(k, 5, y)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0.37258157, 0.45169416, 0.53249956, 0.64578407],\n       [0.24588501, 0.30451981, 0.36778453, 0.46397088],\n       [0.18362101, 0.22966758, 0.28054743, 0.36066188]])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}