{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "6d47b8f3",
            "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": "5c680a1f",
            "cell_type": "markdown",
            "source": "Import the necessary modules.\n",
            "metadata": {}
        },
        {
            "id": "91080fa2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.stats import vonmises",
            "outputs": []
        },
        {
            "id": "d070c95d",
            "cell_type": "markdown",
            "source": "Define distribution parameters.\n",
            "metadata": {}
        },
        {
            "id": "3de7dbd4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "loc = 0.5 * np.pi  # circular mean\nkappa = 1  # concentration",
            "outputs": []
        },
        {
            "id": "b6b8ad75",
            "cell_type": "markdown",
            "source": "Compute the probability density at ``x=0`` via the ``pdf`` method.\n",
            "metadata": {}
        },
        {
            "id": "27d0f333",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "vonmises.pdf(0, loc=loc, kappa=kappa)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.12570826359722018"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "1af4737e",
            "cell_type": "markdown",
            "source": "Verify that the percentile function ``ppf`` inverts the cumulative\ndistribution function ``cdf`` up to floating point accuracy.\n",
            "metadata": {}
        },
        {
            "id": "a31717f1",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = 1\ncdf_value = vonmises.cdf(x, loc=loc, kappa=kappa)\nppf_value = vonmises.ppf(cdf_value, loc=loc, kappa=kappa)\nx, cdf_value, ppf_value",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(1, 0.31489339900904967, 1.0000000000000004)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "85870e04",
            "cell_type": "markdown",
            "source": "Draw 1000 random variates by calling the ``rvs`` method.\n",
            "metadata": {}
        },
        {
            "id": "0db45f42",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "sample_size = 1000\nsample = vonmises(loc=loc, kappa=kappa).rvs(sample_size)",
            "outputs": []
        },
        {
            "id": "bf74f907",
            "cell_type": "markdown",
            "source": "Plot the von Mises density on a Cartesian and polar grid to emphasize\nthat it is a circular distribution.\n",
            "metadata": {}
        },
        {
            "id": "c9620736",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fig = plt.figure(figsize=(12, 6))\nleft = plt.subplot(121)\nright = plt.subplot(122, projection='polar')\nx = np.linspace(-np.pi, np.pi, 500)\nvonmises_pdf = vonmises.pdf(x, loc=loc, kappa=kappa)\nticks = [0, 0.15, 0.3]",
            "outputs": []
        },
        {
            "id": "a559b4a2",
            "cell_type": "markdown",
            "source": "The left image contains the Cartesian plot.\n",
            "metadata": {}
        },
        {
            "id": "2617bb94",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "left.plot(x, vonmises_pdf)\nleft.set_yticks(ticks)\nnumber_of_bins = int(np.sqrt(sample_size))\nleft.hist(sample, density=True, bins=number_of_bins)\nleft.set_title(\"Cartesian plot\")\nleft.set_xlim(-np.pi, np.pi)\nleft.grid(True)",
            "outputs": []
        },
        {
            "id": "8b1ad003",
            "cell_type": "markdown",
            "source": "The right image contains the polar plot.\n",
            "metadata": {}
        },
        {
            "id": "ec0797b6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "right.plot(x, vonmises_pdf, label=\"PDF\")\nright.set_yticks(ticks)\nright.hist(sample, density=True, bins=number_of_bins,\n           label=\"Histogram\")\nright.set_title(\"Polar plot\")\nright.legend(bbox_to_anchor=(0.15, 1.06))",
            "outputs": []
        }
    ]
}