Implement the Logit Link Function
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `logit(p)`, the inverse of the sigmoid. Given a 1-D float64 array `p`, return a new float64 `np.ndarray` of the same shape whose element i is `log(p[i] / (1 - p[i]))` (natural log). The result must match `scipy.special.logit` at the boundaries:
- `p == 0.0` gives `-inf`, and `p == 1.0` gives `+inf`.
- Any `p` strictly outside [0, 1] gives `nan`.
- Boundary values are produced without raising.
- `logit(0.5) == 0.0`.
Example:
```python
p = np.array([0.0, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0, 1.5, -0.5])
logit(p)
# [-inf, log(1/9), log(1/3), 0.0, log(3), log(9), +inf, nan, nan]
```
```hint Check the arithmetic first
Before you write any branches, work out what IEEE-754 division and `np.log` return for p = 0, p = 1 and p = 1.5.
```
### What a Strong Answer Covers
- A vectorized expression for the log-odds, with an explanation of how floating-point arithmetic yields `-inf`, `+inf` and `nan` at the required inputs.
- Keeping the expected divide-by-zero and invalid-value warnings from leaking out, scoped to this function only.
- The return value: a new float64 array with the input's shape, with the input left unchanged.
- Inputs beyond the listed ones, such as NaN, infinities and integer arrays.
- Linear cost, and a note on numerical accuracy away from the boundaries.
### Follow-up Questions
- How would you keep full relative accuracy for `p` very close to 0.5, where the result is near zero?
- Write the sigmoid so it does not overflow for large positive or negative inputs.
- Log-loss code usually clips probabilities to [eps, 1 - eps] before taking logs. Why is that sensible there and wrong here?
- What is the derivative of the logit, and where does it show up when fitting a logistic regression?
Overview: Implement the logit function, the inverse of the sigmoid, as a vectorized NumPy routine that matches scipy at the boundaries: negative infinity at 0, positive infinity at 1 and NaN outside [0, 1]. It tests floating-point edge cases, warning handling and clean array code.