Sample weights

Every estimator takes weights=, the name of a column of sample weights:

import forest_stream as fs

# the effect of a program in a survey sample, weighted back to the population
fit = fs.causal_forest("earnings ~ trained | age + schooling + region", "survey.parquet",
                       weights="survey_weight")
fit.average_treatment_effect().summary()

fs.regression_forest("price ~ area + rooms", "sales.parquet", weights="w")
fs.decision_tree("default ~ income + debt", "loans.parquet", weights="w")

What a weight means

The weights are analytic weights: each row counts in proportion to its weight, and multiplying all the weights by a constant changes nothing. Use them for survey or sampling weights, or to give a row the weight of the several observations it stands for. They should not depend on the outcome (or, for a causal forest, on the potential outcomes) given the features: weights that do can bias the estimates, as in any weighted regression.

Weights enter wherever the data are summarized:

  • Splits. A split is chosen by the weighted criterion: the weighted sum of squares (regression), the weighted impurity of the class shares (classification), or, for a causal forest, the weighted version of the criterion that grf’s causal forest uses, with the weighted means and treatment variation.

  • Leaves and predictions. A leaf’s value is the weighted mean (or class share) of its estimation rows, and a forest averages its trees’ leaf values.

  • Effects and tests. average_treatment_effect is a weighted mean of the doubly robust scores, with the standard error of a weighted mean, clustered when the forest is. best_linear_projection and calibration_test are weighted least squares, with the heteroskedasticity-robust (HC0 to HC3) and cluster-robust (CRV1) variances of a weighted fit. oob_score is weighted.

  • Nuisance functions and fixed effects. A causal forest’s nuisance forests are weighted, and so is every regression on the fixed effects: the linear centering, the group effects of backfitting (with the standard errors of the effects measured with the effective sample size, (sum w)^2 / sum w^2), the group means that fixed_effects="group_means" adds as features, and the between-group shares that "outcome_within" reports.

Weights do not enter the sampling of rows into trees, which is by row (or cluster) and equal for all, and sizes count rows: min_node_size is a number of rows, not a total weight.

Rows that are left out

A row with a missing weight is left out with the other rows that have missing values. A row with a zero weight is left out too; a negative weight is an error, as is a weight column that is not numeric. n_obs counts the rows used.

Results

The weights column is recorded as fit.weights, and in the weights of the effect and projection results. A decision tree’s tree() table has a weight column (the total weight of the rows that estimate each node’s value) beside n (their number).