crc-framework 0.5.4__tar.gz → 0.6.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 (151) hide show
  1. {crc_framework-0.5.4 → crc_framework-0.6.0}/.github/workflows/npm.yaml +17 -0
  2. crc_framework-0.6.0/AGENTS.md +42 -0
  3. crc_framework-0.6.0/CLAUDE.md +1 -0
  4. {crc_framework-0.5.4 → crc_framework-0.6.0}/Cargo.lock +3 -3
  5. {crc_framework-0.5.4 → crc_framework-0.6.0}/Cargo.toml +1 -1
  6. {crc_framework-0.5.4 → crc_framework-0.6.0}/PKG-INFO +2 -2
  7. {crc_framework-0.5.4 → crc_framework-0.6.0}/README.md +1 -1
  8. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/distribution.rs +49 -58
  9. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/impact/expectation.rs +15 -2
  10. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/impact/kernel.rs +63 -40
  11. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/impact/pushforward.rs +55 -23
  12. crc_framework-0.6.0/crates/core/src/impact/quadrature.rs +622 -0
  13. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/impact/record.rs +15 -15
  14. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/impact/source.rs +207 -68
  15. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/curve_fitting.rs +2 -2
  16. crc_framework-0.6.0/crates/core/src/risk.rs +487 -0
  17. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/transform.rs +103 -3
  18. crc_framework-0.6.0/crates/core/tests/core_numerics.rs +176 -0
  19. crc_framework-0.6.0/crates/core/tests/impact_regressions.rs +331 -0
  20. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/tests/quantile_fit_golden.txt +8 -8
  21. {crc_framework-0.5.4 → crc_framework-0.6.0}/docs/README.md +0 -6
  22. {crc_framework-0.5.4 → crc_framework-0.6.0}/docs/development.md +12 -37
  23. {crc_framework-0.5.4 → crc_framework-0.6.0}/docs/python.md +2 -1
  24. {crc_framework-0.5.4 → crc_framework-0.6.0}/docs/releases.md +5 -26
  25. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/README.md +2 -3
  26. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/_core.pyi +6 -0
  27. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/distributions/empirical.py +5 -4
  28. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/impacts/__init__.py +18 -18
  29. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/metrics/distribution.py +5 -1
  30. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/metrics/microscores.py +3 -4
  31. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/transforms/base.py +27 -0
  32. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/transforms/builtin.py +9 -5
  33. {crc_framework-0.5.4 → crc_framework-0.6.0}/src/impact.rs +36 -27
  34. {crc_framework-0.5.4 → crc_framework-0.6.0}/src/lib.rs +98 -40
  35. {crc_framework-0.5.4 → crc_framework-0.6.0}/tests/fixtures/canonical-quantiles.json +25 -25
  36. crc_framework-0.6.0/tests/test_core_numerics.py +189 -0
  37. {crc_framework-0.5.4 → crc_framework-0.6.0}/tests/test_impact_expression.py +39 -0
  38. crc_framework-0.5.4/crates/core/src/impact/quadrature.rs +0 -344
  39. crc_framework-0.5.4/crates/core/src/risk.rs +0 -238
  40. crc_framework-0.5.4/docs/history/wasm-quantile-consumer.md +0 -315
  41. {crc_framework-0.5.4 → crc_framework-0.6.0}/.all-contributorsrc +0 -0
  42. {crc_framework-0.5.4 → crc_framework-0.6.0}/.gitattributes +0 -0
  43. {crc_framework-0.5.4 → crc_framework-0.6.0}/.github/workflows/release.yaml +0 -0
  44. {crc_framework-0.5.4 → crc_framework-0.6.0}/.gitignore +0 -0
  45. {crc_framework-0.5.4 → crc_framework-0.6.0}/.python-version +0 -0
  46. {crc_framework-0.5.4 → crc_framework-0.6.0}/LICENSE +0 -0
  47. {crc_framework-0.5.4 → crc_framework-0.6.0}/THIRD_PARTY_NOTICES.md +0 -0
  48. {crc_framework-0.5.4 → crc_framework-0.6.0}/build.rs +0 -0
  49. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/Cargo.toml +0 -0
  50. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/canonical.rs +0 -0
  51. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/constants.rs +0 -0
  52. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/distribution/lmoments.rs +0 -0
  53. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/error.rs +0 -0
  54. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/impact/definition.rs +0 -0
  55. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/impact/error.rs +0 -0
  56. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/impact/json.rs +0 -0
  57. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/impact/mod.rs +0 -0
  58. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/impact/reference.rs +0 -0
  59. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/impact/registry.rs +0 -0
  60. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/lib.rs +0 -0
  61. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/metrics.rs +0 -0
  62. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/climate/ag_params.rs +0 -0
  63. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/climate/energy_params.rs +0 -0
  64. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/climate/flood_params.rs +0 -0
  65. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/climate/mod.rs +0 -0
  66. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/climate/precipitation_thresholds.rs +0 -0
  67. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/climate/spei_params.rs +0 -0
  68. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/climate/wind_params.rs +0 -0
  69. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/mod.rs +0 -0
  70. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/spatial/continent_r4.rs +0 -0
  71. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/spatial/geography_r5.rs +0 -0
  72. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/spatial/ipcc.rs +0 -0
  73. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/reference/spatial/mod.rs +0 -0
  74. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/src/spatial.rs +0 -0
  75. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/tests/canonical_consumer.rs +0 -0
  76. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/tests/censored_quantile_fit.rs +0 -0
  77. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/tests/impact_contract_canonical.rs +0 -0
  78. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/tests/impact_definition_conformance.rs +0 -0
  79. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/tests/impact_operations.rs +0 -0
  80. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/tests/impact_registry_conformance.rs +0 -0
  81. {crc_framework-0.5.4 → crc_framework-0.6.0}/crates/core/tests/quantile_fit_golden.rs +0 -0
  82. {crc_framework-0.5.4 → crc_framework-0.6.0}/docs/impact-contracts.md +0 -0
  83. {crc_framework-0.5.4 → crc_framework-0.6.0}/docs/javascript.md +0 -0
  84. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/.nvmrc +0 -0
  85. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/build.mjs +0 -0
  86. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/package-lock.json +0 -0
  87. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/package.json +0 -0
  88. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/publish.mjs +0 -0
  89. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/src/index.d.ts +0 -0
  90. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/src/index.js +0 -0
  91. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/test/benchmark.mjs +0 -0
  92. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/test/browser.html +0 -0
  93. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/test/browser.mjs +0 -0
  94. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/test/consumer.mjs +0 -0
  95. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/test/parity.test.mjs +0 -0
  96. {crc_framework-0.5.4 → crc_framework-0.6.0}/js/test/publish.test.mjs +0 -0
  97. {crc_framework-0.5.4 → crc_framework-0.6.0}/pyproject.toml +0 -0
  98. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/__init__.py +0 -0
  99. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/constants.py +0 -0
  100. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/__init__.py +0 -0
  101. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/catalogs/error-codes.json +0 -0
  102. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/catalogs/hazard-exposures.json +0 -0
  103. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/catalogs/native-capabilities.json +0 -0
  104. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/catalogs/registry-overrides.json +0 -0
  105. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/catalogs/semantics-constants.json +0 -0
  106. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/catalogs/unit-aliases.json +0 -0
  107. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/fixtures/canonical-json.json +0 -0
  108. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/fixtures/definitions.json +0 -0
  109. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/fixtures/evaluation-records.json +0 -0
  110. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/fixtures/monotonicity.json +0 -0
  111. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/fixtures/operations.json +0 -0
  112. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/fixtures/point-evaluations.json +0 -0
  113. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/fixtures/registry-references.json +0 -0
  114. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/fixtures/spatial-policies.json +0 -0
  115. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/fixtures/spatial-records.json +0 -0
  116. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/manifest.json +0 -0
  117. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/schemas/impact-definition-1.0.schema.json +0 -0
  118. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/schemas/impact-evaluation-record-1.0.schema.json +0 -0
  119. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/schemas/registry-impact-reference-1.0.schema.json +0 -0
  120. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/schemas/spatial-combination-policy-1.0.schema.json +0 -0
  121. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/contracts/impact/schemas/spatial-impact-evaluation-record-1.0.schema.json +0 -0
  122. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/distributions/__init__.py +0 -0
  123. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/distributions/base.py +0 -0
  124. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/distributions/fitted.py +0 -0
  125. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/distributions/fitting.py +0 -0
  126. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/distributions/hurdle.py +0 -0
  127. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/distributions/point_mass.py +0 -0
  128. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/distributions/tabulated.py +0 -0
  129. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/metrics/__init__.py +0 -0
  130. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/metrics/risk.py +0 -0
  131. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/metrics/spanning.py +0 -0
  132. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/models.py +0 -0
  133. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/py.typed +0 -0
  134. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/spatial.py +0 -0
  135. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/transforms/__init__.py +0 -0
  136. {crc_framework-0.5.4 → crc_framework-0.6.0}/python/crc_framework/transforms/climate.py +0 -0
  137. {crc_framework-0.5.4 → crc_framework-0.6.0}/tests/test_consumer_parity.py +0 -0
  138. {crc_framework-0.5.4 → crc_framework-0.6.0}/tests/test_impact_contracts.py +0 -0
  139. {crc_framework-0.5.4 → crc_framework-0.6.0}/tests/test_impacts.py +0 -0
  140. {crc_framework-0.5.4 → crc_framework-0.6.0}/tests/test_lmoments.py +0 -0
  141. {crc_framework-0.5.4 → crc_framework-0.6.0}/tests/test_sdk.py +0 -0
  142. {crc_framework-0.5.4 → crc_framework-0.6.0}/tests/typing_smoke.py +0 -0
  143. {crc_framework-0.5.4 → crc_framework-0.6.0}/tools/benchmark_lmoments.py +0 -0
  144. {crc_framework-0.5.4 → crc_framework-0.6.0}/tools/benchmarks/lmoments-audit-m2-pro.json +0 -0
  145. {crc_framework-0.5.4 → crc_framework-0.6.0}/tools/generate_consumer_fixtures.py +0 -0
  146. {crc_framework-0.5.4 → crc_framework-0.6.0}/tools/impact_contracts/cases.py +0 -0
  147. {crc_framework-0.5.4 → crc_framework-0.6.0}/tools/impact_contracts/generate.py +0 -0
  148. {crc_framework-0.5.4 → crc_framework-0.6.0}/tools/impact_contracts/reference.py +0 -0
  149. {crc_framework-0.5.4 → crc_framework-0.6.0}/tools/impact_contracts/schemas.py +0 -0
  150. {crc_framework-0.5.4 → crc_framework-0.6.0}/tools/requirements-lmoments-benchmark.txt +0 -0
  151. {crc_framework-0.5.4 → crc_framework-0.6.0}/uv.lock +0 -0
