POPSRegression 0.4.0__tar.gz → 0.5.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 (73) hide show
  1. {popsregression-0.4.0 → popsregression-0.5.0}/.github/workflows/deploy-gh-pages.yml +6 -7
  2. popsregression-0.5.0/.github/workflows/lint.yml +30 -0
  3. {popsregression-0.4.0 → popsregression-0.5.0}/.github/workflows/python-app.yml +8 -8
  4. {popsregression-0.4.0 → popsregression-0.5.0}/.gitignore +10 -12
  5. popsregression-0.5.0/MANIFEST.in +23 -0
  6. popsregression-0.5.0/PKG-INFO +241 -0
  7. popsregression-0.5.0/README.md +212 -0
  8. {popsregression-0.4.0 → popsregression-0.5.0}/popsregression/__init__.py +4 -0
  9. popsregression-0.5.0/popsregression/_base.py +196 -0
  10. popsregression-0.5.0/popsregression/_bayes.py +138 -0
  11. popsregression-0.5.0/popsregression/_ellipse.py +892 -0
  12. popsregression-0.5.0/popsregression/_ellipse_regression.py +1709 -0
  13. popsregression-0.5.0/popsregression/_optimize.py +179 -0
  14. popsregression-0.5.0/popsregression/_pops.py +662 -0
  15. popsregression-0.5.0/popsregression/_projected_ball.py +522 -0
  16. popsregression-0.5.0/popsregression/_version.py +24 -0
  17. popsregression-0.5.0/popsregression/tests/test_ellipse_estimator.py +641 -0
  18. popsregression-0.5.0/popsregression/tests/test_ellipse_gradients.py +186 -0
  19. popsregression-0.5.0/popsregression/tests/test_ellipse_regression.py +511 -0
  20. popsregression-0.5.0/popsregression/tests/test_ellipse_statistics.py +103 -0
  21. popsregression-0.5.0/popsregression/tests/test_pops.py +593 -0
  22. popsregression-0.5.0/popsregression.egg-info/PKG-INFO +241 -0
  23. popsregression-0.5.0/popsregression.egg-info/SOURCES.txt +35 -0
  24. popsregression-0.5.0/popsregression.egg-info/requires.txt +8 -0
  25. popsregression-0.5.0/popsregression.egg-info/scm_file_list.json +109 -0
  26. popsregression-0.5.0/popsregression.egg-info/scm_version.json +8 -0
  27. {popsregression-0.4.0 → popsregression-0.5.0}/pyproject.toml +41 -64
  28. popsregression-0.4.0/.github/workflows/lint.yml +0 -28
  29. popsregression-0.4.0/PKG-INFO +0 -165
  30. popsregression-0.4.0/README.md +0 -140
  31. popsregression-0.4.0/SimpleExample.ipynb +0 -59
  32. popsregression-0.4.0/doc/Makefile +0 -184
  33. popsregression-0.4.0/doc/_static/css/project-template.css +0 -59
  34. popsregression-0.4.0/doc/_static/img/index_api.svg +0 -97
  35. popsregression-0.4.0/doc/_static/img/index_examples.svg +0 -76
  36. popsregression-0.4.0/doc/_static/img/index_getting_started.svg +0 -66
  37. popsregression-0.4.0/doc/_static/img/index_user_guide.svg +0 -67
  38. popsregression-0.4.0/doc/_static/img/logo.png +0 -0
  39. popsregression-0.4.0/doc/_static/js/copybutton.js +0 -63
  40. popsregression-0.4.0/doc/_templates/class.rst +0 -26
  41. popsregression-0.4.0/doc/_templates/function.rst +0 -12
  42. popsregression-0.4.0/doc/_templates/numpydoc_docstring.py +0 -16
  43. popsregression-0.4.0/doc/_templates/sidebar-search-bs.html +0 -14
  44. popsregression-0.4.0/doc/api.rst +0 -27
  45. popsregression-0.4.0/doc/conf.py +0 -114
  46. popsregression-0.4.0/doc/index.rst +0 -108
  47. popsregression-0.4.0/doc/make.bat +0 -242
  48. popsregression-0.4.0/doc/quick_start.rst +0 -67
  49. popsregression-0.4.0/doc/user_guide.rst +0 -114
  50. popsregression-0.4.0/example_image.png +0 -0
  51. popsregression-0.4.0/examples/README.txt +0 -6
  52. popsregression-0.4.0/examples/plot_pops_regression.py +0 -220
  53. popsregression-0.4.0/popsregression/_pops.py +0 -509
  54. popsregression-0.4.0/popsregression/_version.py +0 -34
  55. popsregression-0.4.0/popsregression/tests/test_common.py +0 -17
  56. popsregression-0.4.0/popsregression/tests/test_pops.py +0 -258
  57. popsregression-0.4.0/popsregression/utils/__init__.py +0 -2
  58. popsregression-0.4.0/popsregression/utils/discovery.py +0 -221
  59. popsregression-0.4.0/popsregression/utils/tests/__init__.py +0 -2
  60. popsregression-0.4.0/popsregression/utils/tests/test_discovery.py +0 -32
  61. popsregression-0.4.0/popsregression.egg-info/PKG-INFO +0 -165
  62. popsregression-0.4.0/popsregression.egg-info/SOURCES.txt +0 -49
  63. popsregression-0.4.0/popsregression.egg-info/requires.txt +0 -3
  64. {popsregression-0.4.0 → popsregression-0.5.0}/.coveragerc +0 -0
  65. {popsregression-0.4.0 → popsregression-0.5.0}/.github/dependabot.yml +0 -0
  66. {popsregression-0.4.0 → popsregression-0.5.0}/.github/workflows/publish.yml +0 -0
  67. {popsregression-0.4.0 → popsregression-0.5.0}/.pre-commit-config.yaml +0 -0
  68. {popsregression-0.4.0 → popsregression-0.5.0}/LICENSE +0 -0
  69. {popsregression-0.4.0 → popsregression-0.5.0}/POPSRegression.py +0 -0
  70. {popsregression-0.4.0 → popsregression-0.5.0}/popsregression/tests/__init__.py +0 -0
  71. {popsregression-0.4.0 → popsregression-0.5.0}/popsregression.egg-info/dependency_links.txt +0 -0
  72. {popsregression-0.4.0 → popsregression-0.5.0}/popsregression.egg-info/top_level.txt +0 -0
  73. {popsregression-0.4.0 → popsregression-0.5.0}/setup.cfg +0 -0
