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

Fix #894 Normal distribution and its type parameter #896

Merged
merged 3 commits into from
May 28, 2019
Merged
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
4 changes: 3 additions & 1 deletion src/univariate/continuous/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ params(d::Normal) = (d.μ, d.σ)
location(d::Normal) = d.μ
scale(d::Normal) = d.σ

eltype(::Normal{T}) where {T} = T

#### Statistics

mean(d::Normal) = d.μ
Expand All @@ -87,7 +89,7 @@ cf(d::Normal, t::Real) = exp(im * t * d.μ - d.σ^2/2 * t^2)

#### Sampling

rand(rng::AbstractRNG, d::Normal) = d.μ + d.σ * randn(rng)
rand(rng::AbstractRNG, d::Normal{T}) where {T} = d.μ + d.σ * randn(rng, T)
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if we shouldn't watch out here, since randn does not support all types

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Precisely the concern I mentioned above; although on balance, I think this is an improvement. The prior behavior wasn't technically correct, either due to the wrong output type, or the right output type but the code not actually doing what one would expect (as shown in the examples I gave above).



#### Fitting
Expand Down
15 changes: 15 additions & 0 deletions test/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ using ForwardDiff
@test derivative(t -> logpdf(Normal(1.0, 0.15), t), 2.5) ≈ -66.66666666666667
@test derivative(t -> pdf(Normal(t, 1.0), 0.0), 0.0) == 0.0

# issue #894:
@testset "Normal distribution with non-standard (ie not Float64) parameter types" begin
n32 = Normal(1f0, 0.1f0)
n64 = Normal(1., 0.1)
nbig = Normal(big(pi), big(ℯ))

@test eltype(n32) === Float32
@test eltype(rand(n32)) === Float32
@test eltype(rand(n32, 4)) === Float32

@test eltype(n64) === Float64
@test eltype(rand(n64)) === Float64
@test eltype(rand(n64, 4)) === Float64
end

# Test for numerical problems
@test pdf(Logistic(6,0.01),-2) == 0

Expand Down