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

[RFC] Add angle bijector #168

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/Bijectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export TransformDistribution,
PositiveDistribution,
UnitDistribution,
SimplexDistribution,
AngularDistribution,
PDMatDistribution,
link,
invlink,
Expand Down Expand Up @@ -155,6 +156,8 @@ function logpdf_with_trans(d::UnivariateDistribution, x, transform::Bool)
end
end

const AngularDistribution = Union{VonMises}

## Multivariate

const SimplexDistribution = Union{Dirichlet}
Expand Down
20 changes: 20 additions & 0 deletions src/bijectors/angle.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# non-bijector from an angle to a circle
# its "inverse" is a left inverse from the plane to an angle such that
# (inv(Angle()) ∘ Angle())(θ) == θ and (Angle() ∘ inv(Angle()))(z) == normalize(z)
# Instead of the logdetjac, we use that a standard bivariate normally distributed random
# variable `z` upon normalization is uniformly distributed on the circle.
# so the necessary adjustment to the logpdf by using `z` is -‖z‖^2/2
# see https://mc-stan.org/docs/2_26/reference-manual/unit-vector-section.html

struct Angle <: Bijector{0} end

(b::Angle)(θ::Real) = [sincos(θ)...]

(ib::Inverse{<:Angle})(z::AbstractVector{<:Real}) = atan(z...)

logabsdetjac(::Angle, θ::Real) = one(θ) / 2

function logabsdetjac(::Inverse{<:Angle}, z::AbstractVector{<:Real})
y, x = z
return -(x^2 + y^2) / 2
end
1 change: 1 addition & 0 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ include("bijectors/scale.jl")
include("bijectors/shift.jl")
include("bijectors/permute.jl")
include("bijectors/simplex.jl")
include("bijectors/angle.jl")
include("bijectors/pd.jl")
include("bijectors/corr.jl")
include("bijectors/truncated.jl")
Expand Down
1 change: 1 addition & 0 deletions src/transformed_distribution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ bijector(d::Distributions.AbstractMvNormal) = Identity{1}()
bijector(d::Distributions.AbstractMvLogNormal) = Log{1}()
bijector(d::PositiveDistribution) = Log{0}()
bijector(d::SimplexDistribution) = SimplexBijector{1}()
bijector(d::AngularDistribution) = Angle()
bijector(d::KSOneSided) = Logit(zero(eltype(d)), one(eltype(d)))

bijector_bounded(d, a=minimum(d), b=maximum(d)) = Logit(a, b)
Expand Down