Sample Mean: Expected Value and Variance Under Correlation
Arrays & HashingAuto-graded · 18 test casesLast 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Expected Value
Linearity of expectation states that $E[aY + bZ] = aE[Y] + bE[Z]$ for any random variables, with no requirement that they be independent. Applying this:
$$E[\bar{X}] = E!\left[\frac{1}{n}\sum_{i=1}^n X_i\right] = \frac{1}{n}\sum_{i=1}^n E[X_i] = \frac{1}{n} \cdot n\mu = \mu$$
So expected_mean = mu exactly, for every valid input.
Variance
Step 1 — pull out the scalar. Because $\text{Var}(cY) = c^2 \text{Var}(Y)$:
$$\text{Var}(\bar{X}) = \frac{1}{n^2},\text{Var}!\left(\sum_{i=1}^n X_i\right)$$
Step 2 — expand using the covariance matrix. The variance of a sum equals the sum of every entry of the covariance matrix:
$$\text{Var}!\left(\sum_i X_i\right) = \sum_{i=1}^n\sum_{j=1}^n \text{Cov}(X_i, X_j)$$
Step 3 — count the terms.
- $n$ diagonal entries where $i = j$: each equals $\sigma^2$.
- $n(n-1)$ ordered off-diagonal entries where $i \neq j$: each equals $\rho\sigma^2$.
$$\text{Var}!\left(\sum_i X_i\right) = n\sigma^2 + n(n-1)\rho\sigma^2$$
Step 4 — divide by $n^2$.
$$\text{Var}(\bar{X}) = \frac{\sigma^2\bigl[1 + (n-1)\rho\bigr]}{n}$$
A useful equivalent form is $\rho\sigma^2 + \tfrac{(1-\rho)\sigma^2}{n}$, which separates the variance into a floor term $\rho\sigma^2$ that averaging cannot reduce and a term $(1-\rho)\sigma^2/n$ that shrinks with $n$.
Limit Values
Independent case ($\rho = 0$): the formula becomes $\sigma^2/n$, which tends to $0$ as $n \to \infty$. More data always helps; $\bar{X}$ converges to $\mu$ in probability (the weak law of large numbers).
Perfectly correlated case ($\rho = 1$): substituting gives $\sigma^2(1 + n - 1)/n = \sigma^2$ for every finite $n$. The limit is $\sigma^2$ — identical to the variance of a single observation. All $X_i$ are effectively the same random variable, so no amount of averaging reduces uncertainty.
These limits are constants independent of the inputs, so independent_limit = 0.0 and perfect_positive_correlation_limit = sigma_squared always.
Correctness Checks
- $\rho = 0, n = 1$: formula gives $\sigma^2$. ✓
- $\rho = 1$: formula gives $\sigma^2$ for all $n$. ✓
- Large $n$, small $\rho$: variance ≈ $\rho\sigma^2$ (floors out). ✓
Complexity
- Time: $O(1)$ — a fixed number of arithmetic operations.
- Space: $O(1)$ — only scalar values stored.
from typing import Dict
def sample_mean_properties(n: int, mu: float, sigma_squared: float, rho: float) -> Dict[str, float]:
if n <= 0:
raise ValueError("n must be positive")
# E[X̄] = mu by linearity of expectation (no independence needed)
expected_mean = float(mu)
# Var(X̄) = (1/n²) * [n*sigma² + n(n-1)*rho*sigma²]
# = sigma² * (1 + (n-1)*rho) / n
variance = sigma_squared * (1 + (n - 1) * rho) / n
# As n → ∞ with rho = 0: sigma²/n → 0
independent_limit = 0.0
# As n → ∞ with rho = 1: sigma²*(1+(n-1))/n = sigma² for all n, so limit = sigma²
perfect_positive_correlation_limit = float(sigma_squared)
return {
"expected_mean": expected_mean,
"variance": float(variance),
"independent_limit": independent_limit,
"perfect_positive_correlation_limit": perfect_positive_correlation_limit,
}