Causal Effects in Observational Studies
Matching
- Why randomization might be unavailable
- Why confounders create the confounding problem
- Why grouping by all the confounders solves the confounding problem
- Why we can’t prove we observed all the confounders
- Predicted potential outcomes
- Exact Matching
- Distance between observations
- Nearest-neighbor Matching

Intro: So, this time you can’t randomize, right?
We saw last time that Randomized Experiments are the absolute simplest and most effective way to prove causal claims. Treatment and control groups are guaranteed to be similar on average before the treatment is given, so differences in outcomes between the groups must be the result of the treatment. Done. Publish your paper and get your Nobel prize.
But they have two little problems:
- Unfeasibility. We might be practically unable to randomize treatment or randomization might be way too expensive. E.g. Imagine wanting to see if higher or lower taxation would be better by looking at the impact on employment. You’d have to… randomize citizens into two levels of taxation? Nobody would accept that. Even if they did, it would not reflect reality because now we have a weird society made of a mix of different types of citizens interacting with each other. Impossible. Absurd.
- Unethicality. It might be unethical to randomize treatment. E.g. imagine you are giving people in treatment a certain medicine, and people in control get no medicine at all. That would be terrible: we are knowingly harming people just to measure effectiveness of a certain treatment.
Today we’ll explore how to squeeze the last drop of causal information from messy data where treatment assignments were not determined by researchers. Such non-experimental studies are called observational studies, emphasizing that all researchers do is observe treatment assignments rather than making them.
def: Observational Study
A study based on data where observations were not randomly assigned to treatment and control by the researchers.
Facing our enemy: the Confounder
We can’t solve the Confounding Problem with randomization, so let’s go back to it and see what else we can do.
Remember our example of students enrolling with different professors? I will need to slightly change the setup of the problem because otherwise the math doesn’t add up, but the idea is exactly the same.
Consider again a population of students who could enroll in professor Biegschott’s (treatment) or Nobbo-Dee’s (control) section when taking a college class. The outcome variable \(Y\) is the student’s percentage score at the end of the semester. Assume we know the true potential outcomes for their class scores in the table below.
| Name | D | Potential score with prof. B (\(Y_t\)) | Potential score with prof. N (\(Y_c\)) |
|---|---|---|---|
| Nyx | 1 | 70% | 85% |
| Madhu | 0 | 60% | 60% |
| Jorge | 1 | 85% | 90% |
| Tuta | 0 | 70% | 85% |
| Muhammad | 1 | 90% | 95% |
| Xiaoyu | 0 | 50% | 60% |
| Ratree | 1 | 65% | 80% |
| Dong | 0 | 85% | 95% |
| Dmitry | 1 | 60% | 60% |
| Celeste | 1 | 90% | 90% |
| Layla | 0 | 65% | 80% |
| Fan | 1 | 85% | 95% |
| Lethabo | 0 | 60% | 65% |
| Yozora | 0 | 65% | 85% |
| Mindy | 1 | 85% | 95% |
| June | 1 | 50% | 65% |
| Ziye | 0 | 70% | 80% |
| Durga | 1 | 90% | 90% |
| Forum | 0 | 50% | 65% |
| Jean-Paul | 0 | 90% | 90% |
\[ \begin{aligned} \mbox{ATE} &= \frac{1}{20}\Big( (85\%-90\%) + (85\%-95\%) +(90\%-90\%) + (90\%-95\%)+\\ &\quad +(85\%-95\%) + (90\%-90\%) +(85\%-95\%) + (90\%-90\%)+\\ &\quad +(50\%-65\%) + (60\%-60\%) +(60\%-60\%) + (50\%-65\%)+\\ &\quad +(60\%-65\%) + (50\%-60\%)+(65\%-85\%) + (70\%-85\%)+\\ &\quad +(70\%-80\%) + (65\%-80\%) +(65\%-80\%) + (70\%-85\%) \Big)\\ &= \frac{1}{20}\Big( -5\%-10\%+0\%-5\%-10\%+0\%-10\%+0\%\\ &\quad -15\%+0\%+0\%-15\%-5\%-10\%\\ &\quad -20\%-15\%-10\%-15\%-15\%-15\% \Big)\\ &= \frac{-175\%}{20}\\ &= \boxed{-8.75\%}. \end{aligned} \]
But we instead could only have one of the two potential outcomes available (the actual outcome, \(Y\)), depending on whether the student enrolls in treatment (Biegschott, \(D=1\)) or control (Nobbo-Dee, \(D=0\)):
| Name | D | Actual score (\(Y\)) |
|---|---|---|
| Nyx | 1 | 70% |
| Madhu | 0 | 60% |
| Jorge | 1 | 85% |
| Tuta | 0 | 85% |
| Muhammad | 1 | 90% |
| Xiaoyu | 0 | 60% |
| Ratree | 1 | 65% |
| Dong | 0 | 95% |
| Dmitry | 1 | 60% |
| Celeste | 1 | 90% |
| Layla | 0 | 80% |
| Fan | 1 | 85% |
| Lethabo | 0 | 65% |
| Yozora | 0 | 85% |
| Mindy | 1 | 85% |
| June | 1 | 50% |
| Ziye | 0 | 80% |
| Durga | 1 | 90% |
| Forum | 0 | 65% |
| Jean-Paul | 0 | 90% |
and we tried calculating the average outcome for the treated minus the average outcome for the controls: \[ \begin{aligned} E(Y\mid D=1)-E(Y\mid D=0)&= \frac{1}{10}(85\%+85\%+90\%+90\%+85\%\\ &\qquad +90\%+50\%+60\%+65\%+70\%)\\ &\quad -\frac{1}{10}(95\%+90\%+60\%+65\%+65\%\\ &\qquad +60\%+85\%+85\%+80\%+80\%)\\ &= 77\%-76.5\%\\ &= \boxed{+0.5\%}. \end{aligned} \]
And we sadly noticed: \[\mbox{ATE} = -8.75\% \neq +0.5\% = E(Y|D=1) - E(Y|D=0)\]
Remember why this was? Because students with higher \(Y_t\) ended up in treatment and lower \(Y_c\) ended up in control! They selected themselves!
Who is creating the Confounding Problem?
So far so bad. If this was all the information we had, we would be stuck.
But let’s be more curious. Why did the students select themselves that way? Remember some of the scenarios we came up with?
One was: “maybe early risers, who tend to be more diligent, aggressively optimizing, and high-scoring students, enrolled in treatment, with prof. Biegschott, who’s a very famous researcher, the very moment enrollment opened up”.
I’ll rephrase. Covariate \(X\) (waking pattern) influences enrollment (\(D\)) but it also impacts the potential student scores (\(Y_c\), \(Y_t\)). This is an example of a Confounder.
def: Confounder
A covariate (\(X\)) that is associated both with the potential outcomes of the subjects (\(Y_c\),\(Y_t\)) and their treatment assignment (\(D\)).
Confounders are the reason that the Confounding Problem exists, because they create a difference between the typical outcome of people in treatment and in control!
E.g.
Wealth (\(X\)) is a confounder for the impact of drinking wine (\(D\)) on heart health (\(Y\)). Richer people are both more likely to drink wine (\(D=1\)) and to have better heart health (higher values of \(Y_c\) and/or \(Y_t\))

