Saturday, May 24, 2025

logit, sigmoid function, and probability

step-by-step through how the sigmoid function turns a logit into a probability, with both math and plain English at each step.


✅ Step 1: What is a logit?

The logit of a probability pp is defined as:

logit(p)=log(p1p)\text{logit}(p) = \log\left(\frac{p}{1 - p}\right)

This is called the log-odds, because it’s the natural log of the odds.


✅ Step 2: What if we are given the logit and want to get the probability?

Let’s say we are given a number xx, and we know it’s a logit.

That means:

x=log(p1p)x = \log\left(\frac{p}{1 - p}\right)

We want to solve for pp, the probability.


✅ Step 3: Exponentiate both sides

To get rid of the logarithm, we exponentiate both sides:

ex=p1pe^x = \frac{p}{1 - p}

This tells us the odds in terms of xx.


✅ Step 4: Solve for pp

Now we solve this equation for pp.

Start with:

ex=p1pe^x = \frac{p}{1 - p}

Multiply both sides by 1p1 - p:

ex(1p)=pe^x (1 - p) = p

Distribute the left-hand side:

exexp=pe^x - e^x p = p

Now get all the pp's on one side:

ex=p+expe^x = p + e^x p

Factor out pp on the right-hand side:

ex=p(1+ex)e^x = p(1 + e^x)

Now divide both sides by 1+ex1 + e^x:

p=ex1+exp = \frac{e^x}{1 + e^x}

✅ Step 5: Simplify into the sigmoid form

We now simplify that expression:

p=ex1+ex=11+exex=11+exp = \frac{e^x}{1 + e^x} = \frac{1}{\frac{1 + e^x}{e^x}} = \frac{1}{1 + e^{-x}}

That’s the sigmoid function:

p=σ(x)=11+exp = \sigma(x) = \frac{1}{1 + e^{-x}}

✅ Final Result

So if you’re given a logit value xx, the sigmoid function:

σ(x)=11+ex\sigma(x) = \frac{1}{1 + e^{-x}}

turns it into a probability p(0,1)p \in (0, 1).


🔁 Why this is useful

  • In logistic regression, we compute:

    x=wTx+b(a linear score)x = w^T x + b \quad \text{(a linear score)}
  • Then use the sigmoid to convert that score (logit) into a probability of class 1:

    p=11+exp = \frac{1}{1 + e^{-x}}

This way, the model’s output is interpretable as a probability — suitable for binary classification.

Let me know if you want a plot or visualization to make it even clearer!

No comments:

Post a Comment