
Logistic Regression and GLMs

University of Sheffield

Review

• Last week: Specified Class Conditional Distributions, $p(\inputVector_i|\dataScalar_i, \parameterVector)$.
• Used Bayes Classifier + naive Bayes model to specify joint distribution.
• Used Bayes rule to compute posterior probability of class membership.
• This week:
• direct estimation of probability of class membership.
• introduction of generalised linear models.

Logistic Regression and GLMs

• Modelling entire density allows any question to be answered (also missing data).
• Comes at the possible expense of strong assumptions about data generation distribution.
• In regression we model probability of $\dataScalar_i |\inputVector_i$ directly.
• Allows less flexibility in the question, but more flexibility in the model assumptions.
• Can do this not just for regression, but classification.
• Framework is known as generalized linear models.

Log Odds

• model the log-odds with the basis functions.
• odds are defined as the ratio of the probability of a positive outcome, to the probability of a negative outcome.
• Probability is between zero and one, odds are: $\frac{\pi}{1-\pi}$
• Odds are between $0$ and $\infty$.
• Logarithm of odds maps them to $-\infty$ to $\infty$.

Logistic function

• Logistic (or sigmoid) squashes real line to between 0 & 1. Sometimes also called a ‘squashing function’.

Prediction Function

• Can now write $\pi$ as a function of the input and the parameter vector as, $\pi(\inputVector,\mappingVector) = \frac{1}{1+ \exp\left(-\mappingVector^\top \basisVector(\inputVector)\right)}.$
• Compute the output of a standard linear basis function composition ($\mappingVector^\top \basisVector(\inputVector)$, as we did for linear regression)
• Apply the inverse link function, $g(\mappingVector^\top \basisVector(\inputVector))$.
• Use this value in a Bernoulli distribution to form the likelihood.

Bernoulli Reminder

• From last time $P(\dataScalar_i|\mappingVector, \inputVector) = \pi_i^{\dataScalar_i} (1-\pi_i)^{1-\dataScalar_i}$

• Trick for switching betwen probabilities

Maximum Likelihood

• Conditional independence of data: $P(\dataVector|\mappingVector, \inputMatrix) = \prod_{i=1}^\numData P(\dataScalar_i|\mappingVector, \inputVector_i).$

Log Likelihood

\begin{align*} \log P(\dataVector|\mappingVector, \inputMatrix) = & \sum_{i=1}^\numData \log P(\dataScalar_i|\mappingVector, \inputVector_i) \\ = &\sum_{i=1}^\numData \dataScalar_i \log \pi_i \\ & + \sum_{i=1}^\numData (1-\dataScalar_i)\log (1-\pi_i) \end{align*}

Objective Function

• Probability of positive outcome for the $i$th data point $\pi_i = g\left(\mappingVector^\top \basisVector(\inputVector_i)\right),$ where $g(\cdot)$ is the inverse link function
• Objective function of the form \begin{align*} E(\mappingVector) = & - \sum_{i=1}^\numData \dataScalar_i \log g\left(\mappingVector^\top \basisVector(\inputVector_i)\right) \\& - \sum_{i=1}^\numData(1-\dataScalar_i)\log \left(1-g\left(\mappingVector^\top \basisVector(\inputVector_i)\right)\right). \end{align*}

Minimize Objective

• Grdient wrt $\pi(\inputVector;\mappingVector)$ \begin{align*} \frac{\text{d}E(\mappingVector)}{\text{d}\mappingVector} = & -\sum_{i=1}^\numData \frac{\dataScalar_i}{g\left(\mappingVector^\top \basisVector(\inputVector)\right)}\frac{\text{d}g(\mappingFunction_i)}{\text{d}\mappingFunction_i} \basisVector(\inputVector_i) \\ & + \sum_{i=1}^\numData \frac{1-\dataScalar_i}{1-g\left(\mappingVector^\top \basisVector(\inputVector)\right)}\frac{\text{d}g(\mappingFunction_i)}{\text{d}\mappingFunction_i} \basisVector(\inputVector_i) \end{align*}

\begin{align*} \frac{\text{d}E(\mappingVector)}{\text{d}\mappingVector} = & -\sum_{i=1}^\numData \dataScalar_i\left(1-g\left(\mappingVector^\top \basisVector(\inputVector)\right)\right) \basisVector(\inputVector_i) \\ & + \sum_{i=1}^\numData (1-\dataScalar_i)\left(g\left(\mappingVector^\top \basisVector(\inputVector)\right)\right) \basisVector(\inputVector_i). \end{align*}

Optimization of the Function

• Can’t find a stationary point of the objective function analytically.
• Optimization has to proceed by numerical methods.
• Similarly to matrix factorization, for large data stochastic gradient descent (Robbins Munro (Robbins and Monro, 1951) optimization procedure) works well.

• This approach used in many internet companies.
• Billions of users
• How do you choose who to show what?
• Logistic regression used in combination with decision trees
• Paper available here

{ ## Olivetti Glasses Data

Let’s classify images with logistic regression. We’ll look at a data set of individuals with glasses. We can load in the data from pods as follows.

Going Further: Optimization

Other optimization techniques for generalized linear models include Newton’s method, it requires you to compute the Hessian, or second derivative of the objective function.

Methods that are based on gradients only include L-BFGS and conjugate gradients. Can you find these in python? Are they suitable for very large data sets? }

Other GLMs

• Logistic regression is part of a family known as generalized linear models
• They all take the form $g^{-1}(\mappingFunction_i(x)) = \mappingVector^\top \basisVector(\inputVector_i)$
• Other examples include Poisson regression.

Poisson Distribution

• Poisson distribution is used for ‘count data’. For non-negative integers, $y$, $P(y) = \frac{\lambda^y}{y!}\exp(-y)$
• Here $\lambda$ is a rate parameter that can be thought of as the number of arrivals per unit time.
• Poisson distributions can be used for disease count data. E.g. number of incidence of malaria in a district.

Poisson Regression

• In a Poisson regression make rate a function of space/time. $\log \lambda(\inputVector, t) = \mappingVector_x^\top \basisVector_\inputScalar(\inputVector) + \mappingVector_t^\top \basisVector_t(t)$
• This is known as a log linear or log additive model.
• The link function is a logarithm.
• We can rewrite such a function as $\log \lambda(\inputVector, t) = \mappingFunction_x(\inputVector) + \mappingFunction_t(t)$

Multiplicative Model

• Be careful though … a log additive model is really multiplicative. $\log \lambda(\inputVector, t) = \mappingFunction_x(\inputVector) + \mappingFunction_t(t)$
• Becomes $\lambda(\inputVector, t) = \exp(\mappingFunction_x(\inputVector) + \mappingFunction_t(t))$
• Which is equivalent to $\lambda(\inputVector, t) = \exp(\mappingFunction_x(\inputVector))\exp(\mappingFunction_t(t))$
• Link functions can be deceptive in this way.