Correlation Bounds, Equicorrelated Triples, and Box-Muller Sampling
Math & Bit ManipulationAuto-graded · 16 test casesLast 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:
- Clamp
u1into[1e-15, 1 - 1e-15], and clampu2into[0, 1]. - Compute
r = sqrt(-2 * ln(u1))andtheta = 2 * pi * u2. - Append
r * cos(theta)and thenr * 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
uniformsis 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Think of each standardized variable as a unit vector, where the correlation between two variables is the cosine of the angle between their vectors. All three tasks follow from this picture.
Task A. A set of correlations is feasible exactly when the correlation matrix is positive semidefinite. With a = Corr(X,Y), b = Corr(X,Z) and c = Corr(Y,Z), the 1x1 and 2x2 minors are automatically nonnegative, so only det >= 0 matters:
1 - a^2 - b^2 - c^2 + 2abc >= 0, which rearranges to (c - ab)^2 <= (1 - a^2)(1 - b^2).
So c lies in [ab - s, ab + s] with s = sqrt((1 - a^2)(1 - b^2)). For a = 0.8, b = 0.9, the centre is 0.72 and s = sqrt(0.0684), giving roughly [0.4585, 0.9815]. This also matches the angle view: the angle between Y and Z lies between the difference and the sum of the two known angles.
Task B. Three unit vectors in the plane whose pairwise cosine is -1/2 are 120 degrees apart. Their coordinates are the listed rows. Each row has squared length 1, and the dot products are -1/2, -1/2, and 1/4 - 3/4 = -1/2. Since G1, G2 are independent with unit variance, covariances equal these dot products, and so do the correlations.
Task C. Box-Muller maps two independent uniforms to two independent standard normals. The radius sqrt(-2 ln u1) has the distribution of the length of a 2D standard normal vector, and the angle 2*pi*u2 is uniform on the circle, matching the polar form of the joint normal density. Clamping u1 away from 0 keeps the logarithm finite. Looping over pairs and ignoring a trailing unpaired element gives the required output.
Complexity
- Tasks A and B are O(1).
- Task C is O(n) time and O(n) space for n input values.
Reference solution
import math
from typing import List, Dict
def correlation_bounds_and_box_muller(uniforms: List[float]) -> Dict[str, object]:
a = 0.8
b = 0.9
center = a * b
half_width = math.sqrt((1.0 - a * a) * (1.0 - b * b))
construction = [
[1.0, 0.0],
[-0.5, math.sqrt(3.0) / 2.0],
[-0.5, -math.sqrt(3.0) / 2.0],
]
normals = []
for i in range(0, len(uniforms) - 1, 2):
u1 = min(max(float(uniforms[i]), 1e-15), 1.0 - 1e-15)
u2 = min(max(float(uniforms[i + 1]), 0.0), 1.0)
r = math.sqrt(-2.0 * math.log(u1))
theta = 2.0 * math.pi * u2
normals.append(r * math.cos(theta))
normals.append(r * math.sin(theta))
return {
"corr_yz_range": [center - half_width, center + half_width],
"equicorrelated_construction": construction,
"normal_samples": normals,
}