03 — Foundations

Logistic Regression#

Classification✓ Mathematical
◆ The PatternThe bridge from regression to classification — sigmoid turns scores into probabilities

Logistic regression wraps a linear model in a sigmoid function, producing a probability between 0 and 1. Despite the name, it's a classifier, not a regressor. It's the simplest neural network — a single neuron.

P(y=1|x) = σ(w·x + b) = 1/(1 + e^(−(w·x+b)))
σ = sigmoid  |  outputs probability of class 1  |  decision boundary at 0.5
BCE = −[y·log(ŷ) + (1−y)·log(1−ŷ)]
Binary Cross-Entropy — penalises confident wrong predictions exponentially
// Decision boundary — adjust weight and bias
Weight w2.0
Bias b0.0
# PyTorch logistic regression
model = nn.Sequential(
    nn.Linear(784, 1),
    nn.Sigmoid()
)
loss_fn = nn.BCELoss()          # or BCEWithLogitsLoss (more stable)
loss = loss_fn(model(x), y)
Numerical stability: Never use nn.Sigmoid() + nn.BCELoss(). Use nn.BCEWithLogitsLoss() which combines them with the log-sum-exp trick to avoid overflow/underflow.
Pattern bridge: The sigmoid that squeezes values into [0,1] reappears as probability curves in statistics and mirrors the S-curve of market sentiment cycles — gradual build, rapid shift, saturation.
← Previous
Linear Regression
Open in the full reader, with the topic sidebar →