Quickstart: who gains from job training?

examples/quickstart.py estimates how the effect of a job-training program on earnings differs across people. The data are simulated (examples/job_training_data.py writes 20,000 people to a Parquet file the first time), so the true effect for each person is known and the estimates can be checked against it.

People with low earnings before the program gain the most from it, and people with less schooling gain a little more. People with low earnings are also likelier to enroll, so the treated and the untreated differ before the program starts, and the forest has to adjust for that as well as find the differences in the effect.

The forest

import forest_stream as fs
from job_training_data import load_job_training

path = load_job_training()
fit = fs.causal_forest(
    "earnings ~ enrolled | age + education + prior_earnings + region", path, seed=1
)
fit.summary()      # the forest, and how long each step took

The formula is outcome ~ treatment | features. The forest grows 2,000 honest trees, after centering earnings and enrollment on the features with two regression forests of their own.

The average effect

ate = fit.average_treatment_effect()
print(ate.summary_text())
### earnings ~ enrolled | age + education + prior_earnings + region
estimand: average treatment effect
vcov: iid
obs: 20,000
┌─────────────┬──────────┬────────────┬───────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value   ┆ Pr(>|t|)   │
╞═════════════╪══════════╪════════════╪═══════════╪════════════╡
│ ATE         ┆ 0.805702 ┆ 0.052302   ┆ 15.404843 ┆ 1.5186e-53 │
└─────────────┴──────────┴────────────┴───────────┴────────────┘

The true average effect in these data is 0.860: the estimate is a little over one standard error below it. A comparison of the average earnings of the enrolled and the not enrolled gives -3.0, the wrong sign, because the people who enroll earned less to begin with.

Each person’s effect

est = fit.oob_predictions().sort("row_id")      # columns: row_id, tau, variance

Each person’s estimate comes from the trees whose samples did not include that person. Their correlation with the true effects is 0.96.

fit.plot("effects")                              # the distribution of the estimates
fit.plot("heterogeneity", feature="prior_earnings")
The distribution of the estimated effects The average estimated effect by groups of prior earnings, falling as earnings rise

The second plot’s bars are the standard errors of each group’s mean estimate; they are not intervals for the true effect of the group.

Which features matter

print(fit.variable_importance())
┌────────────────┬────────────┐
│ feature        ┆ importance │
╞════════════════╪════════════╡
│ age            ┆ 0.101341   │
│ education      ┆ 0.091468   │
│ prior_earnings ┆ 0.757861   │
│ region         ┆ 0.04933    │
└────────────────┴────────────┘

Most splits near the root are on prior_earnings, which is where the heterogeneity is.

Is the heterogeneity real?

print(fit.calibration_test().summary_text())
┌────────────────────────────────┬──────────┬────────────┬───────────┬────────────┐
│ Coefficient                    ┆ Estimate ┆ Std. Error ┆ t value   ┆ Pr(>|t|)   │
╞════════════════════════════════╪══════════╪════════════╪═══════════╪════════════╡
│ mean.forest.prediction         ┆ 1.00514  ┆ 0.057714   ┆ 17.415947 ┆ 1.9646e-67 │
│ differential.forest.prediction ┆ 0.949013 ┆ 0.049214   ┆ 19.283422 ┆ 4.1195e-82 │
└────────────────────────────────┴──────────┴────────────┴───────────┴────────────┘

Both coefficients are close to one: the average effect is right, and so is the size of the differences between people. A coefficient near zero on the second would mean the forest found no heterogeneity.

The effect as a linear function

fit.best_linear_projection(path, ["age", "education", "prior_earnings"])
┌────────────────┬───────────┬────────────┬────────────┬────────────┐
│ Coefficient    ┆ Estimate  ┆ Std. Error ┆ t value    ┆ Pr(>|t|)   │
╞════════════════╪═══════════╪════════════╪════════════╪════════════╡
│ Intercept      ┆ 3.508016  ┆ 0.342695   ┆ 10.236544  ┆ 1.5635e-24 │
│ age            ┆ -0.002673 ┆ 0.004695   ┆ -0.56927   ┆ 0.569179   │
│ education      ┆ -0.011988 ┆ 0.022039   ┆ -0.543955  ┆ 0.586479   │
│ prior_earnings ┆ -0.072021 ┆ 0.004459   ┆ -16.152128 ┆ 2.5671e-58 │
└────────────────┴───────────┴────────────┴────────────┴────────────┘

Each extra thousand dollars of earnings before the program lowers the effect by about 0.07 thousand dollars (the simulated effect falls by 2/30 = 0.067), and age and schooling add little once earnings are accounted for.

New people

import polars as pl

new = pl.DataFrame({"age": [25.0, 45.0], "education": [12.0, 16.0],
                    "prior_earnings": [10.0, 60.0], "region": [1.0, 3.0]})
fit.predict(new)
┌───────────┬──────────┐
│ tau       ┆ variance │
╞═══════════╪══════════╡
│ 2.796932  ┆ 0.116975 │
│ -0.894916 ┆ 0.026641 │
└───────────┴──────────┘

Each row gets an effect and the variance of that estimate. Predicting and plotting covers prediction from a file, to a file, in batches.