R² Bounds When Two Predictors Are Combined in OLS
Arrays & HashingAuto-graded · 20 test casesLast 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 ~ x1achieves R² =r2_x1y ~ x2achieves 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
x1andx2carry completely redundant information abouty, adding the second predictor cannot improve fit beyond the better of the two. - If
x1andx2capture entirely non-overlapping variation iny, 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.00.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 to0.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?
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Part 1 — Why R² lives in [0, 1] for in-sample OLS with an intercept
OLS minimizes SSE over all linear functions of the predictors. The intercept-only model (predict the sample mean for every observation) is always a feasible solution, and it achieves SSE = SST by definition. Because OLS cannot do worse than any particular feasible solution, SSE ≤ SST, which gives R² ≥ 0. SSE is a sum of squares so SSE ≥ 0, which gives R² ≤ 1.
R² = 0 when the predictors add nothing beyond the intercept (fitted values are all equal to the mean).
R² = 1 when the data lie exactly on a hyperplane (zero residuals).
Without an intercept the centering identity breaks down and SSE can exceed SST, pushing R² below zero. The upper limit of 1 still holds in sample.
Out of sample both bounds can be violated: a model that overfits training data can produce SSE > SST on new rows (R² < 0), and R² > 1 is impossible only because of arithmetic — in practice out-of-sample R² is essentially always below 1 unless the held-out set happens to lie perfectly on the fitted hyperplane.
Part 2 — Bounds for the combined model
Lower bound — max(r2_x1, r2_x2)
Adding a predictor to an OLS model can never decrease in-sample R². More formally, the combined model y ~ x1 + x2 nests both y ~ x1 (set the coefficient of x2 to zero) and y ~ x2 (set the coefficient of x1 to zero). OLS will find a solution at least as good as either constrained version, so
R²(x1+x2) ≥ max(R²(x1), R²(x2))
This lower bound is tight: it is achieved when x1 and x2 are perfectly correlated (one carries no information beyond the other).
Upper bound — 1.0 (except the all-zero edge case)
Whenever at least one predictor has a nonzero correlation with y, it is possible to construct a dataset where x1 and x2 together explain y perfectly. Concretely, decompose the residuals from the better single-predictor model: a second predictor that is orthogonal to the first and aligns with those residuals drives SSE to zero. Therefore R² = 1 is achievable, and the upper bound is 1.
Edge case: both scores are exactly 0.0
R²(x1) = 0 means x1 has zero (linear) correlation with y, and similarly for x2. A linear combination of two predictors that are each uncorrelated with y is itself uncorrelated with y, so the combined model cannot beat the intercept-only baseline either. The upper bound collapses to 0, giving [0.0, 0.0].
Why the exact combined R² is not determined
The actual value anywhere in [lower, upper] is achievable depending on the correlation structure between x1, x2, and y. Two individual R² values tell you nothing about how much overlap the two predictors share in explaining y, so only the endpoints can be determined analytically.
Complexity
- Time: O(1) — a comparison and a conditional.
- Space: O(1) — two scalar outputs.
Reference solution
from typing import List
def combined_ols_r2_range(r2_x1: float, r2_x2: float) -> List[float]:
# The combined model nests both single-predictor models, so it cannot
# score below the better of the two.
lower = max(r2_x1, r2_x2)
# As long as at least one predictor carries signal, R² = 1 is reachable.
# If both are zero, no linear combination can beat the intercept-only model.
upper = 0.0 if r2_x1 == 0.0 and r2_x2 == 0.0 else 1.0
return [lower, upper]