Skip to content

Commit

Permalink
Fix #894 Normal distribution and its type parameter (#896)
Browse files Browse the repository at this point in the history
* Partially fixes #894, the `rand` return value type matches the normal type parameter

* Fixes #894, return an appropriate (matching) `eltype` for Normal

* Add tests
  • Loading branch information
halleysfifthinc authored and matbesancon committed May 28, 2019
1 parent ba2b28a commit 7810c0d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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)


#### 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

0 comments on commit 7810c0d

Please sign in to comment.