{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "3e9b1998",
            "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": "1e5b9d05",
            "cell_type": "markdown",
            "source": "This example shows how the function can be used to calculate the log of\nthe probability mass function for a geometric discrete random variable.\nThe probability mass function of the geometric distribution is defined\nas follows:\n\n.. math:: f(k) = (1-p)^{k-1} p\n\nwhere $p$ is the probability of a single success\nand $1-p$ is the probability of a single failure\nand $k$ is the number of trials to get the first success.\n",
            "metadata": {}
        },
        {
            "id": "40338d94",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.special import xlog1py\np = 0.5\nk = 100\n_pmf = np.power(1 - p, k - 1) * p\n_pmf",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "7.888609052210118e-31"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d6b3e533",
            "cell_type": "markdown",
            "source": "If we take k as a relatively large number the value of the probability\nmass function can become very low. In such cases taking the log of the\npmf would be more suitable as the log function can change the values\nto a scale that is more appropriate to work with.\n",
            "metadata": {}
        },
        {
            "id": "3265859e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "_log_pmf = xlog1py(k - 1, -p) + np.log(p)\n_log_pmf",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "-69.31471805599453"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "9ae1fc05",
            "cell_type": "markdown",
            "source": "We can confirm that we get a value close to the original pmf value by\ntaking the exponential of the log pmf.\n",
            "metadata": {}
        },
        {
            "id": "11a720d8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "_orig_pmf = np.exp(_log_pmf)\nnp.isclose(_pmf, _orig_pmf)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}