But confounders are also the solution to the problems they created! If … we can observe them.
It shouldn’t be too difficult to find out if students are early risers …
Should I determine if they’re early risers before or after they enroll? Does it matter?
Before! Yes, it matters! Imagine I typically wake up late but I enrolled with a professor in an early class. Then, I will by definition shift my wake time to earlier!
Treatment impacts that covariate so we need to make sure we are talking about its value before the treatment!
Observing Confounders solves Confounding! (How a group_by saved my life)
Assume we have a variable, \(X=\) “waking pattern”, which we could collect using a questionnaire, and which takes three values: “early bird”, “normie”, and “night owl”. We would have the following population then, which I have sorted by values of \(X\):
| Name | Waking pattern (X) | D | Potential score with prof. B (\(Y_t\)) | Potential score with prof. N (\(Y_c\)) |
|---|---|---|---|---|
| Jorge | ‘early bird’ | 1 | 85% | 90% |
| Mindy | ‘early bird’ | 1 | 85% | 95% |
| Durga | ‘early bird’ | 1 | 90% | 90% |
| Muhammad | ‘early bird’ | 1 | 90% | 95% |
| Fan | ‘early bird’ | 1 | 85% | 95% |
| Celeste | ‘early bird’ | 1 | 90% | 90% |
| Dong | ‘early bird’ | 0 | 85% | 95% |
| Jean-Paul | ‘early bird’ | 0 | 90% | 90% |
| June | ‘normie’ | 1 | 50% | 65% |
| Dmitry | ‘normie’ | 1 | 60% | 60% |
| Madhu | ‘normie’ | 0 | 60% | 60% |
| Forum | ‘normie’ | 0 | 50% | 65% |
| Lethabo | ‘normie’ | 0 | 60% | 65% |
| Xiaoyu | ‘normie’ | 0 | 50% | 60% |
| Ratree | ‘night owl’ | 1 | 65% | 80% |
| Nyx | ‘night owl’ | 1 | 70% | 85% |
| Yozora | ‘night owl’ | 0 | 65% | 85% |
| Tuta | ‘night owl’ | 0 | 70% | 85% |
| Ziye | ‘night owl’ | 0 | 70% | 80% |
| Layla | ‘night owl’ | 0 | 65% | 80% |
Bingo! I see what’s happening! Most early birds have both high potential scores and ended up enrolling in treatment!
Notice another huge thing. Inside each group, e.g. check early birds, now the people who enrolled with prof. B have the same typical scores as the people who enrolled with prof. N. That’s amazing! Within an \(X\)-group, the treatment \(D\) is completely unrelated to the \(Y_t\), \(Y_c\)!
I’ll repeat it. That means that, among people with equal values of \(X\), the controls (\(D=0\)) have the same potential outcomes as the treated (\(D=1\))!

