{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "4452f24e",
            "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": "6a3e8bd1",
            "cell_type": "markdown",
            "source": "Generate some data to fit: draw random variates from the `beta`\ndistribution\n",
            "metadata": {}
        },
        {
            "id": "68d831f9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.stats import beta\na, b = 1., 2.\nrng = np.random.default_rng()\nx = beta.rvs(a, b, size=1000, random_state=rng)",
            "outputs": []
        },
        {
            "id": "bc48000e",
            "cell_type": "markdown",
            "source": "Now we can fit all four parameters (``a``, ``b``, ``loc`` and\n``scale``):\n",
            "metadata": {}
        },
        {
            "id": "88b34f50",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a1, b1, loc1, scale1 = beta.fit(x)\na1, b1, loc1, scale1",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(1.0198945204435628, 1.9484708982737828, 4.372241314917588e-05, 0.9979078845964814)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d575bf58",
            "cell_type": "markdown",
            "source": "The fit can be done also using a custom optimizer:\n",
            "metadata": {}
        },
        {
            "id": "26d2b97d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.optimize import minimize\ndef custom_optimizer(func, x0, args=(), disp=0):\n    res = minimize(func, x0, args, method=\"slsqp\", options={\"disp\": disp})\n    if res.success:\n        return res.x\n    raise RuntimeError('optimization routine failed')\na1, b1, loc1, scale1 = beta.fit(x, method=\"MLE\", optimizer=custom_optimizer)\na1, b1, loc1, scale1",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(1.0198821087258905, 1.948484145914738, 4.3705304486881485e-05, 0.9979104663953395)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "5e09a8e1",
            "cell_type": "markdown",
            "source": "We can also use some prior knowledge about the dataset: let's keep\n``loc`` and ``scale`` fixed:\n",
            "metadata": {}
        },
        {
            "id": "d345f699",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a1, b1, loc1, scale1 = beta.fit(x, floc=0, fscale=1)\nloc1, scale1",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(0, 1)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "8aa94c7e",
            "cell_type": "markdown",
            "source": "We can also keep shape parameters fixed by using ``f``-keywords. To\nkeep the zero-th shape parameter ``a`` equal 1, use ``f0=1`` or,\nequivalently, ``fa=1``:\n",
            "metadata": {}
        },
        {
            "id": "81cb10c0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a1, b1, loc1, scale1 = beta.fit(x, fa=1, floc=0, fscale=1)\na1",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "1"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "b96b08e5",
            "cell_type": "markdown",
            "source": "Not all distributions return estimates for the shape parameters.\n``norm`` for example just returns estimates for location and scale:\n",
            "metadata": {}
        },
        {
            "id": "08ee8e60",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import norm\nx = norm.rvs(a, b, size=1000, random_state=123)\nloc1, scale1 = norm.fit(x)\nloc1, scale1",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(0.92087172783841631, 2.0015750750324668)"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}