|
12 | 12 | "id": "cell-1", |
13 | 13 | "metadata": {}, |
14 | 14 | "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)" |
16 | 16 | }, |
17 | 17 | { |
18 | 18 | "cell_type": "markdown", |
|
234 | 234 | "id": "cell-16", |
235 | 235 | "metadata": {}, |
236 | 236 | "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()" |
248 | 238 | }, |
249 | 239 | { |
250 | 240 | "cell_type": "markdown", |
|
260 | 250 | "id": "cell-18", |
261 | 251 | "metadata": {}, |
262 | 252 | "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()" |
279 | 254 | }, |
280 | 255 | { |
281 | 256 | "cell_type": "markdown", |
|
440 | 415 | "id": "cell-27", |
441 | 416 | "metadata": {}, |
442 | 417 | "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()" |
452 | 419 | }, |
453 | 420 | { |
454 | 421 | "cell_type": "markdown", |
|
473 | 440 | { |
474 | 441 | "cell_type": "code", |
475 | 442 | "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())", |
477 | 444 | "metadata": {}, |
478 | 445 | "execution_count": null, |
479 | 446 | "outputs": [] |
|
507 | 474 | { |
508 | 475 | "cell_type": "code", |
509 | 476 | "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()", |
511 | 478 | "metadata": {}, |
512 | 479 | "execution_count": null, |
513 | 480 | "outputs": [] |
|
0 commit comments