Skip to content

Commit 759616b

Browse files
authored
Merge pull request #228 from igerber/fix-power-analysis-notebook
Fix power analysis notebook plot rendering and suppress SDID warning
2 parents c88c890 + c913e17 commit 759616b

1 file changed

Lines changed: 6 additions & 39 deletions

File tree

docs/tutorials/06_power_analysis.ipynb

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"id": "cell-1",
1313
"metadata": {},
1414
"outputs": [],
15-
"source": "import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom diff_diff import (\n PowerAnalysis,\n DifferenceInDifferences,\n CallawaySantAnna,\n SyntheticDiD,\n TripleDifference,\n simulate_power,\n simulate_mde,\n simulate_sample_size,\n compute_mde,\n compute_power,\n compute_sample_size,\n plot_power_curve,\n)"
15+
"source": "%matplotlib inline\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom diff_diff import (\n PowerAnalysis,\n DifferenceInDifferences,\n CallawaySantAnna,\n SyntheticDiD,\n TripleDifference,\n simulate_power,\n simulate_mde,\n simulate_sample_size,\n compute_mde,\n compute_power,\n compute_sample_size,\n plot_power_curve,\n)"
1616
},
1717
{
1818
"cell_type": "markdown",
@@ -234,17 +234,7 @@
234234
"id": "cell-16",
235235
"metadata": {},
236236
"outputs": [],
237-
"source": [
238-
"# Plot the power curve\n",
239-
"plot_power_curve(\n",
240-
" curve_df,\n",
241-
" mde=mde_result.mde,\n",
242-
" target_power=0.80,\n",
243-
" title=\"Power Curve: 50 Treated, 50 Control, SD=10\",\n",
244-
" xlabel=\"Treatment Effect Size\",\n",
245-
" figsize=(10, 6)\n",
246-
")"
247-
]
237+
"source": "# Plot the power curve\nplot_power_curve(\n curve_df,\n mde=mde_result.mde,\n target_power=0.80,\n title=\"Power Curve: 50 Treated, 50 Control, SD=10\",\n xlabel=\"Treatment Effect Size\",\n figsize=(10, 6)\n)\nplt.show()"
248238
},
249239
{
250240
"cell_type": "markdown",
@@ -260,22 +250,7 @@
260250
"id": "cell-18",
261251
"metadata": {},
262252
"outputs": [],
263-
"source": [
264-
"# How does power change with sample size for a fixed effect?\n",
265-
"sample_curve = pa.sample_size_curve(\n",
266-
" effect_size=5.0,\n",
267-
" sigma=10.0\n",
268-
")\n",
269-
"\n",
270-
"# Plot\n",
271-
"plot_power_curve(\n",
272-
" sample_curve,\n",
273-
" target_power=0.80,\n",
274-
" show_mde_line=False,\n",
275-
" title=\"Power vs Sample Size (Effect=5, SD=10)\",\n",
276-
" figsize=(10, 6)\n",
277-
")"
278-
]
253+
"source": "# How does power change with sample size for a fixed effect?\nsample_curve = pa.sample_size_curve(\n effect_size=5.0,\n sigma=10.0\n)\n\n# Plot\nplot_power_curve(\n sample_curve,\n target_power=0.80,\n show_mde_line=False,\n title=\"Power vs Sample Size (Effect=5, SD=10)\",\n figsize=(10, 6)\n)\nplt.show()"
279254
},
280255
{
281256
"cell_type": "markdown",
@@ -440,15 +415,7 @@
440415
"id": "cell-27",
441416
"metadata": {},
442417
"outputs": [],
443-
"source": [
444-
"# Plot simulation-based power curve\n",
445-
"plot_power_curve(\n",
446-
" power_curve_sim,\n",
447-
" target_power=0.80,\n",
448-
" title=\"Simulation-Based Power Curve (100 units, 4 periods, SD=5)\",\n",
449-
" figsize=(10, 6)\n",
450-
")"
451-
]
418+
"source": "# Plot simulation-based power curve\nplot_power_curve(\n power_curve_sim,\n target_power=0.80,\n title=\"Simulation-Based Power Curve (100 units, 4 periods, SD=5)\",\n figsize=(10, 6)\n)\nplt.show()"
452419
},
453420
{
454421
"cell_type": "markdown",
@@ -473,7 +440,7 @@
473440
{
474441
"cell_type": "code",
475442
"id": "x068rpe24gf",
476-
"source": "# Synthetic DiD — note treatment_fraction=0.3 (placebo variance requires\n# more control units than treated units)\nsdid = SyntheticDiD()\n\nsdid_results = simulate_power(\n estimator=sdid,\n n_units=60,\n n_periods=6,\n treatment_effect=5.0,\n treatment_fraction=0.3,\n treatment_period=3,\n sigma=3.0,\n n_simulations=100,\n seed=42,\n progress=False,\n)\n\nprint(sdid_results.summary())",
443+
"source": "import warnings\n\n# Synthetic DiD — note treatment_fraction=0.3 (placebo variance requires\n# more control units than treated units)\nsdid = SyntheticDiD()\n\n# Suppress pre-treatment fit warnings from individual simulation draws;\n# the factor-model DGP can trigger this on some random seeds without\n# affecting the overall power estimate.\nwith warnings.catch_warnings():\n warnings.filterwarnings(\"ignore\", message=\"Pre-treatment fit is poor\")\n sdid_results = simulate_power(\n estimator=sdid,\n n_units=60,\n n_periods=6,\n treatment_effect=5.0,\n treatment_fraction=0.3,\n treatment_period=3,\n sigma=3.0,\n n_simulations=100,\n seed=42,\n progress=False,\n )\n\nprint(sdid_results.summary())",
477444
"metadata": {},
478445
"execution_count": null,
479446
"outputs": []
@@ -507,7 +474,7 @@
507474
{
508475
"cell_type": "code",
509476
"id": "ox3uab7h5bj",
510-
"source": "# Power curve across effect sizes for Callaway-Sant'Anna\ncs_curve = simulate_power(\n estimator=CallawaySantAnna(),\n n_units=100,\n n_periods=6,\n effect_sizes=[1.0, 2.0, 3.0, 5.0, 7.0],\n treatment_period=3,\n sigma=5.0,\n n_simulations=100,\n seed=42,\n progress=False,\n)\n\nplot_power_curve(\n cs_curve.power_curve_df(),\n target_power=0.80,\n title=\"CS Power Curve (100 units, 6 periods, SD=5)\",\n figsize=(10, 6),\n)",
477+
"source": "# Power curve across effect sizes for Callaway-Sant'Anna\ncs_curve = simulate_power(\n estimator=CallawaySantAnna(),\n n_units=100,\n n_periods=6,\n effect_sizes=[1.0, 2.0, 3.0, 5.0, 7.0],\n treatment_period=3,\n sigma=5.0,\n n_simulations=100,\n seed=42,\n progress=False,\n)\n\nplot_power_curve(\n cs_curve.power_curve_df(),\n target_power=0.80,\n title=\"CS Power Curve (100 units, 6 periods, SD=5)\",\n figsize=(10, 6),\n)\nplt.show()",
511478
"metadata": {},
512479
"execution_count": null,
513480
"outputs": []

0 commit comments

Comments
 (0)