@@ -20,20 +20,19 @@ jobs:
20
20
  with:
21
21
  fetch-depth: 0
22
22
 
23
- - uses: prefix-dev/setup-pixi@v0.9.4
23
+ - uses: astral-sh/setup-uv@v6
24
24
  with:
25
- pixi-version: v0.41.4
26
- environments: doc
27
- frozen: false
25
+ enable-cache: true
26
+ python-version: "3.12"
28
27
 
29
28
  - name: Build documentation
30
- run: pixi run -e doc build-doc
29
+ run: uv run --locked --group doc mkdocs build
31
30
 
32
31
  - name: Upload pages artifact
33
32
  if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
34
- uses: actions/upload-pages-artifact@v3
33
+ uses: actions/upload-pages-artifact@v4
35
34
  with:
36
- path: ./doc/_build/html
35
+ path: ./site
37
36
 
38
37
  deploy:
39
38
  if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
@@ -0,0 +1,30 @@
1
+ name: Linter
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+ branches:
9
+ - main
10
+
11
+ concurrency:
12
+ group: ${{ github.workflow }}-${{ github.ref }}
13
+ cancel-in-progress: true
14
+
15
+ jobs:
16
+ build:
17
+ runs-on: ubuntu-latest
18
+ steps:
19
+ - uses: actions/checkout@v6
20
+
21
+ - uses: astral-sh/setup-uv@v6
22
+ with:
23
+ enable-cache: true
24
+ python-version: "3.12"
25
+
26
+ - name: Check formatting
27
+ run: uv run --locked --group lint black --check --diff popsregression examples
28
+
29
+ - name: Run linter
30
+ run: uv run --locked --group lint ruff check --output-format=full popsregression examples
@@ -17,25 +17,25 @@ jobs:
17
17
  strategy:
18
18
  matrix:
19
19
  os: [windows-latest, ubuntu-latest, macos-latest]
20
- environment: [test]
21
20
  runs-on: ${{ matrix.os }}
22
21
  steps:
23
22
  - uses: actions/checkout@v6
24
23
  with:
25
24
  fetch-depth: 0
26
25
 
27
- - uses: prefix-dev/setup-pixi@v0.9.4
26
+ - uses: astral-sh/setup-uv@v6
28
27
  with:
29
- pixi-version: v0.41.4
30
- environments: ${{ matrix.environment }}
31
- frozen: false
28
+ enable-cache: true
29
+ python-version: "3.12"
32
30
 
33
31
  - name: Run tests
34
- run: pixi run -e ${{ matrix.environment }} test
32
+ run: >-
33
+ uv run --locked --group test
34
+ pytest -vsl --cov=popsregression --cov-report=xml popsregression examples/comparisons
35
35
 
36
36
  - name: Upload coverage reports to Codecov
37
- uses: codecov/codecov-action@v5.5.2
37
+ uses: codecov/codecov-action@v5.5.3
38
38
  continue-on-error: true
39
39
  with:
40
40
  token: ${{ secrets.CODECOV_TOKEN }}
41
- slug: tomswinburne/popsregression
41
+ slug: POPS-UQ/popsregression
@@ -6,12 +6,6 @@ __pycache__/
6
6
  # C extensions
7
7
  *.so
