Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2f95980
Move statsmodels notebooks into directory
s3alfisc Jul 30, 2026
838934c
Add PyFixest notebook utilities
s3alfisc Jul 30, 2026
399a462
Add PyFixest Chapter 01 notebook
s3alfisc Jul 30, 2026
cd390a2
Add PyFixest Chapter 02 notebook
s3alfisc Jul 30, 2026
b316492
Add PyFixest Chapter 03 notebook
s3alfisc Jul 30, 2026
fccc721
Add PyFixest Chapter 04 notebook
s3alfisc Jul 30, 2026
93dbb75
Add PyFixest Chapter 05 notebook
s3alfisc Jul 30, 2026
82c4894
Add PyFixest Chapter 06 notebook
s3alfisc Jul 30, 2026
38d3940
Add PyFixest Chapter 07 notebook
s3alfisc Jul 30, 2026
5f0ebfb
Add PyFixest Chapter 08 notebook
s3alfisc Jul 30, 2026
abe6f0f
Add PyFixest Chapter 09 notebook
s3alfisc Jul 30, 2026
92f52f0
Add PyFixest Chapter 10 notebook
s3alfisc Jul 30, 2026
86200d2
Add PyFixest Chapter 11 notebook
s3alfisc Jul 30, 2026
5b2a1c1
Add PyFixest Chapter 12 notebook
s3alfisc Jul 30, 2026
926e931
Add PyFixest Chapter 13 notebook
s3alfisc Jul 30, 2026
56afc47
Add PyFixest Chapter 15 notebook
s3alfisc Jul 30, 2026
a5df741
Add PyFixest Chapter 16 notebook
s3alfisc Jul 30, 2026
c418d3a
Add PyFixest Chapter 17 notebook
s3alfisc Jul 30, 2026
2d4c392
Add PyFixest Chapter 18 notebook
s3alfisc Jul 30, 2026
ca91daa
Add PyFixest Chapter 19 notebook
s3alfisc Jul 30, 2026
459f65b
Add PyFixest Chapter 20 notebook
s3alfisc Jul 30, 2026
bf7c3ce
Add PyFixest Chapter 21 notebook
s3alfisc Jul 30, 2026
35ed772
Add PyFixest Chapter 22 notebook
s3alfisc Jul 30, 2026
1ad3340
Add PyFixest Chapter 23 notebook
s3alfisc Jul 30, 2026
df71f9d
Add PyFixest Chapter 24 notebook
s3alfisc Jul 30, 2026
303ba40
Add PyFixest Chapter 25 notebook
s3alfisc Jul 30, 2026
fef17fd
Add PyFixest Chapter 26 notebook
s3alfisc Jul 30, 2026
667cff9
Add PyFixest Chapter 27 notebook
s3alfisc Jul 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
491 changes: 491 additions & 0 deletions pyfixest/Chapter01CorrAssocSimpsons.ipynb

Large diffs are not rendered by default.

File renamed without changes.
418 changes: 418 additions & 0 deletions pyfixest/Chapter04CREandNeyman.ipynb

Large diffs are not rendered by default.

847 changes: 847 additions & 0 deletions pyfixest/Chapter05StratandPostStrat.ipynb

Large diffs are not rendered by default.

393 changes: 393 additions & 0 deletions pyfixest/Chapter06RegadjRerand.ipynb

Large diffs are not rendered by default.

572 changes: 572 additions & 0 deletions pyfixest/Chapter07MatchedPairs.ipynb

Large diffs are not rendered by default.

579 changes: 579 additions & 0 deletions pyfixest/Chapter08UnifyingFisherNeyman.ipynb

Large diffs are not rendered by default.

217 changes: 217 additions & 0 deletions pyfixest/Chapter09BridgingFinitePopAndSuperPop.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 9: Bridging Finite and Super-population Causal Inference"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from joblib import Parallel, delayed\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import pyfixest as pf\n",
"\n",
"np.random.seed(42)\n",
"%load_ext autoreload\n",
"%autoreload 1\n",
"\n",
"%load_ext watermark\n",
"%watermark --iversions\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def linestimator(Z, Y, X):\n",
" X = (X - X.mean(axis=0)) / X.std(axis=0)\n",
" n, p = X.shape\n",
" # fully interacted OLS\n",
" covariates = pd.DataFrame(X, columns=[f\"x{i}\" for i in range(p)])\n",
" data = covariates.assign(y=Y, z=Z)\n",
" m = pf.feols(\"y ~ z * (\" + \" + \".join(covariates.columns) + \")\", data=data, vcov=\"HC2\")\n",
" est, vehw = m.coef().loc[\"z\"], m.se().loc[\"z\"] ** 2\n",
" # super-population correction\n",
" inter = m.coef().loc[[f\"z:x{i}\" for i in range(p)]].to_numpy()\n",
" # (β_1 - β_0)' Σ (β_1 - β_0) / n\n",
" superCorr = (inter @ np.cov(X.T) @ inter) / n\n",
" vsuper = vehw + superCorr\n",
" return est, np.sqrt(vehw), np.sqrt(vsuper)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.052230404017171474, 0.1475302340448403, 0.1633386978782156)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def onerepl(*args):\n",
" n = 500\n",
" X = np.random.normal(0, 1, n * 2).reshape(n, 2)\n",
" Y0 = X[:, 0] + X[:, 0] ** 2 + np.random.uniform(-0.5, 0.5, n)\n",
" Y1 = X[:, 1] + X[:, 1] ** 2 + np.random.uniform(-1, 1, n)\n",
" Z = np.random.binomial(1, 0.6, n)\n",
" Y = Y0 * (1 - Z) + Y1 * Z\n",
" return linestimator(Z, Y, X)\n",
"onerepl()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"nrep, k = 2_000, 8\n",
"results = Parallel(n_jobs=k)(delayed(onerepl)(i) for i in range(nrep))\n",
"simres = np.vstack(results)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.0033900784280582142, 0.13559452100782549, 0.15029308662381266)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# bias, estimated EHW SE, estimated super-population SE\n",
"simres[:, 0].mean(), simres[:, 1].mean(), simres[:, 2].mean()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.14731850757623555"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# empirical SD\n",
"simres[:, 0].std()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.926"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# EHW coverage\n",
"np.mean(\n",
" (simres[:, 0] - 1.96 * simres[:, 1]) * (simres[:, 0] + 1.96 * simres[:, 1]) <= 0\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"EHW has below nominal coverage for superpopulation."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9515"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# superpop coverage\n",
"np.mean(\n",
" (simres[:, 0] - 1.96 * simres[:, 2]) * (simres[:, 0] + 1.96 * simres[:, 2]) <= 0\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Superpopn is above nom coverage for superpopulation."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "metrics",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading