
Week 11: double machine learning, kernels, and networks
A winning score shows that one model beats another, on one target and one distribution
Deciding whether to offer extra care needs something a score does not give: how much the care changes the outcome, an effect
Today:
Part 1: an effect from flexible fits
Part 2: kernels, features fixed in advance
Part 3: networks, features fitted to the outcome
Treatment \(T\), outcome \(Y\), and, in the model of the DAG beside this, inputs \(Z\) that hold every common cause of \(T\) and \(Y\) and no consequence of \(T\)
Adjustment theorem: \(\mathbb E[Y \mid do(T = t)] = \mathbb E_Z\big[\mathbb E[Y \mid T = t, Z]\big]\), if \(P(T = t \mid Z) > 0\) (for a continuous \(T\), a positive density)
If the effect of \(T\) is the same for every unit and linear in \(t\), the whole curve is one number \(\theta\): \[\mathbb E[Y \mid do(T = t)] = \theta\, t + \text{constant}\]

\[ Y = T\theta + Z\beta + \varepsilon_Y, \qquad T = Z\alpha + \varepsilon_T \]
Estimate \(\hat\theta\) from e.g. 2SLS
\[ Y = T\theta + g(Z) + \varepsilon_Y, \qquad T = m(Z) + \varepsilon_T \]
Replace LS (least squares) with machine learning methods to estimate “nuisance functions” \(\hat g\) and \(\hat m\)
\[ \begin{aligned} Y &= \theta\, T + g(Z) + \varepsilon_Y, &\qquad \mathbb E[\varepsilon_Y \mid T, Z] &= 0\\ T &= m(Z) + \varepsilon_T, & \mathbb E[\varepsilon_T \mid Z] &= 0 \end{aligned} \]
Under the adjustment condition, \(\theta\) is the effect of \(T\) on \(Y\)
Suppose a learner, fitted on other rows, gave us \(\hat g\). Subtract it and regress on \(T\): \[ \hat\theta_0 = \frac{\sum_i T_i\big(Y_i - \hat g(Z_i)\big)}{\sum_i T_i^2}, \qquad \hat\theta_0 - \theta = \frac{\sum_i T_i\,\big(g - \hat g\big)(Z_i)}{\sum_i T_i^2} + \frac{\sum_i T_i\,\varepsilon_{Y,i}}{\sum_i T_i^2} \]
And \(Y\) on \(Z\) estimates \(\ell\), not \(g\): for a continuous \(T\), fitting \(g = \ell - \theta m\) needs \(\theta\) itself
Partial \(Z\) out of both, with any learner, then one regression through the origin: \[ \hat\theta = \frac{\sum_i \big(T_i - \hat m(Z_i)\big)\big(Y_i - \hat\ell(Z_i)\big)}{\sum_i \big(T_i - \hat m(Z_i)\big)^2} \]
Partialling-out theorem: the coefficient on \(x_2\) is the slope of \(y\) on what is left of \(x_2\) after \(\mathbf X_1\) has explained what it can
\(\hat\ell\), \(\hat m\) fitted on other rows, so on these rows they are fixed functions of \(Z\), with errors \(a = \hat\ell - \ell\) and \(b = \hat m - m\)
\[ \begin{alignedat}{2} &T - \hat m(Z) = \varepsilon_T - b &\qquad& \color{gray}\small T = m(Z) + \varepsilon_T \\[2pt] &\class{fragment}{Y - \hat\ell(Z) = \theta\,\varepsilon_T + \varepsilon_Y - a} && \class{fragment}{\color{gray}\small Y - \ell(Z) = \theta\,(T - m(Z)) + \varepsilon_Y} \\[2pt] &\class{fragment}{Y - \hat\ell(Z) - \theta\,\big(T - \hat m(Z)\big) = \varepsilon_Y - a + \theta\, b} && \class{fragment}{\color{gray}\small \text{subtract } \theta \text{ times the first line}} \end{alignedat} \]
\(\hat\theta\) makes \(\sum_i \big(T_i - \hat m(Z_i)\big)\big(Y_i - \hat\ell(Z_i) - \hat\theta\,(T_i - \hat m(Z_i))\big) = 0\). At the true \(\theta\), each row contributes the score \[ \psi = \big(T - \hat m(Z)\big)\big(Y - \hat\ell(Z) - \theta\,(T - \hat m(Z))\big) = (\varepsilon_T - b)(\varepsilon_Y - a + \theta\, b) \]
\[ \begin{aligned} \mathbb E_{T, Y, Z}[\psi] &= \underbrace{\mathbb E[\varepsilon_T\varepsilon_Y] - \mathbb E[\varepsilon_T\, a] + \theta\,\mathbb E[\varepsilon_T\, b] - \mathbb E[b\,\varepsilon_Y]}_{0:\ \mathbb E[\varepsilon_Y \mid T, Z] = 0,\ \ \mathbb E[\varepsilon_T \mid Z] = 0} + \,\mathbb E[ab] - \theta\,\mathbb E[b^2] \end{aligned} \]
So, for \(\mathbb E[b^2] \ll \mathbb E[\varepsilon_T^2]\), \(\hat\theta - \theta \approx \big(\mathbb E[ab] - \theta\,\mathbb E[b^2]\big)/\mathbb E[\varepsilon_T^2]\), plus noise of order \(n^{-1/2}\): the learners’ errors enter as products (the first try had one error, to the first power). Enough, with regularity conditions (Chernozhukov et al. 2018): \[ \|b\|_2\,\big(\|a\|_2 + \|b\|_2\big) = o_p\big(n^{-1/2}\big), \quad \text{for example both errors } o_p\big(n^{-1/4}\big) \]
The cost of a non-free lunch, square-rooted: Lipschitz in \(p\) inputs gives an error of order \(n^{-1/(p+2)}\), smaller than \(n^{-1/4}\) only for \(p = 1\); a sum of one-input curves, \(n^{-1/3}\). Worst cases over a class; the condition is about our learners’ actual errors
Sample splitting, using separate subsets of data to compute
Two ways to think of why this is a good idea
Can be iterated: swap subsets and average \(\hat\theta\) estimates
Beyond least squares the estimator has two parts: residualizing \(T\) as well as \(Y\), and cross-fitting the learners. Design an experiment that shows whether each part is needed.
An ablation: remove one part, keep everything else fixed, run again on the same data, compare. On simulated data the true \(\theta\) is known, so every version can be checked against it

Removing a part that matters shows as bias against the known \(\theta\). Removing cross-fitting changed little for these trees on these data
Standard error from the residual regression, allowing unequal variances (lm()’s default does not), with \(\tilde t_i = T_i - \hat m(Z_i)\) and \(\hat e_i = Y_i - \hat\ell(Z_i) - \hat\theta\,\tilde t_i\): \[
\widehat{\mathrm{se}}(\hat\theta) = \frac{\sqrt{\sum_i \tilde t_i^{\,2}\,\hat e_i^{\,2}}}{\sum_i \tilde t_i^{\,2}}
\] One \(\theta\) says how much the care helps on average, not who it helps most: ranking patients needs an effect that varies with \(Z\)
In the simulation, \(\hat\theta\) averaged 1.010 with standard deviation 0.059; the standard error averaged 0.058; \(\hat\theta \pm 1.96\,\widehat{\mathrm{se}}\) covered \(\theta\) in 94 of 100 data sets
Nothing here makes \(\theta\) causal. That is the adjustment condition, and no learner checks it
Observed predictors must capture everything about how treatment assignment and treatment effectiveness are related

Practically all methods assume this isn’t happening (some conduct sensitivity analysis)
Split training data, use one subset for deciding tree structure (which variables to use for splitting) and the other subset for predictions
e.g. Suppose we fit a tree with depth 1, on one subset of data we find the best split is \(X_7 < c\). Then for predictions we use the values of \(Y\) in a separate subset of data, averaging/voting those \(Y\) values with \(X_7 < c\) for one prediction and those with \(X_7 \geq c\) for the other prediction. (No test/validation yet)
Exercise: explain how this is different from the out-of-bag validation approach in bagging. Draw diagrams (perhaps with inspiration from ISLR Figure 5.5) to represent the two cases
Weak overlap. If \(Z\) nearly determines \(T\), then \(T - \hat m(Z)\) is nearly zero for most rows, and \(\hat\theta\) divides by almost nothing
An effect that varies with \(Z\). If \(Y = \theta(Z)\,T + g(Z) + \varepsilon_Y\), residual on residual estimates \[ \frac{\mathbb E\big[\theta(Z)\,\mathrm{Var}(T \mid Z)\big]}{\mathbb E\big[\mathrm{Var}(T \mid Z)\big]}, \] a weighted average of the effects, weighted toward the units whose treatment varies most, which generally differs from the average effect \(\mathbb E[\theta(Z)]\)
Consider this polynomial transformation:
\[ \varphi(x_1, x_2) = (x_1, x_2, x_1^2, x_2^2, x_1x_2) \]
This function \(f\) is linear in the higher-dimensional features
\[ f(\mathbf x) = \color{#21918c}{\beta_0} + \color{#21918c}{\beta_1} x_1 + \color{#21918c}{\beta_2} x_2 + \color{#8c2981}{\beta_3} x_1^2 + \color{#8c2981}{\beta_4} x_2^2 + \color{#8c2981}{\beta_5} x_1x_2 = \beta_0 + \beta^T \varphi(\mathbf x) \]
A spline is regression on basis functions chosen in advance: the same idea, with a different basis
With \(p = 100\) inputs, the polynomial features of degree up to 4 number \(\binom{104}{4} = 4{,}598{,}126\). With \(n = 1{,}000\) rows, ridge on these features solves a system with millions of unknowns. Is there another way to compute the same predictions?
\(\mathbf y\) and the columns of \(\mathbf X\) (\(n \times p\)) centered, so no intercept. For \(\lambda > 0\): \[ \mathbf X^T(\mathbf X\mathbf X^T + \lambda \mathbf I_n) = \mathbf X^T\mathbf X\mathbf X^T + \lambda\mathbf X^T = (\mathbf X^T\mathbf X + \lambda\mathbf I_p)\,\mathbf X^T \] Both brackets are invertible (\(\mathbf X^T\mathbf X + \lambda\mathbf I\), as for ridge; \(\mathbf X\mathbf X^T + \lambda\mathbf I\), the same way), so \[ \hat\beta_\lambda = (\mathbf X^T\mathbf X + \lambda\mathbf I_p)^{-1}\mathbf X^T\mathbf y = \mathbf X^T(\mathbf X\mathbf X^T + \lambda\mathbf I_n)^{-1}\mathbf y \]
Avoid computing \(\varphi(\mathbf x)\), which may be high (infinite) dimensional
e.g. For degree \(d\) polynomial, dimension \(p \mapsto \binom{p+d}{d}\), but
\[ \langle \varphi(\mathbf x_i), \varphi(\mathbf x_j) \rangle = K(\mathbf x_i, \mathbf x_j) = (1 + \mathbf x_i^T \mathbf x_j)^d \]
For \(p = d = 2\): \(\varphi(\mathbf x) = (1, \sqrt2\, x_1, \sqrt2\, x_2, x_1^2, \sqrt2\, x_1x_2, x_2^2)\), the polynomial embedding with a constant and weights
much higher dimensional inner product
\(p\)-dimensional inner product followed by power of scalar
Pros: fast computation despite high-dimensional embedding
Cons: memory-based, like \(k\)-NN, “memorize the training data”
\(\mathbf y\) centered. Replace every inner product by a kernel \(K(x, x') = \langle\varphi(x), \varphi(x')\rangle\): the \(n \times n\) matrix \(\mathbf K\) with entries \(K(x_i, x_j)\), and \(\mathbf k(x) = \big(K(x, x_1), \ldots, K(x, x_n)\big)\) \[ \hat f(x) = \bar y + \mathbf k(x)^T(\mathbf K + \lambda\mathbf I)^{-1}\mathbf y = \bar y + \sum_{i=1}^n \hat\alpha_i\, K(x, x_i), \qquad \hat\alpha = (\mathbf K + \lambda\mathbf I)^{-1}\mathbf y \]
Representer theorem: minimizing squared error plus \(\lambda\) times the squared norm over the whole space of functions that \(K\) defines gives a fit of this form, a combination of \(K(\cdot, x_i)\) over the \(n\) training points
For any inner product \(\langle \cdot, \cdot \rangle\)


All three are weighted sums of the outcomes, with weights that sum to one; kernel ridge’s can be negative. For the kernel-weighted average the bandwidth plays the part of \(k\): \(h \asymp (k/n)^{1/p}\), and the \(k\)-NN rate’s \((k/n)^{2/p} + 1/k\) becomes \(h^2 + 1/(nh^p)\)
\(k\)-NN rate: \(C_pL^2(k/n)^{2/p} + \sigma^2/k\), balanced at \(k \asymp n^{2/(p+2)}\)
Curse of dimensionality: in many dimensions, the nearest 1% of the data is far away
“Automatic” feature engineering
Outside low-dimensional motivating examples, typically don’t choose type of kernel very carefully
Ultra-algorithmic supervised ML philosophy: focus on expanding expressivity of the function class, let efficient optimization algorithms do the work of “respecting the structure in the data”
Influential idea \(\to\) deep learning
Linear models
Linear models with global transformations of predictors
Additive non-linear models
Compositional non-linear models
\[\hat f(\mathbf x) = \hat f_L \circ \cdots \circ \hat f_2 \circ \hat f_1(\mathbf x) = \hat f_L ( \cdots ( \hat f_2 (\hat f_1(\mathbf x)) \cdots )\]

Draw the hidden units’ weights (dashed) at random and never train them; fit only the output weights. What kind of model is left?
Least squares on \(M\) fixed features \(h_1(x), \ldots, h_M(x)\); with a penalty on the output weights, ridge. By part 2, kernel ridge with \(K(x, x') = 1 + \sum_{k=1}^M h_k(x)\,h_k(x')\), the 1 for the output’s bias: random features, the name from Rahimi and Recht (2007). Training the first layer fits the features to \(y\)
A GLM of GLMs: \(h_k(x) = g^{-1}(w_{k0} + w_k^T x)\), \(f(x) = \beta_0 + \sum_k \beta_k h_k(x)\)
\(p = 10\) standard normal inputs, noise sd 0.5, \(f^\star(x) = \sin(2u) + u^2/2\) with \(u = v^Tx\), one direction \(v\). Excess risk \(\mathbb E\big[(\hat f(X) - f^\star(X))^2\big]\)

Same network, same start, \(M = 10\): training the first layer takes the excess risk at \(n = 2{,}000\) from 0.99 to 0.054; frozen at 20 times the width, 0.55

3 of 10 trained units point along \(v\) (\(|\cos| > 0.99\); at the start, at most 0.71; \(n = 1{,}000\)). Radial truth: kernel ridge does best; every network does about as well as linear regression
No free lunch theorem: choosing a method is an assumption about the world
After network architecture is fixed, how do we train the network / estimate the weights and biases?
To use gradient descent, need to compute gradient of \[ \sigma_L (\cdots \sigma_2(\beta^{(2)}_0 + \sigma_1(\beta^{(1)}_0 + \mathbf X \beta^{(1)}) \beta^{(2)}) \cdots ) \] with respect to all the \(\beta\)’s (\(\sigma_l\): activation functions, the logistic \(g^{-1}\) in our networks)
Chain rule, automatic differentiation, efficient implementations, handling numerical issues, etc
Backpropagation: the chain rule, run from the output backward
Epoch: one gradient descent pass over entire training data
Algorithm and software side of the DL revolution

Each layer’s units are features of the layer below: \(f = f_L \circ \cdots \circ f_1\), with \(M_1(p + 1) + M_2(M_1 + 1) + M_2 + 1\) weights for two layers, all fitted to \(y\)
For deep networks, choose number \(L\) of (hidden) layers, and for every layer choose
Compare to e.g. regularized kernel regression: choose kernel, regularizer, bandwidth. Not that many choices.
Network architecture = combinatorial explosion of choices
Modeling side of the DL revolution

Remove or replace one part, hold everything else fixed, score again on the same rows
Comparing two models on the same rows: pair the losses, \(\Delta_j = \ell_{A,j} - \ell_{B,j}\); the standard error \(\mathrm{sd}(\Delta)/\sqrt m\) is small when the models err on the same rows
Some functions require exponentially many parameters to represent with a 2-layer network, but grow polynomially if depth of network increases
Expressivity depends more strongly on depth
Dropout is like bagging
During training: randomly drop units, iteration of gradient descent uses subnetwork
During testing: use full network
Averaging over many models represented by all possible subnetworks
Non-convex, but doesn’t seem to suffer local minima problems
Overparametrized so global optimum is 0 training loss. Real question is why do these solutions generalize, aren’t overfit?
rnorm(n) (no true signal)Double descent: past the interpolation threshold, the minimum-norm interpolant’s test error can fall again
Early work (1943) motivated by analogy to neurons, “activation” of a neuron cell’s action potential which sends an electric signal to connected neurons
Equivalence between a mathematical model of brain activity and boolean logic
Useful analogy, can inspire research ideas (remember it’s just an analogy, it doesn’t mean actual brain biology works this way or that neural networks are like thinking brains…)
Universal approximation (Cybenko 1989). For any continuous \(f\) on \([0, 1]^p\), any continuous sigmoidal \(\sigma\) (such as \(g^{-1}\)) and any \(\epsilon > 0\), there are \(M\) and weights with \(\big|f(x) - \sum_{k=1}^M \beta_k\, \sigma(w_{k0} + w_k^Tx)\big| < \epsilon\) for every \(x\) in \([0, 1]^p\). Existence only: nothing about \(n\) or the fit
Think of the method you would reach for first in your project. How would you do the job without it?
Terence Tao, to mathematicians: once a favorite tool has proved something, rewrite the argument without it; if you understand the tool you can, at the cost of a longer, messier argument. My generalization: ask how you could do the job without the tools you usually rely on
| Tool | Without it | What that showed |
|---|---|---|
| learners in double ML | linear partialling out | biased: the confounding is curved |
| the polynomial kernel | explicit features, then ridge | the same predictions |
| a trained first layer | frozen random features | far worse on one truth only |
| cross-fitting | in-sample residuals | little change, for these trees |
This week: exercises 1 and 2 at the top of the notebook. Ridge computed two ways, proved with every step; and the expected score of residual on residual, with the slides closed
In the seminar: kernel ridge in a few lines of R against ridge on the explicit features; then double ML’s ablation by simulation, with a learner for which cross-fitting does matter
Residual on residual, with learners fitted on other rows: their errors enter as products. Causal only under the adjustment condition
Kernels fix them in advance and compute without writing them down; networks fit them to \(y\), which helps when \(f^\star\) has structure such as one direction
Remove it, hold the rest fixed, and compare against the noise
Please fill in the course survey now: five minutes
ST310 Machine Learning