8
8
 
9
- # scikit-learn specific
10
- doc/_build/
11
- doc/auto_examples/
12
- doc/modules/generated/
13
- doc/datasets/generated/
14
-
15
9
  # Distribution / packaging
16
10
 
17
11
  .Python
@@ -59,15 +53,12 @@ coverage.xml
59
53
  # Django stuff:
60
54
  *.log
61
55
 
62
- # Sphinx documentation
63
- doc/_build/
64
- doc/generated/
65
- doc/sg_execution_times.rst
56
+ # MkDocs documentation
57
+ site/
66
58
 
67
59
  # PyBuilder
68
60
  target/
69
61
 
70
- .pixi
71
62
 
72
63
  # General
73
64
  .DS_Store
@@ -75,4 +66,11 @@ target/
75
66
  .LSOverride
76
67
 
77
68
  # auto-generated files
78
- popsregression/_version.py
69
+ popsregression/_version.py
70
+
71
+ # Local virtual environments
72
+ .venv/
73
+ venv/
74
+
75
+ # Per-point prediction exports (large, regenerable)
76
+ examples/generated/predictions/
@@ -0,0 +1,23 @@
1
+ # What ships to PyPI.
2
+ #
3
+ # setuptools_scm hands the sdist every git-tracked file, so without these rules
4
+ # `pip install popsregression` from source would also download the example
5
+ # scripts, their bundled ACE data and every figure. Those belong on GitHub
6
+ # only; the sdist keeps just what is needed to build, install and test.
7
+
8
+ include LICENSE
9
+ include README.md
10
+ include pyproject.toml
11
+
12
+ prune examples
13
+ prune docs
14
+
15
+ exclude MERGE_NOTES.md
16
+ exclude SimpleExample.ipynb
17
+ exclude mkdocs.yml
18
+ exclude uv.lock
19
+
20
+ # Belt and braces: never ship bulk data or figures, wherever they are added.
21
+ global-exclude *.npz *.npy *.pkl *.pickle
22
+ global-exclude *.png *.pdf *.ipynb
23
+ global-exclude __pycache__/* *.py[cod]
@@ -0,0 +1,241 @@
1
+ Metadata-Version: 2.4
2
+ Name: popsregression
3
+ Version: 0.5.0
4
+ Summary: Bayesian regression for low-noise data with misspecification uncertainty (POPS algorithm).
5
+ Author-email: Thomas D Swinburne <tswin@umich.edu>, Danny Perez <danny_perez@lanl.gov>
6
+ Project-URL: Homepage, https://github.com/POPS-UQ/popsregression
7
+ Project-URL: Issues, https://github.com/POPS-UQ/popsregression/issues
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: License :: OSI Approved :: BSD License
14
+ Classifier: Operating System :: POSIX
15
+ Classifier: Operating System :: Unix
16
+ Classifier: Operating System :: MacOS
17
+ Classifier: Operating System :: Microsoft :: Windows
18
+ Requires-Python: >=3.10
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: jax>=0.4.35
22
+ Requires-Dist: scipy>=1.7.0
23
+ Requires-Dist: numpy>=1.22.0
24
+ Provides-Extra: examples
25
+ Requires-Dist: matplotlib; extra == "examples"
26
+ Requires-Dist: pandas>=2.3.3; extra == "examples"
27
+ Requires-Dist: scikit-learn>=1.6.1; extra == "examples"
28
+ Dynamic: license-file
29
+
30
+ # popsregression
31
+
32
+ Post-Bayesian regression for misspecified, near-deterministic models. This is the JAX implementation of **Pointwise Optimal Parameter Sets (POPS)** for models linear in their parameters, including polynomial surrogates and linear interatomic potentials.
33
+
34
+ A single coefficient vector can minimize the total training loss while leaving systematic residuals on individual observations. POPS uses pointwise constrained fits to estimate how far those coefficients must vary to account for model-form error. Sampling whole coefficient vectors lets that uncertainty propagate to downstream observables.
35
+
36
+ - **`POPSRegression`** constructs the pointwise ensemble or its PCA-aligned hypercube.
37
+ - **`POPSEllipseRegression`** fits an ellipsoidal parameter distribution with optional empirical-Bayes or PAC-Bayes regularization.
38
+
39
+ [Method and tutorials](https://pops-uq.github.io/) · [Python documentation for this branch](docs/index.md) · [Kermode group regression demo](https://kermodegroup.github.io/demos/regression-demo.html) · [Standalone Julia implementation](https://github.com/POPS-UQ/POPSRegression.jl)
40
+
41
+ ## Installation
42
+
43
+ Install this checkout with Python 3.10 or newer. The numerical dependencies are JAX, SciPy and NumPy.
44
+
45
+ ```bash
46
+ git clone --branch jax-integration https://github.com/POPS-UQ/popsregression.git
47
+ cd popsregression
48
+ python -m pip install -e .
49
+ ```
50
+
51
+ ## Quick start
52
+
53
+ ```python
54
+ import numpy as np
55
+ from popsregression import POPSRegression
56
+
57
+ x = np.linspace(-1, 1, 32)
58
+ X_train = np.polynomial.legendre.legvander(x, 4)
59
+ y_train = np.sin(2 * np.pi * x) + 0.3 * x**2
60
+ X_test = np.polynomial.legendre.legvander(np.linspace(-1, 1, 201), 4)
61
+
62
+ # Fit POPSRegression
63
+ # fit_intercept=False by default
64
+ model = POPSRegression()
65
+ model.fit(X_train, y_train)
66
+
67
+ # Prediction with misspecification & epistemic uncertainty
68
+ y_pred, y_std = model.predict(X_test, return_std=True)
69
+
70
+ # Also return min/max bounds over the posterior
71
+ y_pred, y_std, y_max, y_min = model.predict(
72
+ X_test, return_std=True, return_bounds=True
73
+ )
74
+
75
+ # Also return epistemic-only uncertainty separately
76
+ y_pred, y_std, y_max, y_min, y_epistemic_std = model.predict(
77
+ X_test,
78
+ return_std=True,
79
+ return_bounds=True,
80
+ return_epistemic_std=True,
81
+ )
82
+ ```
83
+
84
+ ## Key parameters
85
+
86
+ | Parameter | Default | Description |
87
+ |---|---|---|
88
+ | `posterior` | `'hypercube'` | Posterior form: `'hypercube'` (PCA-aligned box) or `'ensemble'` (raw corrections); the ellipsoid is `POPSEllipseRegression` (below) |
89
+ | `random_state` | `None` | Seed for the posterior resampling; `None` uses the global NumPy state |
90
+ | `resampling_method` | `'uniform'` | Sampling method: `'uniform'`, `'sobol'`, `'latin'`, `'halton'` |
91
+ | `resample_density` | `1.0` | Number of posterior samples per training point |
92
+ | `prior_precision` | `None` | Shape `Λ` (vector or matrix) of the Gaussian prior `N(0, (λΛ)⁻¹)` on the coefficients; the scale `λ` is learned. `None` is the isotropic prior |
93
+ | `leverage_regularization` | `0.001` | Regularizes the leverage in the pointwise corrections, `e_i A⁻¹x_i / (h_i + eps)` with `eps` this times the mean leverage; `0.0` is the exact (unstable for low-leverage points) pointwise fit |
94
+ | `minimum_relative_correction` | `0.01` | Relative threshold on the correction norm: only points with \|\|dθ_i\|\| >= this times the RMS norm enter the singular decomposition and bounds of the POPS posterior |
95
+ | `mode_threshold` | `1e-8` | Eigenvalue threshold for hypercube dimensionality |
96
+ | `percentile_clipping` | `0.0` | Percentile to clip from hypercube bounds (0–50) |
97
+
98
+ The Bayesian ridge parameters of the mean fit (`max_iter`, `tol`, `alpha_1`,
99
+ `alpha_2`, `lambda_1`, `lambda_2`, `alpha_init`, `lambda_init`,
100
+ `fit_intercept`) are also supported.
101
+
102
+ > [!WARNING]
103
+ > `minimum_relative_error` is deprecated: training points are now selected by
104
+ > the norm of their pointwise correction rather than by their residual. A
105
+ > value passed to it is forwarded to `minimum_relative_correction` with a
106
+ > `FutureWarning`. `leverage_percentile`, `compute_score`, `copy_X` and
107
+ > `verbose` have been removed.
108
+
109
+ ## Key attributes (after fitting)
110
+
111
+ | Attribute | Description |
112
+ |---|---|
113
+ | `coef_`, `intercept_` | Regression coefficients (posterior mean) and intercept |
114
+ | `sigma_` | Epistemic variance-covariance matrix |
115
+ | `misspecification_sigma_` | Misspecification variance-covariance matrix from POPS |
116
+ | `posterior_samples_` | Weight perturbations, with one sample per column |
117
+ | `alpha_` | Estimated noise precision (not used for prediction) |
118
+ | `pointwise_corrections_`, `leverage_`, `selected_` | Pointwise corrections, leverages and the points kept for the posterior |
119
+
120
+ ## Ellipsoid posteriors: `POPSEllipseRegression`
121
+ [Run the PAC-Ellipse tutorial](docs/pac-ellipse.md) to fit a bounded simulator, inspect its risk certificate and propagate joint predictions.
122
+
123
+ `POPSEllipseRegression` fits a parameter distribution that is uniform on an
124
+ ellipsoid. For a linear model its predictive density at `x` has a closed form,
125
+ so the zero-noise generalization error is minimized directly. Predictive
126
+ densities, exact quantiles and parameter draws are available in closed form
127
+ (for the hierarchical layers, exactly for the stored finite mixture of
128
+ hyperparameter draws that represents the predictive). The `regularization` parameter chooses how the finite-data uncertainty
129
+ of the ellipsoid itself is handled:
130
+
131
+ ```python
132
+ from popsregression import POPSEllipseRegression
133
+
134
+ bare = POPSEllipseRegression().fit(X_train, y_train)
135
+
136
+ # Empirical-Bayes layer (Ellipse+EB): a Laplace hyperposterior over the
137
+ # ellipsoid, with its prior centered on the fit. Cheap; its diagnostic_bound_
138
+ # is not a PAC bound.
139
+ eb = POPSEllipseRegression(regularization="empirical-bayes").fit(X_train, y_train)
140
+
141
+ # PAC layer (Ellipse+PAC): the prior over the ellipsoid is fixed on a random
142
+ # half of the data, and a PAC-Bayes bound on the population log risk is
143
+ # evaluated on the other half (and vice versa). y_bounds must be known in
144
+ # advance (e.g. from a maximum principle), not read off the data.
145
+ pac = POPSEllipseRegression(regularization="PAC", y_bounds=(y_lower, y_upper))
146
+ pac.fit(X_train, y_train, groups=case_id) # groups: rows of one simulator case
147
+ pac.certificate_.raw_bound, pac.certificate_.trivial_bound # bound for the returned predictive
148
+
149
+ lo, hi = eb.predict_interval(X_test, level=0.9545) # exact central interval
150
+ logp = eb.predict_logpdf(X_test, y_test)
151
+ fields = eb.sample_predictions(X_test, 1000) # joint draws for propagation
152
+ ```
153
+
154
+ All predictions refer to parameter uncertainty only; no noise term is added.
155
+ The PAC bound certifies the log risk of the predictive density, not interval
156
+ coverage. See the
157
+ [ellipse documentation](docs/ellipse.md)
158
+ and the [glossary](docs/glossary.md) for the
159
+ terms used above.
160
+
161
+ ## Comparison methods and paper studies
162
+
163
+ PACm, PAC²-T, predictive variational inference and a finite
164
+ POPS-dictionary weighting ablation are implemented in `examples/comparisons/`,
165
+ outside the package. The scripts in `examples/` produce every figure and table
166
+ of the accompanying paper; see [examples/README.md](examples/README.md).
167
+
168
+ ## Backend
169
+
170
+ All numerical kernels (the Bayesian ridge fit, the pointwise corrections, the
171
+ ellipsoid optimization, the PAC certification and the mixture predictives) are
172
+ written in [JAX](https://jax.readthedocs.io) and compiled on first use.
173
+
174
+ - The first call for a given problem shape pays a compilation cost of about a
175
+ second; later calls with the same shapes reuse the compiled kernel.
176
+ - Importing the package enables JAX double precision
177
+ (`jax_enable_x64`), a process-wide JAX setting.
178
+ - With a GPU build of JAX the same code runs on the accelerator.
179
+ - Inputs and fitted attributes (`coef_`, `sigma_`, ...) are NumPy arrays.
180
+
181
+ scikit-learn is not required. The estimators keep the familiar
182
+ `fit` / `predict` / `get_params` / `set_params` usage, so they still work
183
+ inside scikit-learn pipelines and parameter searches when it is installed.
184
+
185
+ ## Pipeline compatibility
186
+
187
+ If scikit-learn is installed, both estimators can be used in its pipelines and
188
+ hyperparameter search:
189
+
190
+ ```python
191
+ from sklearn.pipeline import make_pipeline
192
+ from sklearn.preprocessing import PolynomialFeatures
193
+
194
+ pipe = make_pipeline(
195
+ PolynomialFeatures(degree=4),
196
+ POPSRegression(resampling_method='sobol'),
197
+ )
198
+ pipe.fit(X_train, y_train)
199
+ y_pred = pipe.predict(X_test)
200
+ ```
201
+
202
+ ## Development
203
+
204
+ The repository is managed with [uv](https://docs.astral.sh/uv/); `uv run`
205
+ resolves the pinned environment from `uv.lock` on first use.
206
+
207
+ ```bash
208
+ uv run --group test pytest -vsl popsregression examples/comparisons # tests
209
+ uv run --group lint ruff check popsregression examples # linter
210
+ uv run --group lint black --check popsregression examples
211
+ uv run --group doc mkdocs serve # docs at localhost:8000
212
+ cd examples && uv run --extra examples python example_polynomial.py # a figure
213
+ ```
214
+
215
+ Without uv, `pip install -e ".[examples]"` and run the tools directly.
216
+
217
+ ## Citation
218
+
219
+ > *Parameter uncertainties for imperfect surrogate models in the low-noise regime*
220
+ >
221
+ > TD Swinburne and D Perez, [Machine Learning: Science and Technology 2025](https://doi.org/10.1088/2632-2153/ad9fce)
222
+
223
+ ```bibtex
224
+ @article{swinburne2025,
225
+ author={Swinburne, Thomas and Perez, Danny},
226
+ title={Parameter uncertainties for imperfect surrogate models in the low-noise regime},
227
+ journal={Machine Learning: Science and Technology},
228
+ doi={10.1088/2632-2153/ad9fce},
229
+ year={2025}
230
+ }
231
+ ```
232
+
233
+ For the PAC-Bayes construction, see Thomas D. Swinburne, Omar Hussein, Ian Lynch and Danny Perez, [PAC-Bayesian regression for misspecified, near-deterministic surrogate models](https://openreview.net/forum?id=gD3LoosP0w).
234
+
235
+ ## Contributors
236
+
237
+ Thomas D. Swinburne and Danny Perez developed the original POPS method. James Kermode contributed the Gaussian parameter prior for the ellipsoid shape and its initialization in [PR #15](https://github.com/POPS-UQ/popsregression/pull/15). The [JAX adaptation](https://github.com/POPS-UQ/popsregression/commit/8c129db) uses the prior scale learned by the Bayesian ridge fit. Enable it with `prior_kl=True` on `POPSEllipseRegression`.
238
+
239
+ ## AI usage
240
+
241
+ Claude assisted with documentation and test cases. Thomas D. Swinburne reviewed the code described in the original release.
@@ -0,0 +1,212 @@
1
+ # popsregression
2
+
3
+ Post-Bayesian regression for misspecified, near-deterministic models. This is the JAX implementation of **Pointwise Optimal Parameter Sets (POPS)** for models linear in their parameters, including polynomial surrogates and linear interatomic potentials.
4
+
5
+ A single coefficient vector can minimize the total training loss while leaving systematic residuals on individual observations. POPS uses pointwise constrained fits to estimate how far those coefficients must vary to account for model-form error. Sampling whole coefficient vectors lets that uncertainty propagate to downstream observables.
6
+
7
+ - **`POPSRegression`** constructs the pointwise ensemble or its PCA-aligned hypercube.
8
+ - **`POPSEllipseRegression`** fits an ellipsoidal parameter distribution with optional empirical-Bayes or PAC-Bayes regularization.
9
+
10
+ [Method and tutorials](https://pops-uq.github.io/) · [Python documentation for this branch](docs/index.md) · [Kermode group regression demo](https://kermodegroup.github.io/demos/regression-demo.html) · [Standalone Julia implementation](https://github.com/POPS-UQ/POPSRegression.jl)
11
+
12
+ ## Installation
13
+
14
+ Install this checkout with Python 3.10 or newer. The numerical dependencies are JAX, SciPy and NumPy.
15
+
16
+ ```bash
17
+ git clone --branch jax-integration https://github.com/POPS-UQ/popsregression.git
18
+ cd popsregression
19
+ python -m pip install -e .
20
+ ```
21
+
22
+ ## Quick start
23
+
24
+ ```python
25
+ import numpy as np
26
+ from popsregression import POPSRegression
27
+
28
+ x = np.linspace(-1, 1, 32)
29
+ X_train = np.polynomial.legendre.legvander(x, 4)
30
+ y_train = np.sin(2 * np.pi * x) + 0.3 * x**2
31
+ X_test = np.polynomial.legendre.legvander(np.linspace(-1, 1, 201), 4)
32
+
33
+ # Fit POPSRegression
34
+ # fit_intercept=False by default
35
+ model = POPSRegression()
36
+ model.fit(X_train, y_train)
37
+
38
+ # Prediction with misspecification & epistemic uncertainty
39
+ y_pred, y_std = model.predict(X_test, return_std=True)
40
+
41
+ # Also return min/max bounds over the posterior
42
+ y_pred, y_std, y_max, y_min = model.predict(
43
+ X_test, return_std=True, return_bounds=True
44
+ )
45
+
46
+ # Also return epistemic-only uncertainty separately
47
+ y_pred, y_std, y_max, y_min, y_epistemic_std = model.predict(
48
+ X_test,
49
+ return_std=True,
50
+ return_bounds=True,
51
+ return_epistemic_std=True,
52
+ )
53
+ ```
54
+
55
+ ## Key parameters
56
+
57
+ | Parameter | Default | Description |
58
+ |---|---|---|
59
+ | `posterior` | `'hypercube'` | Posterior form: `'hypercube'` (PCA-aligned box) or `'ensemble'` (raw corrections); the ellipsoid is `POPSEllipseRegression` (below) |
60
+ | `random_state` | `None` | Seed for the posterior resampling; `None` uses the global NumPy state |
61
+ | `resampling_method` | `'uniform'` | Sampling method: `'uniform'`, `'sobol'`, `'latin'`, `'halton'` |
62
+ | `resample_density` | `1.0` | Number of posterior samples per training point |
63
+ | `prior_precision` | `None` | Shape `Λ` (vector or matrix) of the Gaussian prior `N(0, (λΛ)⁻¹)` on the coefficients; the scale `λ` is learned. `None` is the isotropic prior |
64
+ | `leverage_regularization` | `0.001` | Regularizes the leverage in the pointwise corrections, `e_i A⁻¹x_i / (h_i + eps)` with `eps` this times the mean leverage; `0.0` is the exact (unstable for low-leverage points) pointwise fit |
65
+ | `minimum_relative_correction` | `0.01` | Relative threshold on the correction norm: only points with \|\|dθ_i\|\| >= this times the RMS norm enter the singular decomposition and bounds of the POPS posterior |
66
+ | `mode_threshold` | `1e-8` | Eigenvalue threshold for hypercube dimensionality |
67
+ | `percentile_clipping` | `0.0` | Percentile to clip from hypercube bounds (0–50) |
68
+
69
+ The Bayesian ridge parameters of the mean fit (`max_iter`, `tol`, `alpha_1`,
70
+ `alpha_2`, `lambda_1`, `lambda_2`, `alpha_init`, `lambda_init`,
71
+ `fit_intercept`) are also supported.
72
+
73
+ > [!WARNING]
74
+ > `minimum_relative_error` is deprecated: training points are now selected by
75
+ > the norm of their pointwise correction rather than by their residual. A
76
+ > value passed to it is forwarded to `minimum_relative_correction` with a
77
+ > `FutureWarning`. `leverage_percentile`, `compute_score`, `copy_X` and
78
+ > `verbose` have been removed.
79
+
80
+ ## Key attributes (after fitting)
81
+
82
+ | Attribute | Description |
83
+ |---|---|
84
+ | `coef_`, `intercept_` | Regression coefficients (posterior mean) and intercept |
85
+ | `sigma_` | Epistemic variance-covariance matrix |
86
+ | `misspecification_sigma_` | Misspecification variance-covariance matrix from POPS |
87
+ | `posterior_samples_` | Weight perturbations, with one sample per column |
88
+ | `alpha_` | Estimated noise precision (not used for prediction) |
89
+ | `pointwise_corrections_`, `leverage_`, `selected_` | Pointwise corrections, leverages and the points kept for the posterior |
90
+
91
+ ## Ellipsoid posteriors: `POPSEllipseRegression`
92
+ [Run the PAC-Ellipse tutorial](docs/pac-ellipse.md) to fit a bounded simulator, inspect its risk certificate and propagate joint predictions.
93
+
94
+ `POPSEllipseRegression` fits a parameter distribution that is uniform on an
95
+ ellipsoid. For a linear model its predictive density at `x` has a closed form,
96
+ so the zero-noise generalization error is minimized directly. Predictive
97
+ densities, exact quantiles and parameter draws are available in closed form
98
+ (for the hierarchical layers, exactly for the stored finite mixture of
99
+ hyperparameter draws that represents the predictive). The `regularization` parameter chooses how the finite-data uncertainty
100
+ of the ellipsoid itself is handled:
101
+
102
+ ```python
103
+ from popsregression import POPSEllipseRegression
104
+
105
+ bare = POPSEllipseRegression().fit(X_train, y_train)
106
+
107
+ # Empirical-Bayes layer (Ellipse+EB): a Laplace hyperposterior over the
108
+ # ellipsoid, with its prior centered on the fit. Cheap; its diagnostic_bound_
109
+ # is not a PAC bound.
110
+ eb = POPSEllipseRegression(regularization="empirical-bayes").fit(X_train, y_train)
111
+
112
+ # PAC layer (Ellipse+PAC): the prior over the ellipsoid is fixed on a random
113
+ # half of the data, and a PAC-Bayes bound on the population log risk is
114
+ # evaluated on the other half (and vice versa). y_bounds must be known in
115
+ # advance (e.g. from a maximum principle), not read off the data.
116
+ pac = POPSEllipseRegression(regularization="PAC", y_bounds=(y_lower, y_upper))
117
+ pac.fit(X_train, y_train, groups=case_id) # groups: rows of one simulator case
118
+ pac.certificate_.raw_bound, pac.certificate_.trivial_bound # bound for the returned predictive
119
+
120
+ lo, hi = eb.predict_interval(X_test, level=0.9545) # exact central interval
121
+ logp = eb.predict_logpdf(X_test, y_test)
122
+ fields = eb.sample_predictions(X_test, 1000) # joint draws for propagation
123
+ ```
124
+
125
+ All predictions refer to parameter uncertainty only; no noise term is added.
126
+ The PAC bound certifies the log risk of the predictive density, not interval
127
+ coverage. See the
128
+ [ellipse documentation](docs/ellipse.md)
129
+ and the [glossary](docs/glossary.md) for the
130
+ terms used above.
131
+
132
+ ## Comparison methods and paper studies
133
+
134
+ PACm, PAC²-T, predictive variational inference and a finite
135
+ POPS-dictionary weighting ablation are implemented in `examples/comparisons/`,
136
+ outside the package. The scripts in `examples/` produce every figure and table
137
+ of the accompanying paper; see [examples/README.md](examples/README.md).
138
+
139
+ ## Backend
140
+
141
+ All numerical kernels (the Bayesian ridge fit, the pointwise corrections, the
142
+ ellipsoid optimization, the PAC certification and the mixture predictives) are
143
+ written in [JAX](https://jax.readthedocs.io) and compiled on first use.
144
+
145
+ - The first call for a given problem shape pays a compilation cost of about a
146
+ second; later calls with the same shapes reuse the compiled kernel.
147
+ - Importing the package enables JAX double precision
148
+ (`jax_enable_x64`), a process-wide JAX setting.
149
+ - With a GPU build of JAX the same code runs on the accelerator.
150
+ - Inputs and fitted attributes (`coef_`, `sigma_`, ...) are NumPy arrays.
151
+
152
+ scikit-learn is not required. The estimators keep the familiar
153
+ `fit` / `predict` / `get_params` / `set_params` usage, so they still work
154
+ inside scikit-learn pipelines and parameter searches when it is installed.
155
+
156
+ ## Pipeline compatibility
157
+
158
+ If scikit-learn is installed, both estimators can be used in its pipelines and
159
+ hyperparameter search:
160
+
161
+ ```python
162
+ from sklearn.pipeline import make_pipeline
163
+ from sklearn.preprocessing import PolynomialFeatures
164
+
165
+ pipe = make_pipeline(
166
+ PolynomialFeatures(degree=4),
167
+ POPSRegression(resampling_method='sobol'),
168
+ )
169
+ pipe.fit(X_train, y_train)
170
+ y_pred = pipe.predict(X_test)
171
+ ```
172
+
173
+ ## Development
174
+
175
+ The repository is managed with [uv](https://docs.astral.sh/uv/); `uv run`
176
+ resolves the pinned environment from `uv.lock` on first use.
177
+
178
+ ```bash
179
+ uv run --group test pytest -vsl popsregression examples/comparisons # tests
180
+ uv run --group lint ruff check popsregression examples # linter
181
+ uv run --group lint black --check popsregression examples
182
+ uv run --group doc mkdocs serve # docs at localhost:8000
183
+ cd examples && uv run --extra examples python example_polynomial.py # a figure
184
+ ```
185
+
186
+ Without uv, `pip install -e ".[examples]"` and run the tools directly.
187
+
188
+ ## Citation
189
+
190
+ > *Parameter uncertainties for imperfect surrogate models in the low-noise regime*
191
+ >
192
+ > TD Swinburne and D Perez, [Machine Learning: Science and Technology 2025](https://doi.org/10.1088/2632-2153/ad9fce)
193
+
194
+ ```bibtex
195
+ @article{swinburne2025,
196
+ author={Swinburne, Thomas and Perez, Danny},
197
+ title={Parameter uncertainties for imperfect surrogate models in the low-noise regime},
198
+ journal={Machine Learning: Science and Technology},
199
+ doi={10.1088/2632-2153/ad9fce},
200
+ year={2025}
201
+ }
202
+ ```
203
+
204
+ For the PAC-Bayes construction, see Thomas D. Swinburne, Omar Hussein, Ian Lynch and Danny Perez, [PAC-Bayesian regression for misspecified, near-deterministic surrogate models](https://openreview.net/forum?id=gD3LoosP0w).
205
+
206
+ ## Contributors
207
+
208
+ Thomas D. Swinburne and Danny Perez developed the original POPS method. James Kermode contributed the Gaussian parameter prior for the ellipsoid shape and its initialization in [PR #15](https://github.com/POPS-UQ/popsregression/pull/15). The [JAX adaptation](https://github.com/POPS-UQ/popsregression/commit/8c129db) uses the prior scale learned by the Bayesian ridge fit. Enable it with `prior_kl=True` on `POPSEllipseRegression`.
209
+
210
+ ## AI usage
211
+
212
+ Claude assisted with documentation and test cases. Thomas D. Swinburne reviewed the code described in the original release.
@@ -2,10 +2,14 @@
2
2
  # Danny Perez <danny_perez@lanl.gov>
3
3
  # SPDX-License-Identifier: BSD-3-Clause
4
4
 
5
+ from ._ellipse_regression import PACCertificate, PACFoldBound, POPSEllipseRegression
5
6
  from ._pops import POPSRegression
6
7
  from ._version import __version__
7
8
 
8
9
  __all__ = [
9
10
  "POPSRegression",
11
+ "POPSEllipseRegression",
12
+ "PACCertificate",
13
+ "PACFoldBound",
10
14
  "__version__",
11
15
  ]