{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "c455b004",
            "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": "c32103f4",
            "cell_type": "markdown",
            "source": "Prepare a symmetric positive definite precision matrix ``P`` and a\ndata point ``x``. (If the precision matrix is not already available,\nconsider the other factory methods of the `Covariance` class.)\n",
            "metadata": {}
        },
        {
            "id": "f4c28c8c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import stats\nrng = np.random.default_rng()\nn = 5\nP = rng.random(size=(n, n))\nP = P @ P.T  # a precision matrix must be positive definite\nx = rng.random(size=n)",
            "outputs": []
        },
        {
            "id": "ad9fac00",
            "cell_type": "markdown",
            "source": "Create the `Covariance` object.\n",
            "metadata": {}
        },
        {
            "id": "91fd51db",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "cov = stats.Covariance.from_precision(P)",
            "outputs": []
        },
        {
            "id": "6425ca9b",
            "cell_type": "markdown",
            "source": "Compare the functionality of the `Covariance` object against\nreference implementations.\n",
            "metadata": {}
        },
        {
            "id": "f9c6a6ed",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = cov.whiten(x)\nref = x @ np.linalg.cholesky(P)\nnp.allclose(res, ref)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "8c072626",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = cov.log_pdet\nref = -np.linalg.slogdet(P)[-1]\nnp.allclose(res, ref)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}