{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "afdef439",
            "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": "2635d536",
            "cell_type": "markdown",
            "source": "In the most general case, a censored data set may contain values that\nare left-censored, right-censored, interval-censored, and uncensored.\nFor example, here we create a data set with five observations.  Two\nare uncensored (values 1 and 1.5), one is a left-censored observation\nof 0, one is a right-censored observation of 10 and one is\ninterval-censored in the interval [2, 3].\n",
            "metadata": {}
        },
        {
            "id": "9e2cd137",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.stats import CensoredData\ndata = CensoredData(uncensored=[1, 1.5], left=[0], right=[10],\n                    interval=[[2, 3]])\nprint(data)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "CensoredData(5 values: 2 not censored, 1 left-censored,\n1 right-censored, 1 interval-censored)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "fc4d6056",
            "cell_type": "markdown",
            "source": "Equivalently,\n",
            "metadata": {}
        },
        {
            "id": "25109103",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "data = CensoredData(interval=[[1, 1],\n                              [1.5, 1.5],\n                              [-np.inf, 0],\n                              [10, np.inf],\n                              [2, 3]])\nprint(data)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "CensoredData(5 values: 2 not censored, 1 left-censored,\n1 right-censored, 1 interval-censored)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "14aaf0b1",
            "cell_type": "markdown",
            "source": "A common case is to have a mix of uncensored observations and censored\nobservations that are all right-censored (or all left-censored). For\nexample, consider an experiment in which six devices are started at\nvarious times and left running until they fail.  Assume that time is\nmeasured in hours, and the experiment is stopped after 30 hours, even\nif all the devices have not failed by that time.  We might end up with\ndata such as this\n```\n\nDevice  Start-time  Fail-time  Time-to-failure\n1         0         13           13\n2         2         24           22\n3         5         22           17\n4         8         23           15\n5        10        ***          >20\n6        12        ***          >18\n\n```\nTwo of the devices had not failed when the experiment was stopped;\nthe observations of the time-to-failure for these two devices are\nright-censored.  We can represent this data with\n",
            "metadata": {}
        },
        {
            "id": "ab0246b5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "data = CensoredData(uncensored=[13, 22, 17, 15], right=[20, 18])\nprint(data)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "CensoredData(6 values: 4 not censored, 2 right-censored)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "8e7ab2ce",
            "cell_type": "markdown",
            "source": "Alternatively, we can use the method `CensoredData.right_censored` to\ncreate a representation of this data.  The time-to-failure observations\nare put the list ``ttf``.  The ``censored`` list indicates which values\nin ``ttf`` are censored.\n",
            "metadata": {}
        },
        {
            "id": "b5792e3f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ttf = [13, 22, 17, 15, 20, 18]\ncensored = [False, False, False, False, True, True]",
            "outputs": []
        },
        {
            "id": "eab7d98a",
            "cell_type": "markdown",
            "source": "Pass these lists to `CensoredData.right_censored` to create an\ninstance of `CensoredData`.\n",
            "metadata": {}
        },
        {
            "id": "10116f04",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "data = CensoredData.right_censored(ttf, censored)\nprint(data)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "CensoredData(6 values: 4 not censored, 2 right-censored)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d0183c74",
            "cell_type": "markdown",
            "source": "If the input data is interval censored and already stored in two\narrays, one holding the low end of the intervals and another\nholding the high ends, the class method ``interval_censored`` can\nbe used to create the `CensoredData` instance.\n\nThis example creates an instance with four interval-censored values.\nThe intervals are [10, 11], [0.5, 1], [2, 3], and [12.5, 13.5].\n",
            "metadata": {}
        },
        {
            "id": "0e50106a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a = [10, 0.5, 2, 12.5]  # Low ends of the intervals\nb = [11, 1.0, 3, 13.5]  # High ends of the intervals\ndata = CensoredData.interval_censored(low=a, high=b)\nprint(data)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "CensoredData(4 values: 0 not censored, 4 interval-censored)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "6a233125",
            "cell_type": "markdown",
            "source": "Finally, we create and censor some data from the `weibull_min`\ndistribution, and then fit `weibull_min` to that data. We'll assume\nthat the location parameter is known to be 0.\n",
            "metadata": {}
        },
        {
            "id": "24022f31",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats import weibull_min\nrng = np.random.default_rng()",
            "outputs": []
        },
        {
            "id": "72c4b164",
            "cell_type": "markdown",
            "source": "Create the random data set.\n",
            "metadata": {}
        },
        {
            "id": "ac173a4b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = weibull_min.rvs(2.5, loc=0, scale=30, size=250, random_state=rng)\nx[x > 40] = 40  # Right-censor values greater or equal to 40.",
            "outputs": []
        },
        {
            "id": "81bb5d46",
            "cell_type": "markdown",
            "source": "Create the `CensoredData` instance with the `right_censored` method.\nThe censored values are those where the value is 40.\n",
            "metadata": {}
        },
        {
            "id": "7524d04c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "data = CensoredData.right_censored(x, x == 40)\nprint(data)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "CensoredData(250 values: 215 not censored, 35 right-censored)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ac7f5c2c",
            "cell_type": "markdown",
            "source": "35 values have been right-censored.\n\nFit `weibull_min` to the censored data.  We expect to shape and scale\nto be approximately 2.5 and 30, respectively.\n",
            "metadata": {}
        },
        {
            "id": "7be2d86e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "weibull_min.fit(data, floc=0)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(2.3575922823897315, 0, 30.40650074451254)"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}