physmap 0.2.5__tar.gz → 0.2.6__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 (136) hide show
  1. {physmap-0.2.5/src/physmap.egg-info → physmap-0.2.6}/PKG-INFO +3 -3
  2. {physmap-0.2.5 → physmap-0.2.6}/README.md +2 -2
  3. {physmap-0.2.5 → physmap-0.2.6}/pyproject.toml +1 -1
  4. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/__init__.py +1 -1
  5. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/cli.py +61 -0
  6. physmap-0.2.6/src/physmap/uq/__init__.py +25 -0
  7. physmap-0.2.6/src/physmap/uq/conformal.py +75 -0
  8. physmap-0.2.6/src/physmap/uq/figures.py +598 -0
  9. physmap-0.2.6/src/physmap/uq/lewis.py +175 -0
  10. physmap-0.2.6/src/physmap/uq/naca.py +278 -0
  11. physmap-0.2.6/src/physmap/uq/pbox.py +384 -0
  12. physmap-0.2.6/src/physmap/uqais_runner.py +164 -0
  13. {physmap-0.2.5 → physmap-0.2.6/src/physmap.egg-info}/PKG-INFO +3 -3
  14. {physmap-0.2.5 → physmap-0.2.6}/src/physmap.egg-info/SOURCES.txt +9 -0
  15. {physmap-0.2.5 → physmap-0.2.6}/tests/test_talk_package.py +30 -0
  16. physmap-0.2.6/tests/test_uq.py +194 -0
  17. physmap-0.2.6/tests/test_uqais_runner.py +52 -0
  18. {physmap-0.2.5 → physmap-0.2.6}/LICENSE +0 -0
  19. {physmap-0.2.5 → physmap-0.2.6}/LICENSE-CORPUS +0 -0
  20. {physmap-0.2.5 → physmap-0.2.6}/NOTICE +0 -0
  21. {physmap-0.2.5 → physmap-0.2.6}/setup.cfg +0 -0
  22. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/_paths.py +0 -0
  23. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/applicability/__init__.py +0 -0
  24. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/applicability/fixtures.py +0 -0
  25. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/applicability/screen.py +0 -0
  26. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/baselines/__init__.py +0 -0
  27. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/benchmarks/__init__.py +0 -0
  28. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/benchmarks/architecture_axis.py +0 -0
  29. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/benchmarks/benchmark_report.py +0 -0
  30. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/benchmarks/benchmark_v0_4.py +0 -0
  31. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/benchmarks/compare.py +0 -0
  32. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/benchmarks/home_baseline.py +0 -0
  33. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/benchmarks/registry.py +0 -0
  34. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/benchmarks/report.py +0 -0
  35. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/closures/__init__.py +0 -0
  36. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/closures/data/__init__.py +0 -0
  37. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/closures/data/closure_index.json +0 -0
  38. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/closures/formulas.py +0 -0
  39. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/closures/geometry_classes.py +0 -0
  40. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/closures/index.py +0 -0
  41. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/closures/registry.py +0 -0
  42. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/compat/__init__.py +0 -0
  43. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/core/__init__.py +0 -0
  44. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/core/mechanism.py +0 -0
  45. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/core/signals.py +0 -0
  46. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/corpus/__init__.py +0 -0
  47. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/corpus/calibration.py +0 -0
  48. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/corpus/data/__init__.py +0 -0
  49. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/corpus/data/corpus_seed.jsonl +0 -0
  50. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/corpus/data/evidence_claims_seed.jsonl +0 -0
  51. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/corpus/data/evidence_sources_seed.jsonl +0 -0
  52. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/corpus/data/premium_coverage.json +0 -0
  53. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/corpus/evidence.py +0 -0
  54. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/explain/__init__.py +0 -0
  55. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/explain/benchmark.py +0 -0
  56. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/explain/causal.py +0 -0
  57. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/__init__.py +0 -0
  58. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/aggregator_observability.py +0 -0
  59. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/classify.py +0 -0
  60. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/configs.py +0 -0
  61. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/corpus_regimes.py +0 -0
  62. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/detector_conformal.py +0 -0
  63. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/detector_density.py +0 -0
  64. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/enums.py +0 -0
  65. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/graph.py +0 -0
  66. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/guardrail.py +0 -0
  67. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/io.py +0 -0
  68. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/regime_observability.py +0 -0
  69. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/render.py +0 -0
  70. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/guardrail/weighting_heuristic.py +0 -0
  71. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/infra/__init__.py +0 -0
  72. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/infra/blindspot_oracle.py +0 -0
  73. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/infra/corpus_runtime.py +0 -0
  74. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/integrations/__init__.py +0 -0
  75. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/materiality/__init__.py +0 -0
  76. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/materiality/estimator.py +0 -0
  77. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/materiality/independence.py +0 -0
  78. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/materiality/surrogate_fit.py +0 -0
  79. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/observability/__init__.py +0 -0
  80. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/pipeline/__init__.py +0 -0
  81. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/pipeline/aggregators.py +0 -0
  82. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/pipeline/assessment_v06.py +0 -0
  83. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/pipeline/core.py +0 -0
  84. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/pipeline/defeasible_aggregator.py +0 -0
  85. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/pipeline/detectors.py +0 -0
  86. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/pipeline/observability.py +0 -0
  87. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/pipeline/surrogate.py +0 -0
  88. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/pipeline/validity_signal.py +0 -0
  89. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/pipeline/vehicle_spec.py +0 -0
  90. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/release.py +0 -0
  91. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/stress_tests/__init__.py +0 -0
  92. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/stress_tests/lewis_reuse.py +0 -0
  93. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/substrate/__init__.py +0 -0
  94. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/substrate/corpus_real.py +0 -0
  95. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/substrate/engine.py +0 -0
  96. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/substrate/forrest.py +0 -0
  97. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/substrate/loaders.py +0 -0
  98. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/substrate/naca_tn1451.py +0 -0
  99. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/substrate/naca_wpd_loader.py +0 -0
  100. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/substrate/stage1_ingest.py +0 -0
  101. {physmap-0.2.5 → physmap-0.2.6}/src/physmap/substrate/vehicle_config.py +0 -0
  102. {physmap-0.2.5 → physmap-0.2.6}/src/physmap.egg-info/dependency_links.txt +0 -0
  103. {physmap-0.2.5 → physmap-0.2.6}/src/physmap.egg-info/entry_points.txt +0 -0
  104. {physmap-0.2.5 → physmap-0.2.6}/src/physmap.egg-info/requires.txt +0 -0
  105. {physmap-0.2.5 → physmap-0.2.6}/src/physmap.egg-info/top_level.txt +0 -0
  106. {physmap-0.2.5 → physmap-0.2.6}/tests/test_applicability_screen.py +0 -0
  107. {physmap-0.2.5 → physmap-0.2.6}/tests/test_architecture_axis.py +0 -0
  108. {physmap-0.2.5 → physmap-0.2.6}/tests/test_benchmark_boundaries.py +0 -0
  109. {physmap-0.2.5 → physmap-0.2.6}/tests/test_benchmark_reproduces.py +0 -0
  110. {physmap-0.2.5 → physmap-0.2.6}/tests/test_calibration_corpus.py +0 -0
  111. {physmap-0.2.5 → physmap-0.2.6}/tests/test_casper_vehicle.py +0 -0
  112. {physmap-0.2.5 → physmap-0.2.6}/tests/test_causal_materiality.py +0 -0
  113. {physmap-0.2.5 → physmap-0.2.6}/tests/test_cli_without_checkout.py +0 -0
  114. {physmap-0.2.5 → physmap-0.2.6}/tests/test_closure_index.py +0 -0
  115. {physmap-0.2.5 → physmap-0.2.6}/tests/test_closures_registry.py +0 -0
  116. {physmap-0.2.5 → physmap-0.2.6}/tests/test_corpus_real.py +0 -0
  117. {physmap-0.2.5 → physmap-0.2.6}/tests/test_corpus_resolver.py +0 -0
  118. {physmap-0.2.5 → physmap-0.2.6}/tests/test_d3_pipeline.py +0 -0
  119. {physmap-0.2.5 → physmap-0.2.6}/tests/test_dirker_water_middle.py +0 -0
  120. {physmap-0.2.5 → physmap-0.2.6}/tests/test_evidence_corpus_validators.py +0 -0
  121. {physmap-0.2.5 → physmap-0.2.6}/tests/test_guardrail_public_api.py +0 -0
  122. {physmap-0.2.5 → physmap-0.2.6}/tests/test_home_baseline.py +0 -0
  123. {physmap-0.2.5 → physmap-0.2.6}/tests/test_marineau_vehicle.py +0 -0
  124. {physmap-0.2.5 → physmap-0.2.6}/tests/test_no_conflation.py +0 -0
  125. {physmap-0.2.5 → physmap-0.2.6}/tests/test_no_heavy_imports.py +0 -0
  126. {physmap-0.2.5 → physmap-0.2.6}/tests/test_observability.py +0 -0
  127. {physmap-0.2.5 → physmap-0.2.6}/tests/test_regime_observability_mapping.py +0 -0
  128. {physmap-0.2.5 → physmap-0.2.6}/tests/test_render_ood_wording.py +0 -0
  129. {physmap-0.2.5 → physmap-0.2.6}/tests/test_resolution_semantics.py +0 -0
  130. {physmap-0.2.5 → physmap-0.2.6}/tests/test_split_corpus.py +0 -0
  131. {physmap-0.2.5 → physmap-0.2.6}/tests/test_stress_test_lewis_reuse.py +0 -0
  132. {physmap-0.2.5 → physmap-0.2.6}/tests/test_surrogate_fit.py +0 -0
  133. {physmap-0.2.5 → physmap-0.2.6}/tests/test_vehicle_config.py +0 -0
  134. {physmap-0.2.5 → physmap-0.2.6}/tests/test_velazquez_sco2_middle.py +0 -0
  135. {physmap-0.2.5 → physmap-0.2.6}/tests/test_version.py +0 -0
  136. {physmap-0.2.5 → physmap-0.2.6}/tests/test_weighting_heuristic.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: physmap
