ssbc 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.
- ssbc-0.1.0/CONTRIBUTING.md +119 -0
- ssbc-0.1.0/HISTORY.md +5 -0
- ssbc-0.1.0/LICENSE +21 -0
- ssbc-0.1.0/MANIFEST.in +10 -0
- ssbc-0.1.0/PKG-INFO +266 -0
- ssbc-0.1.0/README.md +223 -0
- ssbc-0.1.0/docs/index.md +16 -0
- ssbc-0.1.0/docs/installation.md +38 -0
- ssbc-0.1.0/docs/usage.md +7 -0
- ssbc-0.1.0/pyproject.toml +82 -0
- ssbc-0.1.0/setup.cfg +4 -0
- ssbc-0.1.0/setup.py +9 -0
- ssbc-0.1.0/src/ssbc/__init__.py +59 -0
- ssbc-0.1.0/src/ssbc/__main__.py +4 -0
- ssbc-0.1.0/src/ssbc/cli.py +21 -0
- ssbc-0.1.0/src/ssbc/conformal.py +333 -0
- ssbc-0.1.0/src/ssbc/core.py +205 -0
- ssbc-0.1.0/src/ssbc/hyperparameter.py +258 -0
- ssbc-0.1.0/src/ssbc/simulation.py +148 -0
- ssbc-0.1.0/src/ssbc/ssbc.py +1 -0
- ssbc-0.1.0/src/ssbc/statistics.py +158 -0
- ssbc-0.1.0/src/ssbc/utils.py +2 -0
- ssbc-0.1.0/src/ssbc/visualization.py +459 -0
- ssbc-0.1.0/src/ssbc.egg-info/PKG-INFO +266 -0
- ssbc-0.1.0/src/ssbc.egg-info/SOURCES.txt +35 -0
- ssbc-0.1.0/src/ssbc.egg-info/dependency_links.txt +1 -0
- ssbc-0.1.0/src/ssbc.egg-info/entry_points.txt +2 -0
- ssbc-0.1.0/src/ssbc.egg-info/requires.txt +19 -0
- ssbc-0.1.0/src/ssbc.egg-info/top_level.txt +1 -0
- ssbc-0.1.0/tests/__init__.py +1 -0
- ssbc-0.1.0/tests/test_conformal.py +326 -0
- ssbc-0.1.0/tests/test_core.py +215 -0
- ssbc-0.1.0/tests/test_hyperparameter.py +423 -0
- ssbc-0.1.0/tests/test_simulation.py +209 -0
- ssbc-0.1.0/tests/test_ssbc.py +22 -0
- ssbc-0.1.0/tests/test_statistics.py +279 -0
- ssbc-0.1.0/tests/test_visualization.py +323 -0
@@ -0,0 +1,119 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
|
4
|
+
|
5
|
+
You can contribute in many ways:
|
6
|
+
|
7
|
+
## Types of Contributions
|
8
|
+
|
9
|
+
### Report Bugs
|
10
|
+
|
11
|
+
Report bugs at https://github.com/phzwart/ssbc/issues.
|
12
|
+
|
13
|
+
If you are reporting a bug, please include:
|
14
|
+
|
15
|
+
- Your operating system name and version.
|
16
|
+
- Any details about your local setup that might be helpful in troubleshooting.
|
17
|
+
- Detailed steps to reproduce the bug.
|
18
|
+
|
19
|
+
### Fix Bugs
|
20
|
+
|
21
|
+
Look through the GitHub issues for bugs. Anything tagged with "bug" and "help wanted" is open to whoever wants to implement it.
|
22
|
+
|
23
|
+
### Implement Features
|
24
|
+
|
25
|
+
Look through the GitHub issues for features. Anything tagged with "enhancement" and "help wanted" is open to whoever wants to implement it.
|
26
|
+
|
27
|
+
### Write Documentation
|
28
|
+
|
29
|
+
ssbc could always use more documentation, whether as part of the official docs, in docstrings, or even on the web in blog posts, articles, and such.
|
30
|
+
|
31
|
+
### Submit Feedback
|
32
|
+
|
33
|
+
The best way to send feedback is to file an issue at https://github.com/phzwart/ssbc/issues.
|
34
|
+
|
35
|
+
If you are proposing a feature:
|
36
|
+
|
37
|
+
- Explain in detail how it would work.
|
38
|
+
- Keep the scope as narrow as possible, to make it easier to implement.
|
39
|
+
- Remember that this is a volunteer-driven project, and that contributions are welcome :)
|
40
|
+
|
41
|
+
## Get Started!
|
42
|
+
|
43
|
+
Ready to contribute? Here's how to set up `ssbc` for local development.
|
44
|
+
|
45
|
+
1. Fork the `ssbc` repo on GitHub.
|
46
|
+
2. Clone your fork locally:
|
47
|
+
|
48
|
+
```sh
|
49
|
+
git clone git@github.com:your_name_here/ssbc.git
|
50
|
+
```
|
51
|
+
|
52
|
+
3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:
|
53
|
+
|
54
|
+
```sh
|
55
|
+
mkvirtualenv ssbc
|
56
|
+
cd ssbc/
|
57
|
+
python setup.py develop
|
58
|
+
```
|
59
|
+
|
60
|
+
4. Create a branch for local development:
|
61
|
+
|
62
|
+
```sh
|
63
|
+
git checkout -b name-of-your-bugfix-or-feature
|
64
|
+
```
|
65
|
+
|
66
|
+
Now you can make your changes locally.
|
67
|
+
|
68
|
+
5. When you're done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:
|
69
|
+
|
70
|
+
```sh
|
71
|
+
make lint
|
72
|
+
make test
|
73
|
+
# Or
|
74
|
+
make test-all
|
75
|
+
```
|
76
|
+
|
77
|
+
To get flake8 and tox, just pip install them into your virtualenv.
|
78
|
+
|
79
|
+
6. Commit your changes and push your branch to GitHub:
|
80
|
+
|
81
|
+
```sh
|
82
|
+
git add .
|
83
|
+
git commit -m "Your detailed description of your changes."
|
84
|
+
git push origin name-of-your-bugfix-or-feature
|
85
|
+
```
|
86
|
+
|
87
|
+
7. Submit a pull request through the GitHub website.
|
88
|
+
|
89
|
+
## Pull Request Guidelines
|
90
|
+
|
91
|
+
Before you submit a pull request, check that it meets these guidelines:
|
92
|
+
|
93
|
+
1. The pull request should include tests.
|
94
|
+
2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.md.
|
95
|
+
3. The pull request should work for Python 3.12 and 3.13. Tests run in GitHub Actions on every pull request to the main branch, make sure that the tests pass for all supported Python versions.
|
96
|
+
|
97
|
+
## Tips
|
98
|
+
|
99
|
+
To run a subset of tests:
|
100
|
+
|
101
|
+
```sh
|
102
|
+
pytest tests.test_ssbc
|
103
|
+
```
|
104
|
+
|
105
|
+
## Deploying
|
106
|
+
|
107
|
+
A reminder for the maintainers on how to deploy. Make sure all your changes are committed (including an entry in HISTORY.md). Then run:
|
108
|
+
|
109
|
+
```sh
|
110
|
+
bump2version patch # possible: major / minor / patch
|
111
|
+
git push
|
112
|
+
git push --tags
|
113
|
+
```
|
114
|
+
|
115
|
+
You can set up a [GitHub Actions workflow](https://docs.github.com/en/actions/use-cases-and-examples/building-and-testing/building-and-testing-python#publishing-to-pypi) to automatically deploy your package to PyPI when you push a new tag.
|
116
|
+
|
117
|
+
## Code of Conduct
|
118
|
+
|
119
|
+
Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.
|
ssbc-0.1.0/HISTORY.md
ADDED
ssbc-0.1.0/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025, Petrus H Zwart
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
ssbc-0.1.0/MANIFEST.in
ADDED
ssbc-0.1.0/PKG-INFO
ADDED
@@ -0,0 +1,266 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: ssbc
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: Small Sample Beta Correction - PAC guarantees with small datasets
|
5
|
+
Author-email: Petrus H Zwart <phzwart@lbl.gov>
|
6
|
+
Maintainer-email: Petrus H Zwart <phzwart@lbl.gov>
|
7
|
+
License-Expression: MIT
|
8
|
+
Project-URL: bugs, https://github.com/phzwart/ssbc/issues
|
9
|
+
Project-URL: changelog, https://github.com/phzwart/ssbc/blob/master/changelog.md
|
10
|
+
Project-URL: homepage, https://github.com/phzwart/ssbc
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
13
|
+
Classifier: Intended Audience :: Developers
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
19
|
+
Classifier: Topic :: Scientific/Engineering
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
22
|
+
Requires-Python: >=3.10
|
23
|
+
Description-Content-Type: text/markdown
|
24
|
+
License-File: LICENSE
|
25
|
+
Requires-Dist: matplotlib
|
26
|
+
Requires-Dist: numpy
|
27
|
+
Requires-Dist: pandas
|
28
|
+
Requires-Dist: plotly
|
29
|
+
Requires-Dist: rich
|
30
|
+
Requires-Dist: scipy
|
31
|
+
Requires-Dist: typer
|
32
|
+
Provides-Extra: test
|
33
|
+
Requires-Dist: coverage; extra == "test"
|
34
|
+
Requires-Dist: pytest; extra == "test"
|
35
|
+
Requires-Dist: pytest-cov; extra == "test"
|
36
|
+
Requires-Dist: ruff; extra == "test"
|
37
|
+
Requires-Dist: ty; extra == "test"
|
38
|
+
Requires-Dist: ipdb; extra == "test"
|
39
|
+
Provides-Extra: dev
|
40
|
+
Requires-Dist: pre-commit; extra == "dev"
|
41
|
+
Requires-Dist: bandit[toml]; extra == "dev"
|
42
|
+
Dynamic: license-file
|
43
|
+
|
44
|
+
# SSBC: Small-Sample Beta Correction
|
45
|
+
|
46
|
+

|
47
|
+
[](https://ssbc.readthedocs.io/en/latest/?version=latest)
|
48
|
+
|
49
|
+
**Small-Sample Beta Correction** provides PAC (Probably Approximately Correct) guarantees for conformal prediction with small calibration sets.
|
50
|
+
|
51
|
+
* PyPI package: https://pypi.org/project/ssbc/
|
52
|
+
* Free software: MIT License
|
53
|
+
* Documentation: https://ssbc.readthedocs.io.
|
54
|
+
|
55
|
+
## Overview
|
56
|
+
|
57
|
+
SSBC addresses the challenge of constructing valid prediction sets when you have limited calibration data. Traditional conformal prediction assumes large calibration sets, but in practice, data is often scarce. SSBC provides finite-sample correction with PAC guarantees.
|
58
|
+
|
59
|
+
### Key Features
|
60
|
+
|
61
|
+
- ✅ **Small-Sample Correction**: PAC-valid conformal prediction for small calibration sets
|
62
|
+
- ✅ **Mondrian Conformal Prediction**: Per-class calibration for handling class imbalance
|
63
|
+
- ✅ **Comprehensive Statistics**: Detailed reporting with Clopper-Pearson confidence intervals
|
64
|
+
- ✅ **Hyperparameter Tuning**: Interactive parallel coordinates visualization for parameter optimization
|
65
|
+
- ✅ **Simulation Tools**: Built-in data generators for testing and validation
|
66
|
+
|
67
|
+
## Installation
|
68
|
+
|
69
|
+
```bash
|
70
|
+
pip install ssbc
|
71
|
+
```
|
72
|
+
|
73
|
+
Or from source:
|
74
|
+
|
75
|
+
```bash
|
76
|
+
git clone https://github.com/yourusername/ssbc.git
|
77
|
+
cd ssbc
|
78
|
+
pip install -e .
|
79
|
+
```
|
80
|
+
|
81
|
+
## Quick Start
|
82
|
+
|
83
|
+
```python
|
84
|
+
import numpy as np
|
85
|
+
from ssbc import (
|
86
|
+
ssbc_correct,
|
87
|
+
BinaryClassifierSimulator,
|
88
|
+
split_by_class,
|
89
|
+
mondrian_conformal_calibrate,
|
90
|
+
report_prediction_stats,
|
91
|
+
)
|
92
|
+
|
93
|
+
# 1. Generate simulated data
|
94
|
+
sim = BinaryClassifierSimulator(
|
95
|
+
p_class1=0.1,
|
96
|
+
beta_params_class0=(2, 8),
|
97
|
+
beta_params_class1=(8, 2),
|
98
|
+
seed=42
|
99
|
+
)
|
100
|
+
labels, probs = sim.generate(n_samples=100)
|
101
|
+
|
102
|
+
# 2. Split by class for Mondrian CP
|
103
|
+
class_data = split_by_class(labels, probs)
|
104
|
+
|
105
|
+
# 3. Calibrate with SSBC correction
|
106
|
+
cal_result, pred_stats = mondrian_conformal_calibrate(
|
107
|
+
class_data=class_data,
|
108
|
+
alpha_target=0.10, # 10% miscoverage
|
109
|
+
delta=0.10, # 90% PAC guarantee
|
110
|
+
mode="beta"
|
111
|
+
)
|
112
|
+
|
113
|
+
# 4. Generate comprehensive report
|
114
|
+
summary = report_prediction_stats(pred_stats, cal_result, verbose=True)
|
115
|
+
```
|
116
|
+
|
117
|
+
## Core Algorithm: SSBC
|
118
|
+
|
119
|
+
The SSBC algorithm finds the optimal corrected miscoverage rate α' that satisfies:
|
120
|
+
|
121
|
+
**P(Coverage(α') ≥ 1 - α_target) ≥ 1 - δ**
|
122
|
+
|
123
|
+
```python
|
124
|
+
from ssbc import ssbc_correct
|
125
|
+
|
126
|
+
result = ssbc_correct(
|
127
|
+
alpha_target=0.10, # Target 10% miscoverage
|
128
|
+
n=50, # Calibration set size
|
129
|
+
delta=0.10, # PAC parameter (90% confidence)
|
130
|
+
mode="beta" # Infinite test window
|
131
|
+
)
|
132
|
+
|
133
|
+
print(f"Corrected α: {result.alpha_corrected:.4f}")
|
134
|
+
print(f"u*: {result.u_star}")
|
135
|
+
```
|
136
|
+
|
137
|
+
### Parameters
|
138
|
+
|
139
|
+
- `alpha_target`: Target miscoverage rate (e.g., 0.10 for 90% coverage)
|
140
|
+
- `n`: Calibration set size
|
141
|
+
- `delta`: PAC risk tolerance (probability of violating guarantee)
|
142
|
+
- `mode`: "beta" (infinite test) or "beta-binomial" (finite test)
|
143
|
+
|
144
|
+
## Module Structure
|
145
|
+
|
146
|
+
The library is organized into focused modules:
|
147
|
+
|
148
|
+
### Core Modules
|
149
|
+
|
150
|
+
- **`ssbc.core`**: Core SSBC algorithm (`ssbc_correct`, `SSBCResult`)
|
151
|
+
- **`ssbc.conformal`**: Mondrian conformal prediction (`mondrian_conformal_calibrate`, `split_by_class`)
|
152
|
+
- **`ssbc.statistics`**: Statistical utilities (`clopper_pearson_intervals`, `cp_interval`)
|
153
|
+
|
154
|
+
### Analysis & Visualization
|
155
|
+
|
156
|
+
- **`ssbc.visualization`**: Reporting and plotting (`report_prediction_stats`, `plot_parallel_coordinates_plotly`)
|
157
|
+
- **`ssbc.hyperparameter`**: Parameter tuning (`sweep_hyperparams_and_collect`, `sweep_and_plot_parallel_plotly`)
|
158
|
+
|
159
|
+
### Testing & Simulation
|
160
|
+
|
161
|
+
- **`ssbc.simulation`**: Data generators (`BinaryClassifierSimulator`)
|
162
|
+
|
163
|
+
## Examples
|
164
|
+
|
165
|
+
The `examples/` directory contains comprehensive demonstrations:
|
166
|
+
|
167
|
+
### 1. Core SSBC Algorithm
|
168
|
+
```bash
|
169
|
+
python examples/ssbc_core_example.py
|
170
|
+
```
|
171
|
+
Demonstrates the SSBC algorithm for different calibration set sizes.
|
172
|
+
|
173
|
+
### 2. Mondrian Conformal Prediction
|
174
|
+
```bash
|
175
|
+
python examples/mondrian_conformal_example.py
|
176
|
+
```
|
177
|
+
Complete workflow: simulation → calibration → reporting.
|
178
|
+
|
179
|
+
### 3. Hyperparameter Sweep
|
180
|
+
```bash
|
181
|
+
python examples/hyperparameter_sweep_example.py
|
182
|
+
```
|
183
|
+
Interactive parameter tuning with parallel coordinates visualization.
|
184
|
+
|
185
|
+
## Hyperparameter Tuning
|
186
|
+
|
187
|
+
Sweep over α and δ values to find optimal configurations:
|
188
|
+
|
189
|
+
```python
|
190
|
+
from ssbc import sweep_and_plot_parallel_plotly
|
191
|
+
import numpy as np
|
192
|
+
|
193
|
+
# Define grid
|
194
|
+
alpha_grid = np.arange(0.05, 0.20, 0.05)
|
195
|
+
delta_grid = np.arange(0.05, 0.20, 0.05)
|
196
|
+
|
197
|
+
# Run sweep and visualize
|
198
|
+
df, fig = sweep_and_plot_parallel_plotly(
|
199
|
+
class_data=class_data,
|
200
|
+
alpha_0=alpha_grid, delta_0=delta_grid,
|
201
|
+
alpha_1=alpha_grid, delta_1=delta_grid,
|
202
|
+
color='err_all' # Color by error rate
|
203
|
+
)
|
204
|
+
|
205
|
+
# Save interactive plot
|
206
|
+
fig.write_html("sweep_results.html")
|
207
|
+
|
208
|
+
# Analyze results
|
209
|
+
print(df[['a0', 'd0', 'cov', 'sing_rate', 'err_all']].head())
|
210
|
+
```
|
211
|
+
|
212
|
+
The interactive plot allows you to:
|
213
|
+
- Brush (select) ranges on any axis to filter configurations
|
214
|
+
- Explore trade-offs between coverage, automation, and error rates
|
215
|
+
- Identify Pareto-optimal hyperparameter settings
|
216
|
+
|
217
|
+
## Understanding the Output
|
218
|
+
|
219
|
+
### Per-Class Statistics (Conditioned on True Label)
|
220
|
+
|
221
|
+
For each class, the report shows:
|
222
|
+
- **Abstentions**: Empty prediction sets
|
223
|
+
- **Singletons**: Confident predictions (automated decisions)
|
224
|
+
- Correct: True label in singleton set
|
225
|
+
- Incorrect: True label not in singleton set
|
226
|
+
- **Doublets**: Both labels included (escalated to human review)
|
227
|
+
|
228
|
+
### Marginal Statistics (Deployment View)
|
229
|
+
|
230
|
+
Overall performance metrics ignoring true labels:
|
231
|
+
- **Coverage**: Fraction of predictions containing the true label
|
232
|
+
- **Singleton rate**: Fraction of confident predictions (automation level)
|
233
|
+
- **Escalation rate**: Fraction requiring human review
|
234
|
+
- **Error rates**: By predicted class and overall
|
235
|
+
|
236
|
+
### PAC Bounds
|
237
|
+
|
238
|
+
The report includes theoretical and observed singleton error rates:
|
239
|
+
- **α'_bound**: Theoretical upper bound from PAC analysis
|
240
|
+
- **α'_observed**: Observed error rate on calibration data
|
241
|
+
- ✓ if observed ≤ bound (PAC guarantee satisfied)
|
242
|
+
|
243
|
+
## Citation
|
244
|
+
|
245
|
+
If you use SSBC in your research, please cite:
|
246
|
+
|
247
|
+
```bibtex
|
248
|
+
@software{ssbc2024,
|
249
|
+
author = {Zwart, Petrus H},
|
250
|
+
title = {SSBC: Small-Sample Beta Correction},
|
251
|
+
year = {2024},
|
252
|
+
url = {https://github.com/yourusername/ssbc}
|
253
|
+
}
|
254
|
+
```
|
255
|
+
|
256
|
+
## Contributing
|
257
|
+
|
258
|
+
Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
259
|
+
|
260
|
+
## License
|
261
|
+
|
262
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
263
|
+
|
264
|
+
## Credits
|
265
|
+
|
266
|
+
This package was created with [Cookiecutter](https://github.com/audreyfeldroy/cookiecutter) and the [audreyfeldroy/cookiecutter-pypackage](https://github.com/audreyfeldroy/cookiecutter-pypackage) project template.
|
ssbc-0.1.0/README.md
ADDED
@@ -0,0 +1,223 @@
|
|
1
|
+
# SSBC: Small-Sample Beta Correction
|
2
|
+
|
3
|
+

|
4
|
+
[](https://ssbc.readthedocs.io/en/latest/?version=latest)
|
5
|
+
|
6
|
+
**Small-Sample Beta Correction** provides PAC (Probably Approximately Correct) guarantees for conformal prediction with small calibration sets.
|
7
|
+
|
8
|
+
* PyPI package: https://pypi.org/project/ssbc/
|
9
|
+
* Free software: MIT License
|
10
|
+
* Documentation: https://ssbc.readthedocs.io.
|
11
|
+
|
12
|
+
## Overview
|
13
|
+
|
14
|
+
SSBC addresses the challenge of constructing valid prediction sets when you have limited calibration data. Traditional conformal prediction assumes large calibration sets, but in practice, data is often scarce. SSBC provides finite-sample correction with PAC guarantees.
|
15
|
+
|
16
|
+
### Key Features
|
17
|
+
|
18
|
+
- ✅ **Small-Sample Correction**: PAC-valid conformal prediction for small calibration sets
|
19
|
+
- ✅ **Mondrian Conformal Prediction**: Per-class calibration for handling class imbalance
|
20
|
+
- ✅ **Comprehensive Statistics**: Detailed reporting with Clopper-Pearson confidence intervals
|
21
|
+
- ✅ **Hyperparameter Tuning**: Interactive parallel coordinates visualization for parameter optimization
|
22
|
+
- ✅ **Simulation Tools**: Built-in data generators for testing and validation
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
```bash
|
27
|
+
pip install ssbc
|
28
|
+
```
|
29
|
+
|
30
|
+
Or from source:
|
31
|
+
|
32
|
+
```bash
|
33
|
+
git clone https://github.com/yourusername/ssbc.git
|
34
|
+
cd ssbc
|
35
|
+
pip install -e .
|
36
|
+
```
|
37
|
+
|
38
|
+
## Quick Start
|
39
|
+
|
40
|
+
```python
|
41
|
+
import numpy as np
|
42
|
+
from ssbc import (
|
43
|
+
ssbc_correct,
|
44
|
+
BinaryClassifierSimulator,
|
45
|
+
split_by_class,
|
46
|
+
mondrian_conformal_calibrate,
|
47
|
+
report_prediction_stats,
|
48
|
+
)
|
49
|
+
|
50
|
+
# 1. Generate simulated data
|
51
|
+
sim = BinaryClassifierSimulator(
|
52
|
+
p_class1=0.1,
|
53
|
+
beta_params_class0=(2, 8),
|
54
|
+
beta_params_class1=(8, 2),
|
55
|
+
seed=42
|
56
|
+
)
|
57
|
+
labels, probs = sim.generate(n_samples=100)
|
58
|
+
|
59
|
+
# 2. Split by class for Mondrian CP
|
60
|
+
class_data = split_by_class(labels, probs)
|
61
|
+
|
62
|
+
# 3. Calibrate with SSBC correction
|
63
|
+
cal_result, pred_stats = mondrian_conformal_calibrate(
|
64
|
+
class_data=class_data,
|
65
|
+
alpha_target=0.10, # 10% miscoverage
|
66
|
+
delta=0.10, # 90% PAC guarantee
|
67
|
+
mode="beta"
|
68
|
+
)
|
69
|
+
|
70
|
+
# 4. Generate comprehensive report
|
71
|
+
summary = report_prediction_stats(pred_stats, cal_result, verbose=True)
|
72
|
+
```
|
73
|
+
|
74
|
+
## Core Algorithm: SSBC
|
75
|
+
|
76
|
+
The SSBC algorithm finds the optimal corrected miscoverage rate α' that satisfies:
|
77
|
+
|
78
|
+
**P(Coverage(α') ≥ 1 - α_target) ≥ 1 - δ**
|
79
|
+
|
80
|
+
```python
|
81
|
+
from ssbc import ssbc_correct
|
82
|
+
|
83
|
+
result = ssbc_correct(
|
84
|
+
alpha_target=0.10, # Target 10% miscoverage
|
85
|
+
n=50, # Calibration set size
|
86
|
+
delta=0.10, # PAC parameter (90% confidence)
|
87
|
+
mode="beta" # Infinite test window
|
88
|
+
)
|
89
|
+
|
90
|
+
print(f"Corrected α: {result.alpha_corrected:.4f}")
|
91
|
+
print(f"u*: {result.u_star}")
|
92
|
+
```
|
93
|
+
|
94
|
+
### Parameters
|
95
|
+
|
96
|
+
- `alpha_target`: Target miscoverage rate (e.g., 0.10 for 90% coverage)
|
97
|
+
- `n`: Calibration set size
|
98
|
+
- `delta`: PAC risk tolerance (probability of violating guarantee)
|
99
|
+
- `mode`: "beta" (infinite test) or "beta-binomial" (finite test)
|
100
|
+
|
101
|
+
## Module Structure
|
102
|
+
|
103
|
+
The library is organized into focused modules:
|
104
|
+
|
105
|
+
### Core Modules
|
106
|
+
|
107
|
+
- **`ssbc.core`**: Core SSBC algorithm (`ssbc_correct`, `SSBCResult`)
|
108
|
+
- **`ssbc.conformal`**: Mondrian conformal prediction (`mondrian_conformal_calibrate`, `split_by_class`)
|
109
|
+
- **`ssbc.statistics`**: Statistical utilities (`clopper_pearson_intervals`, `cp_interval`)
|
110
|
+
|
111
|
+
### Analysis & Visualization
|
112
|
+
|
113
|
+
- **`ssbc.visualization`**: Reporting and plotting (`report_prediction_stats`, `plot_parallel_coordinates_plotly`)
|
114
|
+
- **`ssbc.hyperparameter`**: Parameter tuning (`sweep_hyperparams_and_collect`, `sweep_and_plot_parallel_plotly`)
|
115
|
+
|
116
|
+
### Testing & Simulation
|
117
|
+
|
118
|
+
- **`ssbc.simulation`**: Data generators (`BinaryClassifierSimulator`)
|
119
|
+
|
120
|
+
## Examples
|
121
|
+
|
122
|
+
The `examples/` directory contains comprehensive demonstrations:
|
123
|
+
|
124
|
+
### 1. Core SSBC Algorithm
|
125
|
+
```bash
|
126
|
+
python examples/ssbc_core_example.py
|
127
|
+
```
|
128
|
+
Demonstrates the SSBC algorithm for different calibration set sizes.
|
129
|
+
|
130
|
+
### 2. Mondrian Conformal Prediction
|
131
|
+
```bash
|
132
|
+
python examples/mondrian_conformal_example.py
|
133
|
+
```
|
134
|
+
Complete workflow: simulation → calibration → reporting.
|
135
|
+
|
136
|
+
### 3. Hyperparameter Sweep
|
137
|
+
```bash
|
138
|
+
python examples/hyperparameter_sweep_example.py
|
139
|
+
```
|
140
|
+
Interactive parameter tuning with parallel coordinates visualization.
|
141
|
+
|
142
|
+
## Hyperparameter Tuning
|
143
|
+
|
144
|
+
Sweep over α and δ values to find optimal configurations:
|
145
|
+
|
146
|
+
```python
|
147
|
+
from ssbc import sweep_and_plot_parallel_plotly
|
148
|
+
import numpy as np
|
149
|
+
|
150
|
+
# Define grid
|
151
|
+
alpha_grid = np.arange(0.05, 0.20, 0.05)
|
152
|
+
delta_grid = np.arange(0.05, 0.20, 0.05)
|
153
|
+
|
154
|
+
# Run sweep and visualize
|
155
|
+
df, fig = sweep_and_plot_parallel_plotly(
|
156
|
+
class_data=class_data,
|
157
|
+
alpha_0=alpha_grid, delta_0=delta_grid,
|
158
|
+
alpha_1=alpha_grid, delta_1=delta_grid,
|
159
|
+
color='err_all' # Color by error rate
|
160
|
+
)
|
161
|
+
|
162
|
+
# Save interactive plot
|
163
|
+
fig.write_html("sweep_results.html")
|
164
|
+
|
165
|
+
# Analyze results
|
166
|
+
print(df[['a0', 'd0', 'cov', 'sing_rate', 'err_all']].head())
|
167
|
+
```
|
168
|
+
|
169
|
+
The interactive plot allows you to:
|
170
|
+
- Brush (select) ranges on any axis to filter configurations
|
171
|
+
- Explore trade-offs between coverage, automation, and error rates
|
172
|
+
- Identify Pareto-optimal hyperparameter settings
|
173
|
+
|
174
|
+
## Understanding the Output
|
175
|
+
|
176
|
+
### Per-Class Statistics (Conditioned on True Label)
|
177
|
+
|
178
|
+
For each class, the report shows:
|
179
|
+
- **Abstentions**: Empty prediction sets
|
180
|
+
- **Singletons**: Confident predictions (automated decisions)
|
181
|
+
- Correct: True label in singleton set
|
182
|
+
- Incorrect: True label not in singleton set
|
183
|
+
- **Doublets**: Both labels included (escalated to human review)
|
184
|
+
|
185
|
+
### Marginal Statistics (Deployment View)
|
186
|
+
|
187
|
+
Overall performance metrics ignoring true labels:
|
188
|
+
- **Coverage**: Fraction of predictions containing the true label
|
189
|
+
- **Singleton rate**: Fraction of confident predictions (automation level)
|
190
|
+
- **Escalation rate**: Fraction requiring human review
|
191
|
+
- **Error rates**: By predicted class and overall
|
192
|
+
|
193
|
+
### PAC Bounds
|
194
|
+
|
195
|
+
The report includes theoretical and observed singleton error rates:
|
196
|
+
- **α'_bound**: Theoretical upper bound from PAC analysis
|
197
|
+
- **α'_observed**: Observed error rate on calibration data
|
198
|
+
- ✓ if observed ≤ bound (PAC guarantee satisfied)
|
199
|
+
|
200
|
+
## Citation
|
201
|
+
|
202
|
+
If you use SSBC in your research, please cite:
|
203
|
+
|
204
|
+
```bibtex
|
205
|
+
@software{ssbc2024,
|
206
|
+
author = {Zwart, Petrus H},
|
207
|
+
title = {SSBC: Small-Sample Beta Correction},
|
208
|
+
year = {2024},
|
209
|
+
url = {https://github.com/yourusername/ssbc}
|
210
|
+
}
|
211
|
+
```
|
212
|
+
|
213
|
+
## Contributing
|
214
|
+
|
215
|
+
Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
216
|
+
|
217
|
+
## License
|
218
|
+
|
219
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
220
|
+
|
221
|
+
## Credits
|
222
|
+
|
223
|
+
This package was created with [Cookiecutter](https://github.com/audreyfeldroy/cookiecutter) and the [audreyfeldroy/cookiecutter-pypackage](https://github.com/audreyfeldroy/cookiecutter-pypackage) project template.
|
ssbc-0.1.0/docs/index.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Welcome to ssbc's documentation!
|
2
|
+
|
3
|
+
## Contents
|
4
|
+
|
5
|
+
- [Readme](readme.md)
|
6
|
+
- [Installation](installation.md)
|
7
|
+
- [Usage](usage.md)
|
8
|
+
- [Modules](modules.md)
|
9
|
+
- [Contributing](contributing.md)
|
10
|
+
- [History](history.md)
|
11
|
+
|
12
|
+
## Indices and tables
|
13
|
+
|
14
|
+
- [Index](genindex)
|
15
|
+
- [Module Index](modindex)
|
16
|
+
- [Search](search)
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Installation
|
2
|
+
|
3
|
+
## Stable release
|
4
|
+
|
5
|
+
To install ssbc, run this command in your terminal:
|
6
|
+
|
7
|
+
```sh
|
8
|
+
uv add ssbc
|
9
|
+
```
|
10
|
+
|
11
|
+
Or if you prefer to use `pip`:
|
12
|
+
|
13
|
+
```sh
|
14
|
+
pip install ssbc
|
15
|
+
```
|
16
|
+
|
17
|
+
## From source
|
18
|
+
|
19
|
+
The source files for ssbc can be downloaded from the [Github repo](https://github.com/phzwart/ssbc).
|
20
|
+
|
21
|
+
You can either clone the public repository:
|
22
|
+
|
23
|
+
```sh
|
24
|
+
git clone git://github.com/phzwart/ssbc
|
25
|
+
```
|
26
|
+
|
27
|
+
Or download the [tarball](https://github.com/phzwart/ssbc/tarball/master):
|
28
|
+
|
29
|
+
```sh
|
30
|
+
curl -OJL https://github.com/phzwart/ssbc/tarball/master
|
31
|
+
```
|
32
|
+
|
33
|
+
Once you have a copy of the source, you can install it with:
|
34
|
+
|
35
|
+
```sh
|
36
|
+
cd ssbc
|
37
|
+
uv pip install .
|
38
|
+
```
|