Compute Correlation from Three Variance Values
Arrays & HashingAuto-graded · 21 test casesLast updated
Tests whether a candidate can apply the variance-of-a-sum identity to extract correlation, and correctly handle the case where one variance is missing.
Compute Correlation from Three Variance Values
You are given three quantities describing two random variables A and B with finite second moments:
var_sum: the variance of their sum, Var(A + B)var_a: the variance of A, Var(A)var_b: the variance of B, Var(B) — this may be absent (passed asNone)
Implement a function that returns Corr(A, B) as a float, or None when the correlation cannot be determined.
Background
The variance of a sum expands as:
Var(A + B) = Var(A) + Var(B) + 2 · Cov(A, B)
Correlation is defined as:
Corr(A, B) = Cov(A, B) / sqrt(Var(A) · Var(B))
and is only meaningful when both variances are strictly positive.
Task
Given the three inputs above, return the correlation between A and B, subject to the following rules:
- If
var_bisNone, the correlation cannot be computed — returnNone. - If either
var_aorvar_bis zero or negative, the correlation is undefined — returnNone. - Otherwise, derive the covariance from the variance identity and return the correlation as a
float.
Constraints
0.0 <= var_sum(when provided as a real number)var_aandvar_bare real numbers;var_bmay beNone- These are population variances from a consistent probability model
- You do not need to validate that the result falls within
[-1, 1]; inputs are guaranteed to be consistent when all three values are present and positive
Examples
Example 1
Input: compute_correlation(13, 4, 9)
Output: 0
Example 2
Input: compute_correlation(5, 4, None)
Output: None
Example 3
Input: compute_correlation(2, 1, 1)
Output: 0
Hints
Hint 1
Start from the identity Var(A + B) = Var(A) + Var(B) + 2·Cov(A, B) and ask yourself: how many unknowns are there if var_b is missing?
Hint 2
When all three variances are available, rearrange the identity to isolate Cov(A, B), then remember that correlation divides by both standard deviations — not both variances.
Hint 3
Handle the two None-return cases (missing var_b, and non-positive variance) before doing any arithmetic.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The entire solution rests on one algebraic identity and two guard clauses.
Step 1 — check identifiability. The variance-of-a-sum identity is:
Var(A + B) = Var(A) + Var(B) + 2 · Cov(A, B)
If var_b is None, this single equation contains two unknowns — Var(B) and Cov(A, B) — so it cannot be solved for either one. Even if we could somehow extract Cov(A, B), we would still need Var(B) to form the denominator of the correlation. Return None immediately.
Step 2 — check that correlation is defined.
Correlation is the ratio Cov / (σ_A · σ_B). If either variance is zero or negative, the corresponding standard deviation is not a positive real number, and the ratio is undefined. Return None.
Step 3 — isolate the covariance. Rearranging the identity:
Cov(A, B) = (Var(A + B) − Var(A) − Var(B)) / 2
The factor of 2 is easy to drop by accident; keep it.
Step 4 — compute correlation. Divide the covariance by the geometric mean of the two variances:
Corr(A, B) = Cov(A, B) / sqrt(Var(A) · Var(B))
Why this is correct. The derivation is purely algebraic — no independence assumption is made. The identity holds for any two random variables with finite second moments, regardless of their joint distribution.
Example.
var_sum = 13, var_a = 4, var_b = 9
- Cov = (13 − 4 − 9) / 2 = 0
- Corr = 0 / sqrt(4 · 9) = 0
Time complexity: O(1) — a fixed number of arithmetic operations.
Space complexity: O(1) — no additional storage.
from math import sqrt
from typing import Optional
def compute_correlation(var_sum: float, var_a: float, var_b: Optional[float]) -> Optional[float]:
# If Var(B) is absent, the identity Var(A+B) = Var(A) + Var(B) + 2·Cov(A,B)
# has two unknowns (Var(B) and Cov(A,B)), so correlation is not identifiable.
if var_b is None:
return None
# Correlation requires strictly positive variances for both variables.
if var_a <= 0 or var_b <= 0:
return None
# Rearrange the variance-of-a-sum identity to recover the covariance.
covariance = (var_sum - var_a - var_b) / 2.0
# Normalize by the product of the two standard deviations.
return covariance / sqrt(var_a * var_b)