Skip to content

Commit

Permalink
Fix and simplification of Logit (#172)
Browse files Browse the repository at this point in the history
* Update logit.jl

Simplified Logit implementation a bit and fixed constructor

* added back _logit thanks to @devmotion

* removed specialized _logit to real arguments

* version bump

* Update src/bijectors/logit.jl

Co-authored-by: David Widmann <[email protected]>
  • Loading branch information
torfjelde and devmotion authored Mar 29, 2021
1 parent ad6b01c commit daa8340
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Bijectors"
uuid = "76274a88-744f-5084-9051-94815aaf08c4"
version = "0.8.15"
version = "0.8.16"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
22 changes: 8 additions & 14 deletions src/bijectors/logit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ struct Logit{N, T<:Real} <: Bijector{N}
a::T
b::T
end
function Logit(a, b)
Logit(a::Real, b::Real) = Logit{0}(a, b)
Logit(a::AbstractArray{<:Real, N}, b::AbstractArray{<:Real, N}) where {N} = Logit{N}(a, b)
function Logit{N}(a, b) where {N}
T = promote_type(typeof(a), typeof(b))
Logit{0, T}(a, b)
Logit{N, T}(a, b)
end

# fields are numerical parameters
Expand All @@ -25,22 +27,14 @@ up1(b::Logit{N, T}) where {N, T} = Logit{N + 1, T}(b.a, b.b)
# For equality of Logit with Float64 fields to one with Duals
Base.:(==)(b1::Logit, b2::Logit) = b1.a == b2.a && b1.b == b2.b

(b::Logit{0})(x::Real) = _logit(x, b.a, b.b)
(b::Logit{0})(x) = _logit.(x, b.a, b.b)
(b::Logit{1})(x::AbstractVector) = _logit.(x, b.a, b.b)
(b::Logit{1})(x::AbstractMatrix) = _logit.(x, b.a, b.b)
(b::Logit{2})(x::AbstractMatrix) = _logit.(x, b.a, b.b)
(b::Logit{2})(x::AbstractArray{<:AbstractMatrix}) = map(b, x)
(b::Logit)(x) = _logit.(x, b.a, b.b)
(b::Logit)(x::AbstractArray{<:AbstractArray}) = map(b, x)
_logit(x, a, b) = logit((x - a) / (b - a))

(ib::Inverse{<:Logit{0}})(y::Real) = _ilogit(y, ib.orig.a, ib.orig.b)
(ib::Inverse{<:Logit{0}})(y) = _ilogit.(y, ib.orig.a, ib.orig.b)
(ib::Inverse{<:Logit{1}})(x::AbstractVecOrMat) = _ilogit.(x, ib.orig.a, ib.orig.b)
(ib::Inverse{<:Logit{2}})(x::AbstractMatrix) = _ilogit.(x, ib.orig.a, ib.orig.b)
(ib::Inverse{<:Logit{2}})(x::AbstractArray{<:AbstractMatrix}) = map(ib, x)
(ib::Inverse{<:Logit})(y) = _ilogit.(y, ib.orig.a, ib.orig.b)
(ib::Inverse{<:Logit})(x::AbstractArray{<:AbstractArray}) = map(ib, x)
_ilogit(y, a, b) = (b - a) * logistic(y) + a

logabsdetjac(b::Logit{0}, x::Real) = logit_logabsdetjac(x, b.a, b.b)
logabsdetjac(b::Logit{0}, x) = logit_logabsdetjac.(x, b.a, b.b)
logabsdetjac(b::Logit{1}, x::AbstractVector) = sum(logit_logabsdetjac.(x, b.a, b.b))
logabsdetjac(b::Logit{1}, x::AbstractMatrix) = vec(sum(logit_logabsdetjac.(x, b.a, b.b), dims = 1))
Expand Down

2 comments on commit daa8340

@torfjelde
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/33077

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.16 -m "<description of version>" daa8340a77f9c811ddab990817a3fa97dd0a68c7
git push origin v0.8.16

Please sign in to comment.