Convert Probabilities to Odds in NumPy

Quick Overview

Implement a vectorized NumPy function that turns an array of probabilities into odds, the quantity behind logistic regression's log-odds. It tests the odds formula, the difference between odds and log-odds, division by zero at a probability of one, and how to compare floating-point outputs in tests.

Convert Probabilities to Odds in NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `prob_to_odds(p)`. Given a 1-D `float64` NumPy array `p` of probabilities in [0, 1), return a new `float64` array of the same shape with element `i` equal to `p[i] / (1 - p[i])`. Return odds, not log-odds. Example: ```python p = np.array([0.0, 0.2, 0.5, 0.75, 0.8, 0.9]) prob_to_odds(p) # array([0. , 0.25, 1. , 3. , 4. , 9. ]) ``` NumPy prints the result at its default precision. At full precision the `float64` results for 0.8 and 0.9 print as `4.000000000000001` and `9.000000000000002`, so any test should compare with a tolerance. ```hint Check the ends of the range Try your expression at p = 0 and as p gets close to 1. Also confirm that it returns a new array instead of writing into `p`. ``` ### Constraints & Assumptions - The input is a 1-D `float64` NumPy array and may be empty. - Every value satisfies 0 <= p < 1. - The output has the same shape and dtype as the input, and the input is not modified. ### What a Strong Answer Covers - The definition of odds as the probability of the event divided by the probability of its complement, and how odds differ from log-odds. - A vectorized implementation that allocates a new array and leaves the input unchanged. - Why the denominator is never zero on [0, 1), and what happens at or near p = 1. - How floating-point rounding affects the outputs and how to test them. ### Follow-up Questions - How would you compute the inverse, `odds_to_prob(o)`? - What breaks if `p` can equal 1.0, and what are your options for handling it? - Logistic regression is linear in the log-odds. How does a coefficient translate into a change in the odds? - How would you compute log-odds from `p`, and where in [0, 1) does floating-point precision limit the result?

Overview: Implement a vectorized NumPy function that turns an array of probabilities into odds, the quantity behind logistic regression's log-odds. It tests the odds formula, the difference between odds and log-odds, division by zero at a probability of one, and how to compare floating-point outputs in tests.

Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement prob_to_odds(p). Given a 1-D float64 NumPy array p of probabilities in [0, 1), return a new float64 array of the same shape with element i equal to p[i] / (1 - p[i]). Return odds, not log-odds.

Example:

p = np.array([0.0, 0.2, 0.5, 0.75, 0.8, 0.9])
prob_to_odds(p)
# array([0.  , 0.25, 1.  , 3.  , 4.  , 9.  ])

NumPy prints the result at its default precision. At full precision the float64 results for 0.8 and 0.9 print as 4.000000000000001 and 9.000000000000002, so any test should compare with a tolerance.

Constraints & Assumptions

  • The input is a 1-D float64 NumPy array and may be empty.
  • Every value satisfies 0 <= p < 1.
  • The output has the same shape and dtype as the input, and the input is not modified.

What a Strong Answer Covers Guidance

  • The definition of odds as the probability of the event divided by the probability of its complement, and how odds differ from log-odds.
  • A vectorized implementation that allocates a new array and leaves the input unchanged.
  • Why the denominator is never zero on [0, 1), and what happens at or near p = 1.
  • How floating-point rounding affects the outputs and how to test them.

Follow-up Questions Guidance

  • How would you compute the inverse, odds_to_prob(o) ?
  • What breaks if p can equal 1.0, and what are your options for handling it?
  • Logistic regression is linear in the log-odds. How does a coefficient translate into a change in the odds?
  • How would you compute log-odds from p , and where in [0, 1) does floating-point precision limit the result?