Kaynağa Gözat

copenican lifetime equation

WillKoehrsen 6 yıl önce
ebeveyn
işleme
2878c32752
2 değiştirilmiş dosya ile 1297 ekleme ve 9 silme
  1. 620 0
      copernican/Copernican Principle Time.ipynb
  2. 677 9
      medium/Stats.ipynb

+ 620 - 0
copernican/Copernican Principle Time.ipynb

@@ -0,0 +1,620 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:03:05.877741Z",
+     "start_time": "2018-12-28T21:03:04.233205Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "# Data science imports\n",
+    "import pandas as pd\n",
+    "import numpy as np\n",
+    "\n",
+    "%load_ext autoreload\n",
+    "%autoreload 2\n",
+    "\n",
+    "import sys\n",
+    "sys.path.append('../..')\n",
+    "\n",
+    "# Options for pandas\n",
+    "pd.options.display.max_columns = 20\n",
+    "\n",
+    "# Display all cell outputs\n",
+    "from IPython.core.interactiveshell import InteractiveShell\n",
+    "InteractiveShell.ast_node_interactivity = 'all'\n",
+    "\n",
+    "import plotly.plotly as py\n",
+    "import plotly.graph_objs as go\n",
+    "import cufflinks\n",
+    "cufflinks.go_offline()\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:03:51.548602Z",
+     "start_time": "2018-12-28T21:03:51.518253Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "def calculate_multipliers(ci):\n",
+    "    z = (1-ci)/2\n",
+    "    return z/(1-z), (1-z)/z"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:04:14.255563Z",
+     "start_time": "2018-12-28T21:04:14.223267Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "def calculate_lifetime(t_current, ci):\n",
+    "    low, high = calculate_multipliers(ci)\n",
+    "    return t_current*low, t_current*high"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:15:08.428012Z",
+     "start_time": "2018-12-28T21:15:08.397695Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "from scipy.stats import norm"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T23:03:35.984174Z",
+     "start_time": "2018-12-28T23:03:35.940951Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "n = 1000\n",
+    "min_ = -3\n",
+    "max_ = 3\n",
+    "x = np.linspace(min_, max_, num=n)\n",
+    "sd = np.sqrt(1.191) * np.log10(39) / 1.96\n",
+    "y = norm.pdf(x, loc=0, scale=sd)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T23:04:01.573628Z",
+     "start_time": "2018-12-28T23:04:01.526883Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "y[:235].sum()/y.sum()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T23:04:08.543032Z",
+     "start_time": "2018-12-28T23:04:08.499508Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "y[765:].sum()/y.sum()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T23:03:52.054892Z",
+     "start_time": "2018-12-28T23:03:52.011720Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "np.where(x>np.log10(39))[0].min()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T23:03:52.888799Z",
+     "start_time": "2018-12-28T23:03:52.842509Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "np.where(x<np.log10(1/39))[0].max()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T23:05:37.618842Z",
+     "start_time": "2018-12-28T23:05:37.472552Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "df=pd.DataFrame({'x': np.power(10, x), 'y': y})\n",
+    "df.iplot(x='x', y='y', mode='markers',) \n",
+    "         # layout=dict(xaxis=dict(type='log')))\n",
+    "\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T22:55:12.761764Z",
+     "start_time": "2018-12-28T22:55:12.619914Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "df=pd.DataFrame({'x': x, 'y': y})\n",
+    "df.iplot(x='x', y='y', mode='markers',) # markers=dict(size=2))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:32:03.344315Z",
+     "start_time": "2018-12-28T21:32:03.287707Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "x = np.logspace(-4, 4, num=1000)\n",
+    "y = norm.pdf(np.log10(x), loc=0, scale=0.62)\n",
+    "y[np.where(x>(1/39))[0].min()]\n",
+    "y[np.where(x>(39))[0].min()]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:35:33.029430Z",
+     "start_time": "2018-12-28T21:35:32.982628Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "len(y)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:35:28.751208Z",
+     "start_time": "2018-12-28T21:35:28.703829Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "len(y[x>39])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:38:00.440263Z",
+     "start_time": "2018-12-28T21:38:00.392276Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "38 * 10 / 1.96"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:34:47.914309Z",
+     "start_time": "2018-12-28T21:34:47.855029Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "y[np.all]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:23:02.385014Z",
+     "start_time": "2018-12-28T21:23:02.337434Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "from plotly.offline import iplot"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:31:47.527820Z",
+     "start_time": "2018-12-28T21:31:47.407177Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "data = go.Scatter(x=x, y=y, mode='markers')\n",
+    "figure = go.Figure(data=[data], layout=go.Layout(xaxis=dict(type='log')))\n",
+    "iplot(figure)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:05:15.665411Z",
+     "start_time": "2018-12-28T21:05:15.630761Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "from datetime import datetime, timedelta\n",
+    "now = datetime.now()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:07:25.075735Z",
+     "start_time": "2018-12-28T21:07:25.043317Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "low, high = calculate_lifetime(6, 0.95)\n",
+    "print(f'Data science will last until {(now + timedelta(days=low*365)).date()} at the min to {(now + timedelta(days=high*365)).date()} at the max.')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:07:50.441528Z",
+     "start_time": "2018-12-28T21:07:50.405146Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "low, high = calculate_lifetime(2000, 0.95)\n",
+    "print(f'Medicine will last until {(now + timedelta(days=low*365)).date()} at the min to {(now + timedelta(days=high*365)).date()} at the max.')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:08:00.082829Z",
+     "start_time": "2018-12-28T21:08:00.051234Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "high"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:11:25.060640Z",
+     "start_time": "2018-12-28T21:11:25.028286Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "pd.Timedelta(days=1e5).total_seconds()/(3600 )"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:12:22.929240Z",
+     "start_time": "2018-12-28T21:12:22.899705Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "f'{timedelta(days=9e8).total_seconds() / (3600 * 24 * 365):,.0f} years'"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-28T21:06:34.707349Z",
+     "start_time": "2018-12-28T21:06:34.674254Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "timedelta(days=10.5)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-29T00:12:55.099492Z",
+     "start_time": "2018-12-29T00:12:55.051164Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "x = np.linspace(0, 100, num = 1000)\n",
+    "y =  1 / (x + 1)\n",
+    "max(y)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-29T00:12:55.770587Z",
+     "start_time": "2018-12-29T00:12:55.724467Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "min(y)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-29T00:12:56.262965Z",
+     "start_time": "2018-12-29T00:12:56.211077Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "import seaborn as sns"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-29T03:22:16.189739Z",
+     "start_time": "2018-12-29T03:22:16.146082Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "from scipy import stats\n",
+    "x = np.logspace(-3, 3, num=100)\n",
+    "y = norm.pdf(np.log10(x), loc=0, scale=0.6)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-29T03:37:13.948794Z",
+     "start_time": "2018-12-29T03:37:13.888006Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "t_c = 5\n",
+    "df = pd.DataFrame({'x': x * t_c, 'y': y})\n",
+    "df.iplot(x='x', y='y', layout=dict(xaxis=dict(type='log', tickfont=dict(size=16),\n",
+    "                                              title=r'$t_{future} \\text{ (years)}$'),\n",
+    "                                  yaxis=dict(title='probability'), title = 'PDF of Years',\n",
+    "                                   shapes=[dict(type='line',\n",
+    "                                               x0 = 39 * t_c, x1= 39*t_c, \n",
+    "                                               y0 = 0, y1=1,\n",
+    "                                               line=dict(color='black', dash='dash')),\n",
+    "                                          dict(type='line',\n",
+    "                                               x0 = (1/39)*t_c, x1= (1/39)*t_c, \n",
+    "                                               y0 = 0, y1=1, name='39',\n",
+    "                                               line=dict(color='black', dash='dash'))]))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-29T03:34:44.809230Z",
+     "start_time": "2018-12-29T03:34:44.750366Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "df = pd.DataFrame({'x': x, 'y': y})\n",
+    "df.iplot(x='x', y='y', layout=dict(xaxis=dict(type='log', tickfont=dict(size=16),\n",
+    "                                              title=r'$\\frac{t_{future}}{t_{currrent}}$'),\n",
+    "                                  yaxis=dict(title='probability'), title = 'PDF',\n",
+    "                                   shapes=[dict(type='line',\n",
+    "                                               x0 = 39, x1= 39, \n",
+    "                                               y0 = 0, y1=1,\n",
+    "                                               line=dict(color='black', dash='dash')),\n",
+    "                                          dict(type='line',\n",
+    "                                               x0 = 1/39, x1= 1/39, \n",
+    "                                               y0 = 0, y1=1, name='39',\n",
+    "                                               line=dict(color='black', dash='dash'))]))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-29T00:13:09.543405Z",
+     "start_time": "2018-12-29T00:13:09.378060Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "import matplotlib.pyplot as plt\n",
+    "plt.plot(1/y)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2018-12-29T00:12:58.248514Z",
+     "start_time": "2018-12-29T00:12:58.079774Z"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "sns.kdeplot(np.log10(y))"
+   ]
+  },
+  {
+   "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
+}

Dosya farkı çok büyük olduğundan ihmal edildi
+ 677 - 9
medium/Stats.ipynb