KatInterview

Compute Post-Match Standardized Mean Differences

Arrays & HashingAuto-graded · 23 test cases
EasyAuto-gradedArrays & Hashingstatisticsmath-bitsarrays

Last 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$:

  1. Extract the feature values for all treated subjects in matched_pairs and all control subjects in matched_pairs.
  2. Compute the sample means $\bar{x}_T$ and $\bar{x}_C$.
  3. 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$.
  4. Calculate the pooled standard deviation: $$\text{pooled_sd} = \sqrt{\frac{s_T^2 + s_C^2}{2}}$$
  5. 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_pairs contains $K$ pairs where $0 \le K \le N$.
  • All values in covariates are numeric floats or integers.
  • treatment contains 0 or 1 for 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.