KatInterview

Equicorrelation Floor, Reverse Slope, and Max-Min Covariance

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashingcorrelationlinear-regressionorder-statistics

Last 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

  1. 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.

  2. 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_squared and slope_y_on_x.

  3. 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 <= 1
  • slope_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.