Model Insights

The Model Insights feature lets you monitor how the environment that your model operates within may be changing in ways that affect it’s predictions so that you can intervene (retrain) in an efficient and timely manner. Changes in the inputs, data drift, can occur due to errors in the data processing pipeline or due to changes in the environment such as user preference or behavior.

The validation framework performs per infererence range checks with count frequency based thresholds for alerts and is ideal for catching many errors in input and output data.

In complement to the validation framework model insights focuses on the differences in the distributions of data in a time based window measured against a baseline for a given pipeline and can detect situations where values are still within the expected range but the distribution has shifted. For example, if your model predicts housing prices you might expect the predictions to be between \$200,000 and \$1,000,000 with a distribution centered around \$400,000. If your model suddenly starts predicting prices centered around \$250,000 or \$750,000 the predictions may still be within the expected range but the shift may signal something has changed that should be investigated.

Ideally we’d also monitor the quality of the predictions, concept drift. However this can be difficult as true labels are often not available or are severely delayed in practice. That is there may be a signficant lag between the time the prediction is made and the true (sale price) value is observed.

Consequently, model insights uses data drift detection techniques on both inputs and outputs to detect changes in the distributions of the data.

There are many useful statistical tests for calculating the difference between distributions; however, they typically require assumptions about the underlying distributions or confusing and expensive calculations. We’ve implemented a data drift framework that is easy to understand, fast to compute, runs in an automated fashion and is extensible to many specific use cases.

The methodology currently revolves around calculating the specific percentile-based bins of the baseline distribution and measuring how future distributions fall into these bins. This approach is both visually intuitive and supports an easy to calculate difference score between distributions. Users can tune the scoring mechanism to emphasize different regions of the distribution: for example, you may only care if there is a change in the top 20th percentile of the distribution, compared to the baseline.

You can specify the inputs or outputs that you want to monitor and the data to use for your baselines. You can also specify how often you want to monitor distributions and set parameters to define what constitutes a meaningful change in a distribution for your application.

Once you’ve set up a monitoring task, called an assay, comparisons against your baseline are then run automatically on a scheduled basis. You can be notified if the system notices any abnormally different behavior. The framework also allows you to quickly investigate the cause of any unexpected drifts in your predictions.

The rest of this notebook will shows how to create assays to monitor your pipelines.

NOTE: model insights operates over time and is difficult to demo in a notebook without pre-canned data. We assume you have an active pipeline that has been running and making predictions over time and show you the code you may use to analyze your pipeline.

Workflow

Model Insights has the capability to perform interactive assays so that you can explore the data from a pipeline and learn how the data is behaving. With this information and the knowledge of your particular business use case you can then choose appropriate thresholds for persistent automatic assays as desired.

To get started lets import some libraries we’ll need.

[1]:
import matplotlib.pyplot as plt
import pandas as pd

import wallaroo
from wallaroo.assay_config import BinMode, Aggregation, Metric


plt.rcParams["figure.figsize"] = (12,6)
pd.options.display.float_format = '{:,.2f}'.format

Then lets get a handle to your existing running pipeline

[2]:
client = wallaroo.Client()

pipeline_name = 'mypipeline'
model_name = 'mymodel'

pipeline = client.pipelines_by_name(pipeline_name)[0]

We assume the pipeline has been running for a while and there is a period of time that is free of errors that we’d like to use as the baseline. Lets note the start and end times. For this example we have 30 days of data from Jan 2022 and well use Jan 1 data as our baseline.

[3]:
import datetime
baseline_start = datetime.datetime.fromisoformat('2022-01-01T00:00:00+00:00')
baseline_end = datetime.datetime.fromisoformat('2022-01-02T00:00:00+00:00')
last_day = datetime.datetime.fromisoformat('2022-02-01T00:00:00+00:00')

Lets create an assay using that pipeline and the model in the pipeline. We also specify the start end end of the baseline.

[4]:
assay_name = "example assay"
assay_builder = client.build_assay(assay_name, pipeline, model_name, baseline_start, baseline_end)

We don’t know much about our baseline data yet so lets examine the data and create a couple of visual representations. First lets get some basic stats on the baseline data.

[5]:
baseline_run = assay_builder.build().interactive_baseline_run()
baseline_run.baseline_stats()
[5]:
Baseline
count 1813
min 11.88
max 15.02
mean 13.02
median 13.00
std 0.46
start 2022-01-01T00:00:00Z
end 2022-01-02T00:00:00Z

Now lets look at a histogram, kernel density estimate (KDE), and Emperical Cumulative Distribution (ecdf) charts of the baseline data. These will give us insite into the distributions of the predictions and features that the assay is configured for.

[6]:
assay_builder.baseline_histogram()
_images/model-insights_11_0.png
[7]:
assay_builder.baseline_kde()
_images/model-insights_12_0.png
[8]:
assay_builder.baseline_ecdf()
_images/model-insights_13_0.png

Interactive Baseline Runs

We can do an interactive run of just the baseline part to see how the baseline data will be put into bins. This assay uses quintiles so all 5 bins (not counting the outlier bins) have 20% of the predictions. We can see the bin boundaries along the x-axis.

[9]:
baseline_run.chart()
baseline mean = 13.021036998645679
baseline median = 13.004066467285156
bin_mode = Quantile
aggregation = Density
metric = PSI
weighted = False
_images/model-insights_15_1.png

We can also get a dataframe with the bin/edge information.

[10]:
baseline_run.baseline_bins()
[10]:
b_edges b_edge_names b_aggregated_values b_aggregation
0 11.88 left_outlier 0.00 Density
1 12.63 q_20 0.20 Density
2 12.90 q_40 0.20 Density
3 13.11 q_60 0.20 Density
4 13.36 q_80 0.20 Density
5 15.02 q_100 0.20 Density
6 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density

The previous assay used quintiles so all of the bins had the same percentage/count of samples. To get bins that are divided equaly along the range of values we can use BinMode.EQUAL.

[11]:
equal_bin_builder = client.build_assay(assay_name, pipeline, model_name, baseline_start, baseline_end)
equal_bin_builder.summarizer_builder.add_bin_mode(BinMode.EQUAL)
equal_baseline = equal_bin_builder.build().interactive_baseline_run()
equal_baseline.chart()
baseline mean = 13.021036998645679
baseline median = 13.004066467285156
bin_mode = Equal
aggregation = Density
metric = PSI
weighted = False
_images/model-insights_19_1.png

We now see very different bin edges and sample percentages per bin.

[12]:
equal_baseline.baseline_bins()
[12]:
b_edges b_edge_names b_aggregated_values b_aggregation
0 11.88 left_outlier 0.00 Density
1 12.51 p_1.25e1 0.12 Density
2 13.13 p_1.31e1 0.50 Density
3 13.76 p_1.38e1 0.31 Density
4 14.39 p_1.44e1 0.06 Density
5 15.02 p_1.50e1 0.01 Density
6 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density

Interactive Assay Runs

By default the assay builder creates an assay with some good starting parameters. In particular the assay is configured to run a new analysis for every 24 hours starting at the end of the baseline period. Additionally, it sets the number of bins to 5 so creates quintiles, and sets the target iopath to "outputs 0 0" which means we want to monitor the first column of the first output/prediction.

We can do an interactive run of just the baseline part to see how the baseline data will be put into bins. This assay uses quintiles so all 5 bins (not counting the outlier bins) have 20% of the predictions. We can see the bin boundaries along the x-axis.

We then run it with interactive_run and convert it to a dataframe for easy analysis with to_dataframe.

Now lets do an interactive run of the first assay as it is configured. Interactive runs, don’t save the assay to the database (so they won’t be scheduled in the future) nor do they save the assay results. Instead the results are returned after a short while for further analysis.

[13]:
assay_builder = client.build_assay(assay_name, pipeline, model_name, baseline_start, baseline_end)
assay_config = assay_builder.add_run_until(last_day).build()
assay_results = assay_config.interactive_run()
assay_df = assay_results.to_dataframe()
assay_df
[13]:
assay_id name iopath score start min max mean median std warning_threshold alert_threshold status
0 None example assay output 0 0 0.02 2022-01-02T00:00:00Z 11.85 15.28 13.02 13.00 0.45 None 0.25 Ok
1 None example assay output 0 0 0.01 2022-01-03T00:00:00Z 11.89 15.46 13.01 12.99 0.45 None 0.25 Ok
2 None example assay output 0 0 0.01 2022-01-04T00:00:00Z 11.65 14.88 13.02 13.01 0.45 None 0.25 Ok
3 None example assay output 0 0 0.02 2022-01-05T00:00:00Z 11.78 15.37 13.02 13.00 0.45 None 0.25 Ok
4 None example assay output 0 0 0.02 2022-01-06T00:00:00Z 11.84 15.20 13.01 13.00 0.44 None 0.25 Ok
5 None example assay output 0 0 0.02 2022-01-07T00:00:00Z 11.93 15.06 13.02 13.01 0.45 None 0.25 Ok
6 None example assay output 0 0 0.01 2022-01-08T00:00:00Z 11.90 15.57 13.00 12.99 0.44 None 0.25 Ok
7 None example assay output 0 0 0.02 2022-01-09T00:00:00Z 11.87 15.27 13.02 13.00 0.45 None 0.25 Ok
8 None example assay output 0 0 0.00 2022-01-10T00:00:00Z 11.89 14.80 13.02 13.00 0.44 None 0.25 Ok
9 None example assay output 0 0 0.01 2022-01-11T00:00:00Z 11.84 14.93 13.02 13.01 0.44 None 0.25 Ok
10 None example assay output 0 0 0.01 2022-01-12T00:00:00Z 11.47 14.89 13.01 13.00 0.46 None 0.25 Ok
11 None example assay output 0 0 0.03 2022-01-13T00:00:00Z 8.20 14.82 12.95 12.95 0.50 None 0.25 Ok
12 None example assay output 0 0 0.99 2022-01-14T00:00:00Z 6.68 15.56 12.46 12.83 1.35 None 0.25 Alert
13 None example assay output 0 0 4.56 2022-01-15T00:00:00Z 5.66 17.48 10.93 10.99 1.83 None 0.25 Alert
14 None example assay output 0 0 3.93 2022-01-16T00:00:00Z 6.43 18.89 11.29 11.22 1.86 None 0.25 Alert
15 None example assay output 0 0 3.49 2022-01-17T00:00:00Z 7.26 20.83 11.73 11.68 1.98 None 0.25 Alert
16 None example assay output 0 0 3.16 2022-01-18T00:00:00Z 7.91 19.74 12.16 12.03 2.02 None 0.25 Alert
17 None example assay output 0 0 2.81 2022-01-19T00:00:00Z 8.65 22.57 12.60 12.43 1.92 None 0.25 Alert
18 None example assay output 0 0 2.01 2022-01-20T00:00:00Z 9.24 23.23 13.30 13.09 1.81 None 0.25 Alert
19 None example assay output 0 0 2.00 2022-01-21T00:00:00Z 10.18 23.73 13.99 13.86 1.74 None 0.25 Alert
20 None example assay output 0 0 2.92 2022-01-22T00:00:00Z 11.00 24.38 14.86 14.69 1.71 None 0.25 Alert
21 None example assay output 0 0 5.02 2022-01-23T00:00:00Z 11.82 26.12 15.69 15.50 1.70 None 0.25 Alert
22 None example assay output 0 0 7.95 2022-01-24T00:00:00Z 12.52 24.01 16.42 16.26 1.59 None 0.25 Alert
23 None example assay output 0 0 7.88 2022-01-25T00:00:00Z 13.39 26.72 17.30 17.18 1.67 None 0.25 Alert
24 None example assay output 0 0 9.03 2022-01-26T00:00:00Z 14.78 27.51 18.15 17.97 1.67 None 0.25 Alert
25 None example assay output 0 0 3.65 2022-01-27T00:00:00Z 12.08 29.19 16.74 17.41 3.17 None 0.25 Alert
26 None example assay output 0 0 0.01 2022-01-28T00:00:00Z 12.04 14.81 12.99 13.01 0.37 None 0.25 Ok
27 None example assay output 0 0 0.03 2022-01-29T00:00:00Z 12.01 14.45 13.00 13.01 0.36 None 0.25 Ok
28 None example assay output 0 0 0.02 2022-01-30T00:00:00Z 11.97 14.46 12.99 13.01 0.37 None 0.25 Ok
29 None example assay output 0 0 0.02 2022-01-31T00:00:00Z 12.05 14.80 12.98 13.01 0.38 None 0.25 Ok

Basic functionality for creating quick charts is included.

[14]:
assay_results.chart_scores()
_images/model-insights_25_0.png

We see that the difference scores are low for a while and then jump up to indicate there is an issue. We can examine that particular window to help us decide if that threshold is set correctly or not.

We can generate a quick chart of the results. This chart shows the 5 quantile bins (quintiles) derived from the baseline data plus one for left outliers and one for right outliers. We also see that the data from the window falls within the baseline quintiles but in a different proportion and is skewing higher. Whether this is an issue or not is specific to your use case.

First lets examine a day that is only slightly different than the baseline. We see that we do see some values that fall outside of the range from the baseline values, the left and right outliers, and that the bin values are different but similar.

[15]:
assay_results[0].chart()
baseline mean = 13.021036998645679
window mean = 13.01618284105465
baseline median = 13.004066467285156
window median = 13.004111289978027
bin_mode = Quantile
aggregation = Density
metric = PSI
weighted = False
score = 0.019674378474700036
scores = [0.00980308228898587, 0.0002603476402290938, 0.00022327892034488384, 0.0031099778889999417, 9.781216093661028e-07, 0.0003905674264324862, 0.005886146188098397]
index = None
_images/model-insights_27_1.png

Other days, however are significantly different.

[16]:
assay_results[12].chart()
baseline mean = 13.021036998645679
window mean = 12.462097006128324
baseline median = 13.004066467285156
window median = 12.829922676086426
bin_mode = Quantile
aggregation = Density
metric = PSI
weighted = False
score = 0.9897758782012277
scores = [0.8594285651741624, 0.0008166837246366183, 0.039563316977030236, 0.05914513530289921, 0.02759016554769916, 0.001900118150881501, 0.0013318933239185415]
index = None
_images/model-insights_29_1.png

If we want to investigate further, we can run interactive assays on each of the inputs to see if any of them show anything abnormal. In this example we’ll provide the feature labels to create more understandable titles.

The current assay expects continous data. Sometimes categorical data is encoded as 1 or 0 in a feature and sometimes in a limited number of values such as 1, 2, 3. If one value has high a percentage the analysis emits a warning so that we know the scores for that feature may not behave as we expect.

[17]:
labels = ['bedrooms', 'bathrooms', 'lat', 'long', 'waterfront', 'sqft_living', 'sqft_lot', 'floors', 'view', 'condition', 'grade', 'sqft_above', 'sqft_basement', 'yr_built', 'yr_renovated', 'sqft_living15', 'sqft_lot15']

baseline_inferences = client.get_raw_pipeline_inference_logs(pipeline_name, baseline_start, baseline_end, model_name)

assay_results = assay_config.interactive_input_run(baseline_inferences, labels)
iadf = assay_results.to_dataframe()
input column distinct_vals label           largest_pct
    0     0             12 bedrooms        0.4567
    0     1             26 bathrooms       0.2421
    0     2           1492 lat             0.0022
    0     3            502 long            0.0077
    0     4              2 waterfront      0.9928 *** May not be continuous feature
    0     5            425 sqft_living     0.0094
    0     6           1407 sqft_lot        0.0188
    0     7              6 floors          0.5036
    0     8              5 view            0.9068 *** May not be continuous feature
    0     9              5 condition       0.6293
    0    10             10 grade           0.4242
    0    11            393 sqft_above      0.0121
    0    12            162 sqft_basement   0.6034
    0    13            116 yr_built        0.0237
    0    14             37 yr_renovated    0.9597 *** May not be continuous feature
    0    15            340 sqft_living15   0.0132
    0    16           1347 sqft_lot15      0.0177

We can chart each of the iopaths and do a visual inspection. From the charts we see that if any of the input features had significant differences in the first two days which we can choose to inspect further. Here we choose to show 3 charts just to save space in this notebook.

[18]:
assay_results.chart_iopaths(labels=labels, selected_labels=['bedrooms', 'lat', 'sqft_living'])
_images/model-insights_33_0.png
_images/model-insights_33_1.png
_images/model-insights_33_2.png

When we are comfortable with what alert threshold should be for our specific purposes we can create and save an assay that will be automatically run on a daily basis.

In this example we’re create an assay that runs everyday against the baseline and has an alert threshold of 0.5.

Once we upload it it will be saved and scheduled for future data as well as run against past data.

[19]:
alert_threshold = 0.5
assay_builder = client.build_assay(assay_name, pipeline, model_name, baseline_start, baseline_end).add_alert_threshold(alert_threshold)
assay_id = assay_builder.upload()

After a short while, we can get the assay results for further analysis.

[20]:
assay_results = client.get_assay_results_logs(baseline_end,datetime.datetime.now(), assay_id=assay_id)

We see that the assays analysis is similar to the interactive run we started with though the analysis for the third day does not exceed the new alert threshold we set. And since we called upload instead of interactive_run the assay was saved to the system and will continue to run automatically on schedule from now on.

[21]:
assay_results.chart_scores()
_images/model-insights_39_0.png

Scheduling Assays

By default assays are scheduled to run every 24 hours starting immediately after the baseline period ends.

However, you can control the start time by setting start and the frequency by setting interval on the window.

So to recap:

  • The window width is the size of the window. The default is 24 hours.

  • The interval is how often the analysis is run, how far the window is slid into the future based on the last run. The default is the window width.

  • The window start is when the analysis should start. The default is the end of the baseline period.

For example to run an analysis every 12 hours on the previous 24 hours of data you’d set the window width to 24 (the default) and the interval to 12.

[22]:
assay_builder = client.build_assay(assay_name, pipeline, model_name, baseline_start, baseline_end)
assay_builder = assay_builder.add_run_until(last_day)

assay_builder.window_builder().add_width(hours=24).add_interval(hours=12)

assay_config = assay_builder.build()

assay_results = assay_config.interactive_run()
print(f"Generated {len(assay_results)} analyses")
Generated 59 analyses

To start a weekly analysis of the previous week on a specific day, set the start date (taking care to specify the desired timezone), and the width and interval to 1 week and of course an analysis won’t be generated till a window is complete.

[23]:
report_start = datetime.datetime.fromisoformat('2022-01-03T00:00:00+00:00')

assay_builder = client.build_assay(assay_name, pipeline, model_name, baseline_start, baseline_end)
assay_builder = assay_builder.add_run_until(last_day)

assay_builder.window_builder().add_width(weeks=1).add_interval(weeks=1).add_start(report_start)

assay_config = assay_builder.build()

assay_results = assay_config.interactive_run()
print(f"Generated {len(assay_results)} analyses")
Generated 4 analyses

Advanced Configuration

The assay can be configured in a variety of ways to help customize it to your particular needs. Specifically you can: * change the BinMode to evenly spaced, quantile or user provided * change the number of bins to use * provide weights to use when scoring the bins * calculate the score using the sum of differences, maximum difference or population stability index * change the value aggregation for the bins to density, cumulative or edges

Lets take a look at these in turn.

Default configuration

First lets look at the default configuration. This is a lot of information but much of it is useful to know where it is available.

We see that the assay is broken up into 4 sections. A top level meta data section, a section for the baseline specification, a section for the window specification and a section that specifies the summarization configuration.

In the meta section we see the name of the assay, that it runs on the first column of the first output "outputs 0 0" and that there is a default threshold of 0.25.

The summarizer section shows us the defaults of Quantile, Density and PSI on 5 bins.

The baseline section shows us that it is configured as a fixed baseline with the specified start and end date times.

And the window tells us what model in the pipeline we are analyzing and how often.

[24]:
assay_builder = client.build_assay("Test Assay", pipeline, model_name, baseline_start, baseline_end).add_run_until(last_day)
print(assay_builder.build().to_json())
{
    "name": "Test Assay",
    "pipeline_id": 1,
    "pipeline_name": "mypipeline",
    "active": true,
    "status": "created",
    "iopath": "output 0 0",
    "baseline": {
        "Fixed": {
            "pipeline": "mypipeline",
            "model": "mymodel",
            "start_at": "2022-01-01T00:00:00+00:00",
            "end_at": "2022-01-02T00:00:00+00:00"
        }
    },
    "window": {
        "pipeline": "mypipeline",
        "model": "mymodel",
        "width": "24 hours",
        "start": null,
        "interval": null
    },
    "summarizer": {
        "type": "UnivariateContinuous",
        "bin_mode": "Quantile",
        "aggregation": "Density",
        "metric": "PSI",
        "num_bins": 5,
        "bin_weights": null,
        "bin_width": null,
        "provided_edges": null,
        "add_outlier_edges": true
    },
    "warning_threshold": null,
    "alert_threshold": 0.25,
    "next_run": "2022-01-02T00:00:00+00:00",
    "run_until": "2022-02-01T00:00:00+00:00",
    "model_insights_url": "http://model-insights:5150"
}

Defaults

We can run the assay interactively and review the first analysis. The method compare_basic_stats gives us a dataframe with basic stats for the baseline and window data.

[25]:
assay_results = assay_builder.build().interactive_run()
ar = assay_results[0]

ar.compare_basic_stats()
[25]:
Baseline Window diff pct_diff
count 1,813.00 1,812.00 -1.00 -0.06
min 11.88 11.85 -0.03 -0.23
max 15.02 15.28 0.26 1.75
mean 13.02 13.02 -0.00 -0.04
median 13.00 13.00 0.00 0.00
std 0.46 0.45 -0.00 -0.97
start 2022-01-01T00:00:00Z 2022-01-02T00:00:00Z NaN NaN
end 2022-01-02T00:00:00Z 2022-01-03T00:00:00Z NaN NaN

The method compare_bins gives us a dataframe with the bin information. Such as the number of bins, the right edges, suggested bin/edge names and the values for each bin in the baseline and the window.

[26]:
ar.compare_bins()
[26]:
b_edges b_edge_names b_aggregated_values b_aggregation w_edges w_edge_names w_aggregated_values w_aggregation diff_in_pcts
0 11.88 left_outlier 0.00 Density 11.88 left_outlier 0.00 Density 0.00
1 12.63 q_20 0.20 Density 12.63 e_1.26e1 0.21 Density 0.01
2 12.90 q_40 0.20 Density 12.90 e_1.29e1 0.21 Density 0.01
3 13.11 q_60 0.20 Density 13.11 e_1.31e1 0.18 Density -0.02
4 13.36 q_80 0.20 Density 13.36 e_1.34e1 0.20 Density -0.00
5 15.02 q_100 0.20 Density 15.02 e_1.50e1 0.21 Density 0.01
6 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density 0.00

We can also plot the chart to visualize the values of the bins.

[27]:
ar.chart()
baseline mean = 13.021036998645679
window mean = 13.01618284105465
baseline median = 13.004066467285156
window median = 13.004111289978027
bin_mode = Quantile
aggregation = Density
metric = PSI
weighted = False
score = 0.019674378474700036
scores = [0.00980308228898587, 0.0002603476402290938, 0.00022327892034488384, 0.0031099778889999417, 9.781216093661028e-07, 0.0003905674264324862, 0.005886146188098397]
index = None
_images/model-insights_51_1.png

Binning Mode

We can change the bin mode algorithm to equal and see that the bins/edges are partitioned at different points and the bins have different values.

[28]:
assay_builder = client.build_assay("Test Assay", pipeline, model_name, baseline_start, baseline_end).add_run_until(last_day)
assay_builder.summarizer_builder.add_bin_mode(BinMode.EQUAL)
assay_results = assay_builder.build().interactive_run()
display(display(assay_results[0].compare_bins()))
assay_results[0].chart()
b_edges b_edge_names b_aggregated_values b_aggregation w_edges w_edge_names w_aggregated_values w_aggregation diff_in_pcts
0 11.88 left_outlier 0.00 Density 11.88 left_outlier 0.00 Density 0.00
1 12.51 p_1.25e1 0.12 Density 12.51 e_1.25e1 0.13 Density 0.00
2 13.13 p_1.31e1 0.50 Density 13.13 e_1.31e1 0.49 Density -0.01
3 13.76 p_1.38e1 0.31 Density 13.76 e_1.38e1 0.33 Density 0.02
4 14.39 p_1.44e1 0.06 Density 14.39 e_1.44e1 0.05 Density -0.01
5 15.02 p_1.50e1 0.01 Density 15.02 e_1.50e1 0.00 Density -0.00
6 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density 0.00
None
baseline mean = 13.021036998645679
window mean = 13.01618284105465
baseline median = 13.004066467285156
window median = 13.004111289978027
bin_mode = Equal
aggregation = Density
metric = PSI
weighted = False
score = 0.02174688998992665
scores = [0.00980308228898587, 0.00012262223368267688, 0.00014769845960773225, 0.000993907207070759, 0.0023700328024471758, 0.002423400810034042, 0.005886146188098397]
index = None
_images/model-insights_53_3.png

User Provided Bin Edges

The values in this dataset run from ~11.6 to ~15.81. And lets say we had a business reason to use specific bin edges. We can specify them with the BinMode.PROVIDED and specifying a list of floats with the right hand / upper edge of each bin and optionally the lower edge of the smallest bin. If the lowest edge is not specified the threshold for left outliers is taken from the smallest value in the baseline dataset.

[29]:
edges = [11.0, 12.0, 13.0, 14.0, 15.0, 16.0]
assay_builder = client.build_assay("Test Assay", pipeline, model_name, baseline_start, baseline_end).add_run_until(last_day)
assay_builder.summarizer_builder.add_bin_mode(BinMode.PROVIDED, edges)
assay_results = assay_builder.build().interactive_run()
display(display(assay_results[0].compare_bins()))
assay_results[0].chart()
b_edges b_edge_names b_aggregated_values b_aggregation w_edges w_edge_names w_aggregated_values w_aggregation diff_in_pcts
0 11.00 left_outlier 0.00 Density 11.00 left_outlier 0.00 Density 0.00
1 12.00 e_1.20e1 0.00 Density 12.00 e_1.20e1 0.00 Density 0.00
2 13.00 e_1.30e1 0.49 Density 13.00 e_1.30e1 0.49 Density -0.00
3 14.00 e_1.40e1 0.48 Density 14.00 e_1.40e1 0.49 Density 0.01
4 15.00 e_1.50e1 0.03 Density 15.00 e_1.50e1 0.02 Density -0.01
5 16.00 e_1.60e1 0.00 Density 16.00 e_1.60e1 0.00 Density 0.00
6 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density 0.00
None
baseline mean = 13.021036998645679
window mean = 13.01618284105465
baseline median = 13.004066467285156
window median = 13.004111289978027
bin_mode = Provided
aggregation = Density
metric = PSI
weighted = False
score = 0.0052011866476329915
scores = [0.0, 0.0024270797943831413, 1.8763984174233916e-05, 0.00011480770647089651, 0.002257487960844032, 0.00038304720176068804, 0.0]
index = None
_images/model-insights_55_3.png

Number of Bins

We could also choose to a different number of bins, lets say 10, which can be evenly spaced or based on the quantiles (deciles).

[30]:
assay_builder = client.build_assay("Test Assay", pipeline, model_name, baseline_start, baseline_end).add_run_until(last_day)
assay_builder.summarizer_builder.add_bin_mode(BinMode.QUANTILE).add_num_bins(10)
assay_results = assay_builder.build().interactive_run()
display(display(assay_results[1].compare_bins()))
assay_results[1].chart()
b_edges b_edge_names b_aggregated_values b_aggregation w_edges w_edge_names w_aggregated_values w_aggregation diff_in_pcts
0 11.88 left_outlier 0.00 Density 11.88 left_outlier 0.00 Density 0.00
1 12.46 q_10 0.10 Density 12.46 e_1.25e1 0.11 Density 0.01
2 12.63 q_20 0.10 Density 12.63 e_1.26e1 0.10 Density -0.00
3 12.78 q_30 0.10 Density 12.78 e_1.28e1 0.10 Density 0.00
4 12.90 q_40 0.10 Density 12.90 e_1.29e1 0.11 Density 0.01
5 13.00 q_50 0.10 Density 13.00 e_1.30e1 0.10 Density 0.00
6 13.11 q_60 0.10 Density 13.11 e_1.31e1 0.09 Density -0.01
7 13.22 q_70 0.10 Density 13.22 e_1.32e1 0.10 Density -0.00
8 13.36 q_80 0.10 Density 13.36 e_1.34e1 0.09 Density -0.00
9 13.60 q_90 0.10 Density 13.60 e_1.36e1 0.10 Density 0.00
10 15.02 q_100 0.10 Density 15.02 e_1.50e1 0.10 Density -0.00
11 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density 0.00
None
baseline mean = 13.021036998645679
window mean = 13.010907055789534
baseline median = 13.004066467285156
window median = 12.993888854980469
bin_mode = Quantile
aggregation = Density
metric = PSI
weighted = False
score = 0.008209060319467209
scores = [0.0, 0.0006680706132507951, 0.00019356523211058643, 1.3374098424755057e-05, 0.0004323412561987516, 3.6626035060501716e-06, 0.0007068329210884422, 2.478165590023963e-06, 0.000247803939212629, 2.8917879748960253e-05, 2.586742223781745e-05, 0.005886146188098397]
index = None
/home/jovyan/wallaroo-plus/sdk/wallaroo/assay.py:317: UserWarning: FixedFormatter should only be used together with FixedLocator
  ax.set_xticklabels(labels=edge_names, rotation=45)
_images/model-insights_57_4.png

Bin Weights

Now lets say we only care about differences at the higher end of the range. We can use weights to specify that difference in the lower bins should not be counted in the score.

If we stick with 10 bins we can provide 10 a vector of 12 weights. One weight each for the original bins plus one at the front for the left outlier bin and one at the end for the right outlier bin.

Note we still show the values for the bins but the scores for the lower 5 and left outlier are 0 and only the right half is counted and reflected in the score.

[31]:
weights = [0] * 6
weights.extend([1] * 6)
print("Using weights: ", weights)
assay_builder = client.build_assay("Test Assay", pipeline, model_name, baseline_start, baseline_end).add_run_until(last_day)
assay_builder.summarizer_builder.add_bin_mode(BinMode.QUANTILE).add_num_bins(10).add_bin_weights(weights)
assay_results = assay_builder.build().interactive_run()
display(display(assay_results[1].compare_bins()))
assay_results[1].chart()
Using weights:  [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
b_edges b_edge_names b_aggregated_values b_aggregation w_edges w_edge_names w_aggregated_values w_aggregation diff_in_pcts
0 11.88 left_outlier 0.00 Density 11.88 left_outlier 0.00 Density 0.00
1 12.46 q_10 0.10 Density 12.46 e_1.25e1 0.11 Density 0.01
2 12.63 q_20 0.10 Density 12.63 e_1.26e1 0.10 Density -0.00
3 12.78 q_30 0.10 Density 12.78 e_1.28e1 0.10 Density 0.00
4 12.90 q_40 0.10 Density 12.90 e_1.29e1 0.11 Density 0.01
5 13.00 q_50 0.10 Density 13.00 e_1.30e1 0.10 Density 0.00
6 13.11 q_60 0.10 Density 13.11 e_1.31e1 0.09 Density -0.01
7 13.22 q_70 0.10 Density 13.22 e_1.32e1 0.10 Density -0.00
8 13.36 q_80 0.10 Density 13.36 e_1.34e1 0.09 Density -0.00
9 13.60 q_90 0.10 Density 13.60 e_1.36e1 0.10 Density 0.00
10 15.02 q_100 0.10 Density 15.02 e_1.50e1 0.10 Density -0.00
11 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density 179,769,313,486,231,570,814,527,423,731,704,356... right_outlier 0.00 Density 0.00
None
baseline mean = 13.021036998645679
window mean = 13.010907055789534
baseline median = 13.004066467285156
window median = 12.993888854980469
bin_mode = Quantile
aggregation = Density
metric = PSI
weighted = True
score = 0.0011496744193293783
scores = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0001178054868480737, 4.130275983373272e-07, 4.1300656535438165e-05, 4.8196466248267085e-06, 4.311237039636242e-06, 0.0009810243646830661]
index = None
/home/jovyan/wallaroo-plus/sdk/wallaroo/assay.py:317: UserWarning: FixedFormatter should only be used together with FixedLocator
  ax.set_xticklabels(labels=edge_names, rotation=45)
_images/model-insights_59_5.png

Metrics

The score is a distance or dis-similarity measure. The larger it is the less similar the two distributions are. We currently support summing the differences of each individual bin, taking the maximum difference and a modified Population Stability Index (PSI).

The following three charts use each of the metrics. Note how the scores change. The best one will depend on your particular use case.

[32]:
assay_builder = client.build_assay("Test Assay", pipeline, model_name, baseline_start, baseline_end).add_run_until(last_day)
assay_results = assay_builder.build().interactive_run()
assay_results[0].chart()
baseline mean = 13.021036998645679
window mean = 13.01618284105465
baseline median = 13.004066467285156
window median = 13.004111289978027
bin_mode = Quantile
aggregation = Density
metric = PSI
weighted = False
score = 0.019674378474700036
scores = [0.00980308228898587, 0.0002603476402290938, 0.00022327892034488384, 0.0031099778889999417, 9.781216093661028e-07, 0.0003905674264324862, 0.005886146188098397]
index = None
_images/model-insights_61_1.png
[33]:
assay_builder = client.build_assay("Test Assay", pipeline, model_name, baseline_start, baseline_end).add_run_until(last_day)
assay_builder.summarizer_builder.add_metric(Metric.SUMDIFF)
assay_results = assay_builder.build().interactive_run()
assay_results[0].chart()
baseline mean = 13.021036998645679
window mean = 13.01618284105465
baseline median = 13.004066467285156
window median = 13.004111289978027
bin_mode = Quantile
aggregation = Density
metric = SumDiff
weighted = False
score = 0.024613747414125847
scores = [0.0005518763796909492, 0.007284889971739539, 0.006732709192501046, 0.02417206367064456, 0.00044168374348130257, 0.008940519110812395, 0.0011037527593818985]
index = None
_images/model-insights_62_1.png
[34]:
assay_builder = client.build_assay("Test Assay", pipeline, model_name, baseline_start, baseline_end).add_run_until(last_day)
assay_builder.summarizer_builder.add_metric(Metric.MAXDIFF)
assay_results = assay_builder.build().interactive_run()
assay_results[0].chart()
baseline mean = 13.021036998645679
window mean = 13.01618284105465
baseline median = 13.004066467285156
window median = 13.004111289978027
bin_mode = Quantile
aggregation = Density
metric = MaxDiff
weighted = False
score = 0.02417206367064456
scores = [0.0005518763796909492, 0.007284889971739539, 0.006732709192501046, 0.02417206367064456, 0.00044168374348130257, 0.008940519110812395, 0.0011037527593818985]
index = 3
_images/model-insights_63_1.png

Aggregation Options

Also, bin aggregation can be done in histogram Aggregation.DENSITY style (the default) where we count the number/percentage of values that fall in each bin or Empirical Cumulative Density Function style Aggregation.CUMULATIVE where we keep a cumulative count of the values/percentages that fall in each bin.

[35]:
assay_builder = client.build_assay("Test Assay", pipeline, model_name, baseline_start, baseline_end).add_run_until(last_day)
assay_builder.summarizer_builder.add_aggregation(Aggregation.DENSITY)
assay_results = assay_builder.build().interactive_run()
assay_results[0].chart()
baseline mean = 13.021036998645679
window mean = 13.01618284105465
baseline median = 13.004066467285156
window median = 13.004111289978027
bin_mode = Quantile
aggregation = Density
metric = PSI
weighted = False
score = 0.019674378474700036
scores = [0.00980308228898587, 0.0002603476402290938, 0.00022327892034488384, 0.0031099778889999417, 9.781216093661028e-07, 0.0003905674264324862, 0.005886146188098397]
index = None
_images/model-insights_65_1.png
[36]:
assay_builder = client.build_assay("Test Assay", pipeline, model_name, baseline_start, baseline_end).add_run_until(last_day)
assay_builder.summarizer_builder.add_aggregation(Aggregation.CUMULATIVE)
assay_results = assay_builder.build().interactive_run()
assay_results[0].chart()
baseline mean = 13.021036998645679
window mean = 13.01618284105465
baseline median = 13.004066467285156
window median = 13.004111289978027
bin_mode = Quantile
aggregation = Cumulative
metric = PSI
weighted = False
score = 0.04370873103134221
scores = [0.0005518763796909492, 0.00783676635143049, 0.014569475543931565, 0.009602588126712996, 0.010044271870194299, 0.001103752759381904, 0.0]
index = None
_images/model-insights_66_1.png
[ ]: