KatInterview

Correlation Bounds, Equicorrelated Triples, and Box-Muller Sampling

Math & Bit ManipulationAuto-graded · 16 test cases
MediumAuto-gradedMath & Bit Manipulationprobabilitycorrelationbox-muller

Last updated

Compute a feasible correlation interval, give coefficients for three equally anti-correlated variables, and convert uniform values to normals with the Box-Muller transform.

Write a function that answers three small probability and simulation tasks at once and returns the results in a dictionary.

The function receives uniforms, a list of floats that are meant to be independent draws from Uniform(0,1).

Task A: feasible correlation interval

Three random variables X, Y, Z satisfy Corr(X,Y) = 0.8 and Corr(X,Z) = 0.9. Find every value that Corr(Y,Z) can take so that the 3x3 correlation matrix is valid (positive semidefinite). Let a = 0.8, b = 0.9. The feasible values form a closed interval centred at a*b with half-width sqrt((1 - a^2) * (1 - b^2)). Return it as [low, high].

Task B: three variables with pairwise correlation -1/2

Let G1 and G2 be independent standard normal variables. Describe three variables, each a linear combination of G1 and G2, with unit variance and pairwise correlation exactly -1/2. Return the coefficients as a 3x2 matrix, where row i holds the weights on (G1, G2) for the i-th variable. Use exactly these rows, in this order:

  • Row 0: (1, 0)
  • Row 1: (-1/2, sqrt(3)/2)
  • Row 2: (-1/2, -sqrt(3)/2)

Task C: normals from uniforms

Convert uniforms into standard normal values using the Box-Muller transform on consecutive, non-overlapping pairs (u1, u2) = (uniforms[0], uniforms[1]), (uniforms[2], uniforms[3]), and so on.

For each pair:

  1. Clamp u1 into [1e-15, 1 - 1e-15], and clamp u2 into [0, 1].
  2. Compute r = sqrt(-2 * ln(u1)) and theta = 2 * pi * u2.
  3. Append r * cos(theta) and then r * sin(theta) to the output, in that order.

If the list has odd length, the final element is ignored. An empty list or a single-element list produces an empty output list.

Return value

Return a dictionary with these keys:

  • "corr_yz_range": the list [low, high] from Task A.
  • "equicorrelated_construction": the 3x2 list of lists from Task B.
  • "normal_samples": the list of normals from Task C.

Constraints

  • 0 <= len(uniforms) <= 10^5
  • Each element of uniforms is a number, normally in [0, 1], though values slightly outside should be handled via the clamping rules above.
  • Floating-point results are compared against the expected values, so follow the formulas exactly.

Examples

Example 1

Input: correlation_bounds_and_box_muller([0.5, 0.5])

Output: {'corr_yz_range': [0.45846606338755974, 0.9815339366124405], 'equicorrelated_construction': [[1, 0], [-0.5, 0.8660254037844386], [-0.5, -0.8660254037844386]], 'normal_samples': [-1.1774100225154747, 1.4419114153575892e-16]}

Example 2

Input: correlation_bounds_and_box_muller([0.25, 0, 0.75, 0.5])

Output: {'corr_yz_range': [0.45846606338755974, 0.9815339366124405], 'equicorrelated_construction': [[1, 0], [-0.5, 0.8660254037844386], [-0.5, -0.8660254037844386]], 'normal_samples': [1.6651092223153954, 0, -0.7585276164409321, 9.289284175392588e-17]}

Example 3

Input: correlation_bounds_and_box_muller([0.1, 0.2, 0.3, 0.4, 0.6, 0.8])

Output: {'corr_yz_range': [0.45846606338755974, 0.9815339366124405], 'equicorrelated_construction': [[1, 0], [-0.5, 0.8660254037844386], [-0.5, -0.8660254037844386]], 'normal_samples': [0.6631399714746835, 2.0409349730505, -1.255396694924721, 0.9120990883801819, 0.31234438201626274, -0.9612971624606308]}

Hints

Hint 1

Treat a valid correlation matrix as one with a nonnegative determinant. Expanding det = 0 as a quadratic in c = Corr(Y,Z) gives (c - ab)^2 <= (1-a^2)(1-b^2).

Hint 2

For three unit vectors in the plane with pairwise cosine -1/2, put them 120 degrees apart. The coordinates are (cos, sin) of 0, 120 and 240 degrees.

Hint 3

Box-Muller: radius sqrt(-2 ln u1), angle 2*pi*u2, and each pair yields cos then sin outputs. Remember to clamp u1 so the log is finite and step through the list two at a time.