Fit a Difference-in-Differences (DiD) regression estimating the effect of some event on treatment_date for treated_tickers, relative to control_tickers.

Also known as: DiD, DD, difference-in-differences estimator.

For more information about the method, and why the Treated x Post interaction coefficient IS the DiD treatment-effect estimate, see causal_inference_model.get_difference_in_differences.

Builds a stacked (ticker, date) panel from every ticker in treated_tickers and control_tickers: each observation’s outcome is that ticker’s return on that date, Treated is 1 for every observation belonging to a treated_tickers asset (regardless of date) and 0 for control_tickers assets, and Post is 1 for observations on or after treatment_date (regardless of asset). This answers “did treated_tickers behave differently after treatment_date, beyond both their normal average gap versus control_tickers and the common market-wide move over that same before/after window?” – e.g. isolating the effect of an event (an index-inclusion announcement, a regulatory change affecting only some tickers, an earnings surprise) that hits treated_tickers but not control_tickers, at a known date.

No programming experience? With the Finance Toolkit MCP server, AI assistants such as Claude and ChatGPT can calculate the Difference-in-Differences (DiD) for you. Just ask in plain English.

Calculate the Difference-in-Differences (DiD) in Python

The Difference-in-Differences (DiD) is available in the Econometrics module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_difference_in_differences as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(["AAPL", "MSFT"], api_key="FINANCIAL_MODELING_PREP_KEY")

toolkit.econometrics.get_difference_in_differences(
    treated_tickers="AAPL", treatment_date="2021-06-30", period="weekly"
)

Which returns:

  Coefficient Std. Error t-Statistic P-Value
Intercept 0.0064 0.0031 2.0495 0.0410
Treated 0.0029 0.0054 0.5399 0.5896
Post -0.0074 0.0045 -1.6572 0.0982
Treated x Post -0.0020 0.0077 -0.2571 0.7972

Parameters

get_difference_in_differences accepts the following parameters:

  • treated_tickers (str | list[str]): The asset(s) subject to the event/treatment.
  • treatment_date (str): The date the event/treatment occurs, in the same format accepted by pd.Timestamp. Observations on or after this date are Post = 1.
  • control_tickers (str | list[str] | None, optional): The untreated comparison asset(s). Defaults to None, which uses every ticker (and “Benchmark”, if present) NOT in treated_tickers.
  • period (str, optional): The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”.
  • column (str, optional): The historical data column to use as the outcome. Defaults to “Return”.
  • add_constant (bool, optional): Whether to include an intercept. Defaults to True.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to None.

The Econometrics module page introduces the module, and the sidebar lists all of its functions.

Share