Equicorrelation Floor, Reverse Slope, and Max-Min Covariance
Arrays & HashingAuto-graded · 19 test casesLast updated
Combine three closed-form statistics results: the lowest shared correlation of three variables, a reverse regression slope from R² and a slope, and the covariance of the max and min of two uniforms.
Equicorrelation Floor, Reverse Slope, and Max-Min Covariance
Write a function that computes three separate statistical quantities and returns them together in a list. Two of the quantities are fixed constants. The third depends on the function's inputs.
Inputs
r_squared(float or int): the coefficient of determination R² from a simple linear regression of Y on X, fitted with an intercept.slope_y_on_x(float or int): the fitted slope from regressing Y on X.
The three quantities
-
Lowest shared correlation. Three random variables A, B, C all have the same pairwise correlation ρ (every pair has correlation ρ). Return the smallest value ρ can take for such a triple to exist.
-
Reverse slope. Using the same simple regression setup, return the slope you would get if you regressed X on Y instead (that is, the slope of the X-on-Y fit), expressed in terms of
r_squaredandslope_y_on_x. -
Max-min covariance. Let U and V be independent and each uniformly distributed on the interval (0, 1). Return
Cov(max(U, V), min(U, V)).
Output
Return a list of three numbers, in this order:
[lowest_shared_correlation, reverse_slope, max_min_covariance]
The first and third entries do not depend on the inputs. The second is computed from r_squared and slope_y_on_x. Compute the covariance as the exact fraction 1/36 in floating point (1.0 / 36.0).
Constraints
0 <= r_squared <= 1slope_y_on_x != 0- Both inputs are finite real numbers.
- The returned list has exactly three entries, in the order given above.
- Results are compared as floating-point numbers.
Examples
Example 1
Input: regression_and_order_stats(0.64, 2)
Output: [-0.5, 0.32, 0.027777777777777776]
Example 2
Input: regression_and_order_stats(0.81, -3)
Output: [-0.5, -0.27, 0.027777777777777776]
Example 3
Input: regression_and_order_stats(1, 0.5)
Output: [-0.5, 2, 0.027777777777777776]
Hints
Hint 1
For three variables with equal pairwise correlation, the variance of their sum (after standardizing) can never be negative. Write it in terms of ρ.
Hint 2
In simple regression, the two slopes are Cov(X,Y)/Var(X) and Cov(X,Y)/Var(Y). Multiply them and see what you get.
Hint 3
For two values, max + min = U + V and max * min = U * V. Use E[max] = 2/3, E[min] = 1/3 and E[UV] = 1/4.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each of the three parts has a short closed-form answer.
1. Lowest shared correlation. Standardize the three variables so each has variance 1. If every pair has correlation ρ, then
Var(A + B + C) = 3 + 6ρ.
A variance cannot be negative, so 3 + 6ρ >= 0, which gives ρ >= -1/2. Equivalently, the correlation matrix (1 - ρ)I + ρ·11ᵀ has eigenvalues 1 + 2ρ (once) and 1 - ρ (twice), and positive semidefiniteness requires 1 + 2ρ >= 0. The bound is attained: take three i.i.d. variables and subtract their sample mean from each. The three centered variables sum to zero and have pairwise correlation exactly -1/2. The answer is -0.5.
2. Reverse slope. In simple regression,
- slope of Y on X = Cov(X,Y) / Var(X)
- slope of X on Y = Cov(X,Y) / Var(Y)
Their product is Cov(X,Y)² / (Var(X)Var(Y)) = ρ² = R². So the reverse slope is r_squared / slope_y_on_x. Note this is not simply the reciprocal of the first slope. The two only agree when R² = 1.
3. Max-min covariance. Let M = max(U, V) and m = min(U, V). Then M·m = U·V, so E[Mm] = 1/4 by independence. Also P(M <= t) = t², so E[M] = 2/3, and since M + m = U + V, E[m] = 1 - 2/3 = 1/3. Therefore
Cov(M, m) = 1/4 - (2/3)(1/3) = 1/4 - 2/9 = 1/36.
Why it is correct
Each part follows directly from the identities above, and together they give exactly the three values returned. The function applies them without any approximation beyond ordinary floating-point division.
Complexity
Time O(1) and space O(1).
Reference solution
from typing import List
def regression_and_order_stats(r_squared: float, slope_y_on_x: float) -> List[float]:
# Equicorrelation matrix for 3 variables must be PSD: 1 + 2*rho >= 0.
lowest_shared_correlation = -0.5
# beta(y|x) * beta(x|y) = Cov^2 / (Var(X) Var(Y)) = R^2 in simple regression.
reverse_slope = r_squared / slope_y_on_x
# E[max*min] = E[UV] = 1/4, E[max] = 2/3, E[min] = 1/3 -> 1/4 - 2/9 = 1/36.
max_min_covariance = 1.0 / 36.0
return [lowest_shared_correlation, reverse_slope, max_min_covariance]