@@ -8,6 +8,23 @@ on:
8
8
  permissions:
9
9
  contents: read
10
10
  jobs:
11
+ lint:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v7
15
+ - uses: dtolnay/rust-toolchain@stable
16
+ with:
17
+ components: rustfmt, clippy
18
+ - uses: Swatinem/rust-cache@v2
19
+ - uses: actions/setup-python@v7
20
+ with:
21
+ python-version: '3.12'
22
+ - name: Format, clippy and mypy
23
+ run: |
24
+ cargo fmt --all -- --check
25
+ cargo clippy --workspace --all-targets --locked -- -D warnings
26
+ pip install numpy mypy
27
+ python -m mypy
11
28
  impact-contracts:
12
29
  runs-on: ubuntu-latest
13
30
  steps:
@@ -0,0 +1,42 @@
1
+ # Agent guide
2
+
3
+ crc-framework is the numerical core of Climate Risk Commons: a Rust crate with
4
+ Python (PyO3) and JavaScript/WASM bindings. Data access and workflows live in
5
+ [crc-sdk](https://github.com/RiskThinking/crc-sdk).
6
+
7
+ ## Read first
8
+
9
+ - [docs/README.md](docs/README.md): reading order and source-of-truth map.
10
+ - [docs/python.md](docs/python.md): Python API, probability conventions and the
11
+ public export list.
12
+ - [docs/javascript.md](docs/javascript.md): canonical curve inputs and errors.
13
+
14
+ ## Layout
15
+
16
+ | Path | Contents |
17
+ | --- | --- |
18
+ | `crates/core` | All algorithms and reference data (edit here first) |
19
+ | `src/` | PyO3 bindings (`crc_framework._core`) |
20
+ | `python/crc_framework` | Typed Python API; `_core.pyi` must match `src/` |
21
+ | `crates/wasm`, `js/` | WASM exports and the JS adapter |
22
+ | `python/crc_framework/contracts/impact` | Generated; edit `tools/impact_contracts/` and regenerate |
23
+
24
+ ## Checks
25
+
26
+ ```sh
27
+ cargo fmt --all -- --check
28
+ cargo clippy --workspace --all-targets -- -D warnings
29
+ cargo test --workspace --locked
30
+ .venv/bin/maturin develop --release && .venv/bin/python -m pytest -q
31
+ .venv/bin/python -m mypy
32
+ ```
33
+
34
+ ## Rules
35
+
36
+ - Probabilities are non-exceedance `q` in `[0, 1]`; upper-tail return period
37
+ `T` is `q = 1 - 1/T`.
38
+ - Keep Python, Rust and WASM numerically consistent; parity tests use explicit
39
+ tolerances.
40
+ - Long native calls release the GIL (`py.detach`).
41
+ - Do not break exported Python names or the JS `CurveProperties` contract.
42
+ - Docs, code and commit messages are in English.
@@ -0,0 +1 @@
1
+ @AGENTS.md
@@ -67,7 +67,7 @@ dependencies = [
67
67
 
68
68
  [[package]]
69
69
  name = "crc-framework-core"
70
- version = "0.5.4"
70
+ version = "0.6.0"
71
71
  dependencies = [
72
72
  "h3o",
73
73
  "phf",
@@ -79,7 +79,7 @@ dependencies = [
79
79
 
80
80
  [[package]]
81
81
  name = "crc-framework-rs"
82
- version = "0.5.4"
82
+ version = "0.6.0"
83
83
  dependencies = [
84
84
  "crc-framework-core",
85
85
  "h3o",
@@ -88,7 +88,7 @@ dependencies = [
88
88
 
89
89
  [[package]]
90
90
  name = "crc-framework-wasm"
91
- version = "0.5.4"
91
+ version = "0.6.0"
92
92
  dependencies = [
93
93
  "crc-framework-core",
94
94
  "js-sys",
@@ -12,7 +12,7 @@ readme = "README.md"
12
12
  members = ["crates/core"]
13
13
 
14
14
  [workspace.package]
15
- version = "0.5.4"
15
+ version = "0.6.0"
16
16
 
17
17
  [lib]
18
18
  name = "_core"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: crc-framework
3
- Version: 0.5.4
3
+ Version: 0.6.0
4
4
  Classifier: Development Status :: 3 - Alpha
5
5
  Classifier: License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)
6
6
  Classifier: Operating System :: OS Independent
@@ -145,7 +145,7 @@ collection once, then call `evaluate(q)` and free the handle when finished.
145
145
  | Bump versions, migrate the npm scope, or publish a release | [Releases](docs/releases.md) |
146
146
 
147
147
  Start with the [documentation index](docs/README.md) for the reading order and
148
- repository boundaries. Historical implementation notes are linked there separately.
148
+ repository boundaries.
149
149
 
150
150
  ## License
151
151
 
@@ -118,7 +118,7 @@ collection once, then call `evaluate(q)` and free the handle when finished.
118
118
  | Bump versions, migrate the npm scope, or publish a release | [Releases](docs/releases.md) |
119
119
 
120
120
  Start with the [documentation index](docs/README.md) for the reading order and
121
- repository boundaries. Historical implementation notes are linked there separately.
121
+ repository boundaries.
122
122
 
123
123
  ## License
124
124
 
@@ -1,4 +1,4 @@
1
- use std::f64::consts::{PI, SQRT_2};
1
+ use std::f64::consts::PI;
2
2
 
3
3
  #[cfg(feature = "sampling")]
4
4
  use rand::{Rng, SeedableRng, rngs::StdRng};
@@ -209,6 +209,9 @@ impl TabulatedDistribution {
209
209
 
210
210
  impl Distribution for TabulatedDistribution {
211
211
  fn cdf(&self, x: f64) -> f64 {
212
+ if x.is_nan() {
213
+ return f64::NAN;
214
+ }
212
215
  if x < self.values[0] {
213
216
  return 0.0;
214
217
  }
@@ -235,6 +238,9 @@ impl Distribution for TabulatedDistribution {
235
238
  }
236
239
 
237
240
  fn cdf_below(&self, x: f64) -> f64 {
241
+ if x.is_nan() {
242
+ return f64::NAN;
243
+ }
238
244
  if x <= self.values[0] {
239
245
  return 0.0;
240
246
  }
@@ -302,6 +308,9 @@ impl EmpiricalDistribution {
302
308
 
303
309
  impl Distribution for EmpiricalDistribution {
304
310
  fn cdf(&self, x: f64) -> f64 {
311
+ if x.is_nan() {
312
+ return f64::NAN;
313
+ }
305
314
  self.samples.partition_point(|sample| *sample <= x) as f64 / self.samples.len() as f64
306
315
  }
307
316
 
@@ -315,8 +324,10 @@ impl Distribution for EmpiricalDistribution {
315
324
  }
316
325
 
317
326
  fn probability_atom(&self, x: f64) -> f64 {
318
- self.samples.iter().filter(|sample| **sample == x).count() as f64
319
- / self.samples.len() as f64
327
+ // Samples are sorted, so the atom is the run of values equal to `x`.
328
+ let upper = self.samples.partition_point(|sample| *sample <= x);
329
+ let lower = self.samples[..upper].partition_point(|sample| *sample < x);
330
+ (upper - lower) as f64 / self.samples.len() as f64
320
331
  }
321
332
  }
322
333
 
@@ -348,7 +359,13 @@ impl Distribution for PointMassDistribution {
348
359
  }
349
360
 
350
361
  fn cdf(&self, x: f64) -> f64 {
351
- if x < self.location { 0.0 } else { 1.0 }
362
+ if x.is_nan() {
363
+ f64::NAN
364
+ } else if x < self.location {
365
+ 0.0
366
+ } else {
367
+ 1.0
368
+ }
352
369
  }
353
370
 
354
371
  fn ppf(&self, probability: f64) -> Result<f64> {
@@ -452,6 +469,15 @@ impl FittedDistribution {
452
469
  if shape.is_some_and(|value| !value.is_finite()) {
453
470
  return Err(CrcError::InvalidInput("shape must be finite".into()));
454
471
  }
472
+ if matches!(
473
+ family,
474
+ DistributionFamily::WeibullMin | DistributionFamily::WeibullMax
475
+ ) && shape.is_some_and(|value| value <= 0.0)
476
+ {
477
+ return Err(CrcError::InvalidInput(
478
+ "weibull shape must be positive".into(),
479
+ ));
480
+ }
455
481
  Ok(Self {
456
482
  family,
457
483
  shape,
@@ -663,15 +689,18 @@ impl Distribution for FittedDistribution {
663
689
  }
664
690
 
665
691
  fn cdf(&self, x: f64) -> f64 {
692
+ if x.is_nan() {
693
+ return f64::NAN;
694
+ }
666
695
  let z = self.z(x);
667
696
  match self.family {
668
697
  DistributionFamily::GumbelRight => (-(-z).exp()).exp(),
669
- DistributionFamily::GumbelLeft => 1.0 - (-z.exp()).exp(),
698
+ DistributionFamily::GumbelLeft => -(-z.exp()).exp_m1(),
670
699
  DistributionFamily::WeibullMin => {
671
700
  if z < 0.0 {
672
701
  0.0
673
702
  } else {
674
- 1.0 - (-z.powf(self.shape.expect("validated"))).exp()
703
+ -(-z.powf(self.shape.expect("validated"))).exp_m1()
675
704
  }
676
705
  }
677
706
  DistributionFamily::WeibullMax => {
@@ -686,13 +715,14 @@ impl Distribution for FittedDistribution {
686
715
  if z < 0.0 {
687
716
  0.0
688
717
  } else if c.abs() < EPS {
689
- 1.0 - (-z).exp()
718
+ -(-z).exp_m1()
690
719
  } else {
691
720
  let support = 1.0 + c * z;
692
721
  if support <= 0.0 {
693
722
  1.0
694
723
  } else {
695
- 1.0 - support.powf(-1.0 / c)
724
+ // 1 - (1 + c z)^(-1/c), without cancellation for small z.
725
+ -(-(c * z).ln_1p() / c).exp_m1()
696
726
  }
697
727
  }
698
728
  }
@@ -831,7 +861,9 @@ impl Distribution for HurdleDistribution {
831
861
  }
832
862
 
833
863
  fn cdf(&self, x: f64) -> f64 {
834
- if x < self.atom_location {
864
+ if x.is_nan() {
865
+ f64::NAN
866
+ } else if x < self.atom_location {
835
867
  0.0
836
868
  } else if x == self.atom_location {
837
869
  self.atom_probability
@@ -1299,15 +1331,7 @@ fn diagnostics_sorted(sorted: &[f64], distribution: &dyn Distribution) -> Diagno
1299
1331
  } else {
1300
1332
  1.0
1301
1333
  };
1302
- let z = (count.sqrt() + 0.12 + 0.11 / count.sqrt()) * ks;
1303
- let ks_pvalue = (1..=100)
1304
- .map(|term| {
1305
- let sign = if term % 2 == 1 { 1.0 } else { -1.0 };
1306
- sign * (-2.0 * (term as f64).powi(2) * z * z).exp()
1307
- })
1308
- .sum::<f64>()
1309
- .mul_add(2.0, 0.0)
1310
- .clamp(0.0, 1.0);
1334
+ let ks_pvalue = reference_curve_fitting::ks_pvalue_asymptotic(ks, sorted.len());
1311
1335
  DiagnosticMetrics {
1312
1336
  ks_statistic: ks,
1313
1337
  ks_pvalue,
@@ -1569,43 +1593,16 @@ fn normal_pdf(x: f64) -> f64 {
1569
1593
  }
1570
1594
 
1571
1595
  fn normal_cdf(x: f64) -> f64 {
1572
- 0.5 * (1.0 + erf(x / SQRT_2))
1596
+ reference_curve_fitting::ndtr_cephes(x)
1573
1597
  }
1574
1598
 
1599
+ /// Skew-normal cdf `Phi(z) - 2 T(z, alpha)` with Owen's T, as in SciPy.
1575
1600
  fn skew_normal_cdf(z: f64, alpha: f64) -> f64 {
1576
- if z <= -10.0 {
1577
- return 0.0;
1578
- }
1579
- if z >= 10.0 {
1580
- return 1.0;
1581
- }
1582
- let lower = -10.0;
1583
- let steps = 400usize;
1584
- let width = (z - lower) / steps as f64;
1585
- let mut total = 0.0;
1586
- for index in 0..=steps {
1587
- let x = lower + index as f64 * width;
1588
- let weight = if index == 0 || index == steps {
1589
- 1.0
1590
- } else if index % 2 == 0 {
1591
- 2.0
1592
- } else {
1593
- 4.0
1594
- };
1595
- total += weight * 2.0 * normal_pdf(x) * normal_cdf(alpha * x);
1601
+ if z.is_nan() {
1602
+ return f64::NAN;
1596
1603
  }
1597
- (total * width / 3.0).clamp(0.0, 1.0)
1598
- }
1599
-
1600
- fn erf(x: f64) -> f64 {
1601
- let sign = x.signum();
1602
- let x = x.abs();
1603
- let t = 1.0 / (1.0 + 0.327_591_1 * x);
1604
- let polynomial =
1605
- (((((1.061_405_429 * t - 1.453_152_027) * t) + 1.421_413_741) * t - 0.284_496_736) * t
1606
- + 0.254_829_592)
1607
- * t;
1608
- sign * (1.0 - polynomial * (-x * x).exp())
1604
+ (reference_curve_fitting::ndtr_cephes(z) - 2.0 * reference_curve_fitting::owens_t(z, alpha))
1605
+ .clamp(0.0, 1.0)
1609
1606
  }
1610
1607
 
1611
1608
  #[cfg(test)]
@@ -1900,13 +1897,7 @@ mod tests {
1900
1897
  #[test]
1901
1898
  fn survival_function_complements_cdf_and_inverts_for_every_family() {
1902
1899
  for distribution in tail_test_distributions() {
1903
- // The skew-normal cdf is a quadrature over an erf approximation.
1904
- let (tolerance, isf_tolerance) =
1905
- if distribution.family == DistributionFamily::SkewNormal {
1906
- (1.0e-6, 1.0e-5)
1907
- } else {
1908
- (1.0e-12, 1.0e-8)
1909
- };
1900
+ let (tolerance, isf_tolerance) = (1.0e-12, 1.0e-8);
1910
1901
  for q in [0.001, 0.05, 0.3, 0.5, 0.7, 0.95, 0.999] {
1911
1902
  let x = distribution.ppf(q).unwrap();
1912
1903
  let survival = distribution.sf(x);
@@ -162,7 +162,20 @@ pub(crate) fn expected_value(
162
162
  let (exact_component, intervals) = peel_atoms(model, clamp, source)?;
163
163
  let intervals = split_at_breakpoints(model, clamp, source, &intervals);
164
164
  let integral = quadrature::integrate(&intervals, &options, |probability| {
165
- let exposure = source.quantile(probability)?;
165
+ let exposure = source.quantile(probability).map_err(|error| {
166
+ // Nodes lie strictly inside each panel, so a node that rounded
167
+ // onto 0 or 1 means bisection has exhausted floating-point
168
+ // resolution in a tail: the integral did not converge.
169
+ if probability <= 0.0 || probability >= 1.0 {
170
+ fail(
171
+ "convergence_failed",
172
+ "",
173
+ "Gauss-Kronrod integration reached the floating-point limit of a tail",
174
+ )
175
+ } else {
176
+ error
177
+ }
178
+ })?;
166
179
  kernel::evaluate(model, clamp, exposure)
167
180
  })?;
168
181
  let value = exact_component + integral.value;
@@ -419,7 +432,7 @@ fn support_is_bounded(source: &ImpactSource) -> bool {
419
432
  lower.is_finite() && upper.is_finite()
420
433
  }
421
434
 
422
- fn restricted_domain_exited(model: &Model, source: &ImpactSource) -> bool {
435
+ pub(crate) fn restricted_domain_exited(model: &Model, source: &ImpactSource) -> bool {
423
436
  let (outside, edges) = match model {
424
437
  Model::Tabulated {
425
438
  points,
@@ -22,47 +22,81 @@ fn apply_clamp(clamp: &Clamp, mut value: f64) -> f64 {
22
22
  if value == 0.0 { 0.0 } else { value }
23
23
  }
24
24
 
25
- fn table_response(
26
- xs: &[f64],
27
- ys: &[f64],
25
+ fn outside_table(
26
+ first: f64,
27
+ last: f64,
28
+ first_value: f64,
29
+ last_value: f64,
28
30
  outside: OutsideDomain,
29
- binned: bool,
30
- interpolation: Interpolation,
31
31
  value: f64,
32
- ) -> ContractResult<f64> {
33
- if value < xs[0] || value > *xs.last().expect("table has endpoints") {
32
+ ) -> Option<ContractResult<f64>> {
33
+ if value < first || value > last {
34
34
  if outside == OutsideDomain::Error {
35
- return Err(fail(
35
+ return Some(Err(fail(
36
36
  "out_of_domain",
37
37
  "",
38
38
  "exposure is outside the table domain",
39
- ));
39
+ )));
40
40
  }
41
- return Ok(if value < xs[0] {
42
- ys[0]
41
+ return Some(Ok(if value < first {
42
+ first_value
43
43
  } else {
44
- *ys.last().expect("table has values")
45
- });
44
+ last_value
45
+ }));
46
46
  }
47
- if binned {
48
- for (index, response) in ys.iter().enumerate() {
49
- if value < xs[index + 1] {
50
- return Ok(*response);
51
- }
52
- }
53
- return Ok(*ys.last().expect("binned model has a final value"));
47
+ None
48
+ }
49
+
50
+ fn binned_response(
51
+ edges: &[f64],
52
+ values: &[f64],
53
+ outside: OutsideDomain,
54
+ value: f64,
55
+ ) -> ContractResult<f64> {
56
+ let last_value = *values.last().expect("binned model has a final value");
57
+ if let Some(result) = outside_table(
58
+ edges[0],
59
+ *edges.last().expect("table has endpoints"),
60
+ values[0],
61
+ last_value,
62
+ outside,
63
+ value,
64
+ ) {
65
+ return result;
66
+ }
67
+ // Bin `i` is `[edges[i], edges[i + 1])`; the final edge belongs to the
68
+ // last bin. A NaN exposure falls through to the last bin as before.
69
+ let bin = edges[1..].partition_point(|edge| *edge <= value);
70
+ if value.is_nan() {
71
+ return Ok(last_value);
72
+ }
73
+ Ok(values.get(bin).copied().unwrap_or(last_value))
74
+ }
75
+
76
+ fn tabulated_response(
77
+ points: &[(f64, f64)],
78
+ interpolation: Interpolation,
79
+ outside: OutsideDomain,
80
+ value: f64,
81
+ ) -> ContractResult<f64> {
82
+ let first = points[0];
83
+ let last = *points.last().expect("table has endpoints");
84
+ if let Some(result) = outside_table(first.0, last.0, first.1, last.1, outside, value) {
85
+ return result;
54
86
  }
55
- if let Some(index) = xs.iter().position(|knot| *knot == value) {
56
- return Ok(ys[index]);
87
+ let upper = points.partition_point(|point| point.0 < value);
88
+ if points[upper].0 == value {
89
+ return Ok(points[upper].1);
57
90
  }
58
- let upper = xs.partition_point(|knot| *knot < value);
59
91
  let lower = upper - 1;
92
+ let (x0, y0) = points[lower];
93
+ let (x1, y1) = points[upper];
60
94
  Ok(match interpolation {
61
- Interpolation::StepLeft => ys[lower],
62
- Interpolation::StepRight => ys[upper],
95
+ Interpolation::StepLeft => y0,
96
+ Interpolation::StepRight => y1,
63
97
  Interpolation::Linear => {
64
- let weight = (value - xs[lower]) / (xs[upper] - xs[lower]);
65
- ys[lower] + weight * (ys[upper] - ys[lower])
98
+ let weight = (value - x0) / (x1 - x0);
99
+ y0 + weight * (y1 - y0)
66
100
  }
67
101
  })
68
102
  }
@@ -98,23 +132,12 @@ pub(crate) fn raw_response(model: &Model, value: f64) -> ContractResult<f64> {
98
132
  points,
99
133
  interpolation,
100
134
  outside_domain,
101
- } => {
102
- let xs = points.iter().map(|point| point.0).collect::<Vec<_>>();
103
- let ys = points.iter().map(|point| point.1).collect::<Vec<_>>();
104
- table_response(&xs, &ys, *outside_domain, false, *interpolation, value)
105
- }
135
+ } => tabulated_response(points, *interpolation, *outside_domain, value),
106
136
  Model::Binned {
107
137
  edges,
108
138
  values,
109
139
  outside_domain,
110
- } => table_response(
111
- edges,
112
- values,
113
- *outside_domain,
114
- true,
115
- Interpolation::Linear,
116
- value,
117
- ),
140
+ } => binned_response(edges, values, *outside_domain, value),
118
141
  Model::Spei {
119
142
  water_consumption_asset,
120
143
  original_water_unit_cost,
@@ -15,6 +15,7 @@ use super::source::{ImpactSource, Tail};
15
15
 
16
16
  pub const ALGORITHM: &str = "crc-pushforward/1";
17
17
  const MASS_COVER: f64 = 1e-12;
18
+ const ATOM_MASS_TOLERANCE: f64 = 1e-9;
18
19
 
19
20
  /// An atom of the pushforward distribution.
20
21
  #[derive(Clone, Copy, Debug, PartialEq)]
@@ -78,12 +79,14 @@ impl ImpactDistribution {
78
79
  if probability == 1.0 {
79
80
  return self.essential(false);
80
81
  }
81
- let source_probability = if *decreasing {
82
- 1.0 - probability
82
+ // A decreasing map reverses order: `Q_Y(p) = f(Q_X^+(1 - p))`
83
+ // with the upper source quantile, so a zero-mass gap in the
84
+ // source resolves to the lower response value.
85
+ let exposure = if *decreasing {
86
+ self.source.upper_quantile(1.0 - probability)?
83
87
  } else {
84
- probability
88
+ self.source.quantile(probability)?
85
89
  };
86
- let exposure = self.source.quantile(source_probability)?;
87
90
  kernel::evaluate(&self.model, &self.clamp, exposure)
88
91
  }
89
92
  Law::LowerThreshold { cut, floor, .. } => {
@@ -205,7 +208,23 @@ pub(crate) fn transform(
205
208
  ));
206
209
  }
207
210
  };
208
- let regions = constant_regions(model, clamp)?;
211
+ if super::expectation::restricted_domain_exited(model, source) {
212
+ return Err(fail(
213
+ "out_of_domain",
214
+ "",
215
+ "the source support leaves a table domain whose outside policy is error",
216
+ ));
217
+ }
218
+ let mut regions = constant_regions(model, clamp)?;
219
+ // Regions are built left to right. Two flat pieces that meet at a knot
220
+ // must not both own it, or the knot's mass would be counted twice.
221
+ for index in 1..regions.len() {
222
+ let previous = regions[index - 1];
223
+ let current = &mut regions[index];
224
+ if previous.end_closed && current.start_closed && previous.end == current.start {
225
+ current.start_closed = false;
226
+ }
227
+ }
209
228
  let mut atoms = Vec::new();
210
229
  for region in &regions {
211
230
  let mass = region.probability(source);
@@ -227,6 +246,13 @@ pub(crate) fn transform(
227
246
  }
228
247
  let atoms = merge_atoms(atoms);
229
248
  let covered = neumaier(atoms.iter().map(|atom| atom.probability));
249
+ if covered > 1.0 + ATOM_MASS_TOLERANCE {
250
+ return Err(fail(
251
+ "invalid_value",
252
+ "",
253
+ "pushforward atoms carry more than the total probability",
254
+ ));
255
+ }
230
256
  let piecewise_constant = matches!(
231
257
  model,
232
258
  Model::Binned { .. }
@@ -357,7 +383,7 @@ fn constant_regions(model: &Model, clamp: &Clamp) -> ContractResult<Vec<Region>>
357
383
  outside_domain,
358
384
  } => match interpolation {
359
385
  TableInterpolation::Linear => {
360
- tabulated_linear_regions(points, clamp, &mut regions)?;
386
+ tabulated_linear_regions(points, *outside_domain, clamp, &mut regions)?;
361
387
  }
362
388
  TableInterpolation::StepLeft => {
363
389
  step_regions(points, *outside_domain, clamp, true, &mut regions)?;
@@ -373,7 +399,8 @@ fn constant_regions(model: &Model, clamp: &Clamp) -> ContractResult<Vec<Region>>
373
399
  } => binned_regions(edges, values, *outside_domain, clamp, &mut regions)?,
374
400
  Model::Sigmoid { zero_below, .. } => {
375
401
  if let Some(threshold) = zero_below {
376
- let floor = kernel::evaluate(model, clamp, threshold - 1.0)?;
402
+ // The raw response is exactly zero below the threshold.
403
+ let floor = apply_clamp(clamp, 0.0);
377
404
  regions.push(Region {
378
405
  start: f64::NEG_INFINITY,
379
406
  end: *threshold,
@@ -466,16 +493,19 @@ fn probes(start: f64, end: f64) -> (f64, f64) {
466
493
 
467
494
  fn tabulated_linear_regions(
468
495
  points: &[(f64, f64)],
496
+ outside: OutsideDomain,
469
497
  clamp: &Clamp,
470
498
  regions: &mut Vec<Region>,
471
499
  ) -> ContractResult<()> {
472
- regions.push(Region {
473
- start: f64::NEG_INFINITY,
474
- end: points[0].0,
475
- start_closed: false,
476
- end_closed: false,
477
- y: apply_clamp(clamp, points[0].1),
478
- });
500
+ if outside == OutsideDomain::Clamp {
501
+ regions.push(Region {
502
+ start: f64::NEG_INFINITY,
503
+ end: points[0].0,
504
+ start_closed: false,
505
+ end_closed: false,
506
+ y: apply_clamp(clamp, points[0].1),
507
+ });
508
+ }
479
509
  for pair in points.windows(2) {
480
510
  let (x0, y0) = pair[0];
481
511
  let (x1, y1) = pair[1];
@@ -506,14 +536,16 @@ fn tabulated_linear_regions(
506
536
  }
507
537
  }
508
538
  }
509
- let last = points.last().expect("tabulated curve has points");
510
- regions.push(Region {
511
- start: last.0,
512
- end: f64::INFINITY,
513
- start_closed: false,
514
- end_closed: false,
515
- y: apply_clamp(clamp, last.1),
516
- });
539
+ if outside == OutsideDomain::Clamp {
540
+ let last = points.last().expect("tabulated curve has points");
541
+ regions.push(Region {
542
+ start: last.0,
543
+ end: f64::INFINITY,
544
+ start_closed: false,
545
+ end_closed: false,
546
+ y: apply_clamp(clamp, last.1),
547
+ });
548
+ }
517
549
  Ok(())
518
550
  }
519
551
 
@@ -630,7 +662,7 @@ fn lower_threshold(
630
662
  else {
631
663
  return Ok(None);
632
664
  };
633
- let floor = kernel::evaluate(model, clamp, threshold - 1.0)?;
665
+ let floor = apply_clamp(clamp, 0.0);
634
666
  let at_threshold = kernel::evaluate(model, clamp, *threshold)?;
635
667
  if floor == at_threshold {
636
668
  return Ok(None);