{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "b2f2e105",
            "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": "a7235785",
            "cell_type": "markdown",
            "source": "QMC quadrature is particularly useful for computing integrals in higher\ndimensions. An example integrand is the probability density function\nof a multivariate normal distribution.\n",
            "metadata": {}
        },
        {
            "id": "f3cc188a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import stats\ndim = 8\nmean = np.zeros(dim)\ncov = np.eye(dim)\ndef func(x):\n    # `multivariate_normal` expects the _last_ axis to correspond with\n    # the dimensionality of the space, so `x` must be transposed\n    return stats.multivariate_normal.pdf(x.T, mean, cov)",
            "outputs": []
        },
        {
            "id": "1c3f6362",
            "cell_type": "markdown",
            "source": "To compute the integral over the unit hypercube:\n",
            "metadata": {}
        },
        {
            "id": "d4ded6f5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.integrate import qmc_quad\na = np.zeros(dim)\nb = np.ones(dim)\nrng = np.random.default_rng()\nqrng = stats.qmc.Halton(d=dim, seed=rng)\nn_estimates = 8\nres = qmc_quad(func, a, b, n_estimates=n_estimates, qrng=qrng)\nres.integral, res.standard_error",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(0.00018429555666024108, 1.0389431116001344e-07)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "bee6f774",
            "cell_type": "markdown",
            "source": "A two-sided, 99% confidence interval for the integral may be estimated\nas:\n",
            "metadata": {}
        },
        {
            "id": "f1befe19",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "t = stats.t(df=n_estimates-1, loc=res.integral,\n            scale=res.standard_error)\nt.interval(0.99)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(0.0001839319802536469, 0.00018465913306683527)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "947f776f",
            "cell_type": "markdown",
            "source": "Indeed, the value reported by `scipy.stats.multivariate_normal` is\nwithin this range.\n",
            "metadata": {}
        },
        {
            "id": "a94fbac9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "stats.multivariate_normal.cdf(b, mean, cov, lower_limit=a)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.00018430867675187443"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}