Inside each \(X\)-group, you found a tiny Randomized Experiment! Confounding is solved.
How do we solve it in practice? Take the \(X=\) ‘early bird’ group.

| Name | Waking pattern (X) | D | Potential score with prof. B (\(Y_t\)) | Potential score with prof. N (\(Y_c\)) |
|---|---|---|---|---|
| Jorge | ‘early bird’ | 1 | 85% | ? |
| Mindy | ‘early bird’ | 1 | 85% | ? |
| Durga | ‘early bird’ | 1 | 90% | ? |
| Muhammad | ‘early bird’ | 1 | 90% | ? |
| Fan | ‘early bird’ | 1 | 85% | ? |
| Celeste | ‘early bird’ | 1 | 90% | ? |
| Dong | ‘early bird’ | 0 | ? | 95% |
| Jean-Paul | ‘early bird’ | 0 | ? | 90% |
Because each \(X\)-group is a randomized experiment, then I can just use the average of the treated (\(E(Y|D=1,X=\mbox{'early bird'})\)) as a substitute for the counterfactual for the controls (I’ll call that \(Y^\ast_t\)), and vice versa the average of the controls (\(E(Y|D=0,X=\mbox{'early bird'})\)) as a substitute counterfactual for the treated (\(Y^\ast_c\)), like here:
| Name | Waking pattern (X) | D | Substitute Potential score with prof. B (\(Y^\ast_t\)) | Substitute Potential score with prof. N (\(Y^\ast_c\)) |
|---|---|---|---|---|
| Jorge | ‘early bird’ | 1 | 85% | \(\begin{array}{c}E(Y|D=0,X=\text{'early bird'})\\=\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Mindy | ‘early bird’ | 1 | 85% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Durga | ‘early bird’ | 1 | 90% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Muhammad | ‘early bird’ | 1 | 90% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Fan | ‘early bird’ | 1 | 85% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Celeste | ‘early bird’ | 1 | 90% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Dong | ‘early bird’ | 0 | \(\begin{array}{c}E(Y|D=1,X=\text{'early bird'})\\=\frac{1}{6}(Y_{\text{Jorge}}+Y_{\text{Mindy}}+Y_{\text{Durga}}\\+Y_{\text{Muhammad}}+Y_{\text{Fan}}+Y_{\text{Celeste}})\\=87.5\%\end{array}\) | 95% |
| Jean-Paul | ‘early bird’ | 0 | \(\begin{array}{c}\frac{1}{6}(Y_{\text{Jorge}}+Y_{\text{Mindy}}+Y_{\text{Durga}}\\+Y_{\text{Muhammad}}+Y_{\text{Fan}}+Y_{\text{Celeste}})\\=87.5\%\end{array}\) | 90% |
Does this work?
Check the boring calculations
Let’s finish filling in the substitute potential outcomes
| Name | Waking pattern (X) | D | Substitute Potential score with prof. B (\(Y^\ast_t\)) | Substitute Potential score with prof. N (\(Y^\ast_c\)) |
|---|---|---|---|---|
| Jorge | ‘early bird’ | 1 | 85% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Mindy | ‘early bird’ | 1 | 85% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Durga | ‘early bird’ | 1 | 90% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Muhammad | ‘early bird’ | 1 | 90% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Fan | ‘early bird’ | 1 | 85% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Celeste | ‘early bird’ | 1 | 90% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Dong}}+Y_{\text{Jean-Paul}})\\=92.5\%\end{array}\) |
| Dong | ‘early bird’ | 0 | \(\begin{array}{c}\frac{1}{6}(Y_{\text{Jorge}}+Y_{\text{Mindy}}+Y_{\text{Durga}}\\+Y_{\text{Muhammad}}+Y_{\text{Fan}}+Y_{\text{Celeste}})\\=87.5\%\end{array}\) | 95% |
| Jean-Paul | ‘early bird’ | 0 | \(\begin{array}{c}\frac{1}{6}(Y_{\text{Jorge}}+Y_{\text{Mindy}}+Y_{\text{Durga}}\\+Y_{\text{Muhammad}}+Y_{\text{Fan}}+Y_{\text{Celeste}})\\=87.5\%\end{array}\) | 90% |
| June | ‘normie’ | 1 | 50% | \(\begin{array}{c}\frac{1}{4}(Y_{\text{Madhu}}+Y_{\text{Forum}}+Y_{\text{Lethabo}}+Y_{\text{Xiaoyu}})\\=62.5\%\end{array}\) |
| Dmitry | ‘normie’ | 1 | 60% | \(\begin{array}{c}\frac{1}{4}(Y_{\text{Madhu}}+Y_{\text{Forum}}+Y_{\text{Lethabo}}+Y_{\text{Xiaoyu}})\\=62.5\%\end{array}\) |
| Madhu | ‘normie’ | 0 | \(\begin{array}{c}\frac{1}{2}(Y_{\text{June}}+Y_{\text{Dmitry}})\\=55\%\end{array}\) | 60% |
| Forum | ‘normie’ | 0 | \(\begin{array}{c}\frac{1}{2}(Y_{\text{June}}+Y_{\text{Dmitry}})\\=55\%\end{array}\) | 65% |
| Lethabo | ‘normie’ | 0 | \(\begin{array}{c}\frac{1}{2}(Y_{\text{June}}+Y_{\text{Dmitry}})\\=55\%\end{array}\) | 65% |
| Xiaoyu | ‘normie’ | 0 | \(\begin{array}{c}\frac{1}{2}(Y_{\text{June}}+Y_{\text{Dmitry}})\\=55\%\end{array}\) | 60% |
| Ratree | ‘night owl’ | 1 | 65% | \(\begin{array}{c}\frac{1}{4}(Y_{\text{Yozora}}+Y_{\text{Tuta}}+Y_{\text{Ziye}}+Y_{\text{Layla}})\\=82.5\%\end{array}\) |
| Nyx | ‘night owl’ | 1 | 70% | \(\begin{array}{c}\frac{1}{4}(Y_{\text{Yozora}}+Y_{\text{Tuta}}+Y_{\text{Ziye}}+Y_{\text{Layla}})\\=82.5\%\end{array}\) |
| Yozora | ‘night owl’ | 0 | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Ratree}}+Y_{\text{Nyx}})\\=67.5\%\end{array}\) | 85% |
| Tuta | ‘night owl’ | 0 | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Ratree}}+Y_{\text{Nyx}})\\=67.5\%\end{array}\) | 85% |
| Ziye | ‘night owl’ | 0 | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Ratree}}+Y_{\text{Nyx}})\\=67.5\%\end{array}\) | 80% |
| Layla | ‘night owl’ | 0 | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Ratree}}+Y_{\text{Nyx}})\\=67.5\%\end{array}\) | 80% |
now compute the population expectation
\[ \begin{aligned} E(Y^\ast_t-Y^\ast_c) &= \frac{1}{20}\Big[ (85\%-92.5\%) + (85\%-92.5\%) + (90\%-92.5\%) + (90\%-92.5\%) +\\ &\qquad (85\%-92.5\%) + (90\%-92.5\%)+ (87.5\%-95\%) + (87.5\%-90\%) +\\ &\qquad + (50\%-62.5\%) + (60\%-62.5\%) + (55\%-60\%) + (55\%-65\%) +\\ &\qquad + (55\%-65\%) + (55\%-60\%)+ (65\%-82.5\%) + (70\%-82.5\%) +\\ &\qquad + (67.5\%-85\%) + (67.5\%-85\%)+ (67.5\%-80\%) + (67.5\%-80\%) \Big]\\[6pt] &= \frac{1}{20}\Big[ -7.5\%-7.5\%-2.5\%-2.5\% -7.5\%-2.5\%-7.5\%-2.5\%\\ &\qquad -12.5\%-2.5\%-5\%-10\% -10\%-5\%-17.5\%-12.5\%\\ &\qquad -17.5\%-17.5\%-12.5\%-12.5\% \Big]\\[6pt] &= \frac{-175\%}{20}\\ &= \boxed{-8.75\% = \mbox{ATE}}. \end{aligned} \]
Yes! \(E(Y^\ast_t-Y^\ast_c) =-8.75\% = \mbox{ATE}\). Let’s put it all together in formulas:
fact: Observing Confounders solves Confounding!
If all confounders X are observed, then we can calculate the \(\mbox{ATE}\) this way:
- Group the observations by values of \(X\)
- Find the substitute potential outcomes inside each \(X\)-group
\[ \begin{aligned} D= 1 \; \Rightarrow Y^\ast_t = Y, \; Y^\ast_c = E(Y|D=0,X)\\ D= 0 \; \Rightarrow Y^\ast_t = E(Y|D=1,X), \; Y^\ast_c=Y \end{aligned} \]
- Finally just take the average \[\mbox{ATE} = E(Y^\ast_t-Y^\ast_c)\]
I think you need to stop scrolling and sit back for a minute. Do you realize what a massive assumption that is? Assuming that you have observed all of the variables \(X_1, X_2, \ldots\) that confounded the impact of \(D\) on \(Y\) requires a (often very) strong leap of faith. It’s a claim that cannot be ultimately proved using data (though that can help). We call that a non-empirical claim.
What if?
What if the students enrolled based on many other factors, like \(X_2=\) year, or \(X_3=\) national origin. What if we forgot to think about these scenarios? That’d be bad, but at least we could observe those and include them later.
What if another confounder was difficult or unethical to measure like \(X_4=\) disability status, maybe the two professors teach in different rooms with different disability access.
What if another confounder was difficult to even define and think about, like “enthusiasm”, or “personal disposition”?
What a mess!
The foundations of Science are based on metaphysical, non-empirical claims that cannot be proven starting from collecting data but instead need to be argued for and agreed on by society and the scientific community based on different ways to argue.
This is why fields that rely heavily on observational studies, like Economics, have very deep ideological and methodological differences, and claims are often questioned or dismissed by opposing schools of thought.
fact: Data doesn’t prove that you have observed all confounders
You cannot use data to prove that all the confounders have been observed. You need to argue for that, and people need to agree with you.
Matching: similar covariates and opposite treatment to estimate the ATE.
Now, remember that populations are great, but completely inaccessible, so we need to learn how to use confounders to estimate the \(\mbox{ATE}\) from a sample.
That’s where Matching comes in.
The process of matching can be tedious and look boring and difficult mathematically but it’s unbelievably simple.
Imagine you have a twin. Same genetics, same age, same socio-economic status, same everything. But one day your twin gets accepted to Stanford (control), while you go to Berkeley (treatment). It seems pretty reasonable that I could use the outcome of your twin as a best-prediction of what your potential outcome in control would have been in a counterfactual world (and vice versa).
def: Matching
For each observation, predict the missing, unobserved potential outcome by using actual outcomes from the observation(s) with the most similar covariates in the opposite treatment group.
We use \(\hat{Y}_{i,t}, \hat{Y}_{i,c}\) to indicate the predicted potential outcomes for observation \(i\).
First try: Exact Matching
This is gonna take a nanosecond. It’s essentially identical to what we had before. Suppose that a sample from the previous population looks like this:
| Name | Waking pattern (X) | D | Actual score (\(Y\)) |
|---|---|---|---|
| Mindy | ‘early bird’ | 1 | 85% |
| Durga | ‘early bird’ | 1 | 90% |
| Muhammad | ‘early bird’ | 1 | 90% |
| Jean-Paul | ‘early bird’ | 0 | 90% |
| Dmitry | ‘normie’ | 1 | 60% |
| Xiaoyu | ‘normie’ | 0 | 60% |
| Ratree | ‘night owl’ | 1 | 65% |
| Nyx | ‘night owl’ | 1 | 70% |
| Yozora | ‘night owl’ | 0 | 85% |
| Layla | ‘night owl’ | 0 | 80% |
For each observation \(i\), if it’s a control (\(D=0\)), predict the missing potential outcome, \(\hat{Y}_{i,t}\), by finding all the other observations with the same values of the covariates \(X\) but opposite value of the treatment (\(D=1\)) and taking a simple average. Vice versa for the treated.
def: Exact Matching
Assume all confounders X are observed.
For each observation \(i\) in the sample:
If \(i\) is a control (\(D_i=0\)), find all the observations in treatment with the same value of the covariate \(X_i\) (matching).
Take the average of these values. That’s your predicted treatment potential outcome!
\[ \begin{aligned} \hat{Y}_{i,t} &= \bar{Y}_{D=1,X=X_i} \\ \hat{Y}_{i,c} &= Y_i \end{aligned} \]
If \(i\) is treated (\(D_i=1\)), find all the observations in control with the same value of the covariate \(X_i\) (matching).
Take the average of these values. That’s your predicted control potential outcome!
\[ \begin{aligned} \hat{Y}_{i,t} &= Y_i \\ \hat{Y}_{i,c} &= \bar{Y}_{D=0,X=X_i} \end{aligned} \]
Finally, estimate the \(\mbox{ATE}\) by taking the sample average:
\[ \widehat{\mbox{ATE}} = \frac{1}{n} \sum_{i=1}^n \left( \hat{Y}_{i,t} - \hat{Y}_{i,c} \right) \]

