KatInterview

Sample Mean: Expected Value and Variance Under Correlation

Arrays & HashingAuto-graded · 18 test cases
MediumAuto-gradedArrays & Hashingstatisticsprobabilityexpected-value

Last updated

Tests ability to compute the expected value and variance of a sample mean when observations share a common correlation, plus limiting behavior in boundary cases.

Sample Mean Properties Under Correlated Observations

You have $n$ random variables $X_1, X_2, \ldots, X_n$, each sharing the same mean $\mu$ and variance $\sigma^2$. Their average is:

$$\bar{X} = \frac{1}{n} \sum_{i=1}^{n} X_i$$

Any two distinct variables $X_i$ and $X_j$ ($i \neq j$) have covariance $\text{Cov}(X_i, X_j) = \rho\sigma^2$, where $\rho$ is a constant correlation coefficient valid for all distinct pairs.


Your Task

Given $n$, $\mu$, $\sigma^2$, and $\rho$, return a dictionary with four keys:

expected_mean The expected value of $\bar{X}$.

variance The variance of $\bar{X}$, expressed in terms of $n$, $\sigma^2$, and $\rho$.

independent_limit The limiting value of $\text{Var}(\bar{X})$ as $n \to \infty$ when observations are independent ($\rho = 0$).

perfect_positive_correlation_limit The limiting value of $\text{Var}(\bar{X})$ as $n \to \infty$ when all observations are perfectly positively correlated ($\rho = 1$).


Key Formulas to Derive

For the expected value, recall that linearity of expectation holds regardless of any dependence structure between variables.

For the variance, expand the variance of a sum using the full covariance matrix: count the diagonal terms (there are $n$ of them) and the off-diagonal terms (there are $n(n-1)$ ordered pairs), then scale by $\frac{1}{n^2}$.

The two limit values are universal constants — they do not depend on the specific inputs $n$, $\mu$, or $\sigma^2$.


Constraints

  • $1 \leq n \leq 10^6$
  • $-10^4 \leq \mu \leq 10^4$
  • $0 \leq \sigma^2 \leq 10^4$
  • $-\tfrac{1}{n-1} \leq \rho \leq 1$ for $n > 1$; $\rho = 0$ when $n = 1$ (the covariance matrix must be positive semi-definite)
  • All returned values are float
  • Return keys exactly as shown: "expected_mean", "variance", "independent_limit", "perfect_positive_correlation_limit"

Examples

Example 1

Input: sample_mean_properties(5, 10, 4, 0)

Output: {'expected_mean': 10, 'variance': 0.8, 'independent_limit': 0, 'perfect_positive_correlation_limit': 4}

Example 2

Input: sample_mean_properties(10, 3, 9, 0.5)

Output: {'expected_mean': 3, 'variance': 4.95, 'independent_limit': 0, 'perfect_positive_correlation_limit': 9}

Example 3

Input: sample_mean_properties(4, -2, 16, 1)

Output: {'expected_mean': -2, 'variance': 16, 'independent_limit': 0, 'perfect_positive_correlation_limit': 16}

Hints

Hint 1

Linearity of expectation requires no assumptions about independence — it holds for any collection of random variables, correlated or not.

Hint 2

To expand Var(X₁ + X₂ + ... + Xₙ), write it as a double sum over all Cov(Xᵢ, Xⱼ) pairs. Count the diagonal terms (i = j) separately from the off-diagonal terms (i ≠ j) and substitute the given values for each.

Hint 3

The two limit keys are the same for every valid input — think about what ρ = 0 and ρ = 1 each imply about the floor of Var(X̄) as n grows without bound.