Skip to content

Commit

Permalink
Fix inference failures (#1722)
Browse files Browse the repository at this point in the history
* Fix test failures

* Add tests

* Bump version
  • Loading branch information
devmotion authored May 17, 2023
1 parent f61831d commit ef42afb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Distributions"
uuid = "31c24e10-a181-5473-b8eb-7969acd0382f"
authors = ["JuliaStats"]
version = "0.25.92"
version = "0.25.93"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
14 changes: 7 additions & 7 deletions src/truncated/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ TruncatedNormal

### statistics

function mode(d::Truncated{<:Normal{<:Real},Continuous})
function mode(d::Truncated{<:Normal{<:Real},Continuous,T}) where {T<:Real}
μ = mean(d.untruncated)
return clamp(μ, extrema(d)...)
return T(clamp(μ, extrema(d)...))
end

modes(d::Truncated{<:Normal{<:Real},Continuous}) = [mode(d)]
Expand Down Expand Up @@ -89,7 +89,7 @@ function _tnvar(a::Real, b::Real)
end
end

function mean(d::Truncated{<:Normal{<:Real},Continuous})
function mean(d::Truncated{<:Normal{<:Real},Continuous,T}) where {T<:Real}
d0 = d.untruncated
μ = mean(d0)
σ = std(d0)
Expand All @@ -99,21 +99,21 @@ function mean(d::Truncated{<:Normal{<:Real},Continuous})
lower, upper = extrema(d)
a = (lower - μ) / σ
b = (upper - μ) / σ
return μ + _tnmom1(a, b) * σ
return T(μ + _tnmom1(a, b) * σ)
end
end

function var(d::Truncated{<:Normal{<:Real},Continuous})
function var(d::Truncated{<:Normal{<:Real},Continuous,T}) where {T<:Real}
d0 = d.untruncated
μ = mean(d0)
σ = std(d0)
if iszero(σ)
return σ
return T(σ)
else
lower, upper = extrema(d)
a = (lower - μ) / σ
b = (upper - μ) / σ
return _tnvar(a, b) * σ^2
return T(_tnvar(a, b) * σ^2)
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/truncated/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ rng = MersenneTwister(123)
# Type stability
for T in (Float32, Float64)
t = truncated(Normal(T(1.5), T(4.1)), 0, 1)
m = @inferred mode(t)
μ = @inferred mean(t)
σ = @inferred std(t)
@test m === T(1)
@test μ 0.50494725270783081889610661619986770485973643194141
@test μ isa T
@test σ 0.28836356398830993140576947440881738258157196701554
Expand Down

2 comments on commit ef42afb

@devmotion
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/83749

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.25.93 -m "<description of version>" ef42afb215cb08304dd64e309da8002c333caef0
git push origin v0.25.93

Please sign in to comment.