diff_diff.PreTrendsPower

class diff_diff.PreTrendsPower[source]

Bases: object

Pre-trends power analysis (Roth 2022).

Computes the power of pre-trends tests to detect violations of parallel trends, and the minimum detectable violation (MDV).

Parameters:
  • alpha (float, default=0.05) – Significance level for the pre-trends test.

  • power (float, default=0.80) – Target power level for MDV calculation.

  • violation_type (str, default='linear') – Type of violation pattern to consider: - ‘linear’: Violations follow a linear trend (most common) - ‘constant’: Same violation in all pre-periods - ‘last_period’: Violation only in the last pre-period - ‘custom’: User-specified violation pattern (via violation_weights)

  • violation_weights (array-like, optional) – Custom weights for violation pattern. Length must equal number of pre-periods. Only used when violation_type=’custom’.

Examples

Basic usage with MultiPeriodDiD results:

>>> from diff_diff import MultiPeriodDiD
>>> from diff_diff.pretrends import PreTrendsPower
>>>
>>> # Fit event study
>>> mp_did = MultiPeriodDiD()
>>> results = mp_did.fit(data, outcome='y', treatment='treated',
...                      time='period', post_periods=[4, 5, 6, 7])
>>>
>>> # Analyze pre-trends power
>>> pt = PreTrendsPower(alpha=0.05, power=0.80)
>>> power_results = pt.fit(results)
>>> print(power_results.summary())
>>>
>>> # Get power curve
>>> curve = pt.power_curve(results)
>>> curve.plot()

Notes

The pre-trends test is typically a joint test that all pre-period coefficients are zero. This test has limited power to detect small violations, especially when:

  1. There are few pre-periods

  2. Standard errors are large

  3. The violation pattern is smooth (e.g., linear trend)

Passing a pre-trends test does NOT mean parallel trends holds. It means violations smaller than the MDV cannot be ruled out. For robust inference, combine with HonestDiD sensitivity analysis.

References

Roth, J. (2022). Pretest with Caution: Event-Study Estimates after Testing

for Parallel Trends. American Economic Review: Insights, 4(3), 305-322.

__init__(alpha=0.05, power=0.8, violation_type='linear', violation_weights=None)[source]
Parameters:
  • alpha (float)

  • power (float)

  • violation_type (Literal['linear', 'constant', 'last_period', 'custom'])

  • violation_weights (ndarray | None)

Methods

__init__([alpha, power, violation_type, ...])

fit(results[, M, pre_periods])

Compute pre-trends power analysis.

get_params()

Get parameters for this estimator.

power_at(results, M[, pre_periods])

Compute power to detect a specific violation magnitude.

power_curve(results[, M_grid, n_points, ...])

Compute power across a range of violation magnitudes.

sensitivity_to_honest_did(results[, pre_periods])

Compare pre-trends power analysis with HonestDiD sensitivity.

set_params(**params)

Set parameters for this estimator.

__init__(alpha=0.05, power=0.8, violation_type='linear', violation_weights=None)[source]
Parameters:
  • alpha (float)

  • power (float)

  • violation_type (Literal['linear', 'constant', 'last_period', 'custom'])

  • violation_weights (ndarray | None)

get_params()[source]

Get parameters for this estimator.

Return type:

Dict[str, Any]

set_params(**params)[source]

Set parameters for this estimator.

Return type:

PreTrendsPower

fit(results, M=None, pre_periods=None)[source]

Compute pre-trends power analysis.

Parameters:
  • results (MultiPeriodDiDResults, CallawaySantAnnaResults, or SunAbrahamResults) – Results from an event study estimation.

  • M (float, optional) – Specific violation magnitude to evaluate. If None, evaluates at a default magnitude based on the data.

  • pre_periods (list of int, optional) – Explicit list of pre-treatment periods to use for power analysis. If None, attempts to infer from results.pre_periods. Use this when you’ve estimated an event study with all periods in post_periods and need to specify which are actually pre-treatment.

Returns:

Power analysis results including power and MDV.

Return type:

PreTrendsPowerResults

power_at(results, M, pre_periods=None)[source]

Compute power to detect a specific violation magnitude.

Parameters:
  • results (results object) – Event study results.

  • M (float) – Violation magnitude.

  • pre_periods (list of int, optional) – Explicit list of pre-treatment periods. See fit() for details.

Returns:

Power to detect violation of magnitude M.

Return type:

float

power_curve(results, M_grid=None, n_points=50, pre_periods=None)[source]

Compute power across a range of violation magnitudes.

Parameters:
  • results (results object) – Event study results.

  • M_grid (list of float, optional) – Specific violation magnitudes to evaluate. If None, creates automatic grid from 0 to 2.5 * MDV.

  • n_points (int, default=50) – Number of points in automatic grid.

  • pre_periods (list of int, optional) – Explicit list of pre-treatment periods. See fit() for details.

Returns:

Power curve data with plot method.

Return type:

PreTrendsPowerCurve

sensitivity_to_honest_did(results, pre_periods=None)[source]

Compare pre-trends power analysis with HonestDiD sensitivity.

This method helps interpret how informative a passing pre-trends test is in the context of HonestDiD’s relative magnitudes restriction.

Parameters:
  • results (results object) – Event study results.

  • pre_periods (list of int, optional) – Explicit list of pre-treatment periods. See fit() for details.

Returns:

Dictionary with: - mdv: Minimum detectable violation from pre-trends test - honest_M_at_mdv: Corresponding M value for HonestDiD - interpretation: Text explaining the relationship

Return type:

dict