bc-cpit 1.0.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.
Files changed (44) hide show
  1. bc_cpit-1.0.0/LICENSE +21 -0
  2. bc_cpit-1.0.0/PKG-INFO +128 -0
  3. bc_cpit-1.0.0/README.md +104 -0
  4. bc_cpit-1.0.0/bc_cpit.egg-info/PKG-INFO +128 -0
  5. bc_cpit-1.0.0/bc_cpit.egg-info/SOURCES.txt +42 -0
  6. bc_cpit-1.0.0/bc_cpit.egg-info/dependency_links.txt +1 -0
  7. bc_cpit-1.0.0/bc_cpit.egg-info/requires.txt +6 -0
  8. bc_cpit-1.0.0/bc_cpit.egg-info/top_level.txt +3 -0
  9. bc_cpit-1.0.0/cpit/__init__.py +50 -0
  10. bc_cpit-1.0.0/cpit/baselines/__init__.py +14 -0
  11. bc_cpit-1.0.0/cpit/baselines/quantile_baselines.py +90 -0
  12. bc_cpit-1.0.0/cpit/baselines/score_baselines.py +219 -0
  13. bc_cpit-1.0.0/cpit/bc/__init__.py +30 -0
  14. bc_cpit-1.0.0/cpit/bc/apply.py +75 -0
  15. bc_cpit-1.0.0/cpit/bc/fit.py +300 -0
  16. bc_cpit-1.0.0/cpit/bc/params.py +28 -0
  17. bc_cpit-1.0.0/cpit/calibrator.py +96 -0
  18. bc_cpit-1.0.0/cpit/data_splitter.py +72 -0
  19. bc_cpit-1.0.0/cpit/evaluation/__init__.py +42 -0
  20. bc_cpit-1.0.0/cpit/evaluation/local_diagnostics.py +92 -0
  21. bc_cpit-1.0.0/cpit/evaluation/metrics.py +297 -0
  22. bc_cpit-1.0.0/cpit/evaluation/pit_histogram.py +16 -0
  23. bc_cpit-1.0.0/cpit/inference.py +310 -0
  24. bc_cpit-1.0.0/cpit/pipeline.py +140 -0
  25. bc_cpit-1.0.0/cpit/pit.py +40 -0
  26. bc_cpit-1.0.0/cpit/weighted_samples.py +96 -0
  27. bc_cpit-1.0.0/pyproject.toml +46 -0
  28. bc_cpit-1.0.0/setup.cfg +4 -0
  29. bc_cpit-1.0.0/tests/test_bias_corrector.py +202 -0
  30. bc_cpit-1.0.0/tests/test_data_splitter.py +37 -0
  31. bc_cpit-1.0.0/tests/test_draft6_spec_matrix.py +33 -0
  32. bc_cpit-1.0.0/tests/test_evaluation.py +283 -0
  33. bc_cpit-1.0.0/tests/test_golden_design1.py +56 -0
  34. bc_cpit-1.0.0/tests/test_inference_api.py +79 -0
  35. bc_cpit-1.0.0/tests/test_inference_extra.py +162 -0
  36. bc_cpit-1.0.0/tests/test_metrics_extra.py +246 -0
  37. bc_cpit-1.0.0/tests/test_pipeline.py +98 -0
  38. bc_cpit-1.0.0/tests/test_pit_calibrator.py +182 -0
  39. bc_cpit-1.0.0/tests/test_pit_extra.py +60 -0
  40. bc_cpit-1.0.0/tests/test_pit_histogram.py +32 -0
  41. bc_cpit-1.0.0/tests/test_predictive_cdf.py +202 -0
  42. bc_cpit-1.0.0/tests/test_quantile_baselines.py +85 -0
  43. bc_cpit-1.0.0/tests/test_score_baselines.py +34 -0
  44. bc_cpit-1.0.0/tests/test_score_baselines_extra.py +126 -0
bc_cpit-1.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Space Time Viz
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.
bc_cpit-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,128 @@
1
+ Metadata-Version: 2.4
2
+ Name: bc-cpit
3
+ Version: 1.0.0
4
+ Summary: Calibrated Predictive Distributions from Sample-Based Generators
5
+ Author-email: Wen-Ting Wang <egpivo@gmail.com>
6
+ License: MIT
7
+ Keywords: conformal prediction,calibration,probability integral transform,bias correction
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Science/Research
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
15
+ Requires-Python: >=3.11
16
+ Description-Content-Type: text/markdown
17
+ License-File: LICENSE
18
+ Requires-Dist: numpy>=1.20
19
+ Requires-Dist: scipy>=1.7
20
+ Requires-Dist: pygam>=0.9
21
+ Provides-Extra: dev
22
+ Requires-Dist: pytest>=7.0; extra == "dev"
23
+ Dynamic: license-file
24
+
25
+ # bc-cpit
26
+
27
+ [![CI](https://github.com/egpivo/cpit/actions/workflows/ci.yml/badge.svg)](https://github.com/egpivo/cpit/actions/workflows/ci.yml)
28
+ [![codecov](https://codecov.io/gh/egpivo/cpit/graph/badge.svg?token=SNoFZfquk5)](https://codecov.io/gh/egpivo/cpit)
29
+ [![PyPI](https://img.shields.io/pypi/v/bc-cpit)](https://pypi.org/project/bc-cpit/)
30
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
31
+
32
+ Bias-corrected conformal PIT calibration for sample-based predictive distributions.
33
+
34
+ ## Install
35
+
36
+ ```bash
37
+ pip install bc-cpit
38
+ ```
39
+
40
+ Development:
41
+
42
+ ```bash
43
+ git clone https://github.com/egpivo/bc-cpit.git
44
+ cd cpit
45
+ pip install -e ".[dev]"
46
+ ```
47
+
48
+ ## Quick start
49
+
50
+ ```python
51
+ from cpit.bc import fit_global_affine, apply_affine
52
+ from cpit import fit_conformal_calibrator, get_weighted_samples_at_x
53
+ from cpit import quantile_from_weighted_samples, central_interval_from_weighted_samples
54
+
55
+ # 1. Bias correction
56
+ params = fit_global_affine(y_bias, y_bias_samples)
57
+
58
+ # 2. Conformal calibration
59
+ cal_samples_adj = [apply_affine(s, params) for s in cal_samples]
60
+ _, c_hat = fit_conformal_calibrator(cal_samples_adj, y_cal)
61
+
62
+ # 3. Inference
63
+ y_pts, weights = get_weighted_samples_at_x(y_test_samples, params, c_hat)
64
+ q80 = quantile_from_weighted_samples(y_pts, weights, 0.80)
65
+ lo, hi = central_interval_from_weighted_samples(y_pts, weights, alpha=0.10)
66
+ ```
67
+
68
+ x-dependent (GAM) correction:
69
+
70
+ ```python
71
+ from cpit.bc import fit_x_dependent_affine_gam
72
+
73
+ params_gam = fit_x_dependent_affine_gam(x_bias, y_bias, y_bias_samples)
74
+ y_pts, weights = get_weighted_samples_at_x(y_test_samples, params_gam, c_hat, x=x_test)
75
+ ```
76
+
77
+ High-level pipeline:
78
+
79
+ ```python
80
+ from cpit.pipeline import run_pipeline, predict_interval
81
+
82
+ state = run_pipeline(X, y, generator_fn)
83
+ lo, hi = predict_interval(state, X_test, y_samples_test, alpha=0.10)
84
+ ```
85
+
86
+ ## Reproduce paper results
87
+
88
+ ```bash
89
+ make test # unit tests
90
+ make run-sim # Designs 1/2/3 simulations (§5)
91
+ make pit-figures # PIT histogram figures
92
+ make run-wb2 # WB2 application: Taiwan + Europe (§6)
93
+ ```
94
+
95
+ ## Layout
96
+
97
+ ```
98
+ cpit/ Python package
99
+ bc/ bias correction (fit, apply)
100
+ baselines/ competing methods
101
+ evaluation/ metrics, PIT histograms, diagnostics
102
+ calibrator.py / pit.py / inference.py / weighted_samples.py / pipeline.py
103
+
104
+ examples/ orchestration scripts (call cpit)
105
+ simulation/ §5 designs 1–3
106
+ real_data/ §6 WB2 Taiwan + Europe
107
+ ```
108
+
109
+ ## Citation
110
+
111
+ The paper is currently being submitted to arXiv; the entry below will be updated with the final arXiv ID once it is live.
112
+
113
+ If you use this package, please cite:
114
+
115
+ > Wang, W.-T., Tzeng, S., Fan, Y.-T., & Huang, H.-C. (2026). Calibrated Predictive Distributions from Sample-Based Generators. *arXiv preprint arXiv:XXXX.XXXXX*.
116
+
117
+ ```bibtex
118
+ @article{wang2026calibrated,
119
+ title = {Calibrated Predictive Distributions from Sample-Based Generators},
120
+ author = {Wang, Wen-Ting and Tzeng, ShengLi and Fan, Yu-Ting and Huang, Hsin-Cheng},
121
+ journal = {arXiv preprint arXiv:XXXX.XXXXX},
122
+ year = {2026}
123
+ }
124
+ ```
125
+
126
+ ## License
127
+
128
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,104 @@
1
+ # bc-cpit
2
+
3
+ [![CI](https://github.com/egpivo/cpit/actions/workflows/ci.yml/badge.svg)](https://github.com/egpivo/cpit/actions/workflows/ci.yml)
4
+ [![codecov](https://codecov.io/gh/egpivo/cpit/graph/badge.svg?token=SNoFZfquk5)](https://codecov.io/gh/egpivo/cpit)
5
+ [![PyPI](https://img.shields.io/pypi/v/bc-cpit)](https://pypi.org/project/bc-cpit/)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
7
+
8
+ Bias-corrected conformal PIT calibration for sample-based predictive distributions.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ pip install bc-cpit
14
+ ```
15
+
16
+ Development:
17
+
18
+ ```bash
19
+ git clone https://github.com/egpivo/bc-cpit.git
20
+ cd cpit
21
+ pip install -e ".[dev]"
22
+ ```
23
+
24
+ ## Quick start
25
+
26
+ ```python
27
+ from cpit.bc import fit_global_affine, apply_affine
28
+ from cpit import fit_conformal_calibrator, get_weighted_samples_at_x
29
+ from cpit import quantile_from_weighted_samples, central_interval_from_weighted_samples
30
+
31
+ # 1. Bias correction
32
+ params = fit_global_affine(y_bias, y_bias_samples)
33
+
34
+ # 2. Conformal calibration
35
+ cal_samples_adj = [apply_affine(s, params) for s in cal_samples]
36
+ _, c_hat = fit_conformal_calibrator(cal_samples_adj, y_cal)
37
+
38
+ # 3. Inference
39
+ y_pts, weights = get_weighted_samples_at_x(y_test_samples, params, c_hat)
40
+ q80 = quantile_from_weighted_samples(y_pts, weights, 0.80)
41
+ lo, hi = central_interval_from_weighted_samples(y_pts, weights, alpha=0.10)
42
+ ```
43
+
44
+ x-dependent (GAM) correction:
45
+
46
+ ```python
47
+ from cpit.bc import fit_x_dependent_affine_gam
48
+
49
+ params_gam = fit_x_dependent_affine_gam(x_bias, y_bias, y_bias_samples)
50
+ y_pts, weights = get_weighted_samples_at_x(y_test_samples, params_gam, c_hat, x=x_test)
51
+ ```
52
+
53
+ High-level pipeline:
54
+
55
+ ```python
56
+ from cpit.pipeline import run_pipeline, predict_interval
57
+
58
+ state = run_pipeline(X, y, generator_fn)
59
+ lo, hi = predict_interval(state, X_test, y_samples_test, alpha=0.10)
60
+ ```
61
+
62
+ ## Reproduce paper results
63
+
64
+ ```bash
65
+ make test # unit tests
66
+ make run-sim # Designs 1/2/3 simulations (§5)
67
+ make pit-figures # PIT histogram figures
68
+ make run-wb2 # WB2 application: Taiwan + Europe (§6)
69
+ ```
70
+
71
+ ## Layout
72
+
73
+ ```
74
+ cpit/ Python package
75
+ bc/ bias correction (fit, apply)
76
+ baselines/ competing methods
77
+ evaluation/ metrics, PIT histograms, diagnostics
78
+ calibrator.py / pit.py / inference.py / weighted_samples.py / pipeline.py
79
+
80
+ examples/ orchestration scripts (call cpit)
81
+ simulation/ §5 designs 1–3
82
+ real_data/ §6 WB2 Taiwan + Europe
83
+ ```
84
+
85
+ ## Citation
86
+
87
+ The paper is currently being submitted to arXiv; the entry below will be updated with the final arXiv ID once it is live.
88
+
89
+ If you use this package, please cite:
90
+
91
+ > Wang, W.-T., Tzeng, S., Fan, Y.-T., & Huang, H.-C. (2026). Calibrated Predictive Distributions from Sample-Based Generators. *arXiv preprint arXiv:XXXX.XXXXX*.
92
+
93
+ ```bibtex
94
+ @article{wang2026calibrated,
95
+ title = {Calibrated Predictive Distributions from Sample-Based Generators},
96
+ author = {Wang, Wen-Ting and Tzeng, ShengLi and Fan, Yu-Ting and Huang, Hsin-Cheng},
97
+ journal = {arXiv preprint arXiv:XXXX.XXXXX},
98
+ year = {2026}
99
+ }
100
+ ```
101
+
102
+ ## License
103
+
104
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,128 @@
1
+ Metadata-Version: 2.4
2
+ Name: bc-cpit
3
+ Version: 1.0.0
4
+ Summary: Calibrated Predictive Distributions from Sample-Based Generators
5
+ Author-email: Wen-Ting Wang <egpivo@gmail.com>
6
+ License: MIT
7
+ Keywords: conformal prediction,calibration,probability integral transform,bias correction
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Science/Research
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
15
+ Requires-Python: >=3.11
16
+ Description-Content-Type: text/markdown
17
+ License-File: LICENSE
18
+ Requires-Dist: numpy>=1.20
19
+ Requires-Dist: scipy>=1.7
20
+ Requires-Dist: pygam>=0.9
21
+ Provides-Extra: dev
22
+ Requires-Dist: pytest>=7.0; extra == "dev"
23
+ Dynamic: license-file
24
+
25
+ # bc-cpit
26
+
27
+ [![CI](https://github.com/egpivo/cpit/actions/workflows/ci.yml/badge.svg)](https://github.com/egpivo/cpit/actions/workflows/ci.yml)
28
+ [![codecov](https://codecov.io/gh/egpivo/cpit/graph/badge.svg?token=SNoFZfquk5)](https://codecov.io/gh/egpivo/cpit)
29
+ [![PyPI](https://img.shields.io/pypi/v/bc-cpit)](https://pypi.org/project/bc-cpit/)
30
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
31
+
32
+ Bias-corrected conformal PIT calibration for sample-based predictive distributions.
33
+
34
+ ## Install
35
+
36
+ ```bash
37
+ pip install bc-cpit
38
+ ```
39
+
40
+ Development:
41
+
42
+ ```bash
43
+ git clone https://github.com/egpivo/bc-cpit.git
44
+ cd cpit
45
+ pip install -e ".[dev]"
46
+ ```
47
+
48
+ ## Quick start
49
+
50
+ ```python
51
+ from cpit.bc import fit_global_affine, apply_affine
52
+ from cpit import fit_conformal_calibrator, get_weighted_samples_at_x
53
+ from cpit import quantile_from_weighted_samples, central_interval_from_weighted_samples
54
+
55
+ # 1. Bias correction
56
+ params = fit_global_affine(y_bias, y_bias_samples)
57
+
58
+ # 2. Conformal calibration
59
+ cal_samples_adj = [apply_affine(s, params) for s in cal_samples]
60
+ _, c_hat = fit_conformal_calibrator(cal_samples_adj, y_cal)
61
+
62
+ # 3. Inference
63
+ y_pts, weights = get_weighted_samples_at_x(y_test_samples, params, c_hat)
64
+ q80 = quantile_from_weighted_samples(y_pts, weights, 0.80)
65
+ lo, hi = central_interval_from_weighted_samples(y_pts, weights, alpha=0.10)
66
+ ```
67
+
68
+ x-dependent (GAM) correction:
69
+
70
+ ```python
71
+ from cpit.bc import fit_x_dependent_affine_gam
72
+
73
+ params_gam = fit_x_dependent_affine_gam(x_bias, y_bias, y_bias_samples)
74
+ y_pts, weights = get_weighted_samples_at_x(y_test_samples, params_gam, c_hat, x=x_test)
75
+ ```
76
+
77
+ High-level pipeline:
78
+
79
+ ```python
80
+ from cpit.pipeline import run_pipeline, predict_interval
81
+
82
+ state = run_pipeline(X, y, generator_fn)
83
+ lo, hi = predict_interval(state, X_test, y_samples_test, alpha=0.10)
84
+ ```
85
+
86
+ ## Reproduce paper results
87
+
88
+ ```bash
89
+ make test # unit tests
90
+ make run-sim # Designs 1/2/3 simulations (§5)
91
+ make pit-figures # PIT histogram figures
92
+ make run-wb2 # WB2 application: Taiwan + Europe (§6)
93
+ ```
94
+
95
+ ## Layout
96
+
97
+ ```
98
+ cpit/ Python package
99
+ bc/ bias correction (fit, apply)
100
+ baselines/ competing methods
101
+ evaluation/ metrics, PIT histograms, diagnostics
102
+ calibrator.py / pit.py / inference.py / weighted_samples.py / pipeline.py
103
+
104
+ examples/ orchestration scripts (call cpit)
105
+ simulation/ §5 designs 1–3
106
+ real_data/ §6 WB2 Taiwan + Europe
107
+ ```
108
+
109
+ ## Citation
110
+
111
+ The paper is currently being submitted to arXiv; the entry below will be updated with the final arXiv ID once it is live.
112
+
113
+ If you use this package, please cite:
114
+
115
+ > Wang, W.-T., Tzeng, S., Fan, Y.-T., & Huang, H.-C. (2026). Calibrated Predictive Distributions from Sample-Based Generators. *arXiv preprint arXiv:XXXX.XXXXX*.
116
+
117
+ ```bibtex
118
+ @article{wang2026calibrated,
119
+ title = {Calibrated Predictive Distributions from Sample-Based Generators},
120
+ author = {Wang, Wen-Ting and Tzeng, ShengLi and Fan, Yu-Ting and Huang, Hsin-Cheng},
121
+ journal = {arXiv preprint arXiv:XXXX.XXXXX},
122
+ year = {2026}
123
+ }
124
+ ```
125
+
126
+ ## License
127
+
128
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,42 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ bc_cpit.egg-info/PKG-INFO
5
+ bc_cpit.egg-info/SOURCES.txt
6
+ bc_cpit.egg-info/dependency_links.txt
7
+ bc_cpit.egg-info/requires.txt
8
+ bc_cpit.egg-info/top_level.txt
9
+ cpit/__init__.py
10
+ cpit/calibrator.py
11
+ cpit/data_splitter.py
12
+ cpit/inference.py
13
+ cpit/pipeline.py
14
+ cpit/pit.py
15
+ cpit/weighted_samples.py
16
+ cpit/baselines/__init__.py
17
+ cpit/baselines/quantile_baselines.py
18
+ cpit/baselines/score_baselines.py
19
+ cpit/bc/__init__.py
20
+ cpit/bc/apply.py
21
+ cpit/bc/fit.py
22
+ cpit/bc/params.py
23
+ cpit/evaluation/__init__.py
24
+ cpit/evaluation/local_diagnostics.py
25
+ cpit/evaluation/metrics.py
26
+ cpit/evaluation/pit_histogram.py
27
+ tests/test_bias_corrector.py
28
+ tests/test_data_splitter.py
29
+ tests/test_draft6_spec_matrix.py
30
+ tests/test_evaluation.py
31
+ tests/test_golden_design1.py
32
+ tests/test_inference_api.py
33
+ tests/test_inference_extra.py
34
+ tests/test_metrics_extra.py
35
+ tests/test_pipeline.py
36
+ tests/test_pit_calibrator.py
37
+ tests/test_pit_extra.py
38
+ tests/test_pit_histogram.py
39
+ tests/test_predictive_cdf.py
40
+ tests/test_quantile_baselines.py
41
+ tests/test_score_baselines.py
42
+ tests/test_score_baselines_extra.py
@@ -0,0 +1,6 @@
1
+ numpy>=1.20
2
+ scipy>=1.7
3
+ pygam>=0.9
4
+
5
+ [dev]
6
+ pytest>=7.0
@@ -0,0 +1,3 @@
1
+ cpit
2
+ dist
3
+ envs
@@ -0,0 +1,50 @@
1
+ """cpit: bias-corrected conformal PIT calibration for sample-based generators."""
2
+
3
+ from .calibrator import (
4
+ build_c_hat,
5
+ build_c_hat_inv,
6
+ build_c_hat_plus,
7
+ c_hat_monotone_and_bounds,
8
+ fit_conformal_calibrator,
9
+ )
10
+ from .inference import (
11
+ calibrated_resample,
12
+ central_interval_from_f_tilde,
13
+ central_interval_from_weighted_samples,
14
+ hdr_interval_from_weighted_samples,
15
+ pit_inverted_interval,
16
+ quantile_from_f_tilde,
17
+ quantile_from_weighted_samples,
18
+ quantile_from_weighted_samples_batched,
19
+ )
20
+ from .pit import randomized_pit, randomized_pit_batch
21
+ from .weighted_samples import (
22
+ empirical_cdf_at_y,
23
+ f_adj_from_samples,
24
+ f_tilde_from_samples,
25
+ get_weighted_samples_at_x,
26
+ weighted_samples_from_cdf,
27
+ )
28
+
29
+ __all__ = [
30
+ "randomized_pit",
31
+ "randomized_pit_batch",
32
+ "build_c_hat",
33
+ "build_c_hat_plus",
34
+ "fit_conformal_calibrator",
35
+ "build_c_hat_inv",
36
+ "c_hat_monotone_and_bounds",
37
+ "empirical_cdf_at_y",
38
+ "f_adj_from_samples",
39
+ "f_tilde_from_samples",
40
+ "weighted_samples_from_cdf",
41
+ "get_weighted_samples_at_x",
42
+ "quantile_from_weighted_samples",
43
+ "quantile_from_weighted_samples_batched",
44
+ "central_interval_from_weighted_samples",
45
+ "hdr_interval_from_weighted_samples",
46
+ "quantile_from_f_tilde",
47
+ "central_interval_from_f_tilde",
48
+ "pit_inverted_interval",
49
+ "calibrated_resample",
50
+ ]
@@ -0,0 +1,14 @@
1
+ from .quantile_baselines import (
2
+ cqr_intervals_batch,
3
+ cqr_radius_from_calibration,
4
+ quantile_interval_from_samples,
5
+ )
6
+ from .score_baselines import crps_split_conformal_interval, sr_split_conformal_interval
7
+
8
+ __all__ = [
9
+ "quantile_interval_from_samples",
10
+ "cqr_radius_from_calibration",
11
+ "cqr_intervals_batch",
12
+ "sr_split_conformal_interval",
13
+ "crps_split_conformal_interval",
14
+ ]
@@ -0,0 +1,90 @@
1
+ """
2
+ Quantile-based baselines: raw quantile interval (QR0/QR), CQR from samples.
3
+ CQR: Conformalized Quantile Regression — radius = conformal quantile of calibration
4
+ residuals; interval = [q_lo - radius, q_hi + radius] per test point.
5
+ """
6
+
7
+ from typing import Tuple
8
+
9
+ import numpy as np
10
+
11
+ from cpit.bc import AffineParams, apply_affine
12
+
13
+
14
+ def _conformal_quantile_level(n: int, alpha: float) -> float:
15
+ """Level for (1-alpha) coverage: ceil((n+1)(1-alpha))/n."""
16
+ if n == 0:
17
+ raise ValueError("n must be positive")
18
+ return min(1.0, np.ceil((n + 1) * (1.0 - alpha)) / n)
19
+
20
+
21
+ def quantile_interval_from_samples(
22
+ y_samples: np.ndarray,
23
+ alpha: float,
24
+ ) -> Tuple[float, float]:
25
+ """
26
+ Central (1-alpha) interval from raw sample quantiles (no calibration).
27
+ QR0-style: use empirical quantiles of y_samples.
28
+ """
29
+ y = np.asarray(y_samples).ravel()
30
+ low = np.quantile(y, alpha / 2)
31
+ high = np.quantile(y, 1.0 - alpha / 2)
32
+ return float(low), float(high)
33
+
34
+
35
+ def cqr_radius_from_calibration(
36
+ y_calibration_samples: np.ndarray,
37
+ y_calibration_observed: np.ndarray,
38
+ alpha: float,
39
+ params: AffineParams | None = None,
40
+ ) -> float:
41
+ """
42
+ CQR radius from calibration: residual_i = max(q_lo_i - y_i, y_i - q_hi_i),
43
+ radius = conformal (1-alpha) quantile of residuals (method="higher").
44
+ If params is not None, apply affine to samples before taking quantiles.
45
+ Negative radius is allowed (§4.1): a negative q shrinks an over-conservative base interval.
46
+ """
47
+ y_cal_s = np.asarray(y_calibration_samples)
48
+ y_cal_o = np.asarray(y_calibration_observed).ravel()
49
+ if y_cal_s.ndim == 1:
50
+ y_cal_s = y_cal_s.reshape(1, -1)
51
+ n_cal = y_cal_s.shape[0]
52
+ if params is not None:
53
+ y_cal_s = np.array([apply_affine(y_cal_s[i], params) for i in range(n_cal)])
54
+ low_cal = np.quantile(y_cal_s, alpha / 2, axis=1)
55
+ high_cal = np.quantile(y_cal_s, 1.0 - alpha / 2, axis=1)
56
+ residuals = np.maximum(low_cal - y_cal_o, y_cal_o - high_cal)
57
+ level = _conformal_quantile_level(n_cal, alpha)
58
+ return float(np.quantile(residuals, level, method="higher"))
59
+
60
+
61
+ def cqr_intervals_batch(
62
+ y_calibration_samples: np.ndarray,
63
+ y_calibration_observed: np.ndarray,
64
+ y_test_samples: np.ndarray,
65
+ alpha: float,
66
+ params: AffineParams | None = None,
67
+ ) -> tuple[np.ndarray, float]:
68
+ """
69
+ CQR for many test points: one radius from calibration; per-test interval
70
+ [q_lo - radius, q_hi + radius] using (optionally affine-corrected) quantiles.
71
+ Returns (intervals (n_test, 2), radius). Negative radius allowed (§4.1).
72
+ """
73
+ radius = cqr_radius_from_calibration(
74
+ y_calibration_samples,
75
+ y_calibration_observed,
76
+ alpha,
77
+ params=params,
78
+ )
79
+ y_test_s = np.asarray(y_test_samples)
80
+ if y_test_s.ndim == 1:
81
+ y_test_s = y_test_s.reshape(1, -1)
82
+ n_test = y_test_s.shape[0]
83
+ if params is not None:
84
+ y_test_s = np.array([apply_affine(y_test_s[i], params) for i in range(n_test)])
85
+ low_test = np.quantile(y_test_s, alpha / 2, axis=1)
86
+ high_test = np.quantile(y_test_s, 1.0 - alpha / 2, axis=1)
87
+ # When radius < 0 (base over-covers), low - r > high + r; sort so [min, max]
88
+ endpoints = np.stack([low_test - radius, high_test + radius], axis=1)
89
+ intervals = np.sort(endpoints, axis=1)
90
+ return intervals, radius