Repository navigation
Fix as(Real, left, right) exceeding right - #184
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #183.
Problem
as(Real, left, right)wasTVShift(left) ∘ TVScale(right - left) ∘ TVLogistic(). Whenlogistic(x)rounds to1.0, the resultleft + (right - left)can exceedright(e.g.transform(as(Real, -1.0, 0.1), 40.0) == 0.10000000000000009), andrightcannot be recovered from the stored fields.Change
New exported transform
TVInterval(left, right, to_unit = TVLogistic()), mapping ℝ to(left, right);to_unitcan beTVLogistic()orTVCauchy().as(Real, left, right)now returnsTVInterval(left, right).-|x|(sop ≤ 1/2never rounds to 1) and the result is computed from the nearer endpoint (left + s*porright - 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).inversealso starts from the nearer endpoint, avoidinglogit(q)withq ≈ 1. Nearrightthe old composite had inverse relative errors up to ~1e-2; now ~1e-16.ext/ReactantExt.jladds anifelsemethod forTracedRNumber, as for_cauchy_cdf.TVShift/TVScale, bounds may be non-Real(e.g. Unitful) fortransform/inverse; the log-Jacobian methods requireRealbounds.as(Real, -Inf, 1.0),(-floatmax(), floatmax())) now throw anArgumentErrorinstead of producingNaNs.As discussed in the issue,
TVShift,TVScale,∘and hand-built compositions such asTVShift(a) ∘ TVScale(b) ∘ TVLogistic()are unchanged. Only the type returned byas(Real, left, right)changes;showstill printsas(Real, left, right).Why the CDF is part of the transform
The inaccuracy near
rightis not caused bylogistic/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: nearright,q = (y - left)/(right - left) = 1 - δonly resolvesδto abouteps(1.0)/2in absolute terms, andlogit(q) ≈ -log(δ)turns that into the error ofx. For(-1.0, 0.1), relative errors of the inverse:logiterror for the Float64qqThe 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 symmetryF(-x) = 1 - F(x).Alternatives considered
left + (right - left) * pto[left, right]: requires storingrightanyway, and does not improve accuracy nearright.scaleuntilleft + scale ≤ right: no new type, but saturation andshowland up to a few ulps belowright, and accuracy is unchanged.TVShift ∘ TVScale ∘ TVLogisticin∘(flat or nested composite, specialized on shift-scale ∘ logistic): hand-built chains never specifyright, 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∘.TVAffine(left, right) ∘ TVLogistic(), split atp = 1/2: bounded and exact at the endpoints, and works after any map to (0, 1). But it has to passp ≈ 1between the transforms, so it keeps the accuracy problem above (inverse relative error up to ~1e-2 nearright).(1 - p) * left + p * right: exact atright, but drops belowleftfor tinyp(1 - prounds below 1) and is not monotone. Out of 300k random cases: 25 out of bounds, 14 monotonicity violations.∘forTVAffine ∘ {TVLogistic, TVCauchy}: numerically equivalent to this PR, but a publicTVIntervalis simpler than special-casing∘.Tests
transform,inverseandinverse_and_logjac(the old composite fails these nearright).x = ±Inf, 0, -0.0,yat the endpoints, out-of-rangey, invalid constructor arguments.x = 0;inverse_eltypefor Int, Float32 and Unitful; Reactant values including saturation.🤖 Generated with Claude Code