{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "806cc95c",
            "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": "e382ac9c",
            "cell_type": "markdown",
            "source": "Generate some random two-dimensional data:\n",
            "metadata": {}
        },
        {
            "id": "b4a66dd5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import stats\ndef measure(n):\n    \"Measurement model, return two coupled measurements.\"\n    m1 = np.random.normal(size=n)\n    m2 = np.random.normal(scale=0.5, size=n)\n    return m1+m2, m1-m2",
            "outputs": []
        },
        {
            "id": "0e4f4b40",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "m1, m2 = measure(2000)\nxmin = m1.min()\nxmax = m1.max()\nymin = m2.min()\nymax = m2.max()",
            "outputs": []
        },
        {
            "id": "8598511b",
            "cell_type": "markdown",
            "source": "Perform a kernel density estimate on the data:\n",
            "metadata": {}
        },
        {
            "id": "abbb93d7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]\npositions = np.vstack([X.ravel(), Y.ravel()])\nvalues = np.vstack([m1, m2])\nkernel = stats.gaussian_kde(values)\nZ = np.reshape(kernel(positions).T, X.shape)",
            "outputs": []
        },
        {
            "id": "cb4c60c1",
            "cell_type": "markdown",
            "source": "Plot the results:\n",
            "metadata": {}
        },
        {
            "id": "c1e0403d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nfig, ax = plt.subplots()\nax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r,\n          extent=[xmin, xmax, ymin, ymax])\nax.plot(m1, m2, 'k.', markersize=2)\nax.set_xlim([xmin, xmax])\nax.set_ylim([ymin, ymax])\nplt.show()",
            "outputs": []
        },
        {
            "id": "1f66c8c2",
            "cell_type": "markdown",
            "source": "Compare against manual KDE at a point:\n",
            "metadata": {}
        },
        {
            "id": "348f71f8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "point = [1, 2]\nmean = values.T\ncov = kernel.factor**2 * np.cov(values)\nX = stats.multivariate_normal(cov=cov)\nres = kernel.pdf(point)\nref = X.pdf(point - mean).sum() / len(mean)\nnp.allclose(res, ref)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}