{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "9c60d91b",
            "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": "2d857fca",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.stats import nhypergeom\nimport matplotlib.pyplot as plt",
            "outputs": []
        },
        {
            "id": "c4fb5dab",
            "cell_type": "markdown",
            "source": "Suppose we have a collection of 20 animals, of which 7 are dogs.\nThen if we want to know the probability of finding a given number\nof dogs (successes) in a sample with exactly 12 animals that\naren't dogs (failures), we can initialize a frozen distribution\nand plot the probability mass function:\n",
            "metadata": {}
        },
        {
            "id": "dea7afb5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "M, n, r = [20, 7, 12]\nrv = nhypergeom(M, n, r)\nx = np.arange(0, n+2)\npmf_dogs = rv.pmf(x)",
            "outputs": []
        },
        {
            "id": "73c60f9b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fig = plt.figure()\nax = fig.add_subplot(111)\nax.plot(x, pmf_dogs, 'bo')\nax.vlines(x, 0, pmf_dogs, lw=2)\nax.set_xlabel('# of dogs in our group with given 12 failures')\nax.set_ylabel('nhypergeom PMF')\nplt.show()",
            "outputs": []
        },
        {
            "id": "aa20fc0d",
            "cell_type": "markdown",
            "source": "Instead of using a frozen distribution we can also use `nhypergeom`\nmethods directly.  To for example obtain the probability mass\nfunction, use:\n",
            "metadata": {}
        },
        {
            "id": "29533410",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "prb = nhypergeom.pmf(x, M, n, r)",
            "outputs": []
        },
        {
            "id": "107fe502",
            "cell_type": "markdown",
            "source": "And to generate random numbers:\n",
            "metadata": {}
        },
        {
            "id": "6aa836f0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "R = nhypergeom.rvs(M, n, r, size=10)",
            "outputs": []
        },
        {
            "id": "1b95d05d",
            "cell_type": "markdown",
            "source": "To verify the relationship between `hypergeom` and `nhypergeom`, use:\n",
            "metadata": {}
        },
        {
            "id": "09796db3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import hypergeom, nhypergeom\nM, n, r = 45, 13, 8\nk = 6\nnhypergeom.pmf(k, M, n, r)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.06180776620271643"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "68c7cbc4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "hypergeom.pmf(k, M, n, k+r-1) * (M - n - (r-1)) / (M - (k+r-1))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.06180776620271644"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}