Compute Cohen's d for two samples with a pooled standard deviation

Quick Overview

Implement Cohen's d for two independent samples using a pooled standard deviation built from sample variances. It tests effect size conventions, degrees of freedom in the pooled variance, the sign of the difference and edge cases such as tiny groups or zero variance.

Compute Cohen's d for two samples with a pooled standard deviation

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: easy

Interview Round: Other

Implement `cohens_d(control: np.ndarray, treatment: np.ndarray) -> float`. Given two 1-D arrays, return Cohen's d using ```text sp = sqrt(((n1 - 1) * s1**2 + (n2 - 1) * s2**2) / (n1 + n2 - 2)) d = (mean(treatment) - mean(control)) / sp ``` where `n1`, `s1` belong to the control group and `n2`, `s2` to the treatment group. Requirements: - `s1` and `s2` are sample standard deviations with `ddof=1`, never `ddof=0`. - Sign: treatment minus control, so `d` is positive when the treatment mean is higher. - No Welch or unpooled standard deviation, and no Hedges' g correction. - Return an unrounded `float`. Example: ```python control = np.array([10, 12, 11, 13]) treatment = np.array([14, 15, 13, 16]) cohens_d(control, treatment) # about 2.3238 (treatment mean is higher) ``` ```hint Degrees of freedom Check which `ddof` NumPy's `std` uses by default, and compare it with the sample standard deviation the formula asks for. ``` ### Constraints & Assumptions - Both inputs are 1-D numeric arrays from independent groups. - Each group has at least two observations, so a sample standard deviation exists. ### What a Strong Answer Covers - Sample standard deviations with `ddof=1`, combined into a pooled variance weighted by each group's degrees of freedom. - The treatment-minus-control sign convention. - An unrounded float with no Welch-style pooling and no small-sample correction. - Edge cases: a group with fewer than two values, a pooled standard deviation of zero, and missing values. - What the value means, and when pooling is a poor choice. ### Follow-up Questions - When would you report Hedges' g instead, and how is the correction computed? - The two groups have very different variances. What does the pooled d hide, and what would you report instead? - How does Cohen's d relate to the pooled two-sample t-statistic? - Why do product A/B tests rarely use d as the headline result?

Overview: Implement Cohen's d for two independent samples using a pooled standard deviation built from sample variances. It tests effect size conventions, degrees of freedom in the pooled variance, the sign of the difference and edge cases such as tiny groups or zero variance.

Nov 25, 2026
easyData ScientistOtherAnalytics & Experimentation
0
0

Implement cohens_d(control: np.ndarray, treatment: np.ndarray) -> float. Given two 1-D arrays, return Cohen's d using

sp = sqrt(((n1 - 1) * s1**2 + (n2 - 1) * s2**2) / (n1 + n2 - 2))
d  = (mean(treatment) - mean(control)) / sp

where n1, s1 belong to the control group and n2, s2 to the treatment group.

Requirements:

  • s1 and s2 are sample standard deviations with ddof=1 , never ddof=0 .
  • Sign: treatment minus control, so d is positive when the treatment mean is higher.
  • No Welch or unpooled standard deviation, and no Hedges' g correction.
  • Return an unrounded float .

Example:

control = np.array([10, 12, 11, 13])
treatment = np.array([14, 15, 13, 16])
cohens_d(control, treatment)  # about 2.3238 (treatment mean is higher)

Constraints & Assumptions

  • Both inputs are 1-D numeric arrays from independent groups.
  • Each group has at least two observations, so a sample standard deviation exists.

What a Strong Answer Covers Guidance

  • Sample standard deviations with ddof=1 , combined into a pooled variance weighted by each group's degrees of freedom.
  • The treatment-minus-control sign convention.
  • An unrounded float with no Welch-style pooling and no small-sample correction.
  • Edge cases: a group with fewer than two values, a pooled standard deviation of zero, and missing values.
  • What the value means, and when pooling is a poor choice.

Follow-up Questions Guidance

  • When would you report Hedges' g instead, and how is the correction computed?
  • The two groups have very different variances. What does the pooled d hide, and what would you report instead?
  • How does Cohen's d relate to the pooled two-sample t-statistic?
  • Why do product A/B tests rarely use d as the headline result?