Compare Log-Loss and MSE on the Same Predicted Probabilities
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `log_loss_vs_mse(y_true: np.ndarray, y_pred: np.ndarray) -> tuple[float, float]`. `y_true` is a 1-D array of 0/1 labels of shape `(n,)`, and `y_pred` is a 1-D array of `P(y=1)` of shape `(n,)` with every value strictly inside `(0, 1)`. Compute both metrics from scratch and return the 2-tuple of Python floats `(log_loss, mse)`, where:
- `log_loss = -(1/n) * sum(y*log(p) + (1-y)*log(1-p))`, using the natural log.
- `mse = (1/n) * sum((y - p)**2)`.
Use `numpy` only (no `sklearn.metrics`).
```text
Input: y_true = [1, 0, 1, 1, 0], y_pred = [0.9, 0.1, 0.8, 0.6, 0.3]
Output: (0.26027303006691704, 0.062)
Input: y_true = [1], y_pred = [1e-7]
Output: (16.11809565095832, 0.9999998000000101)
```
```hint Which term applies
Each metric is a mean of an elementwise expression over the same two arrays. For the log-loss, check which of its two terms is active when the label is 0.
```
### Constraints & Assumptions
- `y_true` and `y_pred` have the same shape `(n,)`.
- Every value of `y_pred` is strictly inside `(0, 1)`, so both logs are finite.
- Only `numpy` is allowed.
### What a Strong Answer Covers
- Correct vectorized formulas for both metrics, with the right log-loss term for each label.
- Python floats returned in the order `(log_loss, mse)`, matching both examples.
- Why the second example gives a large log-loss and an MSE just under 1.
- Why log-loss is the usual training objective for a logistic model, in terms of gradients and convexity.
- How to handle predictions at or very close to 0 and 1.
### Follow-up Questions
- Why does log-loss explode while MSE saturates near 1 on the confidently wrong case?
- How does MSE on a sigmoid output create a non-convex objective?
- How would you guard against inputs at the boundaries 0 or 1?
Overview: Implement log-loss and mean squared error from scratch in NumPy and score the same predicted probabilities with both. It tests the binary cross-entropy formula and how the two losses treat confident mistakes, gradients and convexity when training a logistic model.