{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "002c18eb",
            "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": "cfe5d055",
            "cell_type": "markdown",
            "source": "Calculate the quality of the sample using the minimum euclidean distance\n(the defaults):\n",
            "metadata": {}
        },
        {
            "id": "63fe6d66",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.stats import qmc\nrng = np.random.default_rng()\nsample = qmc.LatinHypercube(d=2, rng=rng).random(50)\nqmc.geometric_discrepancy(sample)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.03708161435687876"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "fcf55e5a",
            "cell_type": "markdown",
            "source": "Calculate the quality using the mean edge length in the minimum\nspanning tree:\n",
            "metadata": {}
        },
        {
            "id": "596cba82",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "qmc.geometric_discrepancy(sample, method='mst')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.1105149978798376"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "9e120f31",
            "cell_type": "markdown",
            "source": "Display the minimum spanning tree and the points with\nthe smallest distance:\n",
            "metadata": {}
        },
        {
            "id": "196270ce",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nfrom matplotlib.lines import Line2D\nfrom scipy.sparse.csgraph import minimum_spanning_tree\nfrom scipy.spatial.distance import pdist, squareform\ndist = pdist(sample)\nmst = minimum_spanning_tree(squareform(dist))\nedges = np.where(mst.toarray() > 0)\nedges = np.asarray(edges).T\nmin_dist = np.min(dist)\nmin_idx = np.argwhere(squareform(dist) == min_dist)[0]\nfig, ax = plt.subplots(figsize=(10, 5))\n_ = ax.set(aspect='equal', xlabel=r'$x_1$', ylabel=r'$x_2$',\n           xlim=[0, 1], ylim=[0, 1])\nfor edge in edges:\n    ax.plot(sample[edge, 0], sample[edge, 1], c='k')\nax.scatter(sample[:, 0], sample[:, 1])\nax.add_patch(plt.Circle(sample[min_idx[0]], min_dist, color='red', fill=False))\nmarkers = [\n    Line2D([0], [0], marker='o', lw=0, label='Sample points'),\n    Line2D([0], [0], color='k', label='Minimum spanning tree'),\n    Line2D([0], [0], marker='o', lw=0, markerfacecolor='w', markeredgecolor='r',\n           label='Minimum point-to-point distance'),\n]\nax.legend(handles=markers, loc='center left', bbox_to_anchor=(1, 0.5));\nplt.show()",
            "outputs": []
        }
    ]
}