diff_diff.PowerAnalysis#

class diff_diff.PowerAnalysis[source]#

Bases: object

Power analysis for difference-in-differences designs.

Provides analytical power calculations for basic 2x2 DiD and panel DiD designs. For complex designs like staggered adoption, use simulate_power() instead.

Parameters:
  • alpha (float, default=0.05) – Significance level for hypothesis testing.

  • power (float, default=0.80) – Target statistical power.

  • alternative (str, default='two-sided') – Alternative hypothesis: ‘two-sided’, ‘greater’, or ‘less’.

Examples

Calculate minimum detectable effect:

>>> from diff_diff import PowerAnalysis
>>> pa = PowerAnalysis(alpha=0.05, power=0.80)
>>> results = pa.mde(n_treated=50, n_control=50, sigma=1.0)
>>> print(f"MDE: {results.mde:.3f}")

Calculate required sample size:

>>> results = pa.sample_size(effect_size=0.5, sigma=1.0)
>>> print(f"Required N: {results.required_n}")

Calculate power for given sample and effect:

>>> results = pa.power(effect_size=0.5, n_treated=50, n_control=50, sigma=1.0)
>>> print(f"Power: {results.power:.1%}")

Notes

The power calculations are based on the variance of the DiD estimator:

For basic 2x2 DiD:
Var(ATT) = sigma^2 * (1/n_treated_post + 1/n_treated_pre
  • 1/n_control_post + 1/n_control_pre)

For panel DiD with T periods:
Var(ATT) = sigma^2 * (1/(N_treated * T) + 1/(N_control * T))
  • (1 + (T-1)*rho) / (1 + (T-1)*rho)

Where rho is the intra-cluster correlation coefficient.

References

Bloom, H. S. (1995). “Minimum Detectable Effects.” Burlig, F., Preonas, L., & Woerman, M. (2020). “Panel Data and Experimental Design.”

Methods

__init__([alpha, power, alternative])

mde(n_treated, n_control, sigma[, n_pre, ...])

Calculate minimum detectable effect given sample size.

power(effect_size, n_treated, n_control, sigma)

Calculate statistical power for given effect size and sample.

power_curve(n_treated, n_control, sigma[, ...])

Compute power for a range of effect sizes.

sample_size(effect_size, sigma[, n_pre, ...])

Calculate required sample size to detect given effect.

sample_size_curve(effect_size, sigma[, ...])

Compute power for a range of sample sizes.

__init__(alpha=0.05, power=0.8, alternative='two-sided')[source]#
Parameters:
classmethod __new__(*args, **kwargs)#