{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "8de06db3",
            "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": "b54b6fa6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import stats\nimport matplotlib.pyplot as plt",
            "outputs": []
        },
        {
            "id": "24a2f24d",
            "cell_type": "markdown",
            "source": "We can generate some data and determine the optimal ``lmbda`` in various\nways:\n",
            "metadata": {}
        },
        {
            "id": "ab0a0056",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "rng = np.random.default_rng()\nx = stats.loggamma.rvs(5, size=30, random_state=rng) + 5\ny, lmax_mle = stats.boxcox(x)\nlmax_pearsonr = stats.boxcox_normmax(x)",
            "outputs": []
        },
        {
            "id": "a6938563",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "lmax_mle",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "2.217563431465757"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "71ff6535",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "lmax_pearsonr",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "2.238318660200961"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "4e58cd1e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "stats.boxcox_normmax(x, method='all')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([2.23831866, 2.21756343])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "667f853f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fig = plt.figure()\nax = fig.add_subplot(111)\nprob = stats.boxcox_normplot(x, -10, 10, plot=ax)\nax.axvline(lmax_mle, color='r')\nax.axvline(lmax_pearsonr, color='g', ls='--')",
            "outputs": []
        },
        {
            "id": "df3dbf72",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.show()",
            "outputs": []
        },
        {
            "id": "974624db",
            "cell_type": "markdown",
            "source": "Alternatively, we can define our own `optimizer` function. Suppose we\nare only interested in values of `lmbda` on the interval [6, 7], we\nwant to use `scipy.optimize.minimize_scalar` with ``method='bounded'``,\nand we want to use tighter tolerances when optimizing the log-likelihood\nfunction. To do this, we define a function that accepts positional argument\n`fun` and uses `scipy.optimize.minimize_scalar` to minimize `fun` subject\nto the provided bounds and tolerances:\n",
            "metadata": {}
        },
        {
            "id": "8e1ace3b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy import optimize\noptions = {'xatol': 1e-12}  # absolute tolerance on `x`\ndef optimizer(fun):\n    return optimize.minimize_scalar(fun, bounds=(6, 7),\n                                    method=\"bounded\", options=options)\nstats.boxcox_normmax(x, optimizer=optimizer)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "6.000000000"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}