{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "e282b539",
            "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": "81bba2db",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy import stats\nfrom scipy import special",
            "outputs": []
        },
        {
            "id": "1b6622d6",
            "cell_type": "markdown",
            "source": "Create a `ContinuousDistribution` from `scipy.stats.loguniform`.\n",
            "metadata": {}
        },
        {
            "id": "6adaedf7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "LogUniform = stats.make_distribution(stats.loguniform)\nX = LogUniform(a=1.0, b=3.0)\nnp.isclose((X + 0.25).median(), stats.loguniform.ppf(0.5, 1, 3, loc=0.25))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "np.True_"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "acb1d9cd",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "X.plot()\nsample = X.sample(10000, rng=np.random.default_rng())\nplt.hist(sample, density=True, bins=30)\nplt.legend(('pdf', 'histogram'))\nplt.show()",
            "outputs": []
        },
        {
            "id": "b8568543",
            "cell_type": "markdown",
            "source": "Create a custom distribution.\n",
            "metadata": {}
        },
        {
            "id": "dfdf4328",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "class MyLogUniform:\n    @property\n    def __make_distribution_version__(self):\n        return \"1.16.0\"\n\n    @property\n    def parameters(self):\n        return {'a': {'endpoints': (0, np.inf),\n                      'inclusive': (False, False)},\n                'b': {'endpoints': ('a', np.inf),\n                      'inclusive': (False, False)}}\n\n    @property\n    def support(self):\n        return {'endpoints': ('a', 'b'), 'inclusive': (True, True)}\n\n    def pdf(self, x, a, b):\n        return 1 / (x * (np.log(b)- np.log(a)))\n\nMyLogUniform = stats.make_distribution(MyLogUniform())\nY = MyLogUniform(a=1.0, b=3.0)\nnp.isclose(Y.cdf(2.), X.cdf(2.))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "np.True_"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "92b4ab34",
            "cell_type": "markdown",
            "source": "Create a custom distribution with variable support.\n",
            "metadata": {}
        },
        {
            "id": "1527e7ec",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "class MyUniformCube:\n    @property\n    def __make_distribution_version__(self):\n        return \"1.16.0\"\n\n    @property\n    def parameters(self):\n        return {\"a\": (-np.inf, np.inf),\n                \"b\": {'endpoints':('a', np.inf), 'inclusive':(True, False)}}\n\n    @property\n    def support(self):\n        def left(*, a, b):\n            return a**3\n\n        def right(*, a, b):\n            return b**3\n        return (left, right)\n\n    def pdf(self, x, *, a, b):\n        return 1 / (3*(b - a)*np.cbrt(x)**2)\n\n    def cdf(self, x, *, a, b):\n        return (np.cbrt(x) - a) / (b - a)\n\nMyUniformCube = stats.make_distribution(MyUniformCube())\nX = MyUniformCube(a=-2, b=2)\nY = stats.Uniform(a=-2, b=2)**3\nX.support()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(-8.0, 8.0)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "74de660d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "np.isclose(X.cdf(2.1), Y.cdf(2.1))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "np.True_"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "71754b95",
            "cell_type": "markdown",
            "source": "Create a custom distribution with multiple parameterizations. Here we create a\ncustom version of the beta distribution that has an alternative parameterization\nin terms of the mean ``mu`` and a dispersion parameter ``nu``.\n",
            "metadata": {}
        },
        {
            "id": "ba36c62c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "class MyBeta:\n    @property\n    def __make_distribution_version__(self):\n        return \"1.16.0\"\n\n    @property\n    def parameters(self):\n        return ({\"a\": (0, np.inf), \"b\": (0, np.inf)},\n                {\"mu\": (0, 1), \"nu\": (0, np.inf)})\n\n    def process_parameters(self, a=None, b=None, mu=None, nu=None):\n        if a is not None and b is not None:\n            nu = a + b\n            mu = a / nu\n        else:\n            a = mu * nu\n            b = nu - a\n        return dict(a=a, b=b, mu=mu, nu=nu)\n\n    @property\n    def support(self):\n        return {'endpoints': (0, 1)}\n\n    def pdf(self, x, a, b, mu, nu):\n        return special._ufuncs._beta_pdf(x, a, b)\n\n    def cdf(self, x, a, b, mu, nu):\n        return special.betainc(a, b, x)\n\nMyBeta = stats.make_distribution(MyBeta())\nX = MyBeta(a=2.0, b=2.0)\nY = MyBeta(mu=0.5, nu=4.0)\nnp.isclose(X.pdf(0.3), Y.pdf(0.3))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "np.True_"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}