Skip to content

Commit caf7ae1

Browse files
author
mercury0100
committed
docs: clarify TWFE explanation in Banking dataset notebook
- Introduced γ as the global intercept to avoid overloading α - Wrapped dataset variable names (district, year, post_treatment) in \text{} for clearer rendering - Improved equation layout with consistent notation and salmon labels - Revised explanatory bullets for consistency with updated notation
1 parent ba72cda commit caf7ae1

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

docs/source/notebooks/did_pymc_banks.ipynb

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"cells": [
33
{
4-
"attachments": {},
54
"cell_type": "markdown",
65
"metadata": {},
76
"source": [
@@ -38,7 +37,6 @@
3837
]
3938
},
4039
{
41-
"attachments": {},
4240
"cell_type": "markdown",
4341
"metadata": {},
4442
"source": [
@@ -72,7 +70,6 @@
7270
]
7371
},
7472
{
75-
"attachments": {},
7673
"cell_type": "markdown",
7774
"metadata": {},
7875
"source": [
@@ -108,7 +105,6 @@
108105
]
109106
},
110107
{
111-
"attachments": {},
112108
"cell_type": "markdown",
113109
"metadata": {},
114110
"source": [
@@ -313,7 +309,6 @@
313309
]
314310
},
315311
{
316-
"attachments": {},
317312
"cell_type": "markdown",
318313
"metadata": {},
319314
"source": [
@@ -665,7 +660,6 @@
665660
]
666661
},
667662
{
668-
"attachments": {},
669663
"cell_type": "markdown",
670664
"metadata": {},
671665
"source": [
@@ -826,34 +820,34 @@
826820
]
827821
},
828822
{
829-
"cell_type": "markdown",
830-
"metadata": {},
831-
"source": [
832-
"## Analysis 4 - Two-way fixed effects\n",
833-
"\n",
834-
"Finally, we can evaluate the difference-in-differences model in its two-way fixed effects (TWFE) formulation. The TWFE model is widely used in econometrics for causal inference in panel data. It accounts for both unit-specific effects (e.g., differences between districts) and time-specific effects (e.g., shocks or trends affecting all units simultaneously).\n",
835-
"\n",
836-
"The TWFE model is equivalent to the classic 2\\u00d72 DiD model (Model 1) only in the special case of two groups and two time periods. Outside of this case the approaches diverge, and TWFE can have important limitations {cite:p}`hill2020limitations,imai2021twfepanel`. Readers should proceed with caution when applying TWFE in richer settings {cite:p}`kropko2018two,collischon2020let`.\n",
837-
"\n",
838-
"The TWFE approach is similar to the previous model in that the `district:post_treatment` interaction term still defines a treatment indicator variable, and its coefficient $\\Delta$ represents the causal effect of the intervention. The difference is that there is no _linear_ `year` term\\u2014instead we use a _categorical_ year variable. This allows the model to flexibly capture any time-specific shocks that affect all units.\n",
839-
"\n",
840-
"The expected values can be written as:\n",
841-
"\n",
842-
"$$\n",
843-
"\\\\begin{aligned}\n",
844-
"\\\\mu_i \\; &= \\; \\\\gamma \\quad \\\\textcolor{salmon}{\\\\text{(global intercept)}} \\\\\\\\\n",
845-
"&+ \\\\alpha[district_i] \\quad \\\\textcolor{salmon}{\\\\text{(unit fixed effect)}} \\\\\\\\\n",
846-
"&+ \\\\beta[year_i] \\quad \\\\textcolor{salmon}{\\\\text{(time fixed effect)}} \\\\\\\\\n",
847-
"&+ \\\\Delta \\\\cdot district_i \\\\cdot post\\\\_treatment_i \\quad \\\\textcolor{salmon}{\\\\text{(treatment indicator)}}\n",
848-
"\\\\end{aligned}\n",
849-
"$$\n",
850-
"\n",
851-
"Here:\n",
852-
"* $\\\\gamma$ is a scalar global intercept.\n",
853-
"* $\\\\alpha[district_i]$ is a vector of unit (district) fixed effects. With 2 districts, this is a vector of length 2. The index $district_i$ selects the effect for the $i^{th}$ observation.\n",
854-
"* $\\\\beta[year_i]$ is a vector of time (year) fixed effects. With 6 years, this is a vector of length 6. The index $year_i$ selects the effect for the $i^{th}$ observation.\n",
855-
"* $\\\\Delta$ is a scalar treatment effect, the coefficient on the `district:post_treatment` interaction term.\n"
856-
]
823+
"cell_type": "markdown",
824+
"metadata": {},
825+
"source": [
826+
"## Analysis 4 - Two-way fixed effects\n",
827+
"\n",
828+
"Finally, we can evaluate the difference-in-differences model in its two-way fixed effects (TWFE) formulation. The TWFE model is widely used in econometrics for causal inference in panel data. It accounts for both unit-specific effects (e.g., differences between districts) and time-specific effects (e.g., shocks or trends affecting all units simultaneously).\n",
829+
"\n",
830+
"The TWFE model is equivalent to the classic 2$\\times$2 DiD model (Model 1) only in the special case of two groups and two time periods. Outside of this case the approaches diverge, and TWFE can have important limitations {cite:p}`hill2020limitations,imai2021twfepanel`. Readers should proceed with caution when applying TWFE in richer settings {cite:p}`kropko2018two,collischon2020let`.\n",
831+
"\n",
832+
"The TWFE approach is similar to the previous model in that the `district:post_treatment` interaction term still defines a treatment indicator variable, and its coefficient $\\Delta$ represents the causal effect of the intervention. The difference is that there is no _linear_ `year` term, instead we use a _categorical_ year variable. This allows the model to flexibly capture any time-specific shocks that affect all units.\n",
833+
"\n",
834+
"The expected values can be written as:\n",
835+
"\n",
836+
"$$\n",
837+
"\\begin{aligned}\n",
838+
"\\mu_i &= \\gamma && \\color{#FA8072}{\\text{(global intercept)}} \\\\\n",
839+
"&+ \\alpha[\\text{district}_i] && \\color{#FA8072}{\\text{(unit fixed effect)}} \\\\\n",
840+
"&+ \\beta[\\text{year}_i] && \\color{#FA8072}{\\text{(time fixed effect)}} \\\\\n",
841+
"&+ \\Delta \\cdot \\text{district}_i \\cdot \\text{post\\_treatment}_i && \\color{#FA8072}{\\text{(treatment indicator)}}\n",
842+
"\\end{aligned}\n",
843+
"$$\n",
844+
"\n",
845+
"Here: \n",
846+
"* $\\gamma$ is a scalar global intercept. \n",
847+
"* $\\alpha[\\text{district}_i]$ is a vector of unit (district) fixed effects. With 2 districts, this is a vector of length 2. The index $\\text{district}_i$ selects the effect for the $i^{\\text{th}}$ observation. \n",
848+
"* $\\beta[\\text{year}_i]$ is a vector of time (year) fixed effects. With 6 years, this is a vector of length 6. The index $\\text{year}_i$ selects the effect for the $i^{\\text{th}}$ observation. \n",
849+
"* $\\Delta$ is a scalar treatment effect, corresponding to the coefficient on the `district:post_treatment` interaction term.\n"
850+
]
857851
},
858852
{
859853
"cell_type": "code",
@@ -1050,7 +1044,6 @@
10501044
]
10511045
},
10521046
{
1053-
"attachments": {},
10541047
"cell_type": "markdown",
10551048
"metadata": {},
10561049
"source": [
@@ -1063,7 +1056,7 @@
10631056
],
10641057
"metadata": {
10651058
"kernelspec": {
1066-
"display_name": "CausalPy",
1059+
"display_name": "Python 3 (ipykernel)",
10671060
"language": "python",
10681061
"name": "python3"
10691062
},
@@ -1077,7 +1070,7 @@
10771070
"name": "python",
10781071
"nbconvert_exporter": "python",
10791072
"pygments_lexer": "ipython3",
1080-
"version": "3.13.2"
1073+
"version": "3.9.18"
10811074
}
10821075
},
10831076
"nbformat": 4,

0 commit comments

Comments
 (0)