Quick Start Guide: Optimal Policy Trees with Categorical Treatment

This is a Python version of the corresponding OptimalTrees quick start guide.

In this guide we will give a demonstration of how to use Optimal Policy Trees with categoric treatment options. 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 policy 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:

import pandas as pd
df = pd.read_csv('crx.data', names=['A' + str(i) for i in range(1, 17)],
                 na_values='?').dropna()
    A1     A2      A3 A4 A5  A6  A7  ...  A10 A11 A12  A13    A14    A15  A16
0    b  30.83   0.000  u  g   w   v  ...    t   1   f    g  202.0      0    +
1    a  58.67   4.460  u  g   q   h  ...    t   6   f    g   43.0    560    +
2    a  24.50   0.500  u  g   q   h  ...    f   0   f    g  280.0    824    +
3    b  27.83   1.540  u  g   w   v  ...    t   5   t    g  100.0      3    +
4    b  20.17   5.625  u  g   w   v  ...    f   0   f    s  120.0      0    +
5    b  32.08   4.000  u  g   m   v  ...    f   0   t    g  360.0      0    +
6    b  33.17   1.040  u  g   r   h  ...    f   0   t    g  164.0  31285    +
..  ..    ...     ... .. ..  ..  ..  ...  ...  ..  ..  ...    ...    ...  ...
683  b  36.42   0.750  y  p   d   v  ...    f   0   f    g  240.0      3    -
684  b  40.58   3.290  u  g   m   v  ...    f   0   t    s  400.0      0    -
685  b  21.08  10.085  y  p   e   h  ...    f   0   f    g  260.0      0    -
686  a  22.67   0.750  u  g   c   v  ...    t   2   t    g  200.0    394    -
687  a  25.25  13.500  y  p  ff  ff  ...    t   1   t    g  200.0      1    -
688  b  17.92   0.205  u  g  aa   v  ...    f   0   f    g  280.0    750    -
689  b  35.00   3.375  u  g   c   h  ...    f   0   t    g    0.0      0    -

[653 rows x 16 columns]

Policy trees are trained using a features matrix/dataframe X as usual and a rewards matrix that has one column for each potential treatment that contains the outcome for each sample under that treatment.

There are two ways to get this rewards matrix:

  • in rare cases, the problem may have full information about the outcome associated with each treatment for each sample
  • more commonly, we have observational data, and use this partial data to train models to estimate the outcome associated with each treatment

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

In this case, the dataset is observational, and so we will use RewardEstimation to estimate our rewards matrix.

Reward Estimation

First, we split into training and testing:

from interpretableai import iai

X = df.drop(columns=['A1', 'A16'])
treatments = df.A1
outcomes = df.A16 == '+'

for col in X.columns:
    if X.dtypes[col] == 'object':
        X[col] = X[col].astype('category')

(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 so that we save more data for testing to ensure high-quality reward estimation on the test set.

The treatment, A1, is a categoric variable, so we follow the process for estimating rewards with categorical treatments.

Our outcome is binary, so 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:

reward_lnr = iai.CategoricalClassificationRewardEstimator(
    propensity_estimator=iai.RandomForestClassifier(),
    outcome_estimator=iai.RandomForestClassifier(),
    reward_estimator='doubly_robust',
    random_seed=123,
)
train_predictions, train_reward_score = reward_lnr.fit_predict(
    train_X, train_treatments, train_outcomes,
    propensity_score_criterion='auc', outcome_score_criterion='auc')
train_rewards = train_predictions['reward']
            a         b
0    1.160987  1.000000
1    0.510000  1.002207
2    0.410000  1.291632
3    0.590000  1.082920
4    1.211502  0.780000
5    0.310000  1.098134
6    0.700000  1.052259
..        ...       ...
319  0.150000 -0.055007
320 -0.094378  0.190000
321 -0.037992  0.062000
322  0.060000 -0.044416
323  0.240000 -0.152880
324  0.180000 -0.157814
325 -2.972605  0.280000

[326 rows x 2 columns]
train_reward_score
{'propensity': 0.612972582972583, 'outcome': {'b': 0.919700269175269, 'a': 0.9557617382617382}}

We can see that the internal outcome estimation models have AUCs of 0.92 and 0.96, 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.

Optimal Policy Trees

Now that we have a complete rewards matrix, we can train a tree to learn an optimal prescription policy that maximizes credit approval outcome. We will use a GridSearch to fit an OptimalTreePolicyMaximizer (note that if we were trying to minimize the outcomes, we would use OptimalTreePolicyMinimizer):

grid = iai.GridSearch(
    iai.OptimalTreePolicyMaximizer(
        random_seed=121,
        max_categoric_levels_before_warning=20,
    ),
    max_depth=range(6),
)
grid.fit(train_X, train_rewards)
grid.get_learner()
Optimal Trees Visualization

The resulting tree recommends different values for A1 based on other characteristics: namely, the values of A6 and A8. The intensity of the color in each leaf shows the difference in quality between the best and second-best values. We can see that both a and b values are prescribed by the tree, implying each treatment is better suited to certain subgroups.

We can make treatment prescriptions using predict:

grid.predict(train_X)
['b', 'b', 'b', 'b', 'a', 'b', 'b', 'b', 'a', 'a', 'b', 'b', 'b', 'a', 'b', 'a', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'b', 'b', 'a', 'b', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'a', 'b', 'a', 'b', 'b', 'a', 'b', 'b', 'a', 'a', 'b', 'b', 'a', 'b', 'b', 'a', 'b', 'a', 'b', 'a', 'a', 'b', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'a', 'b', 'b', 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'b', 'b', 'b', 'b', 'b', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'a', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'b', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'b', 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'b', 'a', 'a', 'a', 'b', 'b', 'a', 'b', 'b', 'a', 'a', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'a', 'b', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'b', 'a', 'b', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'a', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'b', 'b', 'b', 'b', 'b', 'a', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'b', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'a', 'b', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'b', 'a', 'b', 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'a', 'b', 'b']

If we want more information about the relative performance of treatments for these points, we can predict the full treatment ranking with predict_treatment_rank:

rank = grid.predict_treatment_rank(train_X)
array([['b', 'a'],
       ['b', 'a'],
       ['b', 'a'],
       ...,
       ['a', 'b'],
       ['b', 'a'],
       ['b', 'a']], dtype='<U1')

To quantify the difference in performance behind the treatment rankings, we can use predict_treatment_outcome to extract the estimated quality of each treatment for each point:

grid.predict_treatment_outcome(train_X)
            a         b
0    0.408147  0.745190
1    0.408147  0.745190
2    0.408147  0.745190
3    0.408147  0.745190
4    0.425737  0.261134
5    0.408147  0.745190
6    0.408147  0.745190
..        ...       ...
319  0.128606  0.432777
320  0.425737  0.261134
321  0.425737  0.261134
322  0.425737  0.261134
323  0.886280  0.693037
324  0.128606  0.432777
325  0.408147  0.745190

[326 rows x 2 columns]

We can also extract the standard errors of the outcome estimates with predict_treatment_outcome_standard_error:

grid.predict_treatment_outcome_standard_error(train_X)
            a         b
0    0.078682  0.045202
1    0.078682  0.045202
2    0.078682  0.045202
3    0.078682  0.045202
4    0.073415  0.044538
5    0.078682  0.045202
6    0.078682  0.045202
..        ...       ...
319  0.091610  0.080406
320  0.073415  0.044538
321  0.073415  0.044538
322  0.073415  0.044538
323  0.128199  0.116030
324  0.091610  0.080406
325  0.078682  0.045202

[326 rows x 2 columns]

These standard errors can be combined with the outcome estimates to construct confidence intervals in the usual way.

Evaluating Optimal Policy Trees

It is critical for a fair evaluation that we do not evaluate the quality of the policy using rewards from our existing reward estimator trained on the training set. This is to avoid any information from the training set leaking through to the out-of-sample evaluation.

Instead, what we need to do is to estimate a new set of rewards using only the test set, and evaluate the policy against these rewards:

test_predictions, test_reward_score = reward_lnr.fit_predict(
    test_X, test_treatments, test_outcomes, propensity_score_criterion='auc',
    outcome_score_criterion='auc')
test_rewards = test_predictions['reward']
            a         b
0    0.598000  1.168277
1    1.194955  0.706667
2    0.730000  1.171689
3    0.820000  1.080724
4    0.740000  1.061086
5    0.620000  1.197214
6    0.820000  1.013302
..        ...       ...
320 -0.095825  0.380000
321  0.030000 -0.003974
322  0.040000 -0.029723
323  0.137500 -0.011404
324 -0.907834  0.110000
325  0.100000 -0.143617
326  0.256667 -0.164480

[327 rows x 2 columns]
test_reward_score
{'propensity': 0.6226425120772946, 'outcome': {'b': 0.9240666100198265, 'a': 0.9219693500943501}}

We see the scores are similar to those on the training set, giving us confidence that the estimated rewards are a fair reflection of reality, and will serve as a good basis for evaluation. The AUC for the propensity model is again on the low side, but should be compensated for by means of the doubly-robust estimator.

We can now evaluate the quality using these new estimated rewards. First, we will calculate the average predicted credit approval probability under the treatments prescribed by the tree for the test set. To do this, we use predict_outcomes which uses the model to make prescriptions and looks up the predicted outcomes under these prescriptions:

policy_outcomes = grid.predict_outcomes(test_X, test_rewards)
array([ 0.598     ,  0.70666667,  1.17168947, ...,  0.11      ,
        0.1       , -0.16448008])

We can then get the average estimated approval probability under our treatments:

policy_outcomes.mean()
np.float64(0.41119889912148133)

We can compare this number to the average approval probability under the treatment assignments that were actually observed:

import numpy as np
np.mean([test_rewards[test_treatments[i]][i]
         for i in range(len(test_treatments))])
np.float64(0.4074034869644)

We see this policy is about the same as the real treatments.