Logistic regression, odds, and calibration
Logistic regression adds evidence in log-odds space; calibration and policy give that score operational meaning.
Support tickets need explainable routing under a 15 ms latency budget.
This chapter moves from build the logit to separate probability from policy. We will make the idea concrete in a small code example before placing it back inside a production system with 15 ms batch-one latency.
Check calibration, then choose a threshold from costs and capacity rather than defaulting to 0.5.
Verify the behavior illustrated in logistic_score.py, then measure decision utility, slice reliability, and system latency.
Unscaled revenue dominates kNN distance while perfectly separating features destabilize unregularized logistic coefficients. An unsafe release must degrade to a named baseline or the last known-good version.
Build the idea one layer at a time.
Logistic regression is linear in log-odds, not in probability. The sigmoid turns an unbounded additive score into a value between zero and one.
If a coefficient increases the logit by one, it multiplies the odds by e while other modeled features stay fixed. The same logit change has a smaller probability effect near zero or one because the sigmoid compresses its tails.
Regularization stabilizes coefficients under correlated or separating features, but it does not guarantee calibrated probabilities after population drift. Validate reliability by slice and fit calibration on held-out data when the decision needs trustworthy risk.
- log odds
- Bayes rule
- feature scaling
- distance concentration
Follow the reasoning before touching the code.
- 01
Build the logit
Apply the exact fitted preprocessing, multiply ordered features by coefficients, and include the intercept once.
- 02
Interpret odds carefully
Exponentiate a coefficient for its conditional odds multiplier, without claiming causal influence.
- 03
Separate probability from policy
Check calibration, then choose a threshold from costs and capacity rather than defaulting to 0.5.
from math import exp
features = {"late_before": 1.0, "spend_z": 0.5}
weights = {"late_before": 1.2, "spend_z": -0.4}
intercept = -0.7
logit = intercept + sum(weights[name] * value for name, value in features.items())
odds = exp(logit)
probability = 1 / (1 + exp(-logit))
threshold = 0.65
decision = "escalate" if probability >= threshold else "standard"
print({"logit": round(logit, 3), "odds": round(odds, 3)})
print({"probability": round(probability, 3), "decision": decision})Convert an additive logit into odds, probability, and an action.
What to notice
- The positive late_before coefficient raises odds, while positive standardized spend lowers them in this fitted association.
- Probability and threshold are separate: changing operational cost can change the action without retraining coefficients.
YOUR TURN Pause and predict before revealing the answer.
What happens to the odds if one feature increases the logit by ln(2)?
Need a hint?
Odds are exp(logit), and exp(a + b) = exp(a) × exp(b).
Check your reasoning
The odds double because exp(ln(2)) = 2; the probability increase depends on the starting logit.
Lessons 1 in this course.
Connect the chapter to the course’s quantitative spine.
Logistic regression adds evidence in log-odds space; calibration and policy give that score operational meaning. The logistic probability below is the shared course-level lens that helps quantify the surrounding system. Name each symbol, check its units or shape, and use only values that were available at the production cutoff.
Logistic probability
| Symbol | Meaning / shape / unit |
|---|---|
x | feature vector |
β | learned coefficients |
xᵀβ | linear log-odds score |
Open derivation and numerical substitution
Start from the production quantity being optimized, substitute the observed values with their declared units, then isolate the model-controlled term. Preserve shape annotations at each step so broadcasting or aggregation cannot silently change the result.
- Write the named inputs: x, β, xᵀβ.
- Substitute one small, hand-checkable batch before vectorizing.
- Calculate an independent reference value and compare within a declared tolerance.
# equation → code contract
inputs = validate_shapes_and_units(batch)
value = compute_c04(inputs)
assert is_finite(value)Change one assumption and make the tradeoff visible.
Use the worked chapter as your prediction, then change one input in the lab and explain why the output moves. The lab runs predefined TypeScript and never executes learner code, so the focus stays on the concept rather than setup.
Regularization boundary lab
Increase regularization and watch complexity trade against margin violations.
Assumption: Synthetic standardized two-feature classification problem.
Open nonvisual data table
| Item | Computed state | Interpretation |
|---|---|---|
| x=-2 | logit -2.57 | negative |
| x=-1 | logit -1.29 | negative |
| x=0 | logit 0.00 | negative |
| x=1 | logit 1.29 | positive |
| x=2 | logit 2.57 | positive |
Trace the complete operating path.
Carry the chapter’s central idea into the system: Logistic regression adds evidence in log-odds space; calibration and policy give that score operational meaning. The path below shows where that reasoning needs an artifact, a test, an owner, or a fallback.
- 01
Turn “Build the logit” into a written invariant and a failing test.
- 02
Promote logistic_score.py from illustration to a versioned implementation with representative fixtures.
- 03
Use “Separate probability from policy” as the release checkpoint: Check calibration, then choose a threshold from costs and capacity rather than defaulting to 0.5.
- 04
Serve or materialize behind explicit 15 ms batch-one latency.
- 05
Join telemetry to mature outcomes and retain a rollback path.
Observability
Join service health, input quality, prediction distributions, slice behavior, and mature outcomes by exact version.
Cost
Measure storage, preprocessing, compute, queueing, and human review under a representative arrival pattern.
Failure modes
Unscaled revenue dominates kNN distance while perfectly separating features destabilize unregularized logistic coefficients. Add a detector, owner, mitigation, and stop condition for this class of failure.
Alternatives
Compare a rule, a simpler statistical baseline, and a different system boundary before adding model complexity.
Explain the contract, not just the vocabulary.
Explainable ticket-routing baseline
Compare linear/logistic regression, Naive Bayes, kNN, and SVM under one evaluation and serving contract.
- Preprocessing parity
- Model-family comparison
- Calibration and cost policy
- Serialized latency test
Read primary material with a purpose.
Return to the opening failure.
Compare quality, calibration, latency, memory, and interpretability under one stable validation contract.
For this lesson, the release evidence is the worked code example, a hand-checked formal result, deterministic simulation output, a ≥80% checkpoint, the production rubric, and a named fallback. The course resolves when the system can produce a justified baseline across regression, naive bayes, knn, and svm.