KatInterview

Four Stats Coding Tasks: Sample Size, DiD, Bayes, Logit

Statistics & ExperimentsModel answer
MediumModel answerStatistics & Experimentssample-sizedifference-in-differencesbayes-rule

Last updated

Implement four small statistics routines: z-test sample size, difference-in-differences with a pre-trend check, a Bayes posterior, and top-3 features by logistic regression coefficient.

You are given four independent coding tasks in Python or R, each applying a standard statistical idea. Write a function for each and explain the conventions you chose.

Task 1: Sample size for a two-sided z-test

Inputs:

  • x: historical observations of a metric. Use them to estimate the standard deviation sigma as std(x).
  • alpha: significance level, for example 0.05.
  • power: target power, for example 0.8.
  • effect_size: the smallest absolute difference in means worth detecting, Δ.

Assume a two-sided two-sample z-test with equal group sizes. Return the smallest integer per-group sample size n that detects Δ at level alpha with the requested power.

Task 2: Difference-in-differences with a pre-trend check

Inputs are three equal-length arrays and a number:

  • period[i]: the time of observation i. There is at least one pre period and one post period, and there may be several pre periods.
  • group[i]: 0 for control, 1 for treatment.
  • outcome[i]: a numeric outcome.
  • threshold: the largest acceptable change in the treatment-minus-control gap between consecutive pre periods.

Let Ȳ(g, t) be the mean outcome of group g in period t. The estimate is

DiD = (Ȳ(1, post) − Ȳ(1, pre)) − (Ȳ(0, post) − Ȳ(0, pre)).

For the trend check, compute the gap d_t = Ȳ(1, t) − Ȳ(0, t) for each pre period, ordered by time. The check passes if max |d_t − d_(t−1)| ≤ threshold. With only one pre period the check passes automatically.

Return the DiD estimate and a boolean for the trend check.

Task 3: Bayes posterior

Given p_A = P(A), p_B_given_A = P(B | A) and p_B_given_not_A = P(B | ¬A), return P(A | B).

Task 4: Top-3 features by logistic regression coefficient

Inputs:

  • X: a matrix of shape num_features × num_samples, so each row is a feature and each column an observation.
  • y: binary labels of length num_samples.
  • feature_names: a list of length num_features.

Fit a logistic regression with an intercept. Rank the features by the absolute value of their fitted coefficients, excluding the intercept. Return the names of the top 3, most important first. Break ties by feature name in ascending order.

Inputs are well-formed and numeric. Describe how your code behaves in the edge cases you consider relevant.

Hints

Hint 1

Task 1: the difference in means has variance 2σ²/n. The effect must sit (z for 1−α/2) plus (z for power) standard errors from zero. Solve for n and round up.

Hint 2

Task 2: aggregate to (group, period) cell means first. Decide explicitly which period is post, and whether the pre baseline pools all pre observations or uses one period. State that choice.

Hint 3

Task 3: the denominator is the total probability of the evidence, P(B|A)P(A) + P(B|¬A)(1−P(A)). Leaving out the second term is the classic base-rate mistake.

Hint 4

Task 4: X is features × samples, so transpose it before fitting. Prepend a column of ones for the intercept, and don't let default L2 regularization silently change the ranking.