Skip to content

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

Closed

Description

@devmotion

as(Real, left, right) is TVShift(left) ∘ TVScale(right - left) ∘ TVLogistic(). When logistic(x) rounds to 1.0, the result is left + (right - left), which in floating point need not equal right and can exceed it:

julia> using TransformVariables

julia> t = as(Real, -1.0, 0.1)
as(Real, -1.0, 0.10000000000000009) (dimension 1)

julia> y = transform(t, 40.0)
0.10000000000000009

julia> y > 0.1
true

The lower end is exact (logistic(x) underflows to 0.0 and the result is left), and so are the one-sided transforms. The show output above has the same rounding, since it reconstructs right as shift + scale.

Downstream code that checks the result against the bounds it passed in then rejects it.

The composite only stores left and right - left, so right cannot be recovered exactly from it (left + (right - left) is the rounded value above). A fix could be to keep both endpoints and evaluate from the nearer one, e.g.

x < 0 ? left + (right - left) * logistic(x) : right - (right - left) * logistic(-x)

which returns exactly left or right at saturation and is also more accurate near right. Clamping left + (right - left) * p to [left, right] would also work.

TransformVariables v0.8.27, Julia 1.13.3.

Activity

  1. tpapp commented on Nov 6, 2026

    @tpapp
    Owner

    Yes, the whole transformation pipeline for the above used to be a single one, but #128 broke it up to the components above. Now they are used in the wild (see eg) so we should find a way to avoid the numerical issues while preserving the API.

    I am still thinking about the right solution, so just brainstorming: maybe we could

    1. make TVScale ∘ TVShift fuse into a single TVShiftScale,
    2. replace CompositeScalarTransform with a left-associative nested implementation (currently it is flat),
    3. have that specialize on fields {TVShiftScale,TVLogistic} which would implement the logistic(-x) branch recommended above (which I think is the nicest solution).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions