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=1, train_proportion=0.6))

Note that we have used a training/test split of 60%/40% split, 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.

We use a CategoricalRewardEstimator 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.CategoricalRewardEstimator(
    propensity_estimator=iai.RandomForestClassifier(),
    outcome_estimator=iai.RandomForestClassifier(),
    reward_estimator='doubly_robust',
    random_seed=1,
)

train_rewards, train_reward_score = reward_lnr.fit_predict(
    train_X, train_treatments, train_outcomes, propensity_score_criterion='auc', outcome_score_criterion='auc')
train_rewards
            a         b
0    0.507297  1.127044
1    1.430301  0.717781
2    0.917500  1.237986
3    0.438333  1.273896
4    0.667778  1.045952
5    2.099623  0.712232
6    0.393333  1.883439
..        ...       ...
385 -0.027726  0.121492
386 -0.189727  0.161638
387  0.217105 -0.029932
388  0.081190 -0.085876
389  0.148122 -0.006361
390 -0.215015  0.127157
391  0.400429 -0.041824

[392 rows x 2 columns]
train_reward_score
{'propensity': 0.616021451315569, 'outcome': {'b': 0.9134508412707142, 'a': 0.9431853767147885}}

We can see that the internal outcome estimation models have AUCs of 0.91 and 0.94, 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.62, 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=123,
        max_categoric_levels_before_warning=20,
    ),
    max_depth=range(6),
)
grid.fit(train_X, train_rewards)
grid.get_learner()
Fitted OptimalTreePolicyMaximizer:
  1) Split: A6 in [aa,e,i,k,m,q,r,x] or is missing
    2) Split: A14 < 162
      3) Prescribe: a, 101 points, error 3.475
      4) Prescribe: b, 89 points, error 3.721
    5) Split: A2 < 28.63
      6) Prescribe: b, 95 points, error 3.813
      7) Prescribe: a, 107 points, error 3.706
Optimal Trees Visualization

The resulting tree recommends different values for A1 based on other characteristics: namely, the values of A2, A6, and A14. 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)
['a', 'b', 'b', 'b', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'a', 'a', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'a', 'b', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'a', 'b', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'b', 'a', 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'b', 'a', 'a', 'a', 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'a', 'b', 'a', 'b', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'b', 'b', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'b', 'b', 'a', 'b', 'b', 'a', 'b', 'a', 'b', 'a', 'a', 'a', 'a', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'a', 'b', 'b', 'b', 'a', 'b', 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'b', 'a', 'b', 'a', 'a', 'b', 'a', 'a', 'a', 'b', 'a', 'a', 'b', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'a', 'b', 'b', 'a', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'b', 'a', 'b', 'a', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'a', 'b', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'a', 'b', 'b', 'a', 'a', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'a', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'b', 'a', 'a', 'a', 'b', 'a', 'b', 'a', 'a', 'b', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'a', 'b', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'a', 'b', 'b', 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'b', 'a', 'b', 'b', 'a', 'b', 'a', 'a', 'a', 'b', 'a', 'b', 'b', 'b', 'a', 'a', 'b', 'b', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'a', 'b', 'b', 'a']

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)
rank[0:10]
array([['a', 'b'],
       ['b', 'a'],
       ['b', 'a'],
       ['b', 'a'],
       ['a', 'b'],
       ['a', 'b'],
       ['a', 'b'],
       ['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.510482  0.352249
1    0.317545  0.495565
2    0.317545  0.495565
3    0.317545  0.495565
4    0.510482  0.352249
5    0.741216  0.561335
6    0.741216  0.561335
..        ...       ...
385  0.251901  0.402943
386  0.251901  0.402943
387  0.317545  0.495565
388  0.741216  0.561335
389  0.317545  0.495565
390  0.251901  0.402943
391  0.510482  0.352249

[392 rows x 2 columns]

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_rewards, test_reward_score = reward_lnr.fit_predict(
    test_X, test_treatments, test_outcomes, propensity_score_criterion='auc',
    outcome_score_criterion='auc')
test_rewards
            a         b
0    1.058741  0.958918
1    0.694710  1.040429
2    0.538095  1.471800
3    0.499545  1.296553
4    2.211119  0.588636
5    0.230000  1.279023
6    0.324085  1.082338
..        ...       ...
254 -1.245095  0.368800
255  0.203788 -0.031297
256  0.327500 -0.028093
257  0.068333 -0.008562
258  0.202500 -0.024512
259 -1.531257  0.369921
260  0.112667 -0.215225

[261 rows x 2 columns]
test_reward_score
{'propensity': 0.5759205706574128, 'outcome': {'b': 0.9409791341792622, 'a': 0.9158503401360545}}

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 credict 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)
policy_outcomes[0:10]
array([1.05874091, 1.04042909, 1.47179994, 1.29655277, 0.58863636,
       1.27902272, 0.32408541, 1.50441067, 1.08252732, 1.10613036])

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

policy_outcomes.mean()
0.4443510311691845

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))])
0.42341831736732

We see a small improvement over the real treatments.