Shift Probability In Log-Odds Space

Quick Overview

Implement a vectorized NumPy function that shifts an array of probabilities by a constant in log-odds space and maps the result back with the sigmoid. It tests the link between log-odds and odds ratios and careful handling of float64 rounding near 0 and 1.

Shift Probability In Log-Odds Space

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `shift_prob_by_log_odds(p, delta)`. Given a 1-D float64 array `p` of probabilities and a Python float `delta`, return a new float64 `np.ndarray` of the same shape holding `sigmoid(logit(p) + delta)`. The function must meet two requirements: - Every returned value lies strictly in (0, 1). - `delta = 0.0` returns `p` unchanged. ```python p = np.array([0.1, 0.25, 0.5, 0.75, 0.9]) delta = np.log(3.0) # odds ratio of exactly 3 shift_prob_by_log_odds(p, delta) # approximately [0.25, 0.5, 0.75, 0.9, 0.9642857142857143] shift_prob_by_log_odds(p, 0.0) # unchanged: [0.1, 0.25, 0.5, 0.75, 0.9] ``` ```hint Floating point at the ends The sigmoid never reaches 0 or 1 on paper. Check what float64 gives you for the sigmoid of 40, and whether `expit(logit(p))` returns exactly `p`. ``` ### Constraints & Assumptions - Inputs are guaranteed strictly in (0, 1), so `logit(p)` is finite and no clipping of the input is required. - `delta` is a finite Python float. It can be large enough that the exact result is closer to 0 or 1 than float64 can represent, and the open-interval requirement still applies. ### What a Strong Answer Covers - Moves to log-odds, adds `delta`, and maps back with the sigmoid in one vectorized pass. - Explains what the shift does to the odds and uses that to check the example by hand. - Handles the floating-point cases: shifted values that round to exactly 0 or 1, and `delta = 0.0` returning the original values exactly. - Uses a sigmoid that does not overflow for large negative inputs, and states the time and space cost. ### Follow-up Questions - Why keep the shift in log-odds space instead of adding to the probability directly? - How would you handle inputs that can be exactly 0 or 1? - A model was trained on data with a 50% positive rate, but the deployed population has a 10% positive rate. How could this function correct its predicted probabilities, and what assumption does that need?

Overview: Implement a vectorized NumPy function that shifts an array of probabilities by a constant in log-odds space and maps the result back with the sigmoid. It tests the link between log-odds and odds ratios and careful handling of float64 rounding near 0 and 1.

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

Implement shift_prob_by_log_odds(p, delta). Given a 1-D float64 array p of probabilities and a Python float delta, return a new float64 np.ndarray of the same shape holding sigmoid(logit(p) + delta).

The function must meet two requirements:

  • Every returned value lies strictly in (0, 1).
  • delta = 0.0 returns p unchanged.
p = np.array([0.1, 0.25, 0.5, 0.75, 0.9])
delta = np.log(3.0)   # odds ratio of exactly 3
shift_prob_by_log_odds(p, delta)
# approximately [0.25, 0.5, 0.75, 0.9, 0.9642857142857143]
shift_prob_by_log_odds(p, 0.0)
# unchanged: [0.1, 0.25, 0.5, 0.75, 0.9]

Constraints & Assumptions

  • Inputs are guaranteed strictly in (0, 1), so logit(p) is finite and no clipping of the input is required.
  • delta is a finite Python float. It can be large enough that the exact result is closer to 0 or 1 than float64 can represent, and the open-interval requirement still applies.

What a Strong Answer Covers Guidance

  • Moves to log-odds, adds delta , and maps back with the sigmoid in one vectorized pass.
  • Explains what the shift does to the odds and uses that to check the example by hand.
  • Handles the floating-point cases: shifted values that round to exactly 0 or 1, and delta = 0.0 returning the original values exactly.
  • Uses a sigmoid that does not overflow for large negative inputs, and states the time and space cost.

Follow-up Questions Guidance

  • Why keep the shift in log-odds space instead of adding to the probability directly?
  • How would you handle inputs that can be exactly 0 or 1?
  • A model was trained on data with a 50% positive rate, but the deployed population has a 10% positive rate. How could this function correct its predicted probabilities, and what assumption does that need?