E.g.
For example what would we predict for the missing potential outcome for \(i=3\), “Muhammad”?
First of all, “Muhammad” is a treated (\(D_3=1\)) and they have \(X_3=\)’early bird’.
So we have \(\hat{Y}_{3,t} = Y_3 = 90\%\), and we are missing the control potential outcome \(\hat{Y}_{3,c}\).
Let’s find all the other students who have \(X_j=\)’early bird’, and are controls (\(D_j=0\)). That’s just \(j=4\), “Jean-Paul”!
So, \(\hat{Y}_{3,c} = \bar{Y}_{D=0,X=X_3} = \frac1{1}\sum_{j=4} Y_j = Y_4 = 90\%\)
To sum it up, we predict the potential outcomes for Muhammad as \(\hat{Y}_{3,t} = 90\%, \; \hat{Y}_{3,c} = 90\%\)
Want to try and work out the example?
Let’s fill in all the predicted potential outcomes!
| Name | Waking pattern (X) | D | Predicted Potential score with prof. B (\(\hat{Y}_t\)) | Predicted Potential score with prof. N (\(\hat{Y}_c\)) |
|---|---|---|---|---|
| Mindy | ‘early bird’ | 1 | 85% | \(\begin{array}{c}Y_{\text{Jean-Paul}}\\=90\%\end{array}\) |
| Durga | ‘early bird’ | 1 | 90% | \(\begin{array}{c}Y_{\text{Jean-Paul}}\\=90\%\end{array}\) |
| Muhammad | ‘early bird’ | 1 | 90% | \(\begin{array}{c}Y_{\text{Jean-Paul}}\\=90\%\end{array}\) |
| Jean-Paul | ‘early bird’ | 0 | \(\begin{array}{c}\frac{1}{3}(Y_{\text{Mindy}}+Y_{\text{Durga}}+Y_{\text{Muhammad}})\\=88.33\%\end{array}\) | 90% |
| Dmitry | ‘normie’ | 1 | 60% | \(\begin{array}{c}Y_{\text{Xiaoyu}}\\=60\%\end{array}\) |
| Xiaoyu | ‘normie’ | 0 | \(\begin{array}{c}Y_{\text{Dmitry}}\\=60\%\end{array}\) | 60% |
| Ratree | ‘night owl’ | 1 | 65% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Yozora}}+Y_{\text{Layla}})\\=82.5\%\end{array}\) |
| Nyx | ‘night owl’ | 1 | 70% | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Yozora}}+Y_{\text{Layla}})\\=82.5\%\end{array}\) |
| Yozora | ‘night owl’ | 0 | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Ratree}}+Y_{\text{Nyx}})\\=67.5\%\end{array}\) | 85% |
| Layla | ‘night owl’ | 0 | \(\begin{array}{c}\frac{1}{2}(Y_{\text{Ratree}}+Y_{\text{Nyx}})\\=67.5\%\end{array}\) | 80% |
Then compute the final estimate
\[ \begin{aligned} \widehat{\mbox{ATE}} &= \frac{1}{10}\sum_i\left(\hat{Y}_{i,t}-\hat{Y}_{i,c}\right)=\\ &= \frac{1}{10}\big[ (85-90)+(90-90)+(90-90)+(88.33-90)+\\ &\qquad +(60-60)+(60-60)+(65-82.5)+(70-82.5)+\\ &\qquad +(67.5-85)+(67.5-80) \big]\\ &= \frac{1}{10}\big[ -5+0+0-1.67+0+0-17.5-12.5-17.5-12.5 \big]\\ &= \frac{-66.67}{10}\\ &\approx -6.67 \% \end{aligned} \]
Notice that if we had used the old method we used in Randomized Experiments we would have gotten
\[ \begin{aligned} \widehat{\mbox{ATE}}_{\mbox{naive}} &= \bar{Y}_{D=1}-\bar{Y}_{D=0}\\ &= \frac{1}{6}(85+90+90+60+65+70) - \frac{1}{4}(90+60+85+80)\\ &= 76.67\%-78.75\%\\ &= -2.08 \% \end{aligned} \]
which is a much much worse estimate of the \(\mbox{ATE}\).
So, \(\widehat{\mbox{ATE}} \approx -6.67 \%\). This is not too bad! We remember that the true population value was \(\mbox{ATE}=-8.75 \%\).
But… what happens if we don’t find any matches for our observation \(i\) in the sample?
Try again: Nearest-neighbor Matching
Consider this sample
| Name | Waking pattern (X) | D | Actual score (\(Y\)) |
|---|---|---|---|
| Mindy | ‘early bird’ | 1 | 85% |
| Muhammad | ‘early bird’ | 1 | 90% |
| Xiaoyu | ‘normie’ | 0 | 60% |
| Nyx | ‘night owl’ | 1 | 70% |
| Yozora | ‘night owl’ | 0 | 85% |
| Layla | ‘night owl’ | 0 | 80% |
Whoops. Some of the observations cannot find any exact matches!
Which ones?
Mindy (\(i=1\)), Muhammad (\(i=2\)), Xiaoyu (\(i=3\)) have no exact matches with the same value of \(X_i\) and opposite of \(D_i\).
Well, at this point things get very open ended and we need to come up with some ideas to find maybe the next best match, if we don’t have exact ones.
There are many many ways of doing this, enough to fill a book. Here I am only gonna suggest one: nearest-neighbor matching.
To do that, we need to take a quick break and talk about distances…
How to measure distances between observations?
The problem we are facing is that, for, e.g., an observation in treatment, we want to find the closest, most similar observation in control.
Most similar how? Well, most similar meaning that it has the closest value of the covariates. Don’t forget that it would be a huge mistake to ask that the values of \(Y\), or of \(D\), or of a variable measured after treatment, be also similar.
Okay, but is that helpful? Let’s see a concrete example
Imagine we have a study where we measure covariates \(X_1 = \mbox{age}, X_2 = \mbox{height}, X_3 = \mbox{income}\).
Pick observation \(i\) with covariates \(\left( X_{i1}=34\mbox{ years}, X_{i2}=179\mbox{ cm}, X_{i3}= \mbox{ \$} 110000 \right)\), and observation \(j\) with covariates \(\left( X_{j1}=28\mbox{ years}, X_{j2}=186\mbox{ cm}, X_{j3}= \mbox{ \$} 85000 \right)\).
How would you measure the similarity or maybe the distance between \(i\) and \(j\)?
Should the difference in \(X_1\), \(X_{i1} - X_{j1} = 6\mbox{ years}\) count for more or less than the one in \(X_2\), \(X_{i2} - X_{j2} = -7\mbox{ cm}\)? What about \(X_{i3} - X_{j3} = \mbox{ \$} 25000\)?
Do years count more or less than centimeters? What does it even mean?
Clearly this question doesn’t make a lot of sense in absolute terms. We must make some choices, and the bad news is that there are countless ways of making valid choices. Maybe enough to fill a quarter of a book.
We don’t have the time to go over those and instead I’m going to just propose the most intuitive one: Scaled Euclidean Distance.
def: Scaled Euclidean Distance
Say that the total number of covariates is \(q\).
Step 1
We don’t like that the covariates have units, so we rescale them by dividing by the sample standard deviation (which has the same units). Take one covariate \(X_k\) and compute \(s_{X_k}\). Then:
\[ Z_{ik} = \frac{X_{ik}}{s_{X_k}} \]
Step 2
Finally define the Euclidean distance between the two observations \(i\) and \(j\): \[ d_{ij} = \sqrt{\sum^q_{k=1}(Z_{ik} - Z_{jk})^2} \]
This might look ugly but it’s literally just the usual notion of distance we use between points on a line (if there’s only one covariate, \(q=1\)), plane (if there are two covariates, \(q=2\)), or in 3-D space (if there are three covariates, \(q=3\)).
E.g.
Continuing the example above, assume that we calculated \(s_{X_1} = 3.76 \mbox{ years}\), \(s_{X_2} = 16.77 \mbox{ cm}\), and \(s_{X_3} = \mbox{ \$} 37822\).
So, for \(i\), \(\left( Z_{i1}=\frac{34\mbox{ years}}{3.76 \mbox{ years}}= 9.04, Z_{i2}=\frac{179\mbox{ cm}}{16.77 \mbox{ cm}} =10.67 , Z_{i3}= \frac{\mbox{ \$} 110000}{\mbox{ \$} 37822} =2.91 \right)\), and for \(j\), \(\left( Z_{j1}=\frac{28\mbox{ years}}{3.76 \mbox{ years}}=7.45, Z_{j2}=\frac{186\mbox{ cm}}{16.77 \mbox{ cm}}=11.09, Z_{j3}= \frac{\mbox{ \$} 85000}{\mbox{ \$} 37822}=2.25 \right)\).
Finally,
\[ \begin{aligned} d_{ij} &= \sqrt{ ( 9.04-7.45)^2 +(10.67-11.09)^2 + (2.91-2.25)^2 } =\\ &=\sqrt{ (1.59)^2 +(-0.42)^2 + (0.66)^2 }=\sqrt{ 3.14 } = 1.77 \end{aligned} \]
Is that far? Is that close? Not sure. You’ll have to actually conduct the matching over the whole sample to know.
Nearest-Neighbor Matching
Now we’re all set to define our closest match and use it as our best approximation of the missing potential outcome!
def: Nearest-Neighbor Matching
Assume all confounders X are observed.
For each observation \(i\) in the sample:
If \(i\) is a control (\(D_i=0\)), find the observation \(j_{\text{nearest}}\) in treatment with the closest distance from \(i\)
That’s your predicted treatment potential outcome!
\[ \begin{aligned} \hat{Y}_{i,t} &= Y_{j_{\text{nearest}}} \\ \hat{Y}_{i,c} &= Y_i \end{aligned} \]
If \(i\) is a treated (\(D_i=1\)), find the observation \(j_{\text{nearest}}\) in control with the closest distance from \(i\)
That’s your predicted control potential outcome!
\[ \begin{aligned} \hat{Y}_{i,t} &= Y_i \\ \hat{Y}_{i,c} &= Y_{j_{\text{nearest}}} \end{aligned} \]
Finally, estimate the \(\mbox{ATE}\) by taking the sample average, like before:
\[ \widehat{\mbox{ATE}} = \frac{1}{n} \sum_{i=1}^n \left( \hat{Y}_{i,t} - \hat{Y}_{i,c} \right) \]

