Revenue Tree
Decompose period-over-period revenue change into per-factor contributions.
Revenue = customers x spend per customer = customers x transactions per customer x
spend per transaction, extended with units per transaction x price per unit when
unit_quantity is present (those conditional columns define the .df output).
Provides RevenueTree (with .draw_tree()) and calc_tree_kpis.
RevenueTree
Period-over-period revenue decomposition with per-factor contributions.
df (pandas) has one row per group: a single row labelled 0 when ungrouped, a
CategoricalIndex for one group_col and a MultiIndex for several. A group
missing from one period has 0 customers, transactions and spend for it. Columns are
{metric}_p1 / _p2 / _diff / _pct_diff for customers, transactions,
spend, spend_per_customer, transactions_per_customer and
spend_per_transaction (plus units, units_per_transaction and
price_per_unit with quantity data), {metric}_contrib for the tree's factors, and
frequency_elasticity / price_elasticity. _pct_diff is a fraction (0.1 = +10%).
See calc_tree_kpis for the formulas. draw_tree() renders one row as a diagram.
Source code in openretailscience/analysis/revenue_tree.py
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 | |
__init__(df, period_col, p1_value, p2_value, group_col=None)
Initialize the Revenue Tree and compute the KPI table immediately.
If the input contains the option-derived column.unit_quantity column,
units/price/price-elasticity metrics are added.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | Table
|
Transaction data with the option-derived customer_id, transaction_id, unit_spend, and (optional) unit_quantity columns. |
required |
period_col
|
str
|
Column holding a caller-assigned period label per row (e.g.
|
required |
p1_value
|
str | int | date
|
Label of the first (base) period. |
required |
p2_value
|
str | int | date
|
Label of the second period. |
required |
group_col
|
str | list[str] | None
|
Column(s) to break the analysis down by. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the required columns are not present in the data, if
|
IbisTypeError
|
If |
Source code in openretailscience/analysis/revenue_tree.py
draw_tree(row_index=0, value_labels=None, unit_spend_label='Revenue', customer_id_label='Customers', spend_per_customer_label='Spend / Customer', transactions_per_customer_label='Visits / Customer', spend_per_transaction_label='Spend / Visit', units_per_transaction_label='Units / Visit', price_per_unit_label='Price / Unit')
Draw the revenue tree diagram for one row of df.
The Units / Visit and Price / Unit nodes render only when the input contained the unit_quantity column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row_index
|
int
|
Row of |
0
|
value_labels
|
tuple[str, str] | None
|
(current, previous) = (p2_value, p1_value) labels — easy to reverse. Defaults to ("Current Period", "Previous Period"). |
None
|
unit_spend_label
|
str
|
Header for the Revenue node. |
'Revenue'
|
customer_id_label
|
str
|
Header for the Customers node. |
'Customers'
|
spend_per_customer_label
|
str
|
Header for the Spend / Customer node. |
'Spend / Customer'
|
transactions_per_customer_label
|
str
|
Header for the Visits / Customer node. |
'Visits / Customer'
|
spend_per_transaction_label
|
str
|
Header for the Spend / Visit node. |
'Spend / Visit'
|
units_per_transaction_label
|
str
|
Header for the Units / Visit node. |
'Units / Visit'
|
price_per_unit_label
|
str
|
Header for the Price / Unit node. |
'Price / Unit'
|
Returns:
| Type | Description |
|---|---|
Axes
|
matplotlib.axes.Axes: The matplotlib axes containing the tree visualization. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If |
Source code in openretailscience/analysis/revenue_tree.py
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 | |
calc_tree_kpis(p1_df, p2_df)
Calculate the revenue-tree KPIs from per-period aggregates.
Each frame has one row per group with the option-derived columns column.agg.customer_id,
column.agg.transaction_id, column.agg.unit_spend and, optionally,
column.agg.unit_quantity. Rows are paired across periods by index label; a group
missing from one frame gets 0 counts for that period. For an ungrouped comparison, pass
one row per period with the same label (e.g. both indexed 0).
Output columns are {metric}_p1 / _p2 / _diff / _pct_diff (suffixes
from the column.suffix.* options) plus {metric}_contrib contribution
columns (quantity/price columns present only when units were) and the elasticity
columns (price_elasticity only with units). pct_diff = diff / p1, with p1 = 0
giving NaN. The elasticity columns use ARC (midpoint) percentage changes: price =
%delta units / %delta price per unit, frequency = %delta transactions per customer /
%delta spend per customer.
Each _contrib is in spend units: the factor's change times the period midpoints
of its sibling and of each ancestor's sibling. With C = customers, S = spend per customer,
T = transactions per customer, A = spend per transaction, U = units per
transaction, P = price per unit, dX = X_p2 - X_p1 and mX = (X_p1 + X_p2) / 2:
customers dC * mS
spend_per_customer dS * mC
transactions_per_customer dT * mA * mC
spend_per_transaction dA * mT * mC
units_per_transaction dU * mP * mT * mC
price_per_unit dP * mU * mT * mC
Siblings sum exactly to their parent's contribution (the top pair to spend_diff)
and an unchanged factor contributes 0. This is a level-by-level split, not the
three-factor Shapley value of C x T x A.
When a count is zero, the ratio inputs (S, T, A, U, P) change:
- The
_p1/_p2columns show a ratio over a zero count as NaN. - A period with nothing in it (0 / 0, e.g. a group that opened or closed) takes the
other period's ratio, so that ratio's change is 0 and the group's whole change lands
on
customers. - A non-zero amount over a zero count (spend with no units, or with no customers because every customer_id is null) can't be split into those factors, so the contributions of that ratio, its sibling and every factor below them are NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p1_df
|
DataFrame
|
Period 1 aggregates, one row per group. |
required |
p2_df
|
DataFrame
|
Period 2 aggregates, one row per group. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: One row per group, sorted by group, with the period, difference, percent-difference, and contribution columns described above. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If a required column is missing, a frame repeats an index label, or only one
frame has |
Source code in openretailscience/analysis/revenue_tree.py
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 | |