AllUniqueError
Bases: ValueError
When there are no hierchical columns because everything is unique within it.
Source code in wt_ml/layers/hier_embedding.py
21 22 |
|
BetaGammaDecay
Bases: Module
Class to learn decayed impacts for the ensuing time periods after a spend in a media vehicle
Source code in wt_ml/layers/beta_gamma_decay.py
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 |
|
__call__(batch, training=False, debug=False, skip_metrics=False)
Calculate decays, total impacts using the learned beta gamma parameters
Parameters:
Name | Type | Description | Default |
---|---|---|---|
impact_by_signal_instant |
TensorLike
|
Instant impacts |
required |
hierarchy |
dict[str, TensorLike]
|
Hierarchy placeholder for Hierarchial embedding variable. |
required |
training |
bool
|
Whether this is a training or inference run. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
BetaGammaDecayIntermediaries |
BetaGammaDecayIntermediaries
|
Intermediate calculations for beta gamma decay - beta, gamma, impacts etc. |
Source code in wt_ml/layers/beta_gamma_decay.py
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 |
|
__init__(encodings, hierarchy_categories=None, hyperparameters=None, name=None)
Creates a betagammadecay object to learn decayed impacts using beta and gamma parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hierarchy |
DataFrame
|
The hierarchy that the impact learns on. |
required |
hyperparameters |
Hyperparams | None
|
Dictionary of hyperparameters for buidling this layer. Defaults to None. |
None
|
name |
str | None
|
Name of the layer. Defaults to None. |
None
|
Source code in wt_ml/layers/beta_gamma_decay.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
build(input_shapes)
Build the layer parameters needed for calculating decays.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_shapes |
InputShapes
|
The effect and hierarchy shapes. |
required |
Source code in wt_ml/layers/beta_gamma_decay.py
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 |
|
HierchicalEmbedding
Bases: Module
Hierarchical Embedding creates embeddings for a layer with different input hierarchy levels as trainable weights such that the deviations from the expected deviations are penalized. These trained embeddings are used to calculate the model parameters for a layer.
Source code in wt_ml/layers/hier_embedding.py
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 |
|
__call__(hierarchy, training=False, debug=False, skip_metrics=False)
Returns the model parameters' embeddings calculated from the weights. Adds l2 regularization penalties to loss based on deviations and bias.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hierarchy |
dict[str, TensorLike]
|
Hierarchy placeholder for Hierarchial embedding variable. |
required |
training |
bool
|
Whether this is a training or inference run. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
tf.Tensor: Model parameters' embeddings. |
Source code in wt_ml/layers/hier_embedding.py
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 |
|
__init__(shape, encodings, columns=None, use_bias=True, dropped_columns=[], initializer=0.0, bias_initializer=0.0, hyperparameters=None, feature_names=None, name=None, increase_lr=None)
Initializes the hierarchical embedding object with hierarchy levels, parameter shape and other initializers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shape |
list[int]
|
Desired dimensions of model parameters only within final result. |
required |
hierarchy |
DataFrame
|
The hierarchy for which embeddings are trained. |
required |
columns |
list[str | list[str]] | None
|
Hierarchy levels to learn embeddings. Defaults to None. |
None
|
use_bias |
bool
|
Whether to include bias. Defaults to True. |
True
|
dropped_columns |
list
|
Columns to exclude in hierarchy. Defaults to ["granular", "region", "coastal", "populationdensity", "medianage"]. |
[]
|
initializer |
Initializer
|
Initializer for embeddings(weights). Defaults to 0.0. |
0.0
|
bias_initializer |
Initializer
|
Initializer for bias. Defaults to 0.0. |
0.0
|
hyperparameters |
Hyperparams | None
|
Dictionary of hyperparameters for buidling this layer. Defaults to None. |
None
|
name |
str | None
|
Name of the layer. Defaults to None. |
None
|
Source code in wt_ml/layers/hier_embedding.py
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 |
|
build(input_shapes)
Builds hyperparamters, deviations, embeddings(weights), bias and other intermediate variables.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_shapes |
InputShapes
|
The effect and hierarchy shapes. |
required |
Raises:
Type | Description |
---|---|
AllUniqueError
|
When there are no hierchical columns because everything is unique within it. |
Source code in wt_ml/layers/hier_embedding.py
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 |
|
get_dfs(dy_dweights=None, dy_dbias=None)
Get the learned weights for a HierarchicalEmbedding layer as a DataFrame
Source code in wt_ml/layers/hier_embedding.py
548 549 550 551 552 553 554 555 556 557 |
|
get_hierarchical_parameters(hierarchy)
Returns the model parameters' for every hierarchical level (non-aggregated weights)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hierarchy |
dict[str, TensorLike]
|
Hierarchy placeholder for Hierarchial embedding variable. |
required |
NOTE: this currently does not depend on training flag. Possible we change how things work such that it will.
Returns:
tuple[tf.Tensor, tf.Tensor]: weights, indices
the 1st list[tf.Tensor]=A: A[i] corresponds to the multiplicative data for the continuous aspects
of the hierarchy in self.columns[i]
the 2nd list[tf.Tensor]=B: B[i] corresponds to the indices in self.weights that corresponds to the
correct learned coefficients of the hierarchy in self.columns[i]
Source code in wt_ml/layers/hier_embedding.py
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 |
|
get_reg_config(col_names)
Creates name used for regularization and default value for the penalty multiplier. If all columns are categorical, we can just join their names in order to find penalty. Otherwise, when different continuous features are paired with a same categorical column, the resulting hierarchical categories share same penalty. Always, suffix the continuous string to the end of the name. Example: brand-DEM and brand-GOP have the same penalty called reg_brand-continuous. Examples of mixed categories: reg_brand-continuous, reg_vehicle-continuous.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
col_names |
list[str]
|
Hierarchical column names. |
required |
Returns:
Type | Description |
---|---|
tuple[str, float]
|
tuple[str, float]: Regularization penalty name and the default value. |
Source code in wt_ml/layers/hier_embedding.py
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 |
|
get_reg_mult(col_names)
Returns the penalty multiplier for hierarchy level reg loss.
Source code in wt_ml/layers/hier_embedding.py
271 272 273 274 275 276 277 278 279 |
|
get_tensors(dy_dweights=None, dy_dbias=None)
Get the learned weights for a HierarchicalEmbedding layer
Source code in wt_ml/layers/hier_embedding.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
|
stitched_cols(col_names)
Returns a string representation of the columns.
Source code in wt_ml/layers/hier_embedding.py
241 242 243 |
|
LinearBaseline
Bases: Module
Source code in wt_ml/layers/linear_baseline.py
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 |
|
__call__(batch, training=False, debug=False, skip_metrics=False)
Calcuate baseline using slope-intercept form (y=mx+c).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dates_since_start |
TensorLike
|
Number of timestamps since the last restart. shape = num_time x num_granular. |
required |
sales_num_restarts |
TensorLike
|
Number of restarts that occurred before this point. shape = num_time x num_granular. |
required |
hierarchy |
dict[str, TensorLike]
|
The lookup tables for categorical values. |
required |
mask |
TensorLike
|
Filter for 0 sales or unrealistic sales. |
required |
training |
bool
|
Whether training the layer parameters or not. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
LinearBaselineIntermediaries |
LinearBaselineIntermediaries
|
Intermediate calculations for baseline like slope, intercept, etc. |
Source code in wt_ml/layers/linear_baseline.py
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 |
|
__init__(starting_sales, num_starts, encodings, hyperparameters=None, name=None)
Class initialization to create linear regression lines for calculating baseline, for each granularity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
starting_sales |
ndarray
|
Sales at the start of each baseline. shape = num_starts x num_granularity. |
required |
num_starts |
int
|
No. of starting points for each granularity. |
required |
hyperparameters |
Hyperparams
|
All hyperparameters. |
None
|
name |
str | None
|
Name of the layer. Defaults to None. |
None
|
Source code in wt_ml/layers/linear_baseline.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
build(input_shapes)
Build the layer parameters needed for calculating linear baseline.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_shapes |
Tuple[Tensor, ...]
|
Tuple of tensor shapes of |
required |
Source code in wt_ml/layers/linear_baseline.py
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 |
|
MonotonicPositiveUnboundedLayer
Bases: Module
, IMixedEffect
Source code in wt_ml/layers/monotonic_positive_unbounded.py
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 |
|
__init__(encodings, signal_type, hierarchy_categories=None, has_time=False, has_signal=False, hyperparameters=None, non_pos=False, non_neg=False, non_pos_by_signal=None, non_neg_by_signal=None, maximum_strength=None, use_bias=None, increase_lr=None, name=None)
Monotonic multiplicative factors affecting sales that also scales ROIs of investments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hierarchy |
DataFrame
|
The hierarchy that the impact learns on. |
required |
n_instances |
int
|
Number of mixed effect signals. Axis index 2 of effect. |
required |
has_time |
bool
|
Whether the hierarchy is on the time axis. Defaults to False. |
False
|
hyperparameters |
Hyperparams
|
Dictionary of hyperparameters for buidling this layer. |
None
|
name |
str | None
|
Name of the mixed effect captured. Module parent class sets to name of class if passes as None. |
None
|
Source code in wt_ml/layers/monotonic_positive_unbounded.py
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 |
|
build(input_shapes)
Builds the sales_mult hierarchical variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_shapes |
InputShapes
|
The effect and hierarchy shapes. |
required |
Source code in wt_ml/layers/monotonic_positive_unbounded.py
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 |
|
Pricing
Bases: Module
, IMixedEffect
Source code in wt_ml/layers/pricing.py
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 |
|
__call__(batch, training=False, debug=False, skip_metrics=False)
Pricing Layer Forward Propagation.
We take in the mean normalized \(price\) signal of shape (num_time, num_granular, n_sim)
.
Then we take the \(offset\) and \(exponent\) learnt by the model, each of shape (num_granular,)
.
The impact is calculated as follows:
\(volume = \frac{normalization\_mult} {(price + offset) ^ {exponent}}\)
\(normalization\_{mult} = (1 + offset) ^ {exponent}\)
\(impact = volume * price\)
This \(impact\) is of shape (num_time, num_granular, n_sim)
NOTE: normalization_mult is a factor to neglect the impact of prices which equal the average price.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
price |
TensorLike
|
mean normalized price_per_hl for each granularity each week. Shape: (num_time, num_granular, n_sim) |
required |
hierarchy |
dict[str, TensorLike]
|
Hierarchical Placeholder for creating hierarchical variable. |
required |
training |
bool
|
Whether this is a training or inference run. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
PricingIntermediaries |
PricingIntermediaries
|
Intermediate calculations like offset, asymptote, exponent, etc., and final impact. |
Source code in wt_ml/layers/pricing.py
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 |
|
__init__(encodings, hierarchy_categories=None, hyperparameters=None, name=None)
Multiplicative price elasticity factor affecting baseline sales that also scales ROI of investments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hierarchy |
DataFrame
|
The hierarchy used to build features learnt by the model to generate impacts. |
required |
hyperparameters |
Hyperparams | None
|
An instance of |
None
|
name |
str | None
|
Name of the Pricing Layer.
|
None
|
Source code in wt_ml/layers/pricing.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
build(input_shapes)
Builds the price_params_emb_layer
hierarchical variable
for generating price elasticity curve for each granularity.
Shape of the variable: (num_granular, 2). 2 denotes offset and exponent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_shapes |
InputShapes
|
A tuple of tensor shapes of |
required |
Source code in wt_ml/layers/pricing.py
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 |
|
apply_impacts(baseline, multiplicative_impacts, additive_impacts)
Apply the impacts on top of the basline to get yhat.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
baseline |
Tensor
|
The baseline impact. This will be the starting point where impacts are applied on. |
required |
multiplicative_impacts |
list[Tensor]
|
These impacts scales the baseline multiplicatively (larger effect). |
required |
additive_impacts |
list[Tensor]
|
These impacts, increase the baseline additively (smaller effect). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
tf.Tensor: The yhat after applying all the impacts on the baseline. |
Source code in wt_ml/layers/impact_utils.py
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 |
|
apply_inverse_impacts(y, multiplicative_impacts, additive_impacts)
Remove the impacts from y
to get the baseline back.
baseline = (yhat - Σ(additive effects)) / ∏(multiplicative effects)
Here its inverse impacts, i.e.,
inverse multiplicative effect = 1/multiplicative effect inverse additive effect = -1 * additive effect
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y |
Tensor
|
The |
required |
multiplicative_impacts |
list[Tensor]
|
Multiplicative impacts, are scaled down (larger effect). |
required |
additive_impacts |
list[Tensor]
|
Additive impacts, are subtracted off the |
required |
Returns:
Type | Description |
---|---|
Tensor
|
tf.Tensor: The baseline after all the impacts are removed from |
Source code in wt_ml/layers/impact_utils.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|