{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "df8d60f2",
            "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": "1fd3700e",
            "cell_type": "markdown",
            "source": "Passing a ``np.random.Generator`` instance for better performance:\n",
            "metadata": {}
        },
        {
            "id": "4b9ba9d8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import scipy as sp\nimport numpy as np\nrng = np.random.default_rng()\nS = sp.sparse.random(3, 4, density=0.25, rng=rng)",
            "outputs": []
        },
        {
            "id": "02964884",
            "cell_type": "markdown",
            "source": "Providing a sampler for the values:\n",
            "metadata": {}
        },
        {
            "id": "8ea2c95c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "rvs = sp.stats.poisson(25, loc=10).rvs\nS = sp.sparse.random(3, 4, density=0.25, rng=rng, data_rvs=rvs)\nS.toarray()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 36.,   0.,  33.,   0.],   # random\n       [  0.,   0.,   0.,   0.],\n       [  0.,   0.,  36.,   0.]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "50ffa76c",
            "cell_type": "markdown",
            "source": "Building a custom distribution.\nThis example builds a squared normal from np.random:\n",
            "metadata": {}
        },
        {
            "id": "e07fc3fe",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "def np_normal_squared(size=None, rng=rng):\n    return rng.standard_normal(size) ** 2\nS = sp.sparse.random(3, 4, density=0.25, rng=rng,\n                     data_rvs=np_normal_squared)",
            "outputs": []
        },
        {
            "id": "bf0c375b",
            "cell_type": "markdown",
            "source": "Or we can build it from sp.stats style rvs functions:\n",
            "metadata": {}
        },
        {
            "id": "1ebb1e66",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "def sp_stats_normal_squared(size=None, rng=rng):\n    std_normal = sp.stats.distributions.norm_gen().rvs\n    return std_normal(size=size, random_state=rng) ** 2\nS = sp.sparse.random(3, 4, density=0.25, rng=rng,\n                     data_rvs=sp_stats_normal_squared)",
            "outputs": []
        },
        {
            "id": "a4a62c0c",
            "cell_type": "markdown",
            "source": "Or we can subclass sp.stats rv_continuous or rv_discrete:\n",
            "metadata": {}
        },
        {
            "id": "6c5d9a1f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "class NormalSquared(sp.stats.rv_continuous):\n    def _rvs(self,  size=None, random_state=rng):\n        return rng.standard_normal(size) ** 2\nX = NormalSquared()\nY = X()  # get a frozen version of the distribution\nS = sp.sparse.random(3, 4, density=0.25, rng=rng, data_rvs=Y.rvs)",
            "outputs": []
        }
    ]
}