3
- Version: 0.2.5
3
+ Version: 0.2.6
4
4
  Summary: Physics-aware credibility checks for AI surrogates in multiphysics simulation: closure validity, surrogate observability, and causal materiality.
5
5
  Author: Vishnu Vettrivel
6
6
  License-Expression: MIT
@@ -116,8 +116,8 @@ experiment:
116
116
 
117
117
  | | surrogate error | input-based OOD scores | PhysMAP materiality |
118
118
  |---|---|---|---|
119
- | gravity off — the accurate control | within 0.06 % | quiet at the default setting; identical in both rows | 0 |
120
- | gravity on — Lewis's measurement | 17–18 % downstream | quiet at the default setting; identical in both rows | up to 0.195 |
119
+ | gravity off — the accurate control | within 0.06 % | quiet at the reference percentile, 99; identical in both rows, because the inputs are | 0 |
120
+ | gravity on — Lewis's measurement | 17–18 % downstream | quiet at the reference percentile, 99; identical in both rows, because the inputs are | up to 0.195 |
121
121
 
122
122
  Identical OOD scores and zero-versus-0.195 materiality need no threshold. The flag threshold θ
123
123
  is not yet locked, so materiality is reported as numbers, and θ = 0.10 appears only as an
@@ -82,8 +82,8 @@ experiment:
82
82
 
