Skip to content

Fix as(Real, left, right) exceeding right - #184

Merged
devmotion merged 1 commit into
tpapp:masterfrom
devmotion:dw/interval-endpoints
Nov 7, 2026
Merged

devmotion merged 1 commit into
tpapp:masterfrom
devmotion:dw/interval-endpoints

Conversation

@devmotion

@devmotion devmotion commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #183.

Problem

as(Real, left, right) was TVShift(left) ∘ TVScale(right - left) ∘ TVLogistic(). When logistic(x) rounds to 1.0, the result left + (right - left) can exceed right (e.g. transform(as(Real, -1.0, 0.1), 40.0) == 0.10000000000000009), and right cannot be recovered from the stored fields.

Change

New exported transform TVInterval(left, right, to_unit = TVLogistic()), mapping ℝ to (left, right); to_unit can be TVLogistic() or TVCauchy(). as(Real, left, right) now returns TVInterval(left, right).

  • Both endpoints are stored. The CDF is evaluated at -|x| (so p ≤ 1/2 never rounds to 1) and the result is computed from the nearer endpoint (left + s*p or right - s*p). It is always in [left, right] and exactly at the endpoint when saturated. This relies on both CDFs being symmetric (F(-x) = 1 - F(x), even log-Jacobian).
  • inverse also starts from the nearer endpoint, avoiding logit(q) with q ≈ 1. Near right the old composite had inverse relative errors up to ~1e-2; now ~1e-16.
  • Reactant: the core uses a plain branch; ext/ReactantExt.jl adds an ifelse method for TracedRNumber, as for _cauchy_cdf.
  • As for TVShift/TVScale, bounds may be non-Real (e.g. Unitful) for transform/inverse; the log-Jacobian methods require Real bounds.
  • Intervals with non-finite width (e.g. as(Real, -Inf, 1.0), (-floatmax(), floatmax())) now throw an ArgumentError instead of producing NaNs.

As discussed in the issue, TVShift, TVScale, ∘ and hand-built compositions such as TVShift(a) ∘ TVScale(b) ∘ TVLogistic() are unchanged. Only the type returned by as(Real, left, right) changes; show still prints as(Real, left, right).

Why the CDF is part of the transform

The inaccuracy near right is not caused by logistic/logit — LogExpFunctions' implementations are accurate to well below an ulp for a given input. It comes from forming a value close to 1 in floating point: near right, q = (y - left)/(right - left) = 1 - δ only resolves δ to about eps(1.0)/2 in absolute terms, and logit(q) ≈ -log(δ) turns that into the error of x. For (-1.0, 0.1), relative errors of the inverse:

x logit error for the Float64 q error from forming q near-side fraction (this PR)
5 1.4e-17 4.4e-16 8.5e-17
15 5.9e-17 8.2e-12 1.3e-17
25 6.7e-17 1.5e-7 3.3e-17
35 1.4e-17 1.6e-3 7.0e-17

The forward direction has the same issue (1 - logistic(x) is not representable near 1). Avoiding it requires never passing a value ≈ 1 between two transforms, i.e. the affine map and the CDF have to be evaluated together, using the symmetry F(-x) = 1 - F(x).

Alternatives considered

  • Clamping left + (right - left) * p to [left, right]: requires storing right anyway, and does not improve accuracy near right.
  • Keeping the composite but shrinking scale until left + scale ≤ right: no new type, but saturation and show land up to a few ulps below right, and accuracy is unchanged.
  • Fusing TVShift ∘ TVScale ∘ TVLogistic in ∘ (flat or nested composite, specialized on shift-scale ∘ logistic): hand-built chains never specify right, so they cannot be exact anyway. A left-associative nested composite does not group the pattern inside longer chains (e.g. TVScale ∘ TVShift ∘ TVScale ∘ TVLogistic ∘ TVScale), and it adds special cases to ∘.
  • A separate affine type TVAffine(left, right) ∘ TVLogistic(), split at p = 1/2: bounded and exact at the endpoints, and works after any map to (0, 1). But it has to pass p ≈ 1 between the transforms, so it keeps the accuracy problem above (inverse relative error up to ~1e-2 near right).
  • (1 - p) * left + p * right: exact at right, but drops below left for tiny p (1 - p rounds below 1) and is not monotone. Out of 300k random cases: 25 out of bounds, 14 monotonicity violations.
  • A hidden composed type created by ∘ for TVAffine ∘ {TVLogistic, TVCauchy}: numerically equivalent to this PR, but a public TVInterval is simpler than special-casing ∘.

Tests

  • Issue example; bounds and monotonicity for both CDFs over intervals with mixed signs and magnitudes, a large offset relative to the width, a subnormal width, and Float32.
  • Accuracy against BigFloat near both endpoints for transform, inverse and inverse_and_logjac (the old composite fails these near right).
  • x = ±Inf, 0, -0.0, y at the endpoints, out-of-range y, invalid constructor arguments.
  • ForwardDiff and Enzyme at the branch point x = 0; inverse_eltype for Int, Float32 and Unitful; Reactant values including saturation.

🤖 Generated with Claude Code

Add `TVInterval(left, right, to_unit = TVLogistic())`, which maps the real
line to `(left, right)` and stores both endpoints. It evaluates the
(symmetric) CDF at `-|x|` and works from the nearer endpoint, so results
stay within `[left, right]`, are exact at saturation, and `transform` and
`inverse` are accurate near both ends. `to_unit` can be `TVLogistic()` or
`TVCauchy()`. `as(Real, left, right)` now returns a `TVInterval`; `TVShift`,
`TVScale` and hand-built compositions are unchanged.

Intervals with non-finite width are rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@tpapp tpapp left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@devmotion
devmotion merged commit f9af3b5 into tpapp:master Nov 7, 2026
7 checks passed
@devmotion
devmotion deleted the dw/interval-endpoints branch November 7, 2026 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

as(Real, left, right) can return a value greater than right

2 participants