Odds Ratio Between Two Probabilities

Quick Overview

Write a Python function that turns a treatment probability and a control probability into an odds ratio. It tests converting probabilities to odds, reading the ratio as an effect size, and how the result relates to logistic regression coefficients and to the risk ratio.

Odds Ratio Between Two Probabilities

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `odds_ratio(p_treatment, p_control)`. Given two Python floats, each strictly in `(0, 1)`, return `(p_treatment / (1 - p_treatment)) / (p_control / (1 - p_control))` as a scalar `float` (`1.0` means no effect, `> 1` means treatment raises the odds). Requirements: - Return a scalar Python or NumPy `float`, not an array. - Do not take a logarithm (that gives the log odds ratio). For example: ```python odds_ratio(0.75, 0.5) -> 3.0 odds_ratio(0.5, 0.5) -> 1.0 odds_ratio(0.2, 0.5) -> 0.25 odds_ratio(0.9, 0.75) -> 3.0 # 3.0000000000000004 after float rounding ``` ```hint Check the symmetric case Work out each group's odds on its own before combining them, and confirm your function returns exactly 1.0 when the two probabilities are equal. ``` ### What a Strong Answer Covers - The conversion from a probability to odds and the ratio of the two groups' odds. - How to read the result when it equals 1, is above 1, or is below 1. - Why the inputs must be strictly inside (0, 1), and what happens at the boundaries. - How the odds ratio connects to a logistic regression coefficient. ### Follow-up Questions - How does the odds ratio differ from the risk ratio `p_treatment / p_control`, and when are the two close? - A logistic regression on a single binary treatment indicator fits a coefficient `beta`. How does `exp(beta)` relate to this function's output? - How would you vectorize this over arrays of probability pairs, and how would you handle inputs of exactly 0 or 1?

Overview: Write a Python function that turns a treatment probability and a control probability into an odds ratio. It tests converting probabilities to odds, reading the ratio as an effect size, and how the result relates to logistic regression coefficients and to the risk ratio.

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

Implement odds_ratio(p_treatment, p_control). Given two Python floats, each strictly in (0, 1), return (p_treatment / (1 - p_treatment)) / (p_control / (1 - p_control)) as a scalar float (1.0 means no effect, > 1 means treatment raises the odds).

Requirements:

  • Return a scalar Python or NumPy float , not an array.
  • Do not take a logarithm (that gives the log odds ratio).

For example:

odds_ratio(0.75, 0.5)  -> 3.0
odds_ratio(0.5, 0.5)   -> 1.0
odds_ratio(0.2, 0.5)   -> 0.25
odds_ratio(0.9, 0.75)  -> 3.0   # 3.0000000000000004 after float rounding

What a Strong Answer Covers Guidance

  • The conversion from a probability to odds and the ratio of the two groups' odds.
  • How to read the result when it equals 1, is above 1, or is below 1.
  • Why the inputs must be strictly inside (0, 1), and what happens at the boundaries.
  • How the odds ratio connects to a logistic regression coefficient.

Follow-up Questions Guidance

  • How does the odds ratio differ from the risk ratio p_treatment / p_control , and when are the two close?
  • A logistic regression on a single binary treatment indicator fits a coefficient beta . How does exp(beta) relate to this function's output?
  • How would you vectorize this over arrays of probability pairs, and how would you handle inputs of exactly 0 or 1?