Compute Post-Match Standardized Mean Differences
Arrays & HashingAuto-graded · 23 test casesLast updated
Evaluate post-matching covariate balance by computing the absolute standardized mean difference across features.
In observational studies, evaluating covariate balance after matching is essential for assessing whether treatment and control groups have become comparable.
Given a feature matrix covariates representing observations (rows) and their continuous features (columns), a treatment indicator list, and a list of paired indices matched_pairs, compute the absolute Standardized Mean Difference (SMD) for every covariate across the matched pairs.
Each entry in matched_pairs is a 2-element list [t_idx, c_idx], where t_idx is the index of a treated subject and c_idx is the index of its matched control subject.
For each feature column $j$:
- Extract the feature values for all treated subjects in
matched_pairsand all control subjects inmatched_pairs. - Compute the sample means $\bar{x}_T$ and $\bar{x}_C$.
- Compute the sample variances $s_T^2$ and $s_C^2$ using sample variance ($N-1$ denominator): $$s^2 = \frac{1}{N - 1} \sum_{i=1}^N (x_i - \bar{x})^2$$ If $N \le 1$, the sample variance is $0.0$.
- Calculate the pooled standard deviation: $$\text{pooled_sd} = \sqrt{\frac{s_T^2 + s_C^2}{2}}$$
- Compute the absolute SMD: $$\text{SMD} = \frac{|\bar{x}_T - \bar{x}_C|}{\text{pooled_sd}}$$ If $\text{pooled_sd} == 0$, set $\text{SMD} = 0.0$.
Return a list of floats corresponding to the absolute SMD for each column feature in order.
Constraints
- $N$ (number of total observations) between $0$ and $1000$.
- $M$ (number of features per observation) between $0$ and $100$.
matched_pairscontains $K$ pairs where $0 \le K \le N$.- All values in
covariatesare numeric floats or integers. treatmentcontains0or1for each observation.
Examples
Example 1
Input: standardized_mean_differences([[1, 10], [2, 11], [1.5, 10.5], [8, 20]], [1, 1, 0, 0], [[0, 2], [1, 3]])
Output: [0.9883716976506172, 0.9945054529214061]
Example 2
Input: standardized_mean_differences([[20, 5], [22, 7], [19, 6], [21, 8]], [1, 0, 1, 0], [[0, 1], [2, 3]])
Output: [2.82842712474619, 2.82842712474619]
Hints
Hint 1
First, collect all feature values corresponding to treated units in the matched pairs, and separately collect all feature values for matched control units.
Hint 2
Remember to use sample variance with N-1 in the denominator for calculating s_T^2 and s_C^2.
Hint 3
Handle edge cases such as zero matched pairs or zero pooled standard deviation by setting the SMD for that column to 0.0.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To assess post-matching covariate balance, we compute the standardized mean difference (SMD) across each feature column:
- Extract Matched Observations: Iterate over
matched_pairsand gather the feature vectors for matched treated units and matched control units. - Column-wise Statistics: For each column feature:
- Calculate the mean of the treated matched group ($\bar{x}_T$) and control matched group ($\bar{x}_C$).
- Calculate the sample variance ($N-1$ denominator) for both groups: $$s_T^2 = \frac{1}{K-1} \sum (x_{T, i} - \bar{x}T)^2$$ $$s_C^2 = \frac{1}{K-1} \sum (x{C, i} - \bar{x}_C)^2$$
- Calculate pooled standard deviation $\text{pooled_sd} = \sqrt{(s_T^2 + s_C^2) / 2}$.
- Compute absolute SMD as $|\bar{x}_T - \bar{x}_C| / \text{pooled_sd}$. If $\text{pooled_sd} = 0$, set SMD to $0.0$.
Complexity
- Time Complexity: $\mathcal{O}(K \cdot M)$, where $K$ is the number of matched pairs and $M$ is the number of feature columns.
- Space Complexity: $\mathcal{O}(K \cdot M)$ to store matched feature rows and outputs.
Python Reference Solution
from math import sqrt
from typing import List
def standardized_mean_differences(covariates: List[List[float]], treatment: List[int], matched_pairs: List[List[int]]) -> List[float]:
if not matched_pairs or not covariates:
return [0.0] * (len(covariates[0]) if covariates else 0)
treated_rows = [covariates[t_idx] for t_idx, _ in matched_pairs]
control_rows = [covariates[c_idx] for _, c_idx in matched_pairs]
num_cols = len(covariates[0])
result = []
for col in range(num_cols):
t_vals = [row[col] for row in treated_rows]
c_vals = [row[col] for row in control_rows]
t_mean = sum(t_vals) / len(t_vals)
c_mean = sum(c_vals) / len(c_vals)
if len(t_vals) > 1:
t_var = sum((x - t_mean) ** 2 for x in t_vals) / (len(t_vals) - 1)
else:
t_var = 0.0
if len(c_vals) > 1:
c_var = sum((x - c_mean) ** 2 for x in c_vals) / (len(c_vals) - 1)
else:
c_var = 0.0
pooled_sd = sqrt((t_var + c_var) / 2.0)
if pooled_sd == 0.0:
smd = 0.0
else:
smd = abs(t_mean - c_mean) / pooled_sd
result.append(smd)
return result