Note that this time around, for simplicity, with nearest-neighbor matching, we’re only looking at one single match for each observation, whereas with exact matching we took averages of all the matches.
Want to try and work out the example?
Let’s fill in all the predicted potential outcomes! Unfortunately because \(X\) is categorical, there is no best way of going about it and I will just do my best and pick randomly when there are ties.
| Name | Waking pattern (X) | D | Predicted Potential score with prof. B (\(\hat{Y}_t\)) | Predicted Potential score with prof. N (\(\hat{Y}_c\)) |
|---|---|---|---|---|
| Mindy | ‘early bird’ | 1 | 85% | \(\begin{array}{c}Y_{\text{Xiaoyu}}\\=60\%\end{array}\) |
| Muhammad | ‘early bird’ | 1 | 90% | \(\begin{array}{c}Y_{\text{Xiaoyu}}\\=60\%\end{array}\) |
| Xiaoyu | ‘normie’ | 0 | \(\begin{array}{c}Y_{\text{Nyx}}\\=70\%\end{array}\) | 60% |
| Nyx | ‘night owl’ | 1 | 70% | \(\begin{array}{c}Y_{\text{Yozora}}\\=85\%\end{array}\) |
| Yozora | ‘night owl’ | 0 | \(\begin{array}{c}Y_{\text{Nyx}}\\=70\%\end{array}\) | 85% |
| Layla | ‘night owl’ | 0 | \(\begin{array}{c}Y_{\text{Nyx}}\\=70\%\end{array}\) | 80% |
Then compute the final estimate
\[ \begin{aligned} \widehat{\mbox{ATE}} &= \frac{1}{6}\sum_i\left(\hat{Y}_{i,t}-\hat{Y}_{i,c}\right)\\ &= \frac{1}{6}\big[ (85-60)+(90-60)+(70-60)\\ &\qquad +(70-85)+(70-85)+(70-80) \big]\\ &= \frac{1}{6}\big[ 25+30+10-15-15-10 \big]\\ &= \frac{25}{6}\\ &\approx 4.17\%. \end{aligned} \]
N.B The same treatment observation could end up being the best match for several control observations, and vice versa, which gives them a higher weight in the final estimate.
Is this a problem?
Sort of, yes. I mean, the math works. It does require fancier formulas to compute the standard error or run the hypothesis test, sure, but that’s not the real issue.
The real issue is that in practice our final results will be relying mostly on a few key observations, making them much more “unstable”. What if those observations had some random measurement mistake in them? They might completeley skew our conclusions?
Scary thought.
The estimate we just got is terrible, but that’s because we have a tiny sample with a very undefined matching strategy. If your data is bad you won’t get good results, no matter the fancy technique. Garbage in, garbage out.
That’s why we’ll need to demo this technique with much nicer datasets. See you in the tutorial.
Summary
We don’t need to give up on evaluating causal claims just because we are unable to assign treatments at random as in an experiment. We can still squeeze out one last drop, sometimes.
We saw how the Confounding problem is created by Confounders: covariates that bias the potential outcomes between treatment and control.
At the same time, we saw that if we measure all the confounders, we can attempt to approximate a Randomized Experiment by finding similar individuals with different treatment status in the same data set by matching observations on their covariates.
If we can argue that we measured all the confounders (which is an extremely ambitious claim), and we apply matching, the potential outcome balance is restored across treatment and control, and we can produce reliable estimates of the \(\mbox{ATE}\)!