sketching out logistic regression for my interview, because i need to get the fundamentals down.
the problem is we want to predict binary outcomes (0,1), but we can't do that with linear regression that predicts continuous.
how do we get a model that outputs probabilities between 0 and 1? and how can we make a decision boundary to produce binary outcomes?
the answer is a sigmoid function.
where
this bounds the output between 0 and 1, and we can create a decision boundary at which is where
but what are we modeling here? the probabilities?
the key insight is that logistic regression doesn't directly model probabilities in a linear way - it models the log-odds.
Why log-odds? Because:
-
probability constraints: probability must be between 0 and 1, which isn't compatible with linear modeling (that produces unbounded values)
-
log-odds transformation: when we take , we transform the bounded 0-1 range into an unbounded range ( to )
-
linear relationship: This allows us to model log-odds as a linear function of features:
The magic happens in this transformation. Consider:
- If , log-odds
- If , log-odds
- If , log-odds
- As approaches , log-odds approaches
- As approaches , log-odds approaches
So we're essentially saying:
- we want to model probability
- but we can't directly use linear regression on (bounded)
- so we transform to log-odds (unbounded)
- model log-odds linearly
- transform back to probability using sigmoid
this is why the coefficients in logistic regression represent changes in log-odds, and we can exponentiate them () to get odds ratios.
but how do we estimate our coefficients () that maximizes the probability of observing our training data?
we need a way to estimate the best coefficients () that maximize the probability of observing our training data.
to do that, we need MLE.
we use MLE to find the coefficients that make our observed data most likely:
first we want to likelihood function, to calculate it, we calculate the probability of its actual outcome for each data point
for a binary classification, the likelihood is
Where is the true label (0 or 1)
to make optimization easier, we take the log so it converts the multiplication into an addition, known as the log likelihood
and unlike linear regression's closed-form solution, logistic regression uses iterative methods like gradient descent.
the goal is to find values that maximize this log-likelihood, essentially finding the most probable model given the data.
more resources