{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "c461c33e",
            "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": "da203364",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.special import tklmbda, expit",
            "outputs": []
        },
        {
            "id": "671ff1e9",
            "cell_type": "markdown",
            "source": "Compute the cumulative distribution function (CDF) of the Tukey lambda\ndistribution at several ``x`` values for `lmbda` = -1.5.\n",
            "metadata": {}
        },
        {
            "id": "82fdb856",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = np.linspace(-2, 2, 9)\nx",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5,  2. ])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "b2682e1a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "tklmbda(x, -1.5)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.34688734, 0.3786554 , 0.41528805, 0.45629737, 0.5       ,\n       0.54370263, 0.58471195, 0.6213446 , 0.65311266])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a76472a7",
            "cell_type": "markdown",
            "source": "When `lmbda` is 0, the function is the logistic sigmoid function,\nwhich is implemented in `scipy.special` as `expit`.\n",
            "metadata": {}
        },
        {
            "id": "30074eff",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "tklmbda(x, 0)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.11920292, 0.18242552, 0.26894142, 0.37754067, 0.5       ,\n       0.62245933, 0.73105858, 0.81757448, 0.88079708])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f8d663c8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "expit(x)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.11920292, 0.18242552, 0.26894142, 0.37754067, 0.5       ,\n       0.62245933, 0.73105858, 0.81757448, 0.88079708])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d35f6aed",
            "cell_type": "markdown",
            "source": "When `lmbda` is 1, the Tukey lambda distribution is uniform on the\ninterval [-1, 1], so the CDF increases linearly.\n",
            "metadata": {}
        },
        {
            "id": "572fe847",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "t = np.linspace(-1, 1, 9)\ntklmbda(t, 1)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.   , 0.125, 0.25 , 0.375, 0.5  , 0.625, 0.75 , 0.875, 1.   ])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "03b598ee",
            "cell_type": "markdown",
            "source": "In the following, we generate plots for several values of `lmbda`.\n\nThe first figure shows graphs for `lmbda` <= 0.\n",
            "metadata": {}
        },
        {
            "id": "93a6006f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "styles = ['-', '-.', '--', ':']\nfig, ax = plt.subplots()\nx = np.linspace(-12, 12, 500)\nfor k, lmbda in enumerate([-1.0, -0.5, 0.0]):\n    y = tklmbda(x, lmbda)\n    ax.plot(x, y, styles[k], label=rf'$\\lambda$ = {lmbda:-4.1f}')",
            "outputs": []
        },
        {
            "id": "c19f11e4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ax.set_title(r'tklmbda(x, $\\lambda$)')\nax.set_label('x')\nax.legend(framealpha=1, shadow=True)\nax.grid(True)",
            "outputs": []
        },
        {
            "id": "b9a60332",
            "cell_type": "markdown",
            "source": "The second figure shows graphs for `lmbda` > 0.  The dots in the\ngraphs show the bounds of the support of the distribution.\n",
            "metadata": {}
        },
        {
            "id": "559ce1fa",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fig, ax = plt.subplots()\nx = np.linspace(-4.2, 4.2, 500)\nlmbdas = [0.25, 0.5, 1.0, 1.5]\nfor k, lmbda in enumerate(lmbdas):\n    y = tklmbda(x, lmbda)\n    ax.plot(x, y, styles[k], label=fr'$\\lambda$ = {lmbda}')",
            "outputs": []
        },
        {
            "id": "99ba77e6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ax.set_prop_cycle(None)\nfor lmbda in lmbdas:\n    ax.plot([-1/lmbda, 1/lmbda], [0, 1], '.', ms=8)",
            "outputs": []
        },
        {
            "id": "6e4effe0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ax.set_title(r'tklmbda(x, $\\lambda$)')\nax.set_xlabel('x')\nax.legend(framealpha=1, shadow=True)\nax.grid(True)",
            "outputs": []
        },
        {
            "id": "b4bd27bd",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.tight_layout()\nplt.show()",
            "outputs": []
        },
        {
            "id": "bff38ad2",
            "cell_type": "markdown",
            "source": "The CDF of the Tukey lambda distribution is also implemented as the\n``cdf`` method of `scipy.stats.tukeylambda`.  In the following,\n``tukeylambda.cdf(x, -0.5)`` and ``tklmbda(x, -0.5)`` compute the\nsame values:\n",
            "metadata": {}
        },
        {
            "id": "0eacc59a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import tukeylambda\nx = np.linspace(-2, 2, 9)",
            "outputs": []
        },
        {
            "id": "6130dc51",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "tukeylambda.cdf(x, -0.5)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.21995157, 0.27093858, 0.33541677, 0.41328161, 0.5       ,\n       0.58671839, 0.66458323, 0.72906142, 0.78004843])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "efd109c3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "tklmbda(x, -0.5)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.21995157, 0.27093858, 0.33541677, 0.41328161, 0.5       ,\n       0.58671839, 0.66458323, 0.72906142, 0.78004843])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "126e0bd4",
            "cell_type": "markdown",
            "source": "The implementation in ``tukeylambda`` also provides location and scale\nparameters, and other methods such as ``pdf()`` (the probability\ndensity function) and ``ppf()`` (the inverse of the CDF), so for\nworking with the Tukey lambda distribution, ``tukeylambda`` is more\ngenerally useful.  The primary advantage of ``tklmbda`` is that it is\nsignificantly faster than ``tukeylambda.cdf``.\n",
            "metadata": {}
        }
    ]
}