{ "metadata": { "name": "B1a. Odds, LogOdds and Logit Function " }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Threshold functions for logistic regression\n", "===============================\n", "***\n", "\n", "### Odds, mathematically speaking. \n", "\n", "We are going to take the notion of odds, put a simple mathematical framework around it and then use our previous knowledge of linear regression to create a model that predicts binary outcomes.\n", "\n", "We need two bits of prior knowledge to understand this. \n", "\n", "1. Exponentials and Natural Logarithms [1] \n", "\n", "2. A very elementary understanding of Probability. [2] \n", "\n", "Basically all we need to know is that Probability is a number between 0 and 1 and indicates the likelihood of an event occurring. We remind ourselves that: \n", "\n", "probability = 0 is as good as the event being impossible and \n", "\n", "probability = 1 is as good as it being certain. \n", "\n", "We should also remind ourselves that if \n", "the probability of an event happening is $p$ \n", "then the probability of it not happening is $1 - p$. \n", "\n", "That's all the probability we need.\n", "\n", "In these exercises we will have math gradually added and most of it will be at the simplest possible level needed to work with these techniques. We have a philosophy of \"Just Enough Math\" so if you see a math symbol in a lesson it's because we really need it.\n", "\n", "Having said that let's start talking about odds.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Odds and Odds Ratio\n", "\n", "When bettors say the odds of winning are 1:4 what is this in terms of probability? \n", "\n", "It means 1 part chance of winning to 4 parts chance of losing. Note that total # of parts = 5 and odds of winning is 1 out of 5. So p is 1/5 = 0.2 and 1-p is 0.8. Here p is small and 1-p is large.\n", "\n", "The odds might be 1:1 which means p = 1/2 and 1-p = 1/2 i.e. equal chances of an even happening or not = \"even odds\".\n", "\n", "The odds might be 3:2 which means p = 0.6 and 1-p = 0.4. Here p is greater and 1-p is smaller.\n", "\n", "So depending on the ratio of p to 1-p we have more or less confidence in a bet winning.\n", "\n", "This suggests we might want to look at: \n", "\n", "$$Odds Ratio (OR) = \\frac {p}{1-p}$$\n", "\n", "If OddsRatio is high say: \n", "\n", "$$OR > 4$$ \n", "\n", "then the event might be considered very likely and if: \n", "\n", "$$OR < 0.25$$ \n", "\n", "then very unlikely. \n", "\n", "\n", "### The Logit Function\n", "\n", "Mathematicians like to work with a function derived from this called the Logit function. It's the Log of the OddsRatio\n", "\n", "$$logit(p) = log(\\frac{p}{1-p})$$ \n", "\n", "or the LogOdds function. \n", "\n", "And here is where we wave a magic wand again and bring in our linear regression formula.\n", "Let's say we want to plot a set of events on the x-axis and the logit value for that event on the y-axis and we want to fit a linear model to this. \n", "\n", "So we would say: \n", "\n", "$$logit(p) = log( \\frac{p}{1-p} ) = b_0 + b_1X$$ \n", "\n", "where $X$ is the \"value\" of the event. \n", "\n", "So here instead of $Y = b_0 + b_1X$ we want to plot $logit(p)$ on the $Y$ axis and the event or the score on the $X$ axis. \n", "\n", "So this is how the linear model slips in - we want to express log odds as a linear function of score. \n", "\n", "Patience now, we are just one step away. \n", "\n", "### Finally, the Logistic Function\n", "\n", "We don't really like plotting LogOdds as it involves awkward intermediate steps of calculating the logit() value. \n", "\n", "So we do the hard work once and for all and solve for p as a function of X and when we do that we get\n", "\n", "$$p(X) = \\frac{1}{1 + e^{b_0 + b_1X}}$$\n", "\n", "which, voila, is what we pulled out of the hat - but now we know why we pulled that particular one and why we didn't use any other. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### References \n", "[1] Exponential and Logarithmic functions \n", "[2] Probability \n", " " ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"../styles/custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()\n" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "" ], "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }