Estimating Business Value for Multiclass Classification

This tutorial explains how to use NannyML to estimate business value for multiclass classification models in the absence of target data. To find out how CBPE estimates performance metrics, read the explanation of Confidence-based Performance Estimation.

Note

The following example uses timestamps. These are optional but have an impact on the way data is chunked and results are plotted. You can read more about them in the data requirements.

Just The Code

>>> import nannyml as nml
>>> from IPython.display import display

>>> reference_df, analysis_df, _ = nml.load_synthetic_multiclass_classification_dataset()

>>> display(reference_df.head(3))

>>> # matrix can be provided as a list of lists or a numpy array
>>> business_value_matrix = [
...     [1, 0, -1],
...     [0, 1, 0],
...     [-1, 0, 1]
>>> ]
>>> estimator = nml.CBPE(
...     y_pred_proba={
...         'prepaid_card': 'y_pred_proba_prepaid_card',
...         'highstreet_card': 'y_pred_proba_highstreet_card',
...         'upmarket_card': 'y_pred_proba_upmarket_card'},
...     y_pred='y_pred',
...     y_true='y_true',
...     timestamp_column_name='timestamp',
...     problem_type='classification_multiclass',
...     metrics=['business_value'],
...     business_value_matrix=business_value_matrix,
...     normalize_business_value="per_prediction",
...     chunk_size=6000,
>>> )

>>> estimator.fit(reference_df)

>>> results = estimator.estimate(analysis_df)
>>> display(results.filter(period='analysis').to_df())

>>> metric_fig = results.plot()
>>> metric_fig.show()

Walkthrough

For simplicity this guide is based on a synthetic dataset where the monitored model predicts which type of credit card product new customers should be assigned to. Check out Credit Card Dataset to learn more about this dataset.

In order to monitor a model, NannyML needs to learn about it from a reference dataset. Then it can monitor the data that is subject to actual analysis, provided as the analysis dataset. You can read more about this in our section on data periods.

We start by loading the dataset we’ll be using:

>>> import nannyml as nml
>>> from IPython.display import display

>>> reference_df, analysis_df, _ = nml.load_synthetic_multiclass_classification_dataset()

>>> display(reference_df.head(3))

id

acq_channel

app_behavioral_score

requested_credit_limit

app_channel

credit_bureau_score

stated_income

is_customer

timestamp

y_pred_proba_prepaid_card

y_pred_proba_highstreet_card

y_pred_proba_upmarket_card

y_pred

y_true

0

0

Partner3

1.80823

350

web

309

15000

True

2020-05-02 02:01:30

0.97

0.03

0

prepaid_card

prepaid_card

1

1

Partner2

4.38257

500

mobile

418

23000

True

2020-05-02 02:03:33

0.87

0.13

0

prepaid_card

prepaid_card

2

2

Partner2

-0.787575

400

web

507

24000

False

2020-05-02 02:04:49

0.47

0.35

0.18

prepaid_card

upmarket_card

Next we create the Confidence-based Performance Estimation (CBPE) estimator. To initialize an estimator that estimates business_value, we specify the following parameters:

  • y_pred_proba: the name of the column in the reference data that contains the predicted probabilities.

  • y_pred: the name of the column in the reference data that contains the predicted classes.

  • y_true: the name of the column in the reference data that contains the true classes.

  • timestamp_column_name (Optional): the name of the column in the reference data that contains timestamps.

  • metrics: a list of metrics to estimate. In this example we will estimate the business_value metric.

  • chunk_size (Optional): the number of observations in each chunk of data used to estimate performance. For more information about chunking configurations check out the chunking tutorial.

  • problem_type: the type of problem being monitored. In this example we will monitor a multiclass classification problem.

  • business_value_matrix: A matrix that specifies the value of each corresponding cell in the confusion matrix.

  • normalize_business_value (Optional): how to normalize the business value. The normalization options are:

    • None : returns the total value per chunk

    • “per_prediction” : returns the total value for the chunk divided by the number of observations in a given chunk.

  • thresholds (Optional): the thresholds used to calculate the alert flag. For more information about thresholds, check out the thresholds tutorial.

Note

When calculating business_value, the business_value_matrix parameter is required. A business value matrix is a nxn matrix that specifies the value of each cell in the confusion matrix. The format of the business value matrix must be specified so that each element represents the business value of it’s respective confusion matrix element. Hence the element on the i-th row and j-column of the business value matrix tells us the value of the i-th target when we have predicted the j-th value. The target values that each column and row refer are sorted alphanumerically for both the confusion matrix and the business value matrices.

The business value matrix can be provided as a list of lists or a numpy array. For more information about the business value matrix, check out the Business Value “How it Works” page.

>>> # matrix can be provided as a list of lists or a numpy array
>>> business_value_matrix = [
...     [1, 0, -1],
...     [0, 1, 0],
...     [-1, 0, 1]
>>> ]
>>> estimator = nml.CBPE(
...     y_pred_proba={
...         'prepaid_card': 'y_pred_proba_prepaid_card',
...         'highstreet_card': 'y_pred_proba_highstreet_card',
...         'upmarket_card': 'y_pred_proba_upmarket_card'},
...     y_pred='y_pred',
...     y_true='y_true',
...     timestamp_column_name='timestamp',
...     problem_type='classification_multiclass',
...     metrics=['business_value'],
...     business_value_matrix=business_value_matrix,
...     normalize_business_value="per_prediction",
...     chunk_size=6000,
>>> )

The CBPE estimator is then fitted using the fit() method on the reference data.

>>> estimator.fit(reference_df)

The fitted estimator can be used to estimate performance on other data, for which performance cannot be calculated. Typically, this would be used on the latest production data where target is missing. In our example this is the analysis_df data.

NannyML can then output a dataframe that contains all the results. Let’s have a look at the results for analysis period only.

>>> results = estimator.estimate(analysis_df)
>>> display(results.filter(period='analysis').to_df())

chunk
key
chunk_index
start_index
end_index
start_date
end_date
period
business_value
value
sampling_error
realized
upper_confidence_boundary
lower_confidence_boundary
upper_threshold
lower_threshold
alert

0

[0:5999]

0

0

5999

2020-09-01 03:10:01

2020-09-13 16:15:10

analysis

2.00862

0.00804747

nan

2.03276

1.98448

2.05032

1.9632

False

1

[6000:11999]

1

6000

11999

2020-09-13 16:15:32

2020-09-25 19:48:42

analysis

2.01671

0.00804747

nan

2.04085

1.99257

2.05032

1.9632

False

2

[12000:17999]

2

12000

17999

2020-09-25 19:50:04

2020-10-08 02:53:47

analysis

2.02515

0.00804747

nan

2.04929

2.00101

2.05032

1.9632

False

3

[18000:23999]

3

18000

23999

2020-10-08 02:57:34

2020-10-20 15:48:19

analysis

2.01893

0.00804747

nan

2.04307

1.99479

2.05032

1.9632

False

4

[24000:29999]

4

24000

29999

2020-10-20 15:49:06

2020-11-01 22:04:40

analysis

2.00652

0.00804747

nan

2.03066

1.98238

2.05032

1.9632

False

5

[30000:35999]

5

30000

35999

2020-11-01 22:04:59

2020-11-14 03:55:33

analysis

1.56444

0.00804747

nan

1.58858

1.5403

2.05032

1.9632

True

6

[36000:41999]

6

36000

41999

2020-11-14 03:55:49

2020-11-26 09:19:06

analysis

1.56846

0.00804747

nan

1.5926

1.54432

2.05032

1.9632

True

7

[42000:47999]

7

42000

47999

2020-11-26 09:19:22

2020-12-08 14:33:56

analysis

1.56204

0.00804747

nan

1.58618

1.5379

2.05032

1.9632

True

8

[48000:53999]

8

48000

53999

2020-12-08 14:34:25

2020-12-20 18:30:30

analysis

1.56687

0.00804747

nan

1.59101

1.54272

2.05032

1.9632

True

9

[54000:59999]

9

54000

59999

2020-12-20 18:31:09

2021-01-01 22:57:55

analysis

1.57425

0.00804747

nan

1.59839

1.55011

2.05032

1.9632

True

Apart from chunk-related data, the results data have the following columns for each metric that was estimated:

  • value - the estimate of a metric for a specific chunk.

  • sampling_error - the estimate of the sampling error.

  • realized - when target values are available for a chunk, the realized performance metric will also be calculated and included within the results.

  • upper_confidence_boundary and lower_confidence_boundary - These values show the confidence band of the relevant metric and are equal to estimated value +/- 3 times the estimated sampling error.

  • upper_threshold and lower_threshold - crossing these thresholds will raise an alert on significant performance change. The thresholds are calculated based on the actual performance of the monitored model on chunks in the reference partition. The thresholds are 3 standard deviations away from the mean performance calculated on the reference chunks. The thresholds are calculated during fit phase.

  • alert - flag indicating potentially significant performance change. True if estimated performance crosses upper or lower threshold.

These results can be also plotted. Our plots contains several key elements.

  • The purple dashed step plot shows the estimated performance in each chunk of the provided data. Thick squared point markers indicate the middle of these chunks.

  • The black vertical line splits the reference and analysis periods.

  • The low-saturated purple area around the estimated performance in the analysis period corresponds to the confidence band which is calculated as the estimated performance +/- 3 times the estimated Sampling Error.

  • The red horizontal dashed lines show upper and lower thresholds that indicate the range of expected performance values.

  • The red diamond-shaped point markers in the middle of a chunk indicate that an alert has been raised. Alerts are caused by the estimated performance crossing the upper or lower threshold.

>>> metric_fig = results.plot()
>>> metric_fig.show()
../../../_images/business_value1.svg

Additional information such as the chunk index range and chunk date range (if timestamps were provided) is shown in the hover for each chunk (these are interactive plots, though only static views are included here).

Insights

After reviewing the performance estimation results, we should be able to see any indications of performance change that NannyML has detected based upon the model’s inputs and outputs alone.

What’s next

The Data Drift functionality can help us to understand whether data drift is causing the performance problem. When the target values become available we can compared realized and estimated business value results.