Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error if ordered unsafe to use #241

Merged
merged 5 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/bijectors/ordered.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ struct OrderedBijector <: Bijector{1} end
ordered(d::Distribution)

Return a `Distribution` whose support are ordered vectors, i.e., vectors with increasingly ordered elements.

This transformation is currently only supported for otherwise unconstrained distributions.
"""
ordered(d::ContinuousMultivariateDistribution) = Bijectors.transformed(d, OrderedBijector())
function ordered(d::ContinuousMultivariateDistribution)
if !isa(bijector(d), Identity)
throw(ArgumentError("ordered transform is currently only supported for unconstrained distributions."))
end
return Bijectors.transformed(d, OrderedBijector())
end

(b::OrderedBijector)(y::AbstractVecOrMat) = _transform_ordered(y)

Expand Down
22 changes: 21 additions & 1 deletion test/bijectors/ordered.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Bijectors: OrderedBijector
import Bijectors: OrderedBijector, ordered
using LinearAlgebra

@testset "OrderedBijector" begin
b = OrderedBijector()
Expand All @@ -20,3 +21,22 @@ import Bijectors: OrderedBijector
@test sort(ys[:, 1]) == ys[:, 1]
@test sort(ys[:, 2]) == ys[:, 2]
end

@testset "ordered" begin
d = MvNormal(1:5, Diagonal(6:10))
d_ordered = ordered(d)
@test d_ordered isa Bijectors.TransformedDistribution
@test d_ordered.dist === d
@test d_ordered.transform isa OrderedBijector
y = randn(5)
x = inv(bijector(d_ordered))(y)
@test issorted(x)

d = Product(fill(Normal(), 5))
# currently errors because `bijector(Product(fill(Normal(), 5)))` is not an `Identity`
@test_broken ordered(d) isa Bijectors.TransformedDistribution

# non-Identity bijector is not supported
d = Dirichlet(ones(5))
@test_throws ArgumentError ordered(d)
end