Quick Start Guide: Optimal Prescriptive Trees

In this guide we will give a demonstration of how to use Optimal Prescriptive Trees (OPT). For this example, we will use the Credit Approval dataset, where the task is to predict the approval outcome of credit card application. Because the features have been anonymized for confidentiality reasons, we will arbitrarily select one of the categoric variables A1 to be the treatment.

Note: this case is not intended to serve as a practical application of prescriptive trees, but rather to serve as an illustration of the training and evaluation process.

First we load in the data and drop 37 rows with missing values:

using CSV, DataFrames
df = CSV.read("crx.data", DataFrame, header=string.("A", 1:16),
              missingstring="?", pool=true)
dropmissing!(df)
653×16 DataFrame
 Row │ A1      A2       A3       A4      A5      A6      A7      A8       A9   ⋯
     │ String  Float64  Float64  String  String  String  String  Float64  Stri ⋯
─────┼──────────────────────────────────────────────────────────────────────────
   1 │ b         30.83    0.0    u       g       w       v         1.25   t    ⋯
   2 │ a         58.67    4.46   u       g       q       h         3.04   t
   3 │ a         24.5     0.5    u       g       q       h         1.5    t
   4 │ b         27.83    1.54   u       g       w       v         3.75   t
   5 │ b         20.17    5.625  u       g       w       v         1.71   t    ⋯
   6 │ b         32.08    4.0    u       g       m       v         2.5    t
   7 │ b         33.17    1.04   u       g       r       h         6.5    t
   8 │ a         22.92   11.585  u       g       cc      v         0.04   t
  ⋮  │   ⋮        ⋮        ⋮       ⋮       ⋮       ⋮       ⋮        ⋮       ⋮  ⋱
 647 │ b         36.42    0.75   y       p       d       v         0.585  f    ⋯
 648 │ b         40.58    3.29   u       g       m       v         3.5    f
 649 │ b         21.08   10.085  y       p       e       h         1.25   f
 650 │ a         22.67    0.75   u       g       c       v         2.0    f
 651 │ a         25.25   13.5    y       p       ff      ff        2.0    f    ⋯
 652 │ b         17.92    0.205  u       g       aa      v         0.04   f
 653 │ b         35.0     3.375  u       g       c       h         8.29   f
                                                  8 columns and 638 rows omitted

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 the variable A1, and the outcome is whether the application was approved (which we are trying to make as likely as possible):

X = select(df, Not([:A16, :A1]))
treatments = df.A1
outcomes = df.A16 .== "+"

We can now split into training and test datasets:

(train_X, train_treatments, train_outcomes), (test_X, test_treatments, test_outcomes) =
    IAI.split_data(:policy_maximize, X, treatments, outcomes, seed=123, train_proportion=0.5)

Note that we have used a training/test split of 50%/50%, so that we 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=0.6,
        treatment_minbucket=10,
        random_seed=123,
        max_categoric_levels_before_warning=15
    ),
    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=0.6 to strike a balance between maximizing the outcome and estimating the approval probability, 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. Because the variables are anonymized, we cannot directly interpret the tree, but we can see that both treatments are prescribed based on a variety of the features.

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
327-element Vector{String}:
 "a"
 "b"
 "b"
 "b"
 "a"
 "a"
 "b"
 "a"
 "b"
 "b"
 ⋮
 "b"
 "b"
 "b"
 "b"
 "b"
 "b"
 "b"
 "b"
 "b"
pred_outcomes
327-element Vector{Float64}:
 0.9090909090909091
 0.75
 0.75
 0.75
 0.8
 0.9090909090909091
 0.935483870967742
 0.9090909090909091
 1.0
 0.935483870967742
 ⋮
 0.07894736842105265
 0.07894736842105265
 0.07894736842105265
 0.07894736842105265
 0.07894736842105265
 0.07894736842105265
 0.07894736842105265
 0.07894736842105265
 0.07894736842105265

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

IAI.predict_outcomes(grid, test_X)
327×2 DataFrame
 Row │ a         b
     │ Float64   Float64
─────┼─────────────────────
   1 │ 0.909091  0.416667
   2 │ 0.0       0.75
   3 │ 0.0       0.75
   4 │ 0.0       0.75
   5 │ 0.8       0.28
   6 │ 0.909091  0.416667
   7 │ 1.0       0.935484
   8 │ 0.909091  0.416667
  ⋮  │    ⋮          ⋮
 321 │ 0.0       0.0789474
 322 │ 0.0       0.0789474
 323 │ 0.0       0.0789474
 324 │ 0.0       0.0789474
 325 │ 0.0       0.0789474
 326 │ 0.0       0.0789474
 327 │ 0.0       0.0789474
           312 rows omitted

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 use a CategoricalClassificationRewardEstimator to estimate the approval outcome under each option with a doubly-robust reward estimation method using random forests to estimate both propensity scores and outcomes. 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.CategoricalClassificationRewardEstimator(
    propensity_estimator=IAI.RandomForestClassifier(),
    outcome_estimator=IAI.RandomForestClassifier(),
    reward_estimator=:doubly_robust,
    random_seed=123,
)
test_predictions, test_reward_score = IAI.fit_predict!(
    reward_lnr, test_X, test_treatments, test_outcomes,
    propensity_score_criterion=:auc, outcome_score_criterion=:auc)
test_rewards = test_predictions[:reward]
327×2 DataFrame
 Row │ a          b
     │ Float64    Float64
─────┼────────────────────────
   1 │  0.598      1.17681
   2 │  1.19976    0.706667
   3 │  0.74       1.15996
   4 │  0.82       1.0655
   5 │  0.71       1.06225
   6 │  0.62       1.18302
   7 │  0.82       1.01578
   8 │  0.7        1.30378
  ⋮  │     ⋮           ⋮
 321 │ -0.114177   0.37
 322 │  0.03      -0.00396172
 323 │  0.04      -0.0341455
 324 │  0.1375    -0.00987617
 325 │ -1.07301    0.1
 326 │  0.1       -0.166343
 327 │  0.266667  -0.162051
              312 rows omitted
test_reward_score
Dict{Symbol, Any} with 2 entries:
  :propensity => 0.617077
  :outcome    => Dict("b"=>0.92533, "a"=>0.91986)

We can see that the internal outcome estimation models have AUCs of 0.92, which gives us confidence that the reward estimates are of decent quality, and good to base our training on. The AUC for the propensity model is lower at 0.61, which is not particularly high, and suggests difficulty in reliably estimating the propensity. The doubly-robust estimation method should help to alleviate this problem, as it is designed to deliver good results if either propensity scores or outcomes are estimated well. However, we should always pay attention to these scores and proceed with caution if the estimation quality is low.

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(test_rewards[i, pred_treatments[i]] for i = 1:size(test_X, 1))
0.4363779824788129

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(test_rewards[i, test_treatments[i]] for i = 1:size(test_X, 1))
0.40820588831952376

We can see that the prescriptive tree policy indeed improves on the actual assignments.