83
83
  | | surrogate error | input-based OOD scores | PhysMAP materiality |
84
84
  |---|---|---|---|
85
- | gravity off — the accurate control | within 0.06 % | quiet at the default setting; identical in both rows | 0 |
86
- | gravity on — Lewis's measurement | 17–18 % downstream | quiet at the default setting; identical in both rows | up to 0.195 |
85
+ | gravity off — the accurate control | within 0.06 % | quiet at the reference percentile, 99; identical in both rows, because the inputs are | 0 |
86
+ | gravity on — Lewis's measurement | 17–18 % downstream | quiet at the reference percentile, 99; identical in both rows, because the inputs are | up to 0.195 |
87
87
 
88
88
  Identical OOD scores and zero-versus-0.195 materiality need no threshold. The flag threshold θ
89
89
  is not yet locked, so materiality is reported as numbers, and θ = 0.10 appears only as an
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
6
6
 
7
7
  [project]
8
8
  name = "physmap"
9
- version = "0.2.5"
9
+ version = "0.2.6"
10
10
  description = "Physics-aware credibility checks for AI surrogates in multiphysics simulation: closure validity, surrogate observability, and causal materiality."
11
11
  readme = "README.md"
12
12
  requires-python = ">=3.10"
@@ -27,7 +27,7 @@ from physmap.release import (
27
27
  ReleaseState,
28
28
  )
29
29
 
30
- __version__ = "0.2.5"
30
+ __version__ = "0.2.6"
31
31
 
32
32
 
33
33
  # The CredibilityGuardrail public surface is re-exported LAZILY (PEP 562). A bare
@@ -67,6 +67,39 @@ def build_parser() -> argparse.ArgumentParser:
67
67
  stress.add_argument("--json", metavar="PATH",
68
68
  help="also write the full fresh record to PATH (never the committed bank)")
69
69
 
