Skip to content

Instantly share code, notes, and snippets.

@jon1scr
Created February 18, 2021 15:32
Show Gist options
  • Save jon1scr/e79d4ae88a0ecb6a99e24b9e1784e4f8 to your computer and use it in GitHub Desktop.
Save jon1scr/e79d4ae88a0ecb6a99e24b9e1784e4f8 to your computer and use it in GitHub Desktop.
RiskEngineering notebooks
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Poisson processes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://risk-engineering.org/static/img/logo-RE.png\" width=\"100\" alt=\"\" style=\"float:right;margin:15px;\">\n",
"\n",
"This notebook is an element of the [risk-engineering.org courseware](https://risk-engineering.org/). It can be distributed under the terms of the [Creative Commons Attribution-ShareAlike licence](https://creativecommons.org/licenses/by-sa/4.0/).\n",
"\n",
"Author: Eric Marsden <eric.marsden@risk-engineering.org>. \n",
"\n",
"---\n",
"\n",
"This notebook contains an introduction to the use of Python and SciPy to analyze data concerning Poisson processes. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy\n",
"import scipy.stats\n",
"import pandas\n",
"import matplotlib.pyplot as plt\n",
"plt.style.use(\"bmh\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Earthquakes\n",
"\n",
"Suppose we live in an area where there are typically 0.03 earthquakes of intensity 5 or more per year. Assume that earthquake arrival is a [Poisson counting process](https://en.wikipedia.org/wiki/Poisson_point_process) (the interval between earthquakes follows an exponential distribution, and events are independent). \n",
"\n",
"Let's simulate the random intervals between the next earthquakes of intensity 5 or greater."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1.23658881e+02, 4.66527507e-01, 1.54656670e+01, 7.25553121e+01,\n",
" 1.20041381e+01, 6.26361707e+01, 3.96577482e-01, 3.04319082e+01,\n",
" 4.66770724e+01, 2.01411541e+01, 1.70674321e+01, 4.00946655e+01,\n",
" 5.45694133e+01, 4.23367155e+00, 7.46744783e+01, 1.18744067e+01,\n",
" 2.78208171e+01, 6.53253854e+00, 7.15911038e+00, 1.18011111e+01,\n",
" 1.68632189e+01, 3.02152066e+01, 8.36335319e+00, 5.26832457e+01,\n",
" 2.64991671e+01, 6.75709511e+01, 7.40832911e+00, 1.99123785e+02,\n",
" 1.30016001e+02, 1.51028636e+01, 4.57147639e+00, 8.98047189e+01,\n",
" 1.45742938e+01, 1.08856820e+02, 1.51040948e-01, 6.74018364e+00,\n",
" 8.58840371e-01, 2.10760556e+00, 8.00526734e+01, 2.34880577e+01,\n",
" 1.00823265e+01, 1.19254111e+01, 4.05922179e+01, 5.82675162e+01,\n",
" 1.14731748e+01, 2.48160927e+01, 4.77813117e+01, 4.30768342e+01,\n",
" 1.78342941e+01, 9.39478890e+01])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"intervals = scipy.stats.expon(scale=1/0.03).rvs(50)\n",
"intervals"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Do we really have an average rate of 0.03 per year?\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.026523651792807357"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(intervals) / intervals.sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What's the probability of seeing more than one earthquake of intensity greater than 5 in the next year? It we call $X$ the random variable \"number of earthquakes next year\", then we are interested in $P(X > 1)$. \n",
"\n",
"$P(X > 1)$ is $1 - P(X=0) - P(X=1)$. "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9704455335485082"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# P(X = 0)\n",
"scipy.stats.poisson(0.03).pmf(0)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.029113366006455248"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scipy.stats.poisson(0.03).pmf(1)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0004411004450365977"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 - scipy.stats.poisson(0.03).pmf(0) - scipy.stats.poisson(0.03).pmf(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What's the probability of at least one 5+ earthquake in the next 20 years? It's 1 - the probability of zero earthquakes in the next 20 years, which is 1 minus the probability that the next arrival time is greater than 20. "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.4547"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Monte Carlo simulation\n",
"N = 10000\n",
"count = 0\n",
"for i in range(N):\n",
" if scipy.stats.expon(scale=1/0.03).rvs() > 20:\n",
" count += 1\n",
"1 - count / float(N)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.4579900279842801"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numpy.sum([scipy.stats.expon(scale=1/0.03).pdf(i) for i in range(20)])"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.8111243971624382"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 - scipy.stats.poisson(1/(20 * 0.03)).pmf(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The cumulative distribution function for the arrival times tells us the probability that the arrival time for the next earthquake does not exceed a given number of years. Obviously, it's an increasing function which asymptotically reaches 1 as the arrival time increases (the more time passes, the more likely it is that an earthquake will occur, but since we're talking about a probability we are necessarily less than or equal to 1). "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD9CAYAAABHnDf0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUeJzt3Xl4VfWdx/H3l0ASkkASkrBkAQIEwiaIUXBXxL2IU23H3Vor02mttmo7zmi1dtVuto5MK1q12CpVx7HUIpQqVauiBNn3AMGEkAUSQlay3N/8kegTMZgL3OTknvt5PU+e5Nx7uPkcT/Lxl989iznnEBERf+njdQAREQk9lbuIiA+p3EVEfEjlLiLiQyp3EREfUrmLiPhQl+VuZk+aWbmZbTjC82Zmj5hZgZmtM7NpoY8pIiJHI5iR+9PARZ/x/MVATvvHXOA3xx9LRESOR5fl7px7E6j8jFXmAAtcmxVAkpkNC1VAERE5eqGYc88AijosF7c/JiIiHukbgtewTh7r9JoGZjaXtqkb4uPjT8rNzQ3BtxcRiRyrVq3a55xL62q9UJR7MZDVYTkTKOlsRefcfGA+QF5ensvPzw/BtxcRiRxmtjuY9UIxLbMIuKH9qJkZQLVzbm8IXldERI5RlyN3M3sOOAdINbNi4H6gH4Bz7rfAYuASoACoB27qrrAiIhKcLsvdOXd1F8874OshSyQiIsdNZ6iKiPiQyl1ExIdU7iIiPqRyFxHxoVAc5y4iIp1wzlF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W0XYjdj/5NbDEOZcLTKFt2327j80sA7gNyHPOTaLtEuJX4b/9/DRw0WGPHWm/XgzktH/MBX4TqhBhVe4Ed7PusOec2+uc+6D96xrafukz+OSNyH8PXO5NwtAzs0zgUuCJ9mUDZtJ2w3Xw3/YOBM6i7V4IOOeanHMH8PE+btcX6N9+x7Y4YC8+28/OuTf59J3ojrRf5wALXJsVQJKZheQeouFW7sHcrNtXzGwkcCLwHjDEObcX2v4HAAz2LlnI/Qr4DhBoX04BDrTfcB38t69HARXAU+1TUU+YWTw+3sfOuT3Az4EPaSv1amAV/t7PHznSfu22Tgu3cg/qRtx+YWYJwP8C33TOHfQ6T3cxs88B5c65VR0f7mRVP+3rvsA04DfOuROBOnw0BdOZ9nnmOUA2kA7E0zYtcTg/7eeudNvPebiVezA36/YFM+tHW7H/0Tn3UvvDZR/9ydb+udyrfCF2OnCZmRXSNtU2k7aRfFL7n+/gv31dDBQ7595rX36RtrL36z4GmAXscs5VOOeagZeA0/D3fv7IkfZrt3VauJV7MDfrDnvt882/AzZbZRaXAAABCUlEQVQ7537Z4amONyK/EfhzT2frDs65/3TOZTrnRtK2T193zl0LLKfthuvgo+0FcM6VAkVmNq79ofOATfh0H7f7EJhhZnHtP+MfbbNv93MHR9qvi4Ab2o+amQFUfzR9c9ycc2H1AVwCbAN2APd4naebtvEM2v40Wwesaf+4hLZ56NeA7e2fB3mdtRu2/RzglfavRwHvAwXAC0CM1/lCvK1Tgfz2/fwykOz3fQw8AGwBNgDPADF+28/Ac7S9p9BM28j85iPtV9qmZea199l62o4kCkkOnaEqIuJD4TYtIyIiQVC5i4j4kMpdRMSHVO4iIj6kchcR8SGVu4iID6ncRUR8SOUuIuJD/w8f33xgRhFYzwAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"support = numpy.linspace(0, 100, 100)\n",
"plt.plot(support, scipy.stats.expon(scale=1/0.03).cdf(support));"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.4511883639059736"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scipy.stats.expon(scale=1/0.03).cdf(20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let $Y$ denote the amount of time (in years) until the next earthquake, and $N(t)$ the counting function for the number of earthquakes that have arrived by year $t$. Because $Y$ will be greater than $t$ if and only if no events occur within the next $t$ units of time, we have\n",
"\n",
"$P(Y>t)=P(N(t)=0) = \\text{scipy.stats.expon}(scale=1/0.03).cdf(Y)$"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.029113366006455248"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scipy.stats.poisson(0.03).pmf(1)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.00043670049009682845"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scipy.stats.poisson(0.03).pmf(2)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numpy.sum([scipy.stats.poisson(0.03).pmf(i) for i in range(20)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Births in a hospital"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Births in a hospital occur randomly at an average rate of 1.8 births per hour.\n",
"What is the probability of observing 4 births in a given hour at the hospital?\n",
"\n",
"Let X = number of births in a given hour\n",
"(i) Events occur randomly\n",
"⇒ X ∼ Poisson(1.8)\n",
"(ii) Mean rate λ = 1.8\n",
"\n",
"We can now use the formula to calculate the probability of observing exactly 4\n",
"births in a given hour\n",
"\n",
"Pr(X = 4) = 0.0723\n",
"\n",
"What about the probability of observing more than or equal to 2 births in a given\n",
"hour at the hospital?\n",
"\n",
"We want Pr(X ≥ 2) = Pr(X = 2) + Pr(X = 3) + . . .\n",
"\n",
"but (trick) Pr(X ≥ 2) = Pr(X = 2) + Pr(X = 3) + . . . \n",
"\n",
"= 1 - Pr(X < 2) = 1 - Pr(X=0) - Pr(X=1) = 0.537"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sum of two Poisson variables"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment