Monotonic & Economic Sign Constraints¶
monotonicity
¶
Econometric monotonicity & sign-constraint resolution and solver utilities.
Provides declarative sign-constraint validation, automatic covariate direction
inference (e.g., price* -> -1, promo*/discount* -> +1), feature-order
constraint vector alignment for gradient-boosted trees (LightGBM, XGBoost,
CatBoost), and bounded Tikhonov-regularized least squares
(:func:scipy.optimize.lsq_linear) for regression_lags / ridge_lags.
audit_scenario_monotonicity
¶
Audit simulate_scenarios predictions for directional monotonicity compliance.
Evaluates every (model_type, scenario) pair in sim_df (excluding "baseline")
and checks whether delta_yhat = yhat - baseline_yhat respects the expected economic
response direction implied by constraints and the scenario shock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim_df
|
DataFrame
|
Output DataFrame returned by :func: |
required |
constraints
|
Mapping[str, int] | None
|
Optional |
None
|
scenarios
|
Mapping[str, Mapping[str, Any]] | None
|
Optional scenario specification dictionary passed to |
None
|
tol
|
float
|
Numerical tolerance for directional comparison (default |
1e-06
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Summary DataFrame with columns |
DataFrame
|
|
DataFrame
|
|
Source code in src/scale_forecasting/monotonicity.py
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 | |
build_monotonic_constraint_vector
¶
Align a constraint map or sequence with an ordered design-matrix feature list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_names
|
Sequence[str]
|
Ordered feature names corresponding to columns of the
tabular design matrix |
required |
constraints
|
Mapping[str, int] | Sequence[int] | None
|
Either a |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
List of integers in |
Source code in src/scale_forecasting/monotonicity.py
fit_sign_constrained_ridge
¶
Fit an intercept-augmented L2-regularized linear model with sign constraints.
Follows the convention of RegressionLagsModel._Ridge where column 0
of the augmented design matrix is the unpenalized intercept and columns
1..d correspond to the d features in x.
When constraint_vec is empty or all zeros, uses the exact normal
equations (X'X + alpha * I_0)^(-1) X'y. When any entry in
constraint_vec is -1 or +1, solves the equivalent stacked
bounded least-squares problem via :func:scipy.optimize.lsq_linear with
Tikhonov augmentation sqrt(alpha) * diag(0, 1, ..., 1) and box bounds
[-inf, 0] (for -1) or [0, +inf] (for +1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Design matrix of shape |
required |
y
|
ndarray
|
Target vector of shape |
required |
alpha
|
float
|
Non-negative L2 regularization strength. |
required |
constraint_vec
|
Sequence[int] | None
|
Optional sequence of |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Weight vector |
ndarray
|
intercept and |
Source code in src/scale_forecasting/monotonicity.py
infer_monotonic_constraints
¶
Infer economic sign constraints from covariate column names.
Applies standard demand-theory sign priors:
- Price covariates (price, unit_price, retail_price, *_price,
price_*) map to -1 (downward-sloping demand).
- Promotional / discount covariates (promo, promo_*, *_promo,
discount*, markdown*) map to +1 (non-negative promotional lift).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Sequence[str]
|
Candidate exogenous covariate column names. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Dictionary mapping recognized economic covariates to |
Source code in src/scale_forecasting/monotonicity.py
infer_shock_sign_from_spec
¶
Infer directional shock sign (+1, -1, or 0) from a shock specification.
Source code in src/scale_forecasting/monotonicity.py
list_monotonic_models
¶
Return sorted registered model names supporting training-time monotonic constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
available_only
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted list of canonical model names with |
list[str]
|
|
Source code in src/scale_forecasting/monotonicity.py
resolve_model_monotonic_vector
¶
resolve_model_monotonic_vector(
feature_names,
params=None,
ctx_constraints=None,
*,
recursive_lags_nonnegative=False,
)
Extract and align the monotonic constraint vector for a model's fit() call.
Checks params["monotonic_constraints"], params["monotone_constraints"],
and ctx_constraints (from ModelContext.monotonic_constraints).
When recursive_lags_nonnegative=True and at least one feature has a
non-zero constraint, autoregressive target lags (lag_<k>) default to
+1 so multi-step recursive rollouts preserve exogenous monotonicity
by composition of monotone functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_names
|
Sequence[str]
|
Ordered feature names of the model's design matrix. |
required |
params
|
Mapping[str, Any] | None
|
Model parameter dictionary ( |
None
|
ctx_constraints
|
Mapping[str, int] | None
|
Context-level monotonic constraints ( |
None
|
recursive_lags_nonnegative
|
bool
|
Whether to constrain unmapped |
False
|
Returns:
| Type | Description |
|---|---|
list[int]
|
List of integers in |
Source code in src/scale_forecasting/monotonicity.py
resolve_monotonic_constraints
¶
resolve_monotonic_constraints(
cfg_constraints=None,
param_constraints=None,
candidate_columns=(),
*,
infer_if_empty=False,
)
Resolve effective monotonic constraints from config, model params, and inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg_constraints
|
Mapping[str, Any] | None
|
Constraints declared on |
None
|
param_constraints
|
Mapping[str, Any] | None
|
Model-specific override dictionary in |
None
|
candidate_columns
|
Sequence[str]
|
Known future and past covariate columns eligible for
automatic sign inference when |
()
|
infer_if_empty
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Effective |
Source code in src/scale_forecasting/monotonicity.py
validate_monotonic_constraints_map
¶
Validate and normalize a {column: -1 | 0 | 1} constraint dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
Mapping[str, Any] | None
|
Mapping of feature column names to directional sign constraints
( |
required |
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Normalized |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any key is empty/non-string or any value is not in
|