{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "a317e58a",
            "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": "83f7a2df",
            "cell_type": "markdown",
            "source": "`stdtrit` represents the inverse of the student t distribution CDF which\nis available as `stdtr`. Here, we calculate the CDF for ``df`` at\n``x=1``. `stdtrit` then returns ``1`` up to floating point errors\ngiven the same value for `df` and the computed CDF value.\n",
            "metadata": {}
        },
        {
            "id": "11367c6b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.special import stdtr, stdtrit\nimport matplotlib.pyplot as plt\ndf = 3\nx = 1\ncdf_value = stdtr(df, x)\nstdtrit(df, cdf_value)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.9999999994418539"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "11a5cf2b",
            "cell_type": "markdown",
            "source": "Plot the function for three different degrees of freedom.\n",
            "metadata": {}
        },
        {
            "id": "20804a9f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = np.linspace(0, 1, 1000)\nparameters = [(1, \"solid\"), (2, \"dashed\"), (5, \"dotted\")]\nfig, ax = plt.subplots()\nfor (df, linestyle) in parameters:\n    ax.plot(x, stdtrit(df, x), ls=linestyle, label=f\"$df={df}$\")\nax.legend()\nax.set_ylim(-10, 10)\nax.set_title(\"Student t distribution quantile function\")\nplt.show()",
            "outputs": []
        },
        {
            "id": "fb297d72",
            "cell_type": "markdown",
            "source": "The function can be computed for several degrees of freedom at the same\ntime by providing a NumPy array or list for `df`:\n",
            "metadata": {}
        },
        {
            "id": "72806708",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "stdtrit([1, 2, 3], 0.7)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.72654253, 0.6172134 , 0.58438973])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "046765f5",
            "cell_type": "markdown",
            "source": "It is possible to calculate the function at several points for several\ndifferent degrees of freedom simultaneously by providing arrays for `df`\nand `p` with shapes compatible for broadcasting. Compute `stdtrit` at\n4 points for 3 degrees of freedom resulting in an array of shape 3x4.\n",
            "metadata": {}
        },
        {
            "id": "335e64f1",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "dfs = np.array([[1], [2], [3]])\np = np.array([0.2, 0.4, 0.7, 0.8])\ndfs.shape, p.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "((3, 1), (4,))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "293676ed",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "stdtrit(dfs, p)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[-1.37638192, -0.3249197 ,  0.72654253,  1.37638192],\n       [-1.06066017, -0.28867513,  0.6172134 ,  1.06066017],\n       [-0.97847231, -0.27667066,  0.58438973,  0.97847231]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f22077db",
            "cell_type": "markdown",
            "source": "The t distribution is also available as `scipy.stats.t`. Calling `stdtrit`\ndirectly can be much faster than calling the ``ppf`` method of\n`scipy.stats.t`. To get the same results, one must use the following\nparametrization: ``scipy.stats.t(df).ppf(x) = stdtrit(df, x)``.\n",
            "metadata": {}
        },
        {
            "id": "c986ca86",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import t\ndf, x = 3, 0.5\nstdtrit_result = stdtrit(df, x)  # this can be faster than below\nstats_result = t(df).ppf(x)\nstats_result == stdtrit_result  # test that results are equal",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}