Categorical contrasts in OLS against a caller-chosen reference level

Quick Overview

Fit an OLS regression of a numeric target on a three-level categorical feature with a caller-chosen reference level, and return the two treatment contrasts as a labeled pandas Series. It tests dummy coding, what the intercept and coefficients mean, and control over the baseline category.

Categorical contrasts in OLS against a caller-chosen reference level

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `contrast_vs_reference(df, reference)`. `df` has columns `y` (float) and `g` (object, with levels `'a'`, `'b'`, `'c'`), and `reference` is one of those levels. Fit OLS of `y` on `g` with `reference` as the baseline and return the two non-reference contrasts. ```python def contrast_vs_reference(df: pd.DataFrame, reference: str) -> pd.Series: ... ``` Requirements: - Fit an OLS model with an intercept on dummies for `g`, omitting `reference`. Each coefficient is that level's mean-`y` difference versus the reference. - Index: the two remaining level names in sorted order. Values: their coefficients. - The Series has `dtype` `float64` and `name` `'contrast'`. Example: ```text g: a a b b b c c y: 1.0 3.0 4.0 6.0 8.0 7.0 8.0 contrast_vs_reference(df, reference='b') returns a -4.0 c 1.5 Name: contrast, dtype: float64 ``` ```hint Which level gets dropped Library defaults usually drop the first level in sorted order. Check that the omitted dummy is the caller's reference, including when the reference is not `'a'`. ``` ### Constraints & Assumptions - `g` takes exactly the levels `'a'`, `'b'` and `'c'`, and `reference` is one of them. - Each level has at least one row. ### What a Strong Answer Covers - Builds treatment-coded dummies with the caller's reference as the omitted level, wherever it falls alphabetically. - Fits OLS with an intercept and maps the coefficients back to plain level names. - Returns a `float64` Series named `'contrast'`, indexed by the two other levels in sorted order. - Explains why each coefficient equals a difference in group means. ### Follow-up Questions - How would you recover the reference level's own mean from this fit? - Does the choice of reference level change model fit or predictions? - How would you get the contrast between the two non-reference levels, with its standard error, without refitting?

Overview: Fit an OLS regression of a numeric target on a three-level categorical feature with a caller-chosen reference level, and return the two treatment contrasts as a labeled pandas Series. It tests dummy coding, what the intercept and coefficients mean, and control over the baseline category.

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

Implement contrast_vs_reference(df, reference). df has columns y (float) and g (object, with levels 'a', 'b', 'c'), and reference is one of those levels. Fit OLS of y on g with reference as the baseline and return the two non-reference contrasts.

def contrast_vs_reference(df: pd.DataFrame, reference: str) -> pd.Series:
    ...

Requirements:

  • Fit an OLS model with an intercept on dummies for g , omitting reference . Each coefficient is that level's mean- y difference versus the reference.
  • Index: the two remaining level names in sorted order. Values: their coefficients.
  • The Series has dtype float64 and name 'contrast' .

Example:

g:  a    a    b    b    b    c    c
y:  1.0  3.0  4.0  6.0  8.0  7.0  8.0

contrast_vs_reference(df, reference='b') returns
a   -4.0
c    1.5
Name: contrast, dtype: float64

Constraints & Assumptions

  • g takes exactly the levels 'a' , 'b' and 'c' , and reference is one of them.
  • Each level has at least one row.

What a Strong Answer Covers Guidance

  • Builds treatment-coded dummies with the caller's reference as the omitted level, wherever it falls alphabetically.
  • Fits OLS with an intercept and maps the coefficients back to plain level names.
  • Returns a float64 Series named 'contrast' , indexed by the two other levels in sorted order.
  • Explains why each coefficient equals a difference in group means.

Follow-up Questions Guidance

  • How would you recover the reference level's own mean from this fit?
  • Does the choice of reference level change model fit or predictions?
  • How would you get the contrast between the two non-reference levels, with its standard error, without refitting?