{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from numpy.random import exponential\n", "from lifelines.fitters import ParametricUnivariateFitter\n", "import autograd.numpy as np\n", "np.random.seed(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose we generate data from a mixture of exponentials with no censoring (this could be extended to censoring, but for our application we don't observe censoring). Furthermore, the data is not observed directly, but our instrument has binned the data into integer buckets. \n", "\n", "This model is easy to handle in lifelines. Instead of worrying about the binning, we can treat the system as an interval-censoring problem. That is, if an observation landed in the $i$th bin, then we know the _true_ data point occured somewhere between the $i$th and $i+1$th bin. This is precisely interval censoring. \n", "\n", "We can use *lifelines* custom model semantics to create a mixture model as well. The true model is:\n", "\n", "$$S(t) = p \\exp\\left(\\frac{t}{\\lambda_1}\\right) + (1-p)\\exp\\left(\\frac{t}{\\lambda_2}\\right)$$\n", "\n", "Therefore the cumulative hazard is:\n", "\n", "$$H(t) = -\\log(S(t)) = -\\log\\left(p \\exp\\left(\\frac{t}{\\lambda_1}\\right) + (1-p)\\exp\\left(\\frac{t}{\\lambda_2}\\right)\\right) $$" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "T = [exponential(20) for _ in range(10000)] + [exponential(40) for _ in range(500)]\n", "counts_obs = np.bincount(T)\n", "T_obs = np.arange(np.amax(T))" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0, 100)" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAA0IAAAE/CAYAAABrfXNCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3dfbRddX3n8ffHGwOCgii0I0kkUSMatINtDHa01qU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82yeF4jtMMqKo9wJ8DN9IEoDuAq/Ecp5k10flsSjliPgUhaVYkeTDwIeB3quq7vXXtDwH7KEVNW5JfBm6pqqvneizqjAXAzwJ/U1VPAn5A3zQ4z3EalvbejDU0AfxY4HB+chqTNGOGcT6bT0FoD7CkZ31xWyYNTZIH0oSgv6+qf2iLbx67fNr+95a5Gp8OKk8FTk9yA81U32fS3L/x0HYaCXie03DtBnZX1VXt+qU0wchznGbCycDXqupbVXUP8A805z3PcZpJE53PppQj5lMQ2gwsb582spDmhrvROZo99BgAAAFISURBVB6TDiLt/RnvAq6rqrf2VI0CZ7XLZwH/NNtj08Gnql5bVYurainN+eyKqvo14BPA89pmHm8amqr6JrAryfFt0bOAbXiO08y4EXhKksPaf1/HjjfPcZpJE53PRoEXtU+PewpwR88UugnNqx9UTfJLNHPqR4ALq+pNczwkHUSSPA34V+BafnzPxu/T3Cd0CfBI4OvAC6qq/+Y8acqSPAN4VVX9cpJH0VwhehjwBeCFVXXXXI5PB48kJ9I8nGMhsBN4Mc3/6ek5TkOX5I3AGTRPZf0C8Js092V4jtO0JfkA8AzgaOBm4PXAhxnnfNaG8b+imZ55J/Diqtoy6TbmUxCSJEmSpNkwn6bGSZIkSdKsMAhJkiRJ6hyDkCRJkqTOMQhJkiRJ6hyDkCRJkqTOMQhJkiRJ6hyDkCRJkqTOMQhJkiRJ6pz/D6Jvg31fs63mAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "plt.figure(figsize=(14,5))\n", "plt.hist(counts_obs, bins=T_obs, density=True)\n", "plt.title(\"histogram of observed durations\")\n", "plt.xlim(0, 100)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20.75914285714286\n" ] } ], "source": [ "# To help the model avoid the \"non-identibility\" problem, we can set the upper bound of the first lambda to be \n", "# the average of the observed data, and the lower bound of the second lambda to the same value. Why? \n", "# We wish to partition the postive reals into two halves, each containing one of the lambdas. The sample\n", "# mean of the data is v = p * lambda1 + (1-p) * lambda2, which has the property lambda1 < v < lambda2, thefore \n", "# it will partition the space correctly. \n", "mean_obs = np.average(T_obs, weights=counts_obs)\n", "print(mean_obs)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "number of observations = 10500\n", "number of events observed = 0\n", " log-likelihood = -42593.81\n", " hypothesis = lambda1 != 10.3796, lambda2 != 41.5183, p != 0.5\n", "\n", "---\n", " coef se(coef) lower 0.95 upper 0.95 p -log2(p)\n", "lambda1 20.76 0.63 19.53 21.98 <0.005 203.27\n", "lambda2 38.17 17.50 3.86 72.47 0.85 0.24\n", "p 0.97 0.06 0.86 1.09 <0.005 49.37\n" ] } ], "source": [ "\n", "class MixtureExponential(ParametricUnivariateFitter):\n", "\n", " _fitted_parameter_names = ['lambda1', 'lambda2', 'p']\n", " _bounds = [(0, mean_obs), (mean_obs, None), (0, 1)]\n", "\n", " def _cumulative_hazard(self, params, t):\n", " l1, l2, p = params\n", " return -np.log(p * np.exp(-t/l1) + (1-p) * np.exp(-t/l2))\n", "\n", "\n", "model = MixtureExponential()\n", "model.fit_interval_censoring(\n", " lower_bound=T_obs, \n", " upper_bound=T_obs+1, \n", " weights=counts_obs, \n", " initial_point=np.array([mean_obs/2, mean_obs*2, 0.5])\n", ")\n", "\n", "model.print_summary()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }