Gain Loss Analysis
Gain loss (switching) analysis of customer movement between a focus and a comparison group across two periods.
Pandas-only; period and group selectors are positional boolean masks.
GainLoss
Gain loss (switching) analysis between a focus group and a comparison group.
Results are computed at initialization; there are no df/table attributes.
gain_loss_df has one row per customer, indexed by customer_id (by
(group_col, customer_id) when grouped), so customer_id is not a column. Its 15
columns are focus/comparison/total for _p1, _p2 and _diff, then the
six components new, lost, increased_focus, decreased_focus,
switch_from_comparison and switch_to_comparison, which sum to focus_diff.
Customers whose values are all 0 are dropped.
gain_loss_table_df has the same columns summed over customers: one row when
ungrouped, one row per group_col value otherwise.
Source code in openretailscience/analysis/gain_loss.py
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 | |
__init__(df, p1_index, p2_index, focus_group_index, focus_group_name, comparison_group_index, comparison_group_name, group_col=None, value_col=None, agg_func='sum')
Calculate the gain loss decomposition at the customer level.
The four masks have one entry per row of df and are matched by position, so df
may repeat index labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Transactions with the option-derived customer_id column and |
required |
p1_index
|
list[bool] | ndarray | Series
|
Rows in the first period. |
required |
p2_index
|
list[bool] | ndarray | Series
|
Rows in the second period. |
required |
focus_group_index
|
list[bool] | ndarray | Series
|
Rows in the focus group. |
required |
focus_group_name
|
str
|
Display name of the focus group. |
required |
comparison_group_index
|
list[bool] | ndarray | Series
|
Rows in the comparison group. |
required |
comparison_group_name
|
str
|
Display name of the comparison group. |
required |
group_col
|
str | None
|
Optional column to break the analysis down by. |
None
|
value_col
|
str | None
|
Value column to aggregate; defaults to option |
None
|
agg_func
|
str
|
A pandas groupby aggregation name, such as "sum", "mean" or "nunique" (case-sensitive). Defaults to "sum". |
'sum'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If a mask is not boolean. |
ValueError
|
If |
ValueError
|
If |
ValueError
|
If a mask does not have one entry per row of |
ValueError
|
If the option-derived customer_id or |
Source code in openretailscience/analysis/gain_loss.py
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 | |
plot(title=None, eyebrow=None, subtitle=None, x_label=None, y_label=None, ax=None, source_text=None, move_legend_outside=False, **kwargs)
Plot the gain loss table as a stacked horizontal bar of the six decomposition columns.
Green segments are new / increased_focus / switch_from_comparison; red
segments are lost / decreased_focus / switch_to_comparison. Legend labels
use the focus and comparison group names. stacked and color kwargs are
silently ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str | None
|
Plot title; defaults to "Gain Loss from {focus} to {comparison}". |
None
|
eyebrow
|
str | None
|
Small uppercase label rendered above the title. |
None
|
subtitle
|
str | None
|
Supporting copy rendered below the title. |
None
|
x_label
|
str | None
|
X-axis label; defaults to the value column name. |
None
|
y_label
|
str | None
|
Y-axis label; defaults to the focus group name, or |
None
|
ax
|
Axes | None
|
Axes to plot on. |
None
|
source_text
|
str | None
|
Source annotation. |
None
|
move_legend_outside
|
bool
|
Whether to move the legend outside the plot. |
False
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to the bar plot. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
SubplotBase |
SubplotBase
|
The plot axes. |
Source code in openretailscience/analysis/gain_loss.py
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 | |
process_customer_group(focus_p1, comparison_p1, focus_p2, comparison_p2, focus_diff, comparison_diff)
staticmethod
Decompose a group's focus-period change into the six gain loss components.
Returns (new, lost, increased_focus, decreased_focus, switch_from_comparison,
switch_to_comparison) in that order. new, increased_focus, and
switch_from_comparison are >= 0; lost, decreased_focus, and
switch_to_comparison are <= 0. When both groups are zero in p1 the entire
focus p2 value counts as new; when both are zero in p2 the entire focus
p1 value counts as lost.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
focus_p1
|
float
|
Focus group value in period 1. |
required |
comparison_p1
|
float
|
Comparison group value in period 1. |
required |
focus_p2
|
float
|
Focus group value in period 2. |
required |
comparison_p2
|
float
|
Comparison group value in period 2. |
required |
focus_diff
|
float
|
Focus group change, p2 - p1. |
required |
comparison_diff
|
float
|
Comparison group change, p2 - p1. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float, float, float, float, float]
|
tuple[float, float, float, float, float, float]: The six components, in the unpack order above. |