eb-optimization 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- eb_optimization-0.1.0/LICENSE +28 -0
- eb_optimization-0.1.0/PKG-INFO +117 -0
- eb_optimization-0.1.0/README.md +82 -0
- eb_optimization-0.1.0/pyproject.toml +104 -0
- eb_optimization-0.1.0/setup.cfg +4 -0
- eb_optimization-0.1.0/src/eb_optimization/__init__.py +38 -0
- eb_optimization-0.1.0/src/eb_optimization/_utils.py +122 -0
- eb_optimization-0.1.0/src/eb_optimization/policies/__init__.py +74 -0
- eb_optimization-0.1.0/src/eb_optimization/policies/cost_ratio_policy.py +294 -0
- eb_optimization-0.1.0/src/eb_optimization/policies/ral_policy.py +129 -0
- eb_optimization-0.1.0/src/eb_optimization/policies/tau_policy.py +156 -0
- eb_optimization-0.1.0/src/eb_optimization/search/__init__.py +27 -0
- eb_optimization-0.1.0/src/eb_optimization/search/grid.py +91 -0
- eb_optimization-0.1.0/src/eb_optimization/search/kernels.py +115 -0
- eb_optimization-0.1.0/src/eb_optimization/search/results.py +0 -0
- eb_optimization-0.1.0/src/eb_optimization/tuning/__init__.py +27 -0
- eb_optimization-0.1.0/src/eb_optimization/tuning/cost_ratio.py +270 -0
- eb_optimization-0.1.0/src/eb_optimization/tuning/ral.py +144 -0
- eb_optimization-0.1.0/src/eb_optimization/tuning/sensitivity.py +319 -0
- eb_optimization-0.1.0/src/eb_optimization/tuning/tau.py +510 -0
- eb_optimization-0.1.0/src/eb_optimization.egg-info/PKG-INFO +117 -0
- eb_optimization-0.1.0/src/eb_optimization.egg-info/SOURCES.txt +23 -0
- eb_optimization-0.1.0/src/eb_optimization.egg-info/dependency_links.txt +1 -0
- eb_optimization-0.1.0/src/eb_optimization.egg-info/requires.txt +16 -0
- eb_optimization-0.1.0/src/eb_optimization.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Kyle Corrie
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: eb-optimization
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Electric Barometer: Optimization and tuning utilities for EB objectives and policy parameters.
|
|
5
|
+
Author-email: "Kyle Corrie (Economistician)" <kcorrie@economistician.com>
|
|
6
|
+
License-Expression: BSD-3-Clause
|
|
7
|
+
Project-URL: Homepage, https://github.com/Economistician/eb-optimization
|
|
8
|
+
Project-URL: Repository, https://github.com/Economistician/eb-optimization
|
|
9
|
+
Project-URL: Issues, https://github.com/Economistician/eb-optimization/issues
|
|
10
|
+
Project-URL: Documentation, https://github.com/Economistician/eb-docs
|
|
11
|
+
Keywords: electric-barometer,optimization,tuning,grid-search,calibration,asymmetric-loss,forecasting,pandas
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: numpy>=1.24
|
|
23
|
+
Requires-Dist: pandas>=2.0
|
|
24
|
+
Provides-Extra: eb
|
|
25
|
+
Requires-Dist: eb-metrics<0.3,>=0.2; extra == "eb"
|
|
26
|
+
Requires-Dist: eb-evaluation<0.3,>=0.2; extra == "eb"
|
|
27
|
+
Provides-Extra: opt
|
|
28
|
+
Provides-Extra: test
|
|
29
|
+
Requires-Dist: pytest>=8.0; extra == "test"
|
|
30
|
+
Requires-Dist: scikit-learn>=1.3; extra == "test"
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest-cov>=5.0; extra == "dev"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
# Electric Barometer · Optimization (`eb-optimization`)
|
|
37
|
+
|
|
38
|
+
Decision and policy layer for the Electric Barometer ecosystem, responsible for tuning, calibration, and governed parameter selection.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Overview
|
|
43
|
+
|
|
44
|
+
This repository contains the optimization, tuning, and policy governance layer of the Electric Barometer ecosystem. It defines how key evaluation parameters—such as cost ratios, tolerances, and readiness controls—are selected from data, validated under governance rules, and formalized into deterministic policies that can be reused across systems and environments.
|
|
45
|
+
|
|
46
|
+
Rather than computing metrics or running evaluations, this repository focuses on decision logic: how parameters are calibrated, how tradeoffs are resolved, and how those decisions are frozen into auditable artifacts. It provides the bridge between metric theory and operational deployment, ensuring that forecast evaluation behavior is consistent, explainable, and governed by explicit intent rather than ad-hoc configuration.
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Role in the Electric Barometer Ecosystem
|
|
51
|
+
|
|
52
|
+
`eb-optimization` defines the parameter selection, calibration, and governance logic used throughout the Electric Barometer ecosystem. It is responsible for determining how key operational parameters—such as cost ratios, tolerance bands, and readiness controls—are selected from data in a disciplined, reproducible, and decision-aware manner.
|
|
53
|
+
|
|
54
|
+
This repository focuses exclusively on optimization mechanics and policy formation. It does not define metric primitives, perform evaluation orchestration, manage model interfaces, or execute runtime decision logic. Those responsibilities are handled by adjacent layers in the ecosystem that compute metrics, evaluate forecasts, or apply frozen policies in production workflows.
|
|
55
|
+
|
|
56
|
+
By separating parameter selection and governance from metric semantics and execution concerns, eb-optimization provides a stable optimization layer that enables consistent calibration, transparent decision rules, and auditable policy artifacts across heterogeneous forecasting and operational contexts.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Installation
|
|
61
|
+
|
|
62
|
+
`eb-optimization` is distributed as a standard Python package.
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install eb-optimization
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## Core Concepts
|
|
71
|
+
|
|
72
|
+
- **Parameter governance** — Operational parameters (e.g., cost ratios, tolerances) should be selected through explicit, reproducible rules rather than ad-hoc tuning or implicit defaults.
|
|
73
|
+
- **Search over candidate spaces** — Optimization is framed as deterministic search over bounded, interpretable candidate sets, enabling transparent tradeoffs and stable outcomes.
|
|
74
|
+
- **Cost balance calibration** — Asymmetric operational costs can be balanced by selecting parameters that equalize or appropriately trade off opposing risk exposures.
|
|
75
|
+
- **Tolerance selection from residuals** — Acceptable error bands can be learned directly from historical performance, reflecting empirical system behavior rather than arbitrary thresholds.
|
|
76
|
+
- **Policy separation** — Calibration logic is separated from frozen policy artifacts so that parameter selection is auditable, versioned, and safely applied in downstream systems.
|
|
77
|
+
- **Decision-aligned optimization** — Optimization is evaluated by operational interpretability and governance fitness, not by abstract numerical optimality alone.
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Minimal Example
|
|
82
|
+
|
|
83
|
+
The example below illustrates a typical optimization workflow using `eb-optimization`: calibrating an operational parameter from historical data and applying it via a frozen policy.
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
import numpy as np
|
|
87
|
+
from eb_optimization.policies import (
|
|
88
|
+
CostRatioPolicy,
|
|
89
|
+
apply_cost_ratio_policy,
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
# Historical actuals and forecasts
|
|
93
|
+
y_true = np.array([10, 12, 15, 20])
|
|
94
|
+
y_pred = np.array([9, 14, 18, 17])
|
|
95
|
+
|
|
96
|
+
# Define a frozen cost-ratio policy
|
|
97
|
+
policy = CostRatioPolicy(
|
|
98
|
+
R_grid=(0.5, 1.0, 2.0, 3.0),
|
|
99
|
+
co=1.0,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
# Estimate a global cost ratio R
|
|
103
|
+
R, diagnostics = apply_cost_ratio_policy(
|
|
104
|
+
y_true=y_true,
|
|
105
|
+
y_pred=y_pred,
|
|
106
|
+
policy=policy,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
print(R)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
BSD 3-Clause License.
|
|
117
|
+
© 2025 Kyle Corrie.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Electric Barometer · Optimization (`eb-optimization`)
|
|
2
|
+
|
|
3
|
+
Decision and policy layer for the Electric Barometer ecosystem, responsible for tuning, calibration, and governed parameter selection.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
|
|
9
|
+
This repository contains the optimization, tuning, and policy governance layer of the Electric Barometer ecosystem. It defines how key evaluation parameters—such as cost ratios, tolerances, and readiness controls—are selected from data, validated under governance rules, and formalized into deterministic policies that can be reused across systems and environments.
|
|
10
|
+
|
|
11
|
+
Rather than computing metrics or running evaluations, this repository focuses on decision logic: how parameters are calibrated, how tradeoffs are resolved, and how those decisions are frozen into auditable artifacts. It provides the bridge between metric theory and operational deployment, ensuring that forecast evaluation behavior is consistent, explainable, and governed by explicit intent rather than ad-hoc configuration.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Role in the Electric Barometer Ecosystem
|
|
16
|
+
|
|
17
|
+
`eb-optimization` defines the parameter selection, calibration, and governance logic used throughout the Electric Barometer ecosystem. It is responsible for determining how key operational parameters—such as cost ratios, tolerance bands, and readiness controls—are selected from data in a disciplined, reproducible, and decision-aware manner.
|
|
18
|
+
|
|
19
|
+
This repository focuses exclusively on optimization mechanics and policy formation. It does not define metric primitives, perform evaluation orchestration, manage model interfaces, or execute runtime decision logic. Those responsibilities are handled by adjacent layers in the ecosystem that compute metrics, evaluate forecasts, or apply frozen policies in production workflows.
|
|
20
|
+
|
|
21
|
+
By separating parameter selection and governance from metric semantics and execution concerns, eb-optimization provides a stable optimization layer that enables consistent calibration, transparent decision rules, and auditable policy artifacts across heterogeneous forecasting and operational contexts.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
`eb-optimization` is distributed as a standard Python package.
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install eb-optimization
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Core Concepts
|
|
36
|
+
|
|
37
|
+
- **Parameter governance** — Operational parameters (e.g., cost ratios, tolerances) should be selected through explicit, reproducible rules rather than ad-hoc tuning or implicit defaults.
|
|
38
|
+
- **Search over candidate spaces** — Optimization is framed as deterministic search over bounded, interpretable candidate sets, enabling transparent tradeoffs and stable outcomes.
|
|
39
|
+
- **Cost balance calibration** — Asymmetric operational costs can be balanced by selecting parameters that equalize or appropriately trade off opposing risk exposures.
|
|
40
|
+
- **Tolerance selection from residuals** — Acceptable error bands can be learned directly from historical performance, reflecting empirical system behavior rather than arbitrary thresholds.
|
|
41
|
+
- **Policy separation** — Calibration logic is separated from frozen policy artifacts so that parameter selection is auditable, versioned, and safely applied in downstream systems.
|
|
42
|
+
- **Decision-aligned optimization** — Optimization is evaluated by operational interpretability and governance fitness, not by abstract numerical optimality alone.
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Minimal Example
|
|
47
|
+
|
|
48
|
+
The example below illustrates a typical optimization workflow using `eb-optimization`: calibrating an operational parameter from historical data and applying it via a frozen policy.
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import numpy as np
|
|
52
|
+
from eb_optimization.policies import (
|
|
53
|
+
CostRatioPolicy,
|
|
54
|
+
apply_cost_ratio_policy,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
# Historical actuals and forecasts
|
|
58
|
+
y_true = np.array([10, 12, 15, 20])
|
|
59
|
+
y_pred = np.array([9, 14, 18, 17])
|
|
60
|
+
|
|
61
|
+
# Define a frozen cost-ratio policy
|
|
62
|
+
policy = CostRatioPolicy(
|
|
63
|
+
R_grid=(0.5, 1.0, 2.0, 3.0),
|
|
64
|
+
co=1.0,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
# Estimate a global cost ratio R
|
|
68
|
+
R, diagnostics = apply_cost_ratio_policy(
|
|
69
|
+
y_true=y_true,
|
|
70
|
+
y_pred=y_pred,
|
|
71
|
+
policy=policy,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
print(R)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
BSD 3-Clause License.
|
|
82
|
+
© 2025 Kyle Corrie.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
######################################
|
|
2
|
+
# Project metadata
|
|
3
|
+
######################################
|
|
4
|
+
[project]
|
|
5
|
+
name = "eb-optimization"
|
|
6
|
+
version = "0.1.0"
|
|
7
|
+
description = "Electric Barometer: Optimization and tuning utilities for EB objectives and policy parameters."
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
license = "BSD-3-Clause"
|
|
11
|
+
license-files = ["LICENSE*"]
|
|
12
|
+
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "Kyle Corrie (Economistician)", email = "kcorrie@economistician.com" }
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
keywords = [
|
|
18
|
+
"electric-barometer",
|
|
19
|
+
"optimization",
|
|
20
|
+
"tuning",
|
|
21
|
+
"grid-search",
|
|
22
|
+
"calibration",
|
|
23
|
+
"asymmetric-loss",
|
|
24
|
+
"forecasting",
|
|
25
|
+
"pandas",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
classifiers = [
|
|
29
|
+
"Programming Language :: Python :: 3",
|
|
30
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
31
|
+
"Programming Language :: Python :: 3.10",
|
|
32
|
+
"Programming Language :: Python :: 3.11",
|
|
33
|
+
"Programming Language :: Python :: 3.12",
|
|
34
|
+
"Programming Language :: Python :: 3.13",
|
|
35
|
+
"Operating System :: OS Independent",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
######################################
|
|
39
|
+
# Core runtime dependencies
|
|
40
|
+
######################################
|
|
41
|
+
# Keep this intentionally lean. eb-optimization should be installable even
|
|
42
|
+
# without pulling the rest of the EB stack unless users opt-in via extras.
|
|
43
|
+
dependencies = [
|
|
44
|
+
"numpy>=1.24",
|
|
45
|
+
"pandas>=2.0",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
######################################
|
|
49
|
+
# Project URLs
|
|
50
|
+
######################################
|
|
51
|
+
[project.urls]
|
|
52
|
+
Homepage = "https://github.com/Economistician/eb-optimization"
|
|
53
|
+
Repository = "https://github.com/Economistician/eb-optimization"
|
|
54
|
+
Issues = "https://github.com/Economistician/eb-optimization/issues"
|
|
55
|
+
Documentation = "https://github.com/Economistician/eb-docs"
|
|
56
|
+
|
|
57
|
+
######################################
|
|
58
|
+
# Optional dependencies (extras)
|
|
59
|
+
######################################
|
|
60
|
+
[project.optional-dependencies]
|
|
61
|
+
|
|
62
|
+
# Pull in EB ecosystem deps when tuning objectives/policies that rely on them
|
|
63
|
+
eb = [
|
|
64
|
+
# Electric Barometer compatibility band (0.2.x)
|
|
65
|
+
"eb-metrics>=0.2,<0.3",
|
|
66
|
+
"eb-evaluation>=0.2,<0.3",
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
# Optional optimization backends (future-proofing)
|
|
70
|
+
# Keep empty for now; add later when you actually introduce them.
|
|
71
|
+
opt = [
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
# CI / test-only dependencies
|
|
75
|
+
test = [
|
|
76
|
+
"pytest>=8.0",
|
|
77
|
+
"scikit-learn>=1.3",
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
# Local developer tooling
|
|
81
|
+
dev = [
|
|
82
|
+
"pytest>=8.0",
|
|
83
|
+
"pytest-cov>=5.0",
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
######################################
|
|
87
|
+
# Build system
|
|
88
|
+
######################################
|
|
89
|
+
[build-system]
|
|
90
|
+
requires = ["setuptools>=64", "wheel"]
|
|
91
|
+
build-backend = "setuptools.build_meta"
|
|
92
|
+
|
|
93
|
+
######################################
|
|
94
|
+
# Package discovery
|
|
95
|
+
######################################
|
|
96
|
+
[tool.setuptools.packages.find]
|
|
97
|
+
where = ["src"]
|
|
98
|
+
|
|
99
|
+
######################################
|
|
100
|
+
# Pytest configuration
|
|
101
|
+
######################################
|
|
102
|
+
[tool.pytest.ini_options]
|
|
103
|
+
pythonpath = ["src"]
|
|
104
|
+
addopts = "-ra"
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
`eb_optimization` — optimization and tuning layer for the Electric Barometer ecosystem.
|
|
5
|
+
|
|
6
|
+
This package contains the **optimization layer** of Electric Barometer:
|
|
7
|
+
|
|
8
|
+
- **search**: generic, reusable search mechanics (grids, tie-breaking kernels)
|
|
9
|
+
- **tuning**: calibration and selection utilities (e.g., cost-ratio tuning, sensitivity sweeps)
|
|
10
|
+
- **policies**: frozen, declarative policy artifacts for downstream execution
|
|
11
|
+
|
|
12
|
+
It intentionally does **not** define metric primitives or evaluation math.
|
|
13
|
+
Those live in `eb-metrics` (and orchestration lives in `eb-evaluation`).
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _resolve_version() -> str:
|
|
20
|
+
"""
|
|
21
|
+
Resolve the installed distribution version.
|
|
22
|
+
|
|
23
|
+
Returns
|
|
24
|
+
-------
|
|
25
|
+
str
|
|
26
|
+
Installed version string. If the distribution is not installed (e.g., running
|
|
27
|
+
from source), returns ``"0.0.0"``.
|
|
28
|
+
"""
|
|
29
|
+
try:
|
|
30
|
+
# Must match the distribution name in pyproject.toml ([project].name)
|
|
31
|
+
return version("eb-optimization")
|
|
32
|
+
except PackageNotFoundError:
|
|
33
|
+
return "0.0.0"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
__version__ = _resolve_version()
|
|
37
|
+
|
|
38
|
+
__all__ = ["__version__"]
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from numpy.typing import ArrayLike
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"to_1d_array",
|
|
8
|
+
"broadcast_param",
|
|
9
|
+
"handle_sample_weight",
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def to_1d_array(x: ArrayLike, name: str) -> np.ndarray:
|
|
14
|
+
"""
|
|
15
|
+
Convert input to a 1D numpy float array.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
x
|
|
20
|
+
Array-like input.
|
|
21
|
+
name
|
|
22
|
+
Name used in error messages.
|
|
23
|
+
|
|
24
|
+
Returns
|
|
25
|
+
-------
|
|
26
|
+
numpy.ndarray
|
|
27
|
+
1D float array.
|
|
28
|
+
|
|
29
|
+
Raises
|
|
30
|
+
------
|
|
31
|
+
ValueError
|
|
32
|
+
If the input is not 1-dimensional.
|
|
33
|
+
"""
|
|
34
|
+
arr = np.asarray(x, dtype=float)
|
|
35
|
+
|
|
36
|
+
if arr.ndim != 1:
|
|
37
|
+
raise ValueError(f"{name} must be a 1D array; got shape {arr.shape}")
|
|
38
|
+
|
|
39
|
+
return arr
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def broadcast_param(x: ArrayLike, shape: tuple[int, ...], name: str) -> np.ndarray:
|
|
43
|
+
"""
|
|
44
|
+
Broadcast a scalar or 1D array parameter to a target shape.
|
|
45
|
+
|
|
46
|
+
Rules
|
|
47
|
+
-----
|
|
48
|
+
- Scalars are expanded to the given shape
|
|
49
|
+
- 1D arrays must exactly match the target shape
|
|
50
|
+
|
|
51
|
+
Parameters
|
|
52
|
+
----------
|
|
53
|
+
x
|
|
54
|
+
Scalar or 1D array parameter.
|
|
55
|
+
shape
|
|
56
|
+
Target shape.
|
|
57
|
+
name
|
|
58
|
+
Name used in error messages.
|
|
59
|
+
|
|
60
|
+
Returns
|
|
61
|
+
-------
|
|
62
|
+
numpy.ndarray
|
|
63
|
+
Float array of shape ``shape``.
|
|
64
|
+
|
|
65
|
+
Raises
|
|
66
|
+
------
|
|
67
|
+
ValueError
|
|
68
|
+
If ``x`` is neither scalar nor matches the target shape.
|
|
69
|
+
"""
|
|
70
|
+
arr = np.asarray(x, dtype=float)
|
|
71
|
+
|
|
72
|
+
if arr.ndim == 0:
|
|
73
|
+
return np.full(shape, float(arr), dtype=float)
|
|
74
|
+
|
|
75
|
+
if arr.shape != shape:
|
|
76
|
+
raise ValueError(
|
|
77
|
+
f"{name} must be scalar or have shape {shape}; got shape {arr.shape}"
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
return arr
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def handle_sample_weight(sample_weight: ArrayLike | None, n: int) -> np.ndarray:
|
|
84
|
+
"""
|
|
85
|
+
Normalize sample weights to a non-negative 1D float array of length n.
|
|
86
|
+
|
|
87
|
+
If ``sample_weight`` is None, returns an array of ones.
|
|
88
|
+
|
|
89
|
+
Parameters
|
|
90
|
+
----------
|
|
91
|
+
sample_weight
|
|
92
|
+
None or a 1D array of non-negative weights.
|
|
93
|
+
n
|
|
94
|
+
Expected length.
|
|
95
|
+
|
|
96
|
+
Returns
|
|
97
|
+
-------
|
|
98
|
+
numpy.ndarray
|
|
99
|
+
1D float array of length n.
|
|
100
|
+
|
|
101
|
+
Raises
|
|
102
|
+
------
|
|
103
|
+
ValueError
|
|
104
|
+
If weights are not length-n, not 1D, or contain negative values.
|
|
105
|
+
"""
|
|
106
|
+
if n <= 0:
|
|
107
|
+
raise ValueError(f"n must be a positive integer; got {n}")
|
|
108
|
+
|
|
109
|
+
if sample_weight is None:
|
|
110
|
+
return np.ones(n, dtype=float)
|
|
111
|
+
|
|
112
|
+
w = np.asarray(sample_weight, dtype=float)
|
|
113
|
+
|
|
114
|
+
if w.ndim != 1 or w.shape[0] != n:
|
|
115
|
+
raise ValueError(
|
|
116
|
+
f"sample_weight must be a 1D array of length {n}; got shape {w.shape}"
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
if np.any(w < 0):
|
|
120
|
+
raise ValueError("sample_weight must be non-negative.")
|
|
121
|
+
|
|
122
|
+
return w
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
Frozen policy artifacts for the Electric Barometer optimization layer.
|
|
5
|
+
|
|
6
|
+
The `eb_optimization.policies` package contains **governance-level, immutable
|
|
7
|
+
configuration objects** that define how tuned parameters are selected and applied
|
|
8
|
+
at runtime.
|
|
9
|
+
|
|
10
|
+
Design principles
|
|
11
|
+
-----------------
|
|
12
|
+
- Policies are **frozen** (dataclass(frozen=True)) and versionable
|
|
13
|
+
- Policies contain **no learning or tuning logic**
|
|
14
|
+
- Policies wrap tuning utilities with deterministic application semantics
|
|
15
|
+
- Policies are safe to ship to production systems
|
|
16
|
+
|
|
17
|
+
Layering
|
|
18
|
+
--------
|
|
19
|
+
- tuning/ : derives parameters from data (calibration, grid search)
|
|
20
|
+
- policies/ : freezes configuration + applies tuning deterministically
|
|
21
|
+
- runtime : consumes policy outputs only (no re-tuning)
|
|
22
|
+
|
|
23
|
+
Exported policies
|
|
24
|
+
-----------------
|
|
25
|
+
- Tau (τ) tolerance governance for HR@τ
|
|
26
|
+
- Cost-ratio (R = c_u / c_o) governance for asymmetric loss
|
|
27
|
+
- RAL policy governance (readiness adjustment layer)
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
# ---------------------------------------------------------------------
|
|
31
|
+
# Tau (tolerance) policies
|
|
32
|
+
# ---------------------------------------------------------------------
|
|
33
|
+
from .tau_policy import (
|
|
34
|
+
TauPolicy,
|
|
35
|
+
apply_tau_policy,
|
|
36
|
+
apply_tau_policy_hr,
|
|
37
|
+
apply_entity_tau_policy,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
# ---------------------------------------------------------------------
|
|
41
|
+
# Cost-ratio (R) policies
|
|
42
|
+
# ---------------------------------------------------------------------
|
|
43
|
+
from .cost_ratio_policy import (
|
|
44
|
+
CostRatioPolicy,
|
|
45
|
+
DEFAULT_COST_RATIO_POLICY,
|
|
46
|
+
apply_cost_ratio_policy,
|
|
47
|
+
apply_entity_cost_ratio_policy,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# ---------------------------------------------------------------------
|
|
51
|
+
# RAL policies
|
|
52
|
+
# ---------------------------------------------------------------------
|
|
53
|
+
from .ral_policy import (
|
|
54
|
+
RALPolicy,
|
|
55
|
+
DEFAULT_RAL_POLICY,
|
|
56
|
+
apply_ral_policy,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
__all__ = [
|
|
60
|
+
# Tau policies
|
|
61
|
+
"TauPolicy",
|
|
62
|
+
"apply_tau_policy",
|
|
63
|
+
"apply_tau_policy_hr",
|
|
64
|
+
"apply_entity_tau_policy",
|
|
65
|
+
# Cost ratio policies
|
|
66
|
+
"CostRatioPolicy",
|
|
67
|
+
"DEFAULT_COST_RATIO_POLICY",
|
|
68
|
+
"apply_cost_ratio_policy",
|
|
69
|
+
"apply_entity_cost_ratio_policy",
|
|
70
|
+
# RAL policies
|
|
71
|
+
"RALPolicy",
|
|
72
|
+
"DEFAULT_RAL_POLICY",
|
|
73
|
+
"apply_ral_policy",
|
|
74
|
+
]
|