Write the logistic regression loss function
Company: Apple
Role: Data Scientist
Category: Statistics & Math
Difficulty: Easy
Interview Round: Technical Screen
# Write the logistic regression loss function
## Logistic Regression Loss
Consider binary logistic regression.
- Dataset: \(\{(\mathbf{x}_i, y_i)\}_{i=1}^n\)
- Labels: \(y_i \in \{0,1\}\)
- Model: \(p_i = P(y_i=1\mid \mathbf{x}_i) = \sigma(\mathbf{w}^\top \mathbf{x}_i + b)\), where \(\sigma(z)=\frac{1}{1+e^{-z}}\).
### Question
1. Write the per-example loss.
2. Write the total loss over \(n\) examples (average or sum).
3. (Optional) Write the L2-regularized objective.
### Constraints & Assumptions
- Preserve the scope, facts, inputs, and requested outputs from the prompt above.
- If the prompt leaves a detail unspecified, state a reasonable assumption before relying on it.
- Keep the answer interview-ready: concise enough to present, but concrete enough to implement or evaluate.
### Clarifying Questions to Ask
- Clarify the random variables, distributional assumptions, independence assumptions, and desired output.
- Show enough derivation for the interviewer to follow the reasoning.
- Explain how you would validate the result with simulation or sensitivity checks.
### What a Strong Answer Covers
- A correct setup with definitions, formulas, and boundary conditions.
- A step-by-step derivation or estimation plan.
- Interpretation of the result, including uncertainty and practical limitations.
- Checks for assumptions, edge cases, and numerical stability.
### Follow-up Questions
- How would the result change if the assumptions were relaxed?
- Can you verify the answer with a simulation?
- What is the most likely source of estimation error?
Quick Answer: Write the logistic regression loss function evaluates statistical assumptions, formulas, estimation strategy, uncertainty, edge cases, and interpretation in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.