70
+ # Q3 of UQAIS, the Surrogate Governance Loop's UQ block: aleatory samples through the prediction,
71
+ # the epistemic terms kept on separate lines, a probability box out. Needs no checkout.
72
+ uq = sub.add_parser(
73
+ "uq",
74
+ help="UQAIS, Uncertainty Quantification for AI Surrogates: propagate aleatory input "
75
+ "samples and the separate epistemic terms (emulation, closure validation or "
76
+ "UNBOUNDED, omitted-mechanism materiality) into a probability box, Roy and "
77
+ "Oberkampf style -- never one number")
78
+ uq.add_argument("--samples", required=True, metavar="CSV",
79
+ help="aleatory input samples: a `point` column plus the input columns")
80
+ pred = uq.add_mutually_exclusive_group(required=True)
81
+ pred.add_argument("--predict", metavar="MODULE:FUNCTION",
82
+ help="a callable, called as fn(**{column: array}) per point")
83
+ pred.add_argument("--predictions", metavar="CSV",
84
+ help="precomputed predictions: `point` and `prediction`, one row per "
85
+ "sample, or one per point")
86
+ uq.add_argument("--terms", required=True, metavar="CSV",
87
+ help="per point: emulation, closure_status (inside|outside), bound_kind "
88
+ "(range|set|point|ratio), validation, extrapolation_band (blank = "
89
+ "UNBOUNDED), materiality (`none` = not assessed); all relative to the "
90
+ "prediction")
91
+ uq.add_argument("--inputs", metavar="COLS",
92
+ help="comma-separated input columns to pass to the callable (default: "
93
+ "every column but `point`)")
94
+ uq.add_argument("--emulation", type=float, metavar="X",
95
+ help="default emulation half-width for points whose terms row leaves it blank")
96
+ uq.add_argument("--theta", type=float, metavar="THETA",
97
+ help="apply the materiality term only where materiality >= THETA; theta is "
98
+ "not locked, so the report labels it illustrative")
99
+ uq.add_argument("--quantiles", default="0.05,0.25,0.5,0.75,0.95", metavar="LIST",
100
+ help="quantiles at which the box is read (default 0.05,0.25,0.5,0.75,0.95)")
101
+ uq.add_argument("--out", metavar="JSON", help="write the full report here")
102
+
70
103
  # `reproduce` is added only in a release whose benchmark has cleared the readiness
71
104
  # gate. In a preview build the command does not exist -- `physmap reproduce` is an
72
105
  # unrecognised command, not a runtime "data missing" failure.
@@ -98,6 +131,8 @@ def main(argv: list[str] | None = None) -> int:
98
131
  return _cmd_explain(args)
99
132
  if args.command == "stress-test":
100
133
  return _cmd_stress_test(args)
134
+ if args.command == "uq":
135
+ return _cmd_uq(args)
101
136
  except (CheckoutRequired, TorchRequired) as e:
102
137
  # A pip-installed wheel has no checkout, and PyTorch is an optional extra. One
103
138
  # sentence saying what is missing and how to get it, not a traceback.
@@ -353,5 +388,31 @@ def _cmd_stress_test(args) -> int:
353
388
  return 0 if (not failures and cmp.matches) else 1
354
389
 
355
390
 
391
+ def _cmd_uq(args) -> int:
392
+ # numpy is imported inside physmap.uq.pbox, not here: a bare `import physmap` and
393
+ # `physmap --help` stay free of the scientific stack.
394
+ from physmap.uq import pbox
395
+
396
+ try:
397
+ quantiles = tuple(float(q) for q in args.quantiles.split(","))
398
+ inputs = [c.strip() for c in args.inputs.split(",")] if args.inputs else None
399
+ samples, _, s_notes = pbox.load_samples(args.samples, inputs)
400
+ if args.predict:
401
+ predictions = pbox.predictions_from_callable(pbox.resolve_callable(args.predict), samples)
402
+ else:
403
+ predictions = pbox.predictions_from_csv(args.predictions, samples)
404
+ terms, t_notes = pbox.load_terms(args.terms, samples, {"emulation": args.emulation})
405
+ report = pbox.propagate(samples, predictions, terms, theta=args.theta,
406
+ quantiles=quantiles, inputs_note=s_notes, terms_note=t_notes)
407
+ except (pbox.UQInputError, OSError, ValueError) as e:
408
+ print(f"physmap uq: {e}", file=sys.stderr)
409
+ return 2
410
+ print(pbox.render(report))
411
+ if args.out:
412
+ pbox.write_report(report, args.out)
413
+ print(f"Wrote the report to {args.out}.")
414
+ return 0
415
+
416
+
356
417
  if __name__ == "__main__":
357
418
  raise SystemExit(main())
@@ -0,0 +1,25 @@
1
+ """UQAIS, Uncertainty Quantification for AI Surrogates (pronounced like "ukase").
2
+
3
+ It accounts for the uncertainty a surrogate inherits from the physics it learned, in Roy and
4
+ Oberkampf's accounting, around PhysMAP's two checks.
5
+
6
+ Aleatory uncertainty (input variability) is propagated by sampling. Epistemic uncertainty is
7
+ carried as intervals and never averaged away. The intervals are kept on separate lines:
8
+
9
+ - **emulation error** -- the surrogate against the simulation it learned, on held-out runs;
10
+ - **model form inside the validated range** -- the closure against measurement, a declared
11
+ validation metric;
12
+ - **model form outside the validated range** -- PhysMAP's W-SURR-05: no validation evidence
13
+ bounds it, so it is UNBOUNDED unless an extrapolation band is declared and named;
14
+ - **omitted mechanism** -- PhysMAP's W-SURR-06: the materiality from a matched ablation, where
15
+ the mechanism is active and material.
16
+
17
+ The machine-learning UQ toolbox (ensembles, dropout, Bayesian layers, conformal intervals)
18
+ quantifies the first of these relative to the training distribution. It is silent about the
19
+ others, because they are not in the inputs. `physmap uq` propagates all of them into a
20
+ probability box on the quantity of interest and reports each term on its own line. It never
21
+ reduces the answer to one number, and "unbounded" is a legal, reported outcome.
22
+
23
+ Everything here uses numpy and scipy only, imported inside the modules that need them, so a
24
+ bare `import physmap` stays free of the scientific stack.
25
+ """
@@ -0,0 +1,75 @@
1
+ """Split-conformal intervals, and the small-sample arithmetic that decides whether one exists.
2
+
3
+ Split conformal (Vovk; Lei et al. 2018): with n calibration nonconformity scores and a nominal
4
+ miscoverage alpha, the interval half-width is the k-th smallest score, k = ceil((1 - alpha)(n + 1)).
5
+ If k > n there is no finite interval at that nominal level -- the honest answer is "unbounded",
6
+ not a quantile of too few points. At 90 % that needs at least 9 calibration scores.
7
+
8
+ Scores here are relative: |prediction - value| / value. A prediction covers a value when that
9
+ relative error is at most the half-width.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import math
15
+
16
+ import numpy as np
17
+
18
+ __all__ = [
19
+ "conformal_index",
20
+ "finite_interval_possible",
21
+ "min_calibration_size",
22
+ "conformal_half_width",
23
+ "relative_scores",
24
+ "covered",
25
+ "binomial_band",
26
+ ]
27
+
28
+
29
+ def conformal_index(n_cal: int, alpha: float) -> int:
30
+ """The 1-based order statistic split conformal takes: ceil((1 - alpha)(n + 1))."""
31
+ return math.ceil((1.0 - alpha) * (n_cal + 1))
32
+
33
+
34
+ def finite_interval_possible(n_cal: int, alpha: float) -> bool:
35
+ return conformal_index(n_cal, alpha) <= n_cal
36
+
37
+
38
+ def min_calibration_size(alpha: float) -> int:
39
+ """The smallest calibration set that gives a finite interval at this nominal level."""
40
+ n = 1
41
+ while not finite_interval_possible(n, alpha):
42
+ n += 1
43
+ return n
44
+
45
+
46
+ def conformal_half_width(scores, alpha: float) -> float:
47
+ """The split-conformal half-width, or math.inf when the calibration set is too small."""
48
+ s = np.sort(np.asarray(scores, dtype=float))
49
+ k = conformal_index(len(s), alpha)
50
+ if k > len(s):
51
+ return math.inf
52
+ return float(s[k - 1])
53
+
54
+
55
+ def relative_scores(prediction, value) -> np.ndarray:
56
+ p, v = np.asarray(prediction, dtype=float), np.asarray(value, dtype=float)
57
+ return np.abs(p - v) / np.abs(v)
58
+
59
+
60
+ def covered(prediction, value, half_width: float) -> np.ndarray:
61
+ """True where the value lies inside prediction * (1 ± half_width), in relative terms."""
62
+ return relative_scores(prediction, value) <= half_width
63
+
64
+
65
+ def binomial_band(n: int, p: float, confidence: float = 0.95) -> tuple[int, int]:
66
+ """The central `confidence` band of Binomial(n, p), as counts (inclusive).
67
+
68
+ A coverage count inside it is consistent with the nominal level at this n; a count below it
69
+ is under-coverage that this many points can already show.
70
+ """
71
+ from scipy.stats import binom
72
+ tail = (1.0 - confidence) / 2.0
73
+ lo = int(binom.ppf(tail, n, p))
74
+ hi = int(binom.ppf(1.0 - tail, n, p))
75
+ return lo, hi