The AleaCarta Model
Implementation of the AleaCarta model.
AleaCarta
Bases: BaseBasketModel
Class for the AleaCarta model.
Better Capturing Interactions between Products in Retail: Revisited Negative Sampling for Basket Choice Modeling, Désir, J.; Auriau, V.; Možina, M.; Malherbe, E. (2025), ECML PKDDD
Source code in choice_learn/basket_models/alea_carta.py
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
|
train_iter_method: str
property
Method used to generate sub-baskets from a purchased one.
Available methods are: - 'shopper': randomly orders the purchases and creates the ordered sub-baskets: (1|0); (2|1); (3|1,2); (4|1,2,3); etc... - 'aleacarta': creates all the sub-baskets with N-1 items: (4|1,2,3); (3|1,2,4); (2|1,3,4); (1|2,3,4)
Returns:
Type | Description |
---|---|
str
|
Data generation method. |
trainable_weights: list[tf.Variable]
property
Latent parameters of the model.
Returns:
Type | Description |
---|---|
list[Variable]
|
Latent parameters of the model |
__init__(item_intercept=True, price_effects=False, seasonal_effects=False, latent_sizes={'preferences': 4, 'price': 4, 'season': 4}, n_negative_samples=2, optimizer='adam', callbacks=None, lr=0.001, epochs=10, batch_size=32, grad_clip_value=None, weight_decay=None, momentum=0.0, epsilon_price=1e-05, **kwargs)
Initialize the AleaCarta model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item_intercept |
bool
|
Whether to include item intercept in the model, by default True Corresponds to the item intercept |
True
|
price_effects |
bool
|
Whether to include price effects in the model, by default True |
False
|
seasonal_effects |
bool
|
Whether to include seasonal effects in the model, by default True |
False
|
latent_sizes |
dict[str]
|
Lengths of the vector representation of the latent parameters latent_sizes["preferences"]: length of one vector of theta, gamma latent_sizes["price"]: length of one vector of delta, beta latent_sizes["season"]: length of one vector of nu, mu by default {"preferences": 4, "price": 4, "season": 4} |
{'preferences': 4, 'price': 4, 'season': 4}
|
n_negative_samples |
int
|
Number of negative samples to draw for each positive sample for the training, by default 2 Must be > 0 |
2
|
optimizer |
str
|
Optimizer to use for training, by default "adam" |
'adam'
|
callbacks |
Union[CallbackList, None]
|
List of callbacks to add to model.fit, by default None and only add History |
None
|
lr |
float
|
Learning rate, by default 1e-3 |
0.001
|
epochs |
int
|
Number of epochs, by default 100 |
10
|
batch_size |
int
|
Batch size, by default 32 |
32
|
grad_clip_value |
Union[float, None]
|
Value to clip the gradient, by default None |
None
|
weight_decay |
Union[float, None]
|
Weight decay, by default None |
None
|
momentum |
float
|
Momentum for the optimizer, by default 0. For SGD only |
0.0
|
epsilon_price |
float
|
Epsilon value to add to prices to avoid NaN values (log(0)), by default 1e-5 |
1e-05
|
Source code in choice_learn/basket_models/alea_carta.py
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 |
|
compute_basket_utility(basket=None, store=None, week=None, prices=None, available_item_batch=None, trip=None)
Compute the utility of an (unordered) basket.
Corresponds to the sum of all the conditional utilities: \sum_{i \in basket} U(i | basket \ {i}) Take as input directly a Trip object or separately basket, store, week and prices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
basket |
Union[None, ndarray]
|
ID the of items already in the basket, by default None |
None
|
store |
Union[None, int]
|
Store id, by default None |
None
|
week |
Union[None, int]
|
Week number, by default None |
None
|
prices |
Union[None, ndarray]
|
Prices for each purchased item, by default None Shape must be (len(basket),) |
None
|
trip |
Union[None, Trip]
|
Trip object containing basket, store, week and prices, by default None |
None
|
Returns:
Type | Description |
---|---|
float
|
Utility of the (unordered) basket |
Source code in choice_learn/basket_models/alea_carta.py
compute_batch_loss(item_batch, basket_batch, future_batch, store_batch, week_batch, price_batch, available_item_batch)
Compute log-likelihood and loss for one batch of items.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item_batch |
ndarray
|
Batch of purchased items ID (integers) Shape must be (batch_size,) |
required |
basket_batch |
ndarray
|
Batch of baskets (ID of items already in the baskets) (arrays) for each purchased item Shape must be (batch_size, max_basket_size) |
required |
future_batch |
ndarray
|
Batch of items to be purchased in the future (ID of items not yet in the basket) (arrays) for each purchased item Shape must be (batch_size, max_basket_size) Here for signature reasons, unused for this model |
required |
store_batch |
ndarray
|
Batch of store IDs (integers) for each purchased item Shape must be (batch_size,) |
required |
week_batch |
ndarray
|
Batch of week numbers (integers) for each purchased item Shape must be (batch_size,) |
required |
price_batch |
ndarray
|
Batch of prices (floats) for each purchased item Shape must be (batch_size,) |
required |
available_item_batch |
ndarray
|
List of availability matrices (indicating the availability (1) or not (0) of the products) (arrays) for each purchased item Shape must be (batch_size, n_items) |
required |
Returns:
Name | Type | Description |
---|---|---|
Variable
|
Value of the loss for the batch (binary cross-entropy), Shape must be (1,) |
|
loglikelihood |
Variable
|
Computed log-likelihood of the batch of items Approximated by difference of utilities between positive and negative samples Shape must be (1,) |
Source code in choice_learn/basket_models/alea_carta.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
|
compute_batch_utility(item_batch, basket_batch, store_batch, week_batch, price_batch, available_item_batch)
Compute the utility of all the items in item_batch given the items in basket_batch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item_batch |
Union[ndarray, Tensor]
|
Batch of the purchased items ID (integers) for which to compute the utility Shape must be (batch_size,) (positive and negative samples concatenated together) |
required |
basket_batch |
ndarray
|
Batch of baskets (ID of items already in the baskets) (arrays) for each purchased item Shape must be (batch_size, max_basket_size) |
required |
store_batch |
ndarray
|
Batch of store IDs (integers) for each purchased item Shape must be (batch_size,) |
required |
week_batch |
ndarray
|
Batch of week numbers (integers) for each purchased item Shape must be (batch_size,) |
required |
price_batch |
ndarray
|
Batch of prices (floats) for each purchased item Shape must be (batch_size,) |
required |
available_item_batch |
ndarray
|
Batch of availability matrices (indicating the availability (1) or not (0) of the products) (arrays) for each purchased item Shape must be (batch_size, n_items) |
required |
Returns:
Name | Type | Description |
---|---|---|
item_utilities |
Tensor
|
Utility of all the items in item_batch Shape must be (batch_size,) |
Source code in choice_learn/basket_models/alea_carta.py
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
|
get_negative_samples(available_items, purchased_items, next_item, n_samples)
Sample randomly a set of items.
(set of items not already purchased and not necessarily from the basket)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
available_items |
ndarray
|
Matrix indicating the availability (1) or not (0) of the products Shape must be (n_items,) |
required |
purchased_items |
ndarray
|
List of items already purchased (already in the basket) |
required |
next_item |
int
|
Next item (to be added in the basket) |
required |
n_samples |
int
|
Number of samples to draw |
required |
Returns:
Type | Description |
---|---|
list[int]
|
Random sample of items, each of them distinct from the next item and from the items already in the basket |
Source code in choice_learn/basket_models/alea_carta.py
instantiate(n_items, n_stores=0)
Instantiate the Shopper model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_items |
int
|
Number of items to consider, i.e. the number of items in the dataset |
required |
n_stores |
int
|
Number of stores in the population |
0
|
Source code in choice_learn/basket_models/alea_carta.py
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 |
|