Tutorials › Marginal Structural Models › Random Forests for Treatment Probability Estimation

Inverse Probability of Treatment Weights · Tutorial 109 of 501

Random Forests for Treatment Probability Estimation

Use forest-predicted treatment probabilities to construct inverse weights, and assess when those estimates are too unstable or unreliable for causal analysis.

Advanced 12 min read

What You'll Learn

  • Define binary treatment probabilities conditional on pre-treatment history
  • Calculate a forest class-probability estimate from terminal-node treatment fractions
  • Distinguish averaged class probabilities from hard-classification vote fractions
  • Convert fitted probabilities into weights for the treatment actually received
  • Diagnose extreme weights, weak support, and misleading in-sample predictions
  • State the assumptions needed for inverse-weighting identification

Why Use a Random Forest for Treatment Probabilities?

Inverse probability weighting requires a model for the probability of receiving each treatment, conditional on the measured pre-treatment history. A logistic model can be effective, but its usual linear predictor may not capture thresholds, interactions, or other nonlinear patterns unless they are specified. A random forest offers a flexible alternative: it partitions the covariate space into regions and estimates treatment probabilities from the treatment frequencies in those regions.

Flexibility is not a guarantee of good probabilities. The treatment model must represent the relevant history, its predictions must be suitable for weighting, and the observed data must contain support for the treatment choices being weighted. A forest can adapt to complex patterns while still producing unstable probabilities in sparse regions.

Definition: Let \(A\in\{0,1\}\) be a binary treatment and let \(H\) denote the measured pre-treatment covariates or history used to model treatment assignment. The treatment probability is \(e(H)=P(A=1\mid H)\). The probability of treatment \(a\) is \(e_1(H)=e(H)\) for \(a=1\), and \(e_0(H)=1-e(H)\) for \(a=0\).

From Forest Leaves to Class Probabilities

A classification tree divides the covariate space into terminal leaves. Within a leaf, a natural estimate of the probability of treatment 1 is the fraction of observations in that leaf that received treatment 1. A random forest builds many trees, typically using bootstrap samples and a random subset of candidate predictors at each split. To obtain a probability estimate, the tree-level class probabilities are averaged across trees.

For a training observation \(i\), write \(A_i\) for its treatment and \(H_i\) for its history. In tree \(b\), let \(L_b(H)\) be the terminal leaf containing prediction history \(H\), and let \(S_b\) be the tree's bootstrap sample, counting repeated sampled observations with their multiplicities. A simple tree-level probability is

$$ \hat e_b(H)= \frac{\sum_{i\in S_b} I(H_i\in L_b(H))A_i} {\sum_{i\in S_b} I(H_i\in L_b(H))}. $$

The forest probability is \(\hat e(H)=B^{-1}\sum_{b=1}^B\hat e_b(H)\), where \(B\) is the number of trees. Implementations can use different weighting or tree-building details, but the essential object for inverse weighting is a number between 0 and 1 that estimates the conditional class probability. A reported hard class such as “treated” or “untreated” is not enough.

Result: If each tree prediction is a terminal-leaf treatment fraction and the forest averages those fractions, then \(0\leq\hat e(H)\leq1\). More generally, if \(\hat e(H)=\sum_i\alpha_i(H)A_i\), with \(\alpha_i(H)\geq0\) and \(\sum_i\alpha_i(H)=1\), then \(\hat e(H)\) is a convex combination of the observed treatment labels.

Proof. Each terminal-leaf fraction has a numerator between zero and its denominator, so \(\hat e_b(H)\in[0,1]\). The average of \(B\) numbers in \([0,1]\) is also in \([0,1]\). In the weighted form, \(A_i\in\{0,1\}\) implies \(0\leq\alpha_i(H)A_i\leq\alpha_i(H)\). Summing gives \(0\leq\sum_i\alpha_i(H)A_i\leq\sum_i\alpha_i(H)=1\). \(\square\)

This bound is a mathematical property of the probability calculation, not evidence that the estimated probabilities are accurate. In particular, a forest's class vote fraction—how many trees classify \(H\) as treated—may differ from the average of the trees' treatment probabilities. Use class-probability predictions, not a thresholded class or an unexamined vote output.

Worked Example: Averaging Tree Probabilities

Suppose three trees give estimated probabilities of treatment for the same history \(H\) as \(0.20\), \(0.50\), and \(0.80\). The forest class-probability estimate is

$$ \hat e(H)=\frac{0.20+0.50+0.80}{3} =\frac{1.50}{3}=0.50. $$

If the person received treatment (\(A=1\)), the estimated probability of the treatment actually received is \(0.50\), and the unstabilized inverse probability weight is \(1/0.50=2\). If the person did not receive treatment (\(A=0\)), that probability is \(1-0.50=0.50\), so the weight is also \(1/0.50=2\). The same forest probability supplies both treatment-class probabilities, but the class used in the denominator depends on the observed treatment.

Turning Predictions into Inverse Weights

For each person, the denominator must be the estimated probability of the treatment that person actually received. Given \(\hat e_i=\hat e(H_i)\), define the estimated probability of the observed treatment as

$$ \hat p_i=A_i\hat e_i+(1-A_i)(1-\hat e_i), \qquad \hat W_i=\frac{1}{\hat p_i}. $$

When \(A_i=1\), this gives \(\hat p_i=\hat e_i\); when \(A_i=0\), it gives \(\hat p_i=1-\hat e_i\). A common error is to use \(\hat e_i\) in the denominator for everyone. That is correct only for treated observations.

Worked Example: Choosing the Probability for the Observed Treatment

A forest estimates \(\hat e(H_i)=0.72\) for a person who did not receive treatment. The probability of the observed treatment is \(1-0.72=0.28\), not \(0.72\). Therefore,

$$ \hat W_i=\frac{1}{0.28}\approx3.571. $$

For a treated person with the same history, the observed-treatment probability would instead be \(0.72\), giving a weight of \(1/0.72\approx1.389\). These differ because the observed treatment differs. Using \(1/0.72\) for the untreated person would put the wrong treatment probability in the denominator.

With treatment decisions at multiple times, a fitted probability is needed for each decision given the history available at that time. The denominator for a person's observed treatment sequence is the product of the corresponding observed-treatment probabilities. For example, if two observed treated decisions have fitted probabilities \(0.80\) and \(0.25\), their product is \(0.80(0.25)=0.20\), and the inverse product is \(1/0.20=5\). A small probability at even one decision can make the overall weight large.

Worked Example: A Two-Time Treatment Sequence

Suppose a person receives treatment at both times. At the first decision the forest gives \(\hat e_1=0.80\); at the second, based on the history available then, it gives \(\hat e_2=0.25\). The fitted probability of this observed sequence, using the product of its decision-specific probabilities, is

$$ 0.80\times0.25=0.20. $$

The inverse probability weight from this denominator is

$$ \frac{1}{0.20}=5. $$

This calculation uses the probability for the treatment actually received at each time. If the person had been untreated at the second decision instead, the second factor would be \(1-0.25=0.75\), making the sequence probability \(0.80(0.75)=0.60\) and its inverse \(1/0.60\approx1.667\).

Identification and the Role of the Probability Model

The causal justification for inverse weighting does not depend on using a forest. It depends on assumptions about treatment assignment, outcomes, and support, and on estimating the relevant probabilities adequately. For a single decision, let \(Y(a)\) be the potential outcome under treatment \(a\), and suppose \(H\) is measured before treatment.

Result: Suppose consistency holds, \(Y=Y(A)\); conditional exchangeability holds, \(Y(a)\perp A\mid H\); and positivity holds, \(P(A=a\mid H)>0\) almost surely for each treatment level of interest. If \(E[|Y(a)|]<\infty\), then \(E[I(A=a)Y/P(A=a\mid H)]=E[Y(a)]\).

Proof. Write \(e_a(H)=P(A=a\mid H)\). By iterated expectation,

$$ E\left[\frac{I(A=a)Y}{e_a(H)}\middle|H\right] = \frac{E[I(A=a)Y\mid H]}{e_a(H)}. $$

On the event \(A=a\), consistency gives \(Y=Y(a)\), so the numerator is \(E[I(A=a)Y(a)\mid H]\). Conditional exchangeability makes \(Y(a)\) independent of \(I(A=a)\) given \(H\), yielding

$$ E[I(A=a)Y(a)\mid H] = P(A=a\mid H)E[Y(a)\mid H] = e_a(H)E[Y(a)\mid H]. $$

Positivity permits division by \(e_a(H)>0\). Thus the conditional expectation of the weighted observed outcome is \(E[Y(a)\mid H]\); taking expectations gives \(E[Y(a)]\). The integrability assumption ensures the target mean exists, and under the stated identities the absolute expectation of the weighted term is finite. \(\square\)

In practice, the true \(e_a(H)\) is unknown and is replaced by a fitted probability. The identity therefore explains the goal of treatment-probability estimation; it does not guarantee that a particular finite-sample forest estimate recovers the target. A random forest is an estimation strategy, not a substitute for exchangeability, positivity, correct timing of covariates, or adequate probability estimation.

Diagnostics, Comparisons, and Failure Modes

Inspect both the probability estimates and the weights they generate. The following checks help reveal problems, although none proves that the treatment model is correct.

  • Check the observed-treatment probabilities. Tabulate or summarize \(\hat p_i\), separately by observed treatment. Values near zero imply large weights. Look at the largest weights and at how many observations contribute to the tail.
  • Check treatment support across histories. If a history region contains almost exclusively one treatment, the other class probability is weakly supported. A forest cannot create information about treatment choices absent from the data.
  • Compare predicted probabilities with observed treatment frequencies in groups. Large discrepancies can indicate poor probability prediction. Such a check is a diagnostic, not proof of causal validity; strong prediction metrics alone do not establish that the probabilities are suitable for weighting.
  • Distinguish training from out-of-bag predictions. Predictions for observations used to construct a tree can be overly optimistic. Out-of-bag predictions exclude an observation from the bootstrap sample for the trees used in its prediction and can help assess predictive performance. They do not, by themselves, establish causal identification or resolve all dependence and inference concerns.
  • Review the feature set and timing. Treatment predictors must be available before the decision. Including information measured after treatment can contaminate the assignment model and distort the weights.

Compared with logistic regression, a forest can represent nonlinearities and interactions without prescribing their exact form, but its behavior depends on tree depth, minimum leaf size, the number of candidate predictors, and other choices. A parametric model can be more stable when its form is reasonable and data are limited. Ridge or lasso logistic models impose a different form of regularization, while smoothing sparse treatment-history cells pools information across selected cells. None is universally superior: compare models using probability diagnostics and the resulting weight distribution, not classification accuracy alone.

A forest also does not automatically solve sparse-data or positivity problems. Very small leaves can yield highly variable treatment fractions; large leaves can conceal important differences in treatment assignment. Increasing the number of trees can reduce Monte Carlo variation in the forest average, but it cannot repair inadequate overlap or a systematically unsuitable model. Truncating large weights may reduce variance, but it changes the estimating procedure and can introduce bias; it should not be treated as evidence that the original support problem has disappeared.

Common Mistake

Pitfall: “The forest predicts the correct class for most people, so its probabilities must be good for inverse weighting.” This reasoning is wrong: class accuracy depends on a classification threshold and does not assess how close predicted probabilities are to the true treatment probabilities. A model can classify well while assigning probabilities that are too close to zero or one, creating unstable weights. Inspect class probabilities, observed-treatment probabilities, support, and weight tails directly.

Check Your Understanding

Work through each question, then compare with the solutions below.

  1. A forest estimates \(\hat e(H_i)=0.30\) for a treated person. What is the unstabilized inverse probability weight?
  2. A forest estimates \(\hat e(H_j)=0.85\) for an untreated person. Which probability belongs in the weight denominator, and what is the weight?
  3. Two trees predict probabilities \(0.40\) and \(0.70\). What is their averaged forest probability, and why is a hard class label not a substitute?
  4. State two reasons that a forest can produce unreliable inverse weights even if its classification accuracy is high.

Solutions. (1) The observed treatment probability is \(0.30\), so the weight is \(1/0.30=3.333\ldots\). (2) Use the untreated probability \(1-0.85=0.15\), giving weight \(1/0.15\approx6.667\). (3) The average is \((0.40+0.70)/2=0.55\). A hard class discards probability information and depends on a threshold, whereas weighting requires the probability of the treatment actually received. (4) For example, sparse support can leave one treatment nearly absent in some histories, and poorly chosen leaf sizes can yield unstable or oversmoothed probabilities. High classification accuracy does not rule out either problem.

Clinical Biostats