Clipped Logit Without Infinities

Quick Overview

Implement a NumPy logit that clips probabilities into a small epsilon band before taking log-odds, so inputs of exactly 0 or 1 still give finite outputs. It checks operation order, float64 rounding at the clip bounds and why the two extreme outputs are not exact negatives.

Clipped Logit Without Infinities

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `logit_clipped(p, eps=1e-15)`. Given a 1-D `float64` array `p` of probabilities in [0, 1] and a Python float `eps`, clip `p` into `[eps, 1 - eps]` and return a new `float64` array of the same shape holding `log(p_clipped / (1 - p_clipped))`. Requirements: - Clip before the log, not after. - Compute the upper bound as the plain float64 expression `1 - eps` and use it as-is. - Do not derive the upper extreme as `-logit(eps)` or mirror the lower bound. The extremes are not exact negatives (`1 - 1e-15` is `9.992007221626409e-16` from 1.0). - Every value must be finite: `np.isfinite(out).all()` is asserted. For example: ```python p = np.array([0.0, 0.001, 0.5, 0.999, 1.0]) logit_clipped(p) # eps=1e-15 # [-34.538776394910684, -6.906754778648554, 0.0, # 6.906754778648553, 34.53957599234088] logit_clipped(p, eps=1e-6) # [-13.815509557963773, -6.906754778648554, 0.0, # 6.906754778648553, 13.815509557935018] ``` ```hint Order and endpoints Think about what `log` returns at 0 and which of the two operations has to happen first. Then look at the two endpoint outputs in the example: they are not exact negatives, and your code should produce them without any special case. ``` ### Constraints & Assumptions - `p` is a 1-D `float64` array with values in [0, 1], endpoints included. - `eps` is a Python float, default `1e-15`. - The output is a new `float64` array with the same shape as `p`. The caller's array must not change. ### What a Strong Answer Covers - Clipping into `[eps, 1 - eps]` before the log, with the upper bound computed once as `1 - eps` - The log-odds transform on the clipped values, returned as a new float64 array of the input's shape - Why the two extreme outputs are not exact negatives in float64 - Keeping the caller's array unmodified and every output finite ### Follow-up Questions - Why does clipping before the log matter numerically? - How would you vectorize this to avoid intermediate copies? - Where would a clipped logit show up in a modeling pipeline?

Overview: Implement a NumPy logit that clips probabilities into a small epsilon band before taking log-odds, so inputs of exactly 0 or 1 still give finite outputs. It checks operation order, float64 rounding at the clip bounds and why the two extreme outputs are not exact negatives.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement logit_clipped(p, eps=1e-15). Given a 1-D float64 array p of probabilities in [0, 1] and a Python float eps, clip p into [eps, 1 - eps] and return a new float64 array of the same shape holding log(p_clipped / (1 - p_clipped)).

Requirements:

  • Clip before the log, not after.
  • Compute the upper bound as the plain float64 expression 1 - eps and use it as-is.
  • Do not derive the upper extreme as -logit(eps) or mirror the lower bound. The extremes are not exact negatives ( 1 - 1e-15 is 9.992007221626409e-16 from 1.0).
  • Every value must be finite: np.isfinite(out).all() is asserted.

For example:

p = np.array([0.0, 0.001, 0.5, 0.999, 1.0])

logit_clipped(p)             # eps=1e-15
# [-34.538776394910684, -6.906754778648554, 0.0,
#    6.906754778648553, 34.53957599234088]

logit_clipped(p, eps=1e-6)
# [-13.815509557963773, -6.906754778648554, 0.0,
#    6.906754778648553, 13.815509557935018]

Constraints & Assumptions

  • p is a 1-D float64 array with values in [0, 1], endpoints included.
  • eps is a Python float, default 1e-15 .
  • The output is a new float64 array with the same shape as p . The caller's array must not change.

What a Strong Answer Covers Guidance

  • Clipping into [eps, 1 - eps] before the log, with the upper bound computed once as 1 - eps
  • The log-odds transform on the clipped values, returned as a new float64 array of the input's shape
  • Why the two extreme outputs are not exact negatives in float64
  • Keeping the caller's array unmodified and every output finite

Follow-up Questions Guidance

  • Why does clipping before the log matter numerically?
  • How would you vectorize this to avoid intermediate copies?
  • Where would a clipped logit show up in a modeling pipeline?