├── .gitignore ├── LICENSE ├── README.md ├── data ├── pass_stats.csv └── tracab-like-frames.csv └── notebooks ├── TidyData_PandasSeaborn.ipynb ├── Voronoi Reflection Trick.ipynb └── glicko2.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 ProformAnalytics 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tutorial_nbs 2 | Jupyter notebook tutorials for football analytics 3 | -------------------------------------------------------------------------------- /data/tracab-like-frames.csv: -------------------------------------------------------------------------------- 1 | frame_id,H_01_tid,H_01_shirt,H_01_x,H_01_y,H_01_v,H_01_position,H_02_tid,H_02_shirt,H_02_x,H_02_y,H_02_v,H_02_position,H_03_tid,H_03_shirt,H_03_x,H_03_y,H_03_v,H_03_position,H_04_tid,H_04_shirt,H_04_x,H_04_y,H_04_v,H_04_position,H_05_tid,H_05_shirt,H_05_x,H_05_y,H_05_v,H_05_position,H_06_tid,H_06_shirt,H_06_x,H_06_y,H_06_v,H_06_position,H_07_tid,H_07_shirt,H_07_x,H_07_y,H_07_v,H_07_position,H_08_tid,H_08_shirt,H_08_x,H_08_y,H_08_v,H_08_position,H_09_tid,H_09_shirt,H_09_x,H_09_y,H_09_v,H_09_position,H_10_tid,H_10_shirt,H_10_x,H_10_y,H_10_v,H_10_position,H_11_tid,H_11_shirt,H_11_x,H_11_y,H_11_v,H_11_position,A_01_tid,A_01_shirt,A_01_x,A_01_y,A_01_v,A_01_position,A_02_tid,A_02_shirt,A_02_x,A_02_y,A_02_v,A_02_position,A_03_tid,A_03_shirt,A_03_x,A_03_y,A_03_v,A_03_position,A_04_tid,A_04_shirt,A_04_x,A_04_y,A_04_v,A_04_position,A_05_tid,A_05_shirt,A_05_x,A_05_y,A_05_v,A_05_position,A_06_tid,A_06_shirt,A_06_x,A_06_y,A_06_v,A_06_position,A_07_tid,A_07_shirt,A_07_x,A_07_y,A_07_v,A_07_position,A_08_tid,A_08_shirt,A_08_x,A_08_y,A_08_v,A_08_position,A_09_tid,A_09_shirt,A_09_x,A_09_y,A_09_v,A_09_position,A_10_tid,A_10_shirt,A_10_x,A_10_y,A_10_v,A_10_position,A_11_tid,A_11_shirt,A_11_x,A_11_y,A_11_v,A_11_position,B_x,B_y,B_z,B_v,poss_team,ball_status,dev_status,Half,Time 2 | 1906491,8,13,-3881,110,1.84,Goalkeeper,9,22,-328,-1410,5.16,Defender,24,3,-202,1935,3.08,Defender,25,14,-102,791,3.34,Midfielder,14,4,-907,-547,3.65,Defender,29,15,-1049,1803,1.48,Defender,12,21,1077,1123,4.02,Midfielder,21,8,649,-289,3.73,Midfielder,16,17,2850,51,2.55,Forward,18,5,1832,1741,3.25,Midfielder,15,16,2650,1930,2.39,Midfielder,26,13,4235,112,1.11,Goalkeeper,10,16,2668,1802,2.31,Defender,11,3,1702,-2024,4.55,Defender,17,6,2218,967,2.78,Midfielder,13,4,2967,530,2.49,Defender,2,24,2452,-473,2.66,Defender,6,9,1193,814,4.36,Midfielder,27,20,574,952,4,Midfielder,4,12,-913,-1962,3.86,Forward,1,11,-807,3098,3.47,Forward,19,17,318,-345,4.64,Midfielder,758,1102,7,459.61,A,Alive,NA,1,655.04 3 | 1906492,8,13,-3885,109,1.76,Goalkeeper,9,22,-349,-1408,5.16,Defender,24,3,-214,1936,3.07,Defender,25,14,-114,793,3.31,Midfielder,14,4,-915,-537,3.62,Defender,29,15,-1054,1805,1.47,Defender,12,21,1058,1123,4.21,Midfielder,21,8,635,-283,3.73,Midfielder,16,17,2839,53,2.58,Forward,18,5,1818,1741,3.25,Midfielder,15,16,2641,1932,2.39,Midfielder,26,13,4231,113,1.11,Goalkeeper,10,16,2659,1803,2.3,Defender,11,3,1685,-2027,4.5,Defender,17,6,2206,967,2.78,Midfielder,13,4,2959,532,2.46,Defender,2,24,2443,-470,2.63,Defender,6,9,1177,802,4.49,Midfielder,27,20,570,967,3.92,Midfielder,4,12,-925,-1952,3.87,Forward,1,11,-818,3102,3.47,Forward,19,17,303,-334,4.61,Midfielder,739,1100,6,463.19,A,Alive,NA,1,655.08 4 | 1906493,8,13,-3888,109,1.69,Goalkeeper,9,22,-369,-1406,5.12,Defender,24,3,-226,1936,3.07,Defender,25,14,-127,796,3.31,Midfielder,14,4,-924,-525,3.6,Defender,29,15,-1061,1807,1.49,Defender,12,21,1038,1122,4.33,Midfielder,21,8,621,-278,3.77,Midfielder,16,17,2827,55,2.62,Forward,18,5,1805,1741,3.21,Midfielder,15,16,2632,1933,2.39,Midfielder,26,13,4226,113,1.11,Goalkeeper,10,16,2649,1803,2.3,Defender,11,3,1669,-2029,4.45,Defender,17,6,2195,968,2.78,Midfielder,13,4,2950,536,2.47,Defender,2,24,2434,-467,2.59,Defender,6,9,1160,790,4.59,Midfielder,27,20,564,983,3.92,Midfielder,4,12,-938,-1941,3.87,Forward,1,11,-829,3106,3.45,Forward,19,17,289,-320,4.64,Midfielder,720,1097,6,468.66,A,Alive,NA,1,655.12 5 | 1906494,8,13,-3892,109,1.61,Goalkeeper,9,22,-389,-1404,5.13,Defender,24,3,-238,1937,3.07,Defender,25,14,-140,797,3.33,Midfielder,14,4,-933,-514,3.61,Defender,29,15,-1065,1809,1.47,Defender,12,21,1019,1122,4.37,Midfielder,21,8,607,-275,3.75,Midfielder,16,17,2816,56,2.66,Forward,18,5,1792,1742,3.25,Midfielder,15,16,2623,1933,2.39,Midfielder,26,13,4222,114,1.11,Goalkeeper,10,16,2639,1805,2.34,Defender,11,3,1652,-2031,4.4,Defender,17,6,2183,968,2.82,Midfielder,13,4,2941,539,2.47,Defender,2,24,2426,-466,2.52,Defender,6,9,1143,778,4.72,Midfielder,27,20,560,999,3.87,Midfielder,4,12,-949,-1931,3.87,Forward,1,11,-839,3112,3.42,Forward,19,17,274,-308,4.63,Midfielder,701,1100,5,476.26,A,Alive,NA,1,655.16 6 | 1906495,8,13,-3894,109,1.48,Goalkeeper,9,22,-409,-1402,5.09,Defender,24,3,-249,1936,3.07,Defender,25,14,-152,801,3.3,Midfielder,14,4,-940,-502,3.61,Defender,29,15,-1072,1813,1.48,Defender,12,21,999,1123,4.45,Midfielder,21,8,593,-269,3.71,Midfielder,16,17,2805,57,2.7,Forward,18,5,1780,1744,3.22,Midfielder,15,16,2615,1936,2.35,Midfielder,26,13,4217,115,1.11,Goalkeeper,10,16,2628,1804,2.34,Defender,11,3,1635,-2033,4.35,Defender,17,6,2173,968,2.82,Midfielder,13,4,2932,542,2.47,Defender,2,24,2418,-463,2.49,Defender,6,9,1125,766,4.8,Midfielder,27,20,555,1015,3.87,Midfielder,4,12,-961,-1920,3.89,Forward,1,11,-848,3116,3.34,Forward,19,17,259,-296,4.66,Midfielder,683,1102,3,466.28,A,Alive,NA,1,655.2 -------------------------------------------------------------------------------- /notebooks/glicko2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 15, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import pandas as pd" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "#### Introduction\n", 18 | "\n", 19 | "[Glicko2](https://en.wikipedia.org/wiki/Glicko_rating_system) is a rating system for assessing the relative strength of players in games of skill. The inventor, M. Glickman, produced an example document covering the basic implementation of the Glicko2 system that can be read [here](http://www.glicko.net/glicko/glicko2.pdf).\n", 20 | "\n", 21 | "This notebook contains a Python implementation of the code in Glickman's example document. This notebook alone likely doesn't make a great deal of sense, it should be read alongside Glickman's example." 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "#### Constants\n", 29 | "\n", 30 | "Here $\\tau$ is defined as at the end of pg. 4." 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 16, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "# Tau constrains the change in volatility over time\n", 40 | "TAU = 0.5 \n", 41 | "#For numerical convergence\n", 42 | "EPSILON = 0.0000001 " 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "#### Functions\n", 50 | "\n", 51 | "I've pulled these out to the start of the notebook either because they're used in several places or they're understood better in isolation." 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 17, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "def convert_to_g2(r, RD):\n", 61 | " \"\"\"\n", 62 | " This method is Step 2 of the example\n", 63 | " \"\"\"\n", 64 | " mu = (r-1500) / 173.7178\n", 65 | " phi = RD / 173.7178\n", 66 | " return mu, phi\n", 67 | "\n", 68 | "def convert_from_g2(mu, phi):\n", 69 | " \"\"\"\n", 70 | " This method is Step 8 of the example\n", 71 | " \"\"\"\n", 72 | " r = 173.7178*mu + 1500\n", 73 | " RD = 173.7178*phi\n", 74 | " return r, RD\n", 75 | "\n", 76 | "def g_fun(phi):\n", 77 | " \"\"\"\n", 78 | " This is the function g defined in Step 3\n", 79 | " \"\"\"\n", 80 | " den = np.sqrt(1 + (3*phi**2)/(np.pi**2))\n", 81 | " return 1./den\n", 82 | "\n", 83 | "def E_fun(mu, mu_j, phi_j):\n", 84 | " \"\"\"\n", 85 | " This is the function E defined in step 3\n", 86 | " \"\"\"\n", 87 | " den = 1 + np.exp(-g_fun(phi_j)*(mu - mu_j))\n", 88 | " return 1./den" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "#### The example data\n", 96 | "\n", 97 | "This is the data defined in the **Example calculation** outlined at the bottom of page 4. Putting together this example is Step 1 in the procedure." 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 18, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "# Player in given example\n", 107 | "r, RD, sigma = 1500, 200, 0.06\n", 108 | "\n", 109 | "# Opponents from example (r, RD, s)\n", 110 | "opps = [\n", 111 | " (1400, 30, 1),\n", 112 | " (1550, 100, 0),\n", 113 | " (1700, 300, 0)\n", 114 | "]" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "#### The Glicko 2 algorithm\n", 122 | "\n", 123 | "This is the code for Steps 2-8." 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 19, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/plain": [ 134 | "(0.0, 1.1512924985234674)" 135 | ] 136 | }, 137 | "execution_count": 19, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [ 143 | "# Step 2 convert player score\n", 144 | "mu, phi = convert_to_g2(r, RD)\n", 145 | "mu, phi" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 20, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/html": [ 156 | "
\n", 157 | "\n", 170 | "\n", 171 | " \n", 172 | " \n", 173 | " \n", 174 | " \n", 175 | " \n", 176 | " \n", 177 | " \n", 178 | " \n", 179 | " \n", 180 | " \n", 181 | " \n", 182 | " \n", 183 | " \n", 184 | " \n", 185 | " \n", 186 | " \n", 187 | " \n", 188 | " \n", 189 | " \n", 190 | " \n", 191 | " \n", 192 | " \n", 193 | " \n", 194 | " \n", 195 | " \n", 196 | " \n", 197 | " \n", 198 | " \n", 199 | " \n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | "
mu_jphi_jg_jE_js_j
0-0.5756460.1726940.9954980.6394681
10.2878230.5756460.9531490.4318420
21.1512921.7269390.7242350.3028410
\n", 208 | "
" 209 | ], 210 | "text/plain": [ 211 | " mu_j phi_j g_j E_j s_j\n", 212 | "0 -0.575646 0.172694 0.995498 0.639468 1\n", 213 | "1 0.287823 0.575646 0.953149 0.431842 0\n", 214 | "2 1.151292 1.726939 0.724235 0.302841 0" 215 | ] 216 | }, 217 | "execution_count": 20, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "# Step 2/3\n", 224 | "# This builds the example table at the top of page 5.\n", 225 | "data = []\n", 226 | "for j in [0,1,2]:\n", 227 | " rj, RDj, sj = opps[j] \n", 228 | " muj, phij = convert_to_g2(rj, RDj)\n", 229 | " \n", 230 | " gj = g_fun(phij)\n", 231 | " Ej = E_fun(mu, muj, phij)\n", 232 | " \n", 233 | " data.append([muj, phij, gj, Ej, sj])\n", 234 | " \n", 235 | "pd.DataFrame(data, columns = ['mu_j', 'phi_j', 'g_j', 'E_j', 's_j'])" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 21, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "1.7789770897239976" 247 | ] 248 | }, 249 | "execution_count": 21, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "# Step 3 - compute v\n", 256 | "# Note: this value differs from the example on pg. 5 because\n", 257 | "# that calculation rounds the values in the table first\n", 258 | "v = 1./np.sum([r[2]**2 * r[3] * (1-r[3]) for r in data])\n", 259 | "v " 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 22, 265 | "metadata": {}, 266 | "outputs": [ 267 | { 268 | "data": { 269 | "text/plain": [ 270 | "-0.4839332609836549" 271 | ] 272 | }, 273 | "execution_count": 22, 274 | "metadata": {}, 275 | "output_type": "execute_result" 276 | } 277 | ], 278 | "source": [ 279 | "# Step 4 - compute Delta\n", 280 | "delta = v*np.sum([r[2]*(r[4] - r[3]) for r in data])\n", 281 | "delta" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 23, 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "name": "stdout", 291 | "output_type": "stream", 292 | "text": [ 293 | "Init A: -5.626821433520073\n", 294 | "Init B: -6.126821433520073\n", 295 | "sigma_dash: 0.059995984286488495\n" 296 | ] 297 | }, 298 | { 299 | "data": { 300 | "text/html": [ 301 | "
\n", 302 | "\n", 315 | "\n", 316 | " \n", 317 | " \n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " \n", 322 | " \n", 323 | " \n", 324 | " \n", 325 | " \n", 326 | " \n", 327 | " \n", 328 | " \n", 329 | " \n", 330 | " \n", 331 | " \n", 332 | " \n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | " \n", 338 | " \n", 339 | " \n", 340 | " \n", 341 | " \n", 342 | " \n", 343 | " \n", 344 | "
IterationABfAfB
00-5.626821-6.126821-0.0005361.999675e+00
11-5.626821-5.626955-0.0002681.522830e-08
\n", 345 | "
" 346 | ], 347 | "text/plain": [ 348 | " Iteration A B fA fB\n", 349 | "0 0 -5.626821 -6.126821 -0.000536 1.999675e+00\n", 350 | "1 1 -5.626821 -5.626955 -0.000268 1.522830e-08" 351 | ] 352 | }, 353 | "execution_count": 23, 354 | "metadata": {}, 355 | "output_type": "execute_result" 356 | } 357 | ], 358 | "source": [ 359 | "# Step 5 - Numerical iteration\n", 360 | "def f(x):\n", 361 | " lnum = np.exp(x)*(delta**2 - phi**2 - v - np.exp(x))\n", 362 | " lden = 2*(phi**2 + v + np.exp(x))**2\n", 363 | " \n", 364 | " rnum = (x-a)\n", 365 | " rden = TAU**2\n", 366 | " \n", 367 | " return lnum/lden - rnum/rden\n", 368 | "\n", 369 | "#Init params\n", 370 | "a = np.log(sigma**2)\n", 371 | "A = np.log(sigma**2)\n", 372 | "print('Init A:', A) \n", 373 | "\n", 374 | "#Get the initial B\n", 375 | "if delta**2 > phi**2 + v:\n", 376 | " B = np.log(delta**2 - phi**2 - v)\n", 377 | "else:\n", 378 | " k = 1\n", 379 | " while f(a - k*TAU) < 0:\n", 380 | " k+=1\n", 381 | " B = a-k*TAU\n", 382 | " \n", 383 | "print('Init B:', B)\n", 384 | "\n", 385 | "# Numerical procedure \n", 386 | "# this records convergence history in c_rows\n", 387 | "c_rows, itr = [], 0\n", 388 | "fA, fB = f(A), f(B)\n", 389 | "while np.abs(B-A) > EPSILON:\n", 390 | " C = A + (A-B)*fA / (fB - fA)\n", 391 | " fC = f(C)\n", 392 | " c_rows.append([itr, A, B, fA, fB])\n", 393 | "\n", 394 | " if fC*fB < 0:\n", 395 | " A = B\n", 396 | " fA = fB\n", 397 | " else:\n", 398 | " fA = fA/2.\n", 399 | " \n", 400 | " B = C\n", 401 | " fB = fC\n", 402 | " itr+=1\n", 403 | " \n", 404 | "sigma_dash = np.exp(A/2)\n", 405 | "print(\"sigma_dash: \", sigma_dash)\n", 406 | "pd.DataFrame(c_rows, columns = ['Iteration', 'A', 'B','fA', 'fB'])" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 24, 412 | "metadata": {}, 413 | "outputs": [ 414 | { 415 | "data": { 416 | "text/plain": [ 417 | "1.1528546895801364" 418 | ] 419 | }, 420 | "execution_count": 24, 421 | "metadata": {}, 422 | "output_type": "execute_result" 423 | } 424 | ], 425 | "source": [ 426 | "#Step 6\n", 427 | "phi_star = np.sqrt(phi**2 + sigma_dash**2)\n", 428 | "phi_star" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": 25, 434 | "metadata": {}, 435 | "outputs": [ 436 | { 437 | "data": { 438 | "text/plain": [ 439 | "(0.8721991881307343, -0.20694096667525494)" 440 | ] 441 | }, 442 | "execution_count": 25, 443 | "metadata": {}, 444 | "output_type": "execute_result" 445 | } 446 | ], 447 | "source": [ 448 | "#Step 7 \n", 449 | "phi_dash = 1./np.sqrt(1./phi_star**2 + 1./v)\n", 450 | "mu_dash = mu + (phi_dash**2)*np.sum([r[2]*(r[4] - r[3]) for r in data])\n", 451 | "\n", 452 | "phi_dash, mu_dash" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 27, 458 | "metadata": {}, 459 | "outputs": [ 460 | { 461 | "data": { 462 | "text/plain": [ 463 | "(1464.0506705393013, 151.51652412385727, 0.059995984286488495)" 464 | ] 465 | }, 466 | "execution_count": 27, 467 | "metadata": {}, 468 | "output_type": "execute_result" 469 | } 470 | ], 471 | "source": [ 472 | "#Step8 - convert back to r/RD form\n", 473 | "r_dash, RD_dash = convert_from_g2(mu_dash, phi_dash)\n", 474 | "\n", 475 | "# The fianl updated rating\n", 476 | "r_dash, RD_dash, sigma_dash" 477 | ] 478 | } 479 | ], 480 | "metadata": { 481 | "kernelspec": { 482 | "display_name": "Python 3", 483 | "language": "python", 484 | "name": "python3" 485 | }, 486 | "language_info": { 487 | "codemirror_mode": { 488 | "name": "ipython", 489 | "version": 3 490 | }, 491 | "file_extension": ".py", 492 | "mimetype": "text/x-python", 493 | "name": "python", 494 | "nbconvert_exporter": "python", 495 | "pygments_lexer": "ipython3", 496 | "version": "3.6.8" 497 | } 498 | }, 499 | "nbformat": 4, 500 | "nbformat_minor": 2 501 | } 502 | --------------------------------------------------------------------------------