Quick Start Guide: Optimal Prescriptive Trees

In this example we will give a demonstration of how to use Optimal Prescriptive Trees (OPT). We will examine the impact of job training on annual earnings using the Lalonde sample from the National Supported Work Demonstration dataset.

First we load in the data:

using CSV, DataFrames
colnames = [:treatment, :age, :education, :black, :hispanic, :married,
            :nodegree, :earnings_1975, :earnings_1978]
df_control = DataFrame(CSV.File("nsw_control.txt", header=colnames, delim=' ',
                                ignorerepeated=true))
df_treated = DataFrame(CSV.File("nsw_treated.txt", header=colnames, delim=' ',
                                ignorerepeated=true))
df = [df_control; df_treated]
722×9 DataFrame. Omitted printing of 3 columns
│ Row │ treatment │ age     │ education │ black   │ hispanic │ married │
│     │ Float64   │ Float64 │ Float64   │ Float64 │ Float64  │ Float64 │
├─────┼───────────┼─────────┼───────────┼─────────┼──────────┼─────────┤
│ 1   │ 0.0       │ 23.0    │ 10.0      │ 1.0     │ 0.0      │ 0.0     │
│ 2   │ 0.0       │ 26.0    │ 12.0      │ 0.0     │ 0.0      │ 0.0     │
│ 3   │ 0.0       │ 22.0    │ 9.0       │ 1.0     │ 0.0      │ 0.0     │
│ 4   │ 0.0       │ 34.0    │ 9.0       │ 1.0     │ 0.0      │ 0.0     │
│ 5   │ 0.0       │ 18.0    │ 9.0       │ 1.0     │ 0.0      │ 0.0     │
│ 6   │ 0.0       │ 45.0    │ 11.0      │ 1.0     │ 0.0      │ 0.0     │
│ 7   │ 0.0       │ 18.0    │ 9.0       │ 1.0     │ 0.0      │ 0.0     │
⋮
│ 715 │ 1.0       │ 26.0    │ 10.0      │ 1.0     │ 0.0      │ 0.0     │
│ 716 │ 1.0       │ 25.0    │ 14.0      │ 1.0     │ 0.0      │ 1.0     │
│ 717 │ 1.0       │ 26.0    │ 10.0      │ 1.0     │ 0.0      │ 1.0     │
│ 718 │ 1.0       │ 20.0    │ 9.0       │ 1.0     │ 0.0      │ 0.0     │
│ 719 │ 1.0       │ 31.0    │ 4.0       │ 1.0     │ 0.0      │ 0.0     │
│ 720 │ 1.0       │ 24.0    │ 10.0      │ 1.0     │ 0.0      │ 1.0     │
│ 721 │ 1.0       │ 33.0    │ 11.0      │ 1.0     │ 0.0      │ 1.0     │
│ 722 │ 1.0       │ 33.0    │ 12.0      │ 1.0     │ 0.0      │ 1.0     │

Data for prescriptive problems

Prescriptive trees are trained on observational data, and require three distinct types of data:

  • X: the features for each observation that can be used as the splits in the tree - this can be a matrix or a dataframe as for classification or regression problems
  • treatments: the treatment applied to each observation - this is a vector of the treatment labels similar to the target in a classification problem
  • outcomes: the outcome for each observation under the applied treatment - this is a vector of numeric values similar to the target in a regression problem

Refer to the documentation on data preparation for more information on the data format.

In this case, the treatment is whether or not the subject received job training, and the outcome is their 1978 earnings (which we are trying to maximize):

using DataFrames
X = df[:, 2:(end - 1)]
treatments = [t == 1 ? "training" : "no training" for t in df.treatment]
outcomes = df.earnings_1978

We can now split into training and test datasets:

(train_X, train_treatments, train_outcomes), (test_X, test_treatments, test_outcomes) =
    IAI.split_data(:prescription_maximize, X, treatments, outcomes, seed=2)

Note that we have used the default 70%/30% split, but in many prescriptive problems it is desirable to save more data for testing to ensure high-quality reward estimation on the test set.

Fitting Optimal Prescriptive Trees

We will use a GridSearch to fit an OptimalTreePrescriptionMaximizer (note that if we were trying to minimize the outcomes, we would use OptimalTreePrescriptionMinimizer):

grid = IAI.GridSearch(
    IAI.OptimalTreePrescriptionMaximizer(
        prescription_factor=1,
        treatment_minbucket=20,
        random_seed=234,
    ),
    max_depth=1:5,
)
IAI.fit!(grid, train_X, train_treatments, train_outcomes)
IAI.get_learner(grid)
Optimal Trees Visualization

Here, we have set prescription_factor=1 to focus the trees on maximizing the outcome, and treatment_minbucket=10 so that the tree can only prescribe a treatment in a leaf if there are at least 10 subjects in that leaf that received this treatment. This is to ensure that we have sufficient data on how the treatment affects subjects in this leaf before we can prescribe it.

In the resulting tree, the color in each leaf indicates which treatment is deemed to be stronger in this leaf, and the color intensity indicates the size of the difference. The tree contains some interesting insights about the effect of training, for example:

  • Node 17 is where the training had the weakest effect, which is for older subjects with high earnings in 1975. This seems to make sense, as these people are likely the least in need of training.
  • Node 10 shows that those with low 1975 earnings, at least 9 years of education, and at least 28 years old benefitted greatly from the training.
  • Nodes 6 through 8 show that for those with at least 9 year of education and 1975 earnings below $1103, the effectiveness of the training was highly linked to the age of the subject, with older subjects benefitting much more.

We can make predictions on new data using predict:

pred_treatments, pred_outcomes = IAI.predict(grid, test_X)

This returns the treatment prescribed for each subject as well as the outcome predicted for each subject under the prescribed treatment:

pred_treatments
216-element Array{String,1}:
 "training"
 "no training"
 "no training"
 "training"
 "training"
 "training"
 "no training"
 "training"
 "no training"
 "training"
 ⋮
 "no training"
 "training"
 "no training"
 "no training"
 "training"
 "training"
 "no training"
 "no training"
 "training"
pred_outcomes
216-element Array{Float64,1}:
  6837.618458181823
  6779.621136363639
  4697.901148148165
 11123.96342608695
  5669.543748484844
  6837.618458181823
  7402.351424999986
 11123.96342608695
  3288.4266500000012
 11123.96342608695
     ⋮
  9646.777769696972
  6837.618458181823
  9646.777769696972
  6779.621136363639
  6837.618458181823
  5669.543748484844
  9646.777769696972
  9646.777769696972
  6837.618458181823

You can also use predict_outcomes to get the predicted outcomes for all treatments:

IAI.predict_outcomes(grid, test_X)
216×2 DataFrame
│ Row │ no training │ training │
│     │ Float64     │ Float64  │
├─────┼─────────────┼──────────┤
│ 1   │ 3106.9      │ 6837.62  │
│ 2   │ 6779.62     │ 2578.71  │
│ 3   │ 4697.9      │ 2769.29  │
│ 4   │ 4754.78     │ 11124.0  │
│ 5   │ 3351.41     │ 5669.54  │
│ 6   │ 3106.9      │ 6837.62  │
│ 7   │ 7402.35     │ 3963.33  │
⋮
│ 209 │ 3106.9      │ 6837.62  │
│ 210 │ 9646.78     │ 6320.1   │
│ 211 │ 6779.62     │ 2578.71  │
│ 212 │ 3106.9      │ 6837.62  │
│ 213 │ 3351.41     │ 5669.54  │
│ 214 │ 9646.78     │ 6320.1   │
│ 215 │ 9646.78     │ 6320.1   │
│ 216 │ 3106.9      │ 6837.62  │

Evaluating Optimal Prescriptive Trees

In prescription problems, it is complicated to evaluate the quality of a prescription policy because our data only contains the outcome for the treatment that was received. Because we don't know the outcomes for the treatments that were not received (known as the counterfactuals), we cannot simply evaluate our prescriptions against the test set as we normally do.

A common approach to resolve this problem is reward estimation, where so-called rewards are estimated for each treatment for each observation. These rewards indicate the relative credit a model should be given for prescribing each treatment to each observation, and thus can be used to evaluate the quality of the prescription policy. For more details on how the reward estimation procedure is conducted, refer to the reward estimation documentation.

We will use a RewardEstimator to estimate the rewards (note that we are passing in the test data rather the training data to ensure we get a fair out-of-sample evaluation):

reward_lnr = IAI.RewardEstimator(
    propensity_estimation_method=:random_forest,
    outcome_estimation_method=:random_forest,
    reward_estimation_method=:doubly_robust,
    random_seed=1,
)
rewards = IAI.fit_predict!(reward_lnr, test_X, test_treatments, test_outcomes)
216×2 DataFrame
│ Row │ no training │ training │
│     │ Float64     │ Float64  │
├─────┼─────────────┼──────────┤
│ 1   │ 13466.1     │ 5840.71  │
│ 2   │ 2950.45     │ 5562.14  │
│ 3   │ 10780.6     │ 4377.65  │
│ 4   │ 17391.5     │ 5864.28  │
│ 5   │ -599.99     │ 5225.24  │
│ 6   │ 6346.63     │ 15981.1  │
│ 7   │ -847.699    │ 3445.86  │
⋮
│ 209 │ 7298.05     │ -1284.03 │
│ 210 │ 9441.84     │ 13602.5  │
│ 211 │ 2275.94     │ 10463.2  │
│ 212 │ 5524.17     │ -1373.69 │
│ 213 │ 4849.78     │ 4586.34  │
│ 214 │ 3339.22     │ -366.205 │
│ 215 │ 9404.63     │ 42383.0  │
│ 216 │ 3758.27     │ -4818.63 │

We can now use these reward values to evaluate the prescription in many ways. For example, we might like to see the mean reward achieved across all prescriptions on the test set:

using Statistics
mean(rewards[i, pred_treatments[i]] for i = 1:size(test_X, 1))
5963.83953539401

For comparison's sake, we can compare this to the mean reward achieved under the actual treatment assignments that were observed in the data:

mean(rewards[i, test_treatments[i]] for i = 1:size(test_X, 1))
5494.877142584506

We can see that the prescriptive tree policy indeed achieves better results than the actual assignments.