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\).
Logit Link Function
The Logit function, \[g^{-1}(\pi_i) = \log\frac{\pi_i}{1-\pi_i}.\] This function is known as a link function.
For a standard regression we take, \[f(\inputVector_i) = \mappingVector^\top \basisVector(\inputVector_i),\]
For classification we perform a logistic regression. \[\log \frac{\pi_i}{1-\pi_i} = \mappingVector^\top \basisVector(\inputVector_i)\]
Inverse Link Function
We have defined the link function as taking the form \(g^{-1}(\cdot)\) implying that the inverse link function is given by \(g(\cdot)\). Since we have defined, \[
g^{-1}(\pi(\inputVector)) = \mappingVector^\top\basisVector(\inputVector)
\] we can write \(\pi\) in terms of the inverse link function, \(g(\cdot)\) as \[
\pi(\inputVector) = g(\mappingVector^\top\basisVector(\inputVector)).
\]
Logistic function
Logistic (or sigmoid) squashes real line to between 0 & 1. Sometimes also called a ‘squashing function’.
Basis 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}\]
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*}\]
Also need gradient of inverse link function wrt parameters. \[\begin{align*}
g(\mappingFunction_i) &= \frac{1}{1+\exp(-\mappingFunction_i)}\\
&=(1+\exp(-\mappingFunction_i))^{-1}
\end{align*}\] and the gradient can be computed as \[\begin{align*}
\frac{\text{d}g(\mappingFunction_i)}{\text{d} \mappingFunction_i} & =
\exp(-\mappingFunction_i)(1+\exp(-\mappingFunction_i))^{-2}\\
& = \frac{1}{1+\exp(-\mappingFunction_i)}
\frac{\exp(-\mappingFunction_i)}{1+\exp(-\mappingFunction_i)} \\
& = g(\mappingFunction_i) (1-g(\mappingFunction_i))
\end{align*}\]
Similarly to matrix factorization, for large data stochastic gradient descent (Robbins Munro (Robbins and Monro, 1951) optimization procedure) works well.
Ad Matching for Facebook
This approach used in many internet companies.
Example: ad matching for Facebook.
Millions of advertisers
Billions of users
How do you choose who to show what?
Logistic regression used in combination with decision trees
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.
Batch Gradient Descent
Stochastic Gradient Descent
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 Distribution
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)\]