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

Fix #894 Normal distribution and its type parameter #896

merged 3 commits into from
May 28, 2019

Conversation

halleysfifthinc
Copy link
Contributor

@halleysfifthinc halleysfifthinc commented May 14, 2019

I didn't see any similar tests so I'm not sure if this needs any tests. I don't imagine that fixing the eltype would be particularly breaking (although it is changing the output type of rand(n::Normal, dims)).

However, the fix for the single rand return type could change an incorrect return type into an error, if the eltype of the Normal dist. doesn't support randn. I noticed this when using DoubleFloats (prior to v0.8.1 of DoubleFloats.jl).

# Before PR
julia> n = Normal(Double64(1), Double64(0.1))
Normal{DoubleFloat{Float64}}=1.0, σ=0.1)

julia> x = rand(n)
0.9972028245261662

julia> typeof(x)
DoubleFloat{Float64}

julia> n32 = Normal(1f0, 0.1f0)
Normal{Float32}=r1.0f0, σ=0.1f0)

julia> x32 = rand(n32)
0.9335312783262796

julia> typeof(x32)
Float64

# After PR (with DoubleFloats.jl v0.7.24 as an example of a type that doesn't support `randn`)
julia> n = Normal(Double64(1), Double64(0.1))
Normal{DoubleFloat{Float64}}=1.0, σ=0.1)

julia> x = rand(n)
ERROR: MethodError: no method matching randn(::Random.MersenneTwister, ::Type{DoubleFloat{Float64}})

julia> n32 = Normal(1f0, 0.1f0)
Normal{Float32}=1.0f0, σ=0.1f0)

julia> x32 = rand(n32)
0.9599364f0

julia> typeof(x32)
Float32

So the previous behavior was both inconsistent, and not accurate in the case of the DoubleFloats. The return type was correct due to the type promotion, but the random number wasn't being generated with the precision of the DoubleFloat. randn support for DoubleFloats was added in v0.8.1.

@halleysfifthinc halleysfifthinc changed the title Fix #894 Normal distribution and its type parameter # Requesting a pull to JuliaStats:master from JuliaStats:ah/fixnormaltypes # # Write a message for this pull request. The first block # of text is the title and the rest is the description. Fix #894 Normal distribution and its type parameter May 14, 2019
@@ -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).

@matbesancon
Copy link
Member

@halleysfifthinc thanks a lot for that. Could you add a simple test with for example the eltype, for instance with a Dual from ForwardDiff or Float32

@halleysfifthinc
Copy link
Contributor Author

Yeah I can add some tests. Where would be appropriate? I didn't see any for the Normal distribution. I'm also not familiar with Dual/ForwardDiff, but I can definitely add some for Float32/Float64.

@matbesancon
Copy link
Member

If you don't see any testset / test file where it fits you can just create one

@matbesancon
Copy link
Member

ping @halleysfifthinc

@codecov-io
Copy link

codecov-io commented May 24, 2019

Codecov Report

Merging #896 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #896      +/-   ##
==========================================
+ Coverage   73.78%   73.79%   +<.01%     
==========================================
  Files         107      107              
  Lines        5200     5201       +1     
==========================================
+ Hits         3837     3838       +1     
  Misses       1363     1363
Impacted Files Coverage Δ
src/univariate/continuous/normal.jl 94.38% <100%> (+0.06%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ba2b28a...ecd7143. Read the comment docs.

@matbesancon
Copy link
Member

matbesancon commented May 24, 2019 via email

@matbesancon matbesancon merged commit 7810c0d into JuliaStats:master May 28, 2019
@matbesancon
Copy link
Member

thanks @halleysfifthinc !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants