{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "7d38aa3b",
            "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": "0104b07b",
            "cell_type": "markdown",
            "source": "Compute the function for ``a=1``, ``b=2`` at ``x=5``.\n",
            "metadata": {}
        },
        {
            "id": "0dbf7783",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.special import gdtr\nimport matplotlib.pyplot as plt\ngdtr(1., 2., 5.)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.9595723180054873"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "56dd2c15",
            "cell_type": "markdown",
            "source": "Compute the function for ``a=1`` and ``b=2`` at several points by\nproviding a NumPy array for `x`.\n",
            "metadata": {}
        },
        {
            "id": "429d1a5c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "xvalues = np.array([1., 2., 3., 4])\ngdtr(1., 1., xvalues)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.63212056, 0.86466472, 0.95021293, 0.98168436])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2db528b0",
            "cell_type": "markdown",
            "source": "`gdtr` can evaluate different parameter sets by providing arrays with\nbroadcasting compatible shapes for `a`, `b` and `x`. Here we compute the\nfunction for three different `a` at four positions `x` and ``b=3``,\nresulting in a 3x4 array.\n",
            "metadata": {}
        },
        {
            "id": "f4e97cb4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a = np.array([[0.5], [1.5], [2.5]])\nx = np.array([1., 2., 3., 4])\na.shape, x.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "((3, 1), (4,))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "78e73cae",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "gdtr(a, 3., x)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0.01438768, 0.0803014 , 0.19115317, 0.32332358],\n       [0.19115317, 0.57680992, 0.82642193, 0.9380312 ],\n       [0.45618688, 0.87534798, 0.97974328, 0.9972306 ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "3b85ac7f",
            "cell_type": "markdown",
            "source": "Plot the function for four different parameter sets.\n",
            "metadata": {}
        },
        {
            "id": "2fff4c25",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a_parameters = [0.3, 1, 2, 6]\nb_parameters = [2, 10, 15, 20]\nlinestyles = ['solid', 'dashed', 'dotted', 'dashdot']\nparameters_list = list(zip(a_parameters, b_parameters, linestyles))\nx = np.linspace(0, 30, 1000)\nfig, ax = plt.subplots()\nfor parameter_set in parameters_list:\n    a, b, style = parameter_set\n    gdtr_vals = gdtr(a, b, x)\n    ax.plot(x, gdtr_vals, label=fr\"$a= {a},\\, b={b}$\", ls=style)\nax.legend()\nax.set_xlabel(\"$x$\")\nax.set_title(\"Gamma distribution cumulative distribution function\")\nplt.show()",
            "outputs": []
        },
        {
            "id": "8a41dcea",
            "cell_type": "markdown",
            "source": "The gamma distribution is also available as `scipy.stats.gamma`. Using\n`gdtr` directly can be much faster than calling the ``cdf`` method of\n`scipy.stats.gamma`, especially for small arrays or individual values.\nTo get the same results one must use the following parametrization:\n``stats.gamma(b, scale=1/a).cdf(x)=gdtr(a, b, x)``.\n",
            "metadata": {}
        },
        {
            "id": "eda6539c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import gamma\na = 2.\nb = 3\nx = 1.\ngdtr_result = gdtr(a, b, x)  # this will often be faster than below\ngamma_dist_result = gamma(b, scale=1/a).cdf(x)\ngdtr_result == gamma_dist_result  # test that results are equal",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}