File size: 9,204 Bytes
ad8da65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# Advanced Example: Unique Identifiers

Initial analyses that treat inferences as independent of one another can provide tremendous value. But over time, 
models often make multiple predictions about the same real-world entities. No matter what you're predicting, 
it can be helpful to compare the inputs and outputs of your model on an entity-by-entity basis.

For example, let's say that your model makes predictions about whether customers will make a purchase in the next 30 
days. You might have the following attributes:
- `customer_id`: a non-input attribute
- `will_purchase_pred`: the prediction attribute: whether a customer will make a purchase in the next 30 days
- `will_purchase_gt`: the ground truth attribute: whether a customer actually did make a purchase within 30 days
- `recent_purchase_count`: an input attribute with the total number of purchases the customer made in the last 90 days
- `newsletter_subscriber`: an input attribute depicting whether the customer subscribes to the deals newsletter  

Your model might be run on the full universe of Customer IDs at some regular interval. With Arthur's powerful Query 
API, you can follow inferences for each Customer ID through time and answer questions like:
- How does `recent_purchase_count` tend to change for each customer, from the first to last time inference is conducted?
- What is the per-customer variance of `recent_purchase_count` across time? 
- How many customers changed their newsletter subscription status, from one month ago to today?
- What is the distribution of the lifetimes of Customer IDs?

## Example Queries

We'll walk through some example queries for these entity-by-entity comparisons, exploring the sample case outlined 
above.

### Per-Customer Variance
We can look at how consistent `recent_purchase_count` is _for each customer_ across time. We'll compute the variance in 
`recent_purchase_count` for each customer across all their inferences, and then roll those individual variances up into 
a distribution.

```json
{
  "select": [
    {
      "function": "distribution",
      "alias": "recent_purchase_count_variance_distribution",
      "parameters": {
        "property": {
          "nested_function": {
            "function": "variance",
            "parameters": {
              "property": "recent_purchase_count"
            }
          }
        },
        "num_bins": 20
      }
    }
  ],
  "subquery": {
    "select": [
      {
        "property": "recent_purchase_count"
      },
      {
        "property": "customer_id"
      }
    ],
    "group_by": [
      {
        "property": "customer_id"
      }
    ]
  }
}
```

### Change Across Batches
If our model is a batch model, we might want to compare the values for each customer between two difference batches. 
We'll again look at the distribution of change in the `recent_purchase_count`, but this time look at the difference for 
each customer between two specific batches.

```json
{
  "select": [
    {
      "function": "distribution",
      "alias": "recent_purchase_count_difference_distribution",
      "parameters": {
        "property": {
          "nested_function": {
            "function": "subtract",
            "parameters": {
              "left": "batch1_recent_purchase_count",
              "right": "batch2_recent_purchase_count"
            }
          }
        },
        "num_bins": 20
      }
    }
  ],
  "subquery": {
    "select": [
      {
        "property": "customer_id"
      },
      {
        "property": "batch1_recent_purchase_count"
      },
      {
        "property": "batch2_recent_purchase_count"
      }
    ],
    "subquery": {
      "select": [
        {
          "property": "customer_id"
        },
        {
          "function": "anyIf",
          "parameters": {
            "result": "recent_purchase_count",
            "property": "batch_id",
            "comparator": "eq",
            "value": "batch1"
          },
          "alias": "batch1_recent_purchase_count"
        },
        {
          "function": "anyIf",
          "parameters": {
            "result": "recent_purchase_count",
            "property": "batch_id",
            "comparator": "eq",
            "value": "batch2"
          },
          "alias": "batch2_recent_purchase_count"
        }
      ],
      "group_by": [
        {
          "property": "customer_id"
        }
      ]
    },
    "where": [
      {
        "property": "batch1_recent_purchase_count",
        "comparator": "NotNull"
      },
      {
        "property": "batch2_recent_purchase_count",
        "comparator": "NotNull"
      }
    ]
  }
}
```

### Change Across First to Last Inference Per Customer
We can again compare the difference between two absolute points, but instead of comparing fixed batches compute it for 
the earliest and latest inference for each customer:

```json
{
  "select": [
    {
      "function": "distribution",
      "alias": "recent_purchase_count_difference_distribution",
      "parameters": {
        "property": {
          "nested_function": {
            "function": "subtract",
            "parameters": {
              "left": "newest_recent_purchase_count",
              "right": "oldest_recent_purchase_count"
            }
          }
        },
        "num_bins": 20
      }
    }
  ],
  "subquery": {
    "select": [
      {
        "property": "customer_id"
      },
      {
        "function": "argMax",
        "parameters": {
          "argument": "inference_timestamp",
          "value": "recent_purchase_count"
        },
        "alias": "newest_recent_purchase_count"
      },
      {
        "function": "argMin",
        "parameters": {
          "argument": "inference_timestamp",
          "value": "recent_purchase_count"
        },
        "alias": "oldest_recent_purchase_count"
      }
    ],
    "group_by": [
      {
        "property": "customer_id"
      }
    ]
  }
}
```

### Change in Categorical Variables
We can also look at change in categorical variables on an entity-by-entity basis. Let's look at the distribution of 
customers who remained subscribed, remained unsubscribed, newly subscribed, or newly unsubscribed from one batch to the 
next.

```json
{
  "select": [
    {
      "alias": "batch1_not_subscribed",
      "function": "equals",
      "parameters": {
        "left": "batch1_newsletter_subscriber",
        "right": 0
      }
    },
    {
      "alias": "batch1_is_subscribed",
      "function": "equals",
      "parameters": {
        "left": "batch1_newsletter_subscriber",
        "right": 1
      }
    },
    {
      "alias": "batch2_not_subscribed",
      "function": "equals",
      "parameters": {
        "left": "batch2_newsletter_subscriber",
        "right": 0
      }
    },
    {
      "alias": "batch2_is_subscribed",
      "function": "equals",
      "parameters": {
        "left": "batch2_newsletter_subscriber",
        "right": 1
      }
    },
    {
      "alias": "stayed_unsubscribed_count",
      "function": "and",
      "parameters": {
        "left": {
          "alias_ref": "batch1_not_subscribed"
        },
        "right": {
          "alias_ref": "batch2_not_subscribed"
        }
      }
    },
    {
      "alias": "did_subscribe_count",
      "function": "and",
      "parameters": {
        "left": {
          "alias_ref": "batch1_not_subscribed"
        },
        "right": {
          "alias_ref": "batch2_is_subscribed"
        }
      }
    },
    {
      "alias": "stayed_subscribed_count",
      "function": "and",
      "parameters": {
        "left": {
          "alias_ref": "batch1_is_subscribed"
        },
        "right": {
          "alias_ref": "batch2_is_subscribed"
        }
      }
    },
    {
      "alias": "did_unsubscribe_count",
      "function": "and",
      "parameters": {
        "left": {
          "alias_ref": "batch1_is_subscribed"
        },
        "right": {
          "alias_ref": "batch2_not_subscribed"
        }
      }
    }
  ],
  "subquery": {
    "select": [
      {
        "property": "customer_id"
      },
      {
        "property": "batch1_newsletter_subscriber"
      },
      {
        "property": "batch2_newsletter_subscriber"
      }
    ],
    "subquery": {
      "select": [
        {
          "property": "customer_id"
        },
        {
          "function": "anyIf",
          "parameters": {
            "result": "newsletter_subscriber",
            "property": "batch_id",
            "comparator": "eq",
            "value": "batch1"
          },
          "alias": "batch1_newsletter_subscriber"
        },
        {
          "function": "anyIf",
          "parameters": {
            "result": "newsletter_subscriber",
            "property": "batch_id",
            "comparator": "eq",
            "value": "batch2"
          },
          "alias": "batch2_newsletter_subscriber"
        }
      ],
      "group_by": [
        {
          "property": "customer_id"
        }
      ]
    },
    "where": [
      {
        "property": "batch1_newsletter_subscriber",
        "comparator": "NotNull"
      },
      {
        "property": "batch2_newsletter_subscriber",
        "comparator": "NotNull"
      }
    ]
  }
}
```