{ "cells": [ { "cell_type": "code", "execution_count": 35, "metadata": { "ExecuteTime": { "end_time": "2019-01-20T20:47:39.520979Z", "start_time": "2019-01-20T20:47:39.426004Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/vnd.plotly.v1+html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Standard data science\n", "import pandas as pd\n", "import numpy as np\n", "\n", "np.random.seed(42)\n", "\n", "# Display all cell outputs\n", "from IPython.core.interactiveshell import InteractiveShell\n", "InteractiveShell.ast_node_interactivity = 'all'\n", "\n", "# Visualizations\n", "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "from plotly.offline import iplot\n", "\n", "# Cufflinks for dataframes\n", "import cufflinks as cf\n", "cf.go_offline()\n", "cf.set_config_file(world_readable=True, theme='pearl')\n", "\n", "from scipy.misc import factorial" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "ExecuteTime": { "end_time": "2019-01-20T16:04:17.644018Z", "start_time": "2019-01-20T16:04:17.639119Z" } }, "outputs": [ { "data": { "text/plain": [ "[0, 55, 145, 156, 175, 194, 308, 361, 439, 508, 528]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "success_times = [0]\n", "\n", "for _ in range(10):\n", " last_event = success_times[-1]\n", " next_event = np.random.randint(1, 120) + last_event\n", " success_times.append(next_event)\n", " \n", "success_times" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "ExecuteTime": { "end_time": "2019-01-20T16:04:18.332002Z", "start_time": "2019-01-20T16:04:18.327965Z" } }, "outputs": [ { "data": { "text/plain": [ "52.8" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.diff(success_times).mean()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "ExecuteTime": { "end_time": "2019-01-20T16:04:18.716173Z", "start_time": "2019-01-20T16:04:18.710754Z" } }, "outputs": [], "source": [ "def plot_success_times(success_times):\n", " annotations = [go.layout.Annotation(x=x, y=1, text=f'Day: {x}', ax=0, ay=250) for x in success_times]\n", "\n", " figure = go.Figure(data=[go.Scatter(x=success_times, \n", " y=np.ones(shape=len(success_times)), \n", " mode='markers')], \n", "\n", " layout=go.Layout(annotations=annotations, yaxis=dict(range=(0, 1.1)), \n", " xaxis=dict(title=\"Days\", range=(0, np.max(success_times)+10)), title='Failures over Time'))\n", " return figure" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "ExecuteTime": { "end_time": "2019-01-20T16:04:19.586769Z", "start_time": "2019-01-20T16:04:19.503595Z" } }, "outputs": [], "source": [ "figure = plot_success_times(success_times)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "ExecuteTime": { "end_time": "2019-01-20T16:04:19.995656Z", "start_time": "2019-01-20T16:04:19.990770Z" } }, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plot.ly", "showLink": true }, "data": [ { "mode": "markers", "type": "scatter", "uid": "e1d6b124-c4c4-4d39-9ceb-c8e3794c5433", "x": [ 0, 55, 145, 156, 175, 194, 308, 361, 439, 508, 528 ], "y": [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ] } ], "layout": { "annotations": [ { "ax": 0, "ay": 250, "text": "Day: 0", "x": 0, "y": 1 }, { "ax": 0, "ay": 250, "text": "Day: 55", "x": 55, "y": 1 }, { "ax": 0, "ay": 250, "text": "Day: 145", "x": 145, "y": 1 }, { "ax": 0, "ay": 250, "text": "Day: 156", "x": 156, "y": 1 }, { "ax": 0, "ay": 250, "text": "Day: 175", "x": 175, "y": 1 }, { "ax": 0, "ay": 250, "text": "Day: 194", "x": 194, "y": 1 }, { "ax": 0, "ay": 250, "text": "Day: 308", "x": 308, "y": 1 }, { "ax": 0, "ay": 250, "text": "Day: 361", "x": 361, "y": 1 }, { "ax": 0, "ay": 250, "text": "Day: 439", "x": 439, "y": 1 }, { "ax": 0, "ay": 250, "text": "Day: 508", "x": 508, "y": 1 }, { "ax": 0, "ay": 250, "text": "Day: 528", "x": 528, "y": 1 } ], "title": "Failures over Time", "xaxis": { "range": [ 0, 538 ], "title": "Days" }, "yaxis": { "range": [ 0, 1.1 ] } } }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "iplot(figure)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "ExecuteTime": { "end_time": "2019-01-20T20:47:06.529170Z", "start_time": "2019-01-20T20:47:06.474174Z" } }, "outputs": [], "source": [ "def calc_prob(events_per_minute, minutes, k):\n", " # Calculate probability of k events in specified number of minutes\n", " lam = events_per_minute * minutes\n", " return np.exp(-lam) * np.power(lam, k) / factorial(k)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "ExecuteTime": { "end_time": "2019-01-20T20:48:16.998461Z", "start_time": "2019-01-20T20:48:16.993129Z" } }, "outputs": [], "source": [ "def plot_different_rates(events_per_minute, minutes, ns, title=''):\n", " df = pd.DataFrame()\n", " annotations=[]\n", " colors = ['orange', 'green', 'red', 'blue', 'purple', 'brown']\n", " for i, events in enumerate(events_per_minute):\n", " probs = calc_prob(events, minutes, ns)\n", " df[f'Lambda = {int(events * minutes)}'] = probs\n", " df.index = ns\n", " df.iplot(kind='scatter', mode='markers+lines', colors=colors, size=8, annotations=annotations,\n", " xTitle='Events', yTitle='Probability', title=title)\n", " return df\n", "\n" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "ExecuteTime": { "end_time": "2019-01-20T21:08:24.764770Z", "start_time": "2019-01-20T21:08:24.625550Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.6/site-packages/ipykernel_launcher.py:4: DeprecationWarning:\n", "\n", "`factorial` is deprecated!\n", "Importing `factorial` from scipy.misc is deprecated in scipy 1.0.0. Use `scipy.special.factorial` instead.\n", "\n" ] }, { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plot.ly", "showLink": true }, "data": [ { "line": { "color": "rgba(255, 153, 51, 1.0)", "dash": "solid", "shape": "linear", "width": 1.3 }, "marker": { "size": 8, "symbol": "circle" }, "mode": "markers+lines", "name": "Lambda = 1", "text": "", "type": "scatter", "uid": "758b2b87-12bb-440a-a9aa-0e3d1ded74d0", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ], "y": [ 0.36787944117144233, 0.36787944117144233, 0.18393972058572117, 0.06131324019524039, 0.015328310048810098, 0.0030656620097620196, 0.0005109436682936699, 7.299195261338141e-05, 9.123994076672677e-06, 1.0137771196302974e-06, 1.0137771196302975e-07, 9.216155633002704e-09, 7.68012969416892e-10, 5.907792072437631e-11, 4.2198514803125934e-12 ] }, { "line": { "color": "rgba(0, 128, 0, 1.0)", "dash": "solid", "shape": "linear", "width": 1.3 }, "marker": { "size": 8, "symbol": "circle" }, "mode": "markers+lines", "name": "Lambda = 2", "text": "", "type": "scatter", "uid": "303f05c4-9df6-483c-b656-e295f1f0289c", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ], "y": [ 0.1353352832366127, 0.2706705664732254, 0.2706705664732254, 0.1804470443154836, 0.0902235221577418, 0.03608940886309672, 0.012029802954365574, 0.0034370865583901638, 0.0008592716395975409, 0.00019094925324389798, 3.8189850648779595e-05, 6.943609208869018e-06, 1.1572682014781697e-06, 1.7804126176587226e-07, 2.5434465966553178e-08 ] }, { "line": { "color": "rgba(219, 64, 82, 1.0)", "dash": "solid", "shape": "linear", "width": 1.3 }, "marker": { "size": 8, "symbol": "circle" }, "mode": "markers+lines", "name": "Lambda = 4", "text": "", "type": "scatter", "uid": "b3797233-8af7-47fb-bb34-40eff64649e3", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ], "y": [ 0.009898375607203901, 0.04568481049478724, 0.10542648575720134, 0.16219459347261747, 0.18714760785302018, 0.17275163801817245, 0.13288587539859426, 0.08761706070236983, 0.050548304251367214, 0.025922207308393445, 0.011964095680796975, 0.0050199002856490815, 0.0019307308790958006, 0.0006854665842943671, 0.0002259779948223189 ] }, { "line": { "color": "rgba(55, 128, 191, 1.0)", "dash": "solid", "shape": "linear", "width": 1.3 }, "marker": { "size": 8, "symbol": "circle" }, "mode": "markers+lines", "name": "Lambda = 5", "text": "", "type": "scatter", "uid": "cfa76eaf-892c-44bc-bec6-b13788d9aaa7", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ], "y": [ 0.004276820349208926, 0.023328110995685054, 0.06362212089732287, 0.11567658344967797, 0.15774079561319726, 0.17208086794166974, 0.15643715267424524, 0.12189908000590538, 0.0831130090949355, 0.05037152066359728, 0.027475374907416698, 0.01362415284665291, 0.006192796748478596, 0.002598376258102908, 0.0010123543862738602 ] }, { "line": { "color": "rgba(128, 0, 128, 1.0)", "dash": "solid", "shape": "linear", "width": 1.3 }, "marker": { "size": 8, "symbol": "circle" }, "mode": "markers+lines", "name": "Lambda = 6", "text": "", "type": "scatter", "uid": "e7a5c11d-67ed-4214-8a33-19927aec7e5c", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ], "y": [ 0.001272633801339809, 0.008484225342265393, 0.028280751140884637, 0.0628461136464103, 0.10474352274401716, 0.1396580303253562, 0.15517558925039576, 0.1477862754765674, 0.12315522956380615, 0.09122609597318973, 0.060817397315459806, 0.03685902867603624, 0.020477238153353467, 0.010501147770950497, 0.005000546557595475 ] } ], "layout": { "legend": { "bgcolor": "#F5F6F9", "font": { "color": "#4D5663" } }, "paper_bgcolor": "#F5F6F9", "plot_bgcolor": "#F5F6F9", "title": "Probability of Events in One Interval", "titlefont": { "color": "#4D5663" }, "xaxis": { "gridcolor": "#E1E5ED", "showgrid": true, "tickfont": { "color": "#4D5663" }, "title": "Events", "titlefont": { "color": "#4D5663" }, "zerolinecolor": "#E1E5ED" }, "yaxis": { "gridcolor": "#E1E5ED", "showgrid": true, "tickfont": { "color": "#4D5663" }, "title": "Probability", "titlefont": { "color": "#4D5663" }, "zerolinecolor": "#E1E5ED" } } }, "text/html": [ "" ], "text/vnd.plotly.v1+html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df = plot_different_rates(events_per_minute=np.array([1/60, 1/30, 1/15, 1/13, 1/11, 1/9]),\n", " minutes=60,\n", " ns=list(range(15)), \n", " title='Probability of Events in One Interval')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "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.6.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }