KatInterview

R² Bounds When Two Predictors Are Combined in OLS

Arrays & HashingAuto-graded · 20 test cases
MediumAuto-gradedArrays & HashingstatisticsregressionOLS

Last updated

Tests whether a candidate can derive the valid range of R² for a joint OLS model given the individual R² values of two single-predictor models.

R² Bounds When Two Predictors Are Combined in OLS

In ordinary least squares (OLS) fitted in-sample with an intercept, R² is defined as

R² = 1 − SSE / SST

where SST is the total sum of squares of the response and SSE is the residual sum of squares.


Part 1 — Theoretical range of R²

Explain why in-sample OLS with an intercept and a non-constant response always satisfies

0 ≤ R² ≤ 1

and describe the conditions that correspond to each boundary.

Also explain how this range changes in each of the following two situations:

  • The intercept is dropped (intercept-free model).
  • The model is evaluated on held-out data rather than the training rows.

Part 2 — Range of R² for the combined model

Two separate OLS regressions are fitted on exactly the same response and the same observations:

  • y ~ x1 achieves R² = r2_x1
  • y ~ x2 achieves R² = r2_x2

Given only those two values, what are the tightest possible lower and upper bounds on the R² of y ~ x1 + x2 (also fitted with an intercept on the same data)?

Implement a function that returns those bounds as a two-element list [lower, upper].


Why the two individual scores are not enough to pin down the combined score

The R² of the combined model depends on how correlated x1 and x2 are with each other and with y. Two extreme cases illustrate this:

  • If x1 and x2 carry completely redundant information about y, adding the second predictor cannot improve fit beyond the better of the two.
  • If x1 and x2 capture entirely non-overlapping variation in y, the combined model can, in principle, explain all of that variation.

The actual combined R² can fall anywhere between these extremes, so only the floor and ceiling are determinable from the individual scores alone.


Constraints

  • 0.0 ≤ r2_x1 ≤ 1.0
  • 0.0 ≤ r2_x2 ≤ 1.0
  • Both models use an intercept and are fitted on the same rows and the same response.
  • R² follows the centered definition above (not adjusted R²).
  • Evaluation is strictly in-sample.
  • Return a list of exactly two floats [lower, upper].
  • The only special case: if both individual R² values are 0.0, the combined model is also forced to 0.0, so return [0.0, 0.0].

Examples

Example 1

Input: combined_ols_r2_range(0.1, 0.2)

Output: [0.2, 1]

Example 2

Input: combined_ols_r2_range(0, 0)

Output: [0, 0]

Example 3

Input: combined_ols_r2_range(0.25, 0.25)

Output: [0.25, 1]

Example 4

Input: combined_ols_r2_range(1, 0.3)

Output: [1, 1]

Hints

Hint 1

The combined model nests both single-predictor models: it can always match whichever of the two is better by simply zeroing the other coefficient. What does that tell you about the floor?

Hint 2

Think about what geometric or algebraic condition on x1 and x2 would let the joint model drive SSE all the way to zero. Is that achievable in general when at least one predictor already has predictive power?

Hint 3

The edge case where both scores are exactly 0.0 means neither predictor correlates with y at all. Can adding a useless predictor to another useless predictor ever help?