-
Notifications
You must be signed in to change notification settings - Fork 423
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 type stability of sampling from Chisq
, TDist
, Gamma
#1885
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cae4051
fix type stability of sampling from `Chisq`, `TDist`, `Gamma`
Red-Portal 22486bf
fix remove type specification in `rand(Exponential)`
Red-Portal 0bcbccb
fix type specificaton in `rand(TDist)`
Red-Portal 232999b
fix remove type test for `rand(Chisq)`
Red-Portal f3985fd
fix make `Exponential` use the `Normal` sampling type policy
Red-Portal 46f715d
fix missing type signature
Red-Portal d421262
fix type signature for `rand(Exponential)`
Red-Portal d1ce42a
fix use `@inferred` in tests for `Gamma`
Red-Portal a3622b7
fix use `@inferred` in tests for `TDist`
Red-Portal 9a786d5
add type stability tests for `rand(Exponential)`
Red-Portal eb1602d
add type stability test for `rand(Chisq)`
Red-Portal 2091cd7
fix remove type stability test for `entropy(TDist)` (not stable)
Red-Portal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
test_cgf(Chisq(1), (0.49, -1, -100, -1f6)) | ||
test_cgf(Chisq(3), (0.49, -1, -100, -1f6)) | ||
|
||
@testset "Chisq" begin | ||
test_cgf(Chisq(1), (0.49, -1, -100, -1.0f6)) | ||
test_cgf(Chisq(3), (0.49, -1, -100, -1.0f6)) | ||
|
||
@test rand(Chisq(1.0)) isa Float64 | ||
@test rand(Chisq(1.0f0)) isa Float32 | ||
end | ||
Red-Portal marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,35 @@ | ||
using Test, Distributions, OffsetArrays | ||
|
||
test_cgf(Gamma(1 ,1 ), (0.9, -1, -100f0, -1e6)) | ||
test_cgf(Gamma(10 ,1 ), (0.9, -1, -100f0, -1e6)) | ||
test_cgf(Gamma(0.2, 10), (0.08, -1, -100f0, -1e6)) | ||
@testset "Gamma" begin | ||
test_cgf(Gamma(1, 1), (0.9, -1, -100.0f0, -1e6)) | ||
test_cgf(Gamma(10, 1), (0.9, -1, -100.0f0, -1e6)) | ||
test_cgf(Gamma(0.2, 10), (0.08, -1, -100.0f0, -1e6)) | ||
|
||
@testset "Gamma suffstats and OffsetArrays" begin | ||
a = rand(Gamma(), 11) | ||
wa = 1.0:11.0 | ||
@testset "Gamma suffstats and OffsetArrays" begin | ||
a = rand(Gamma(), 11) | ||
wa = 1.0:11.0 | ||
|
||
resulta = @inferred(suffstats(Gamma, a)) | ||
resulta = @inferred(suffstats(Gamma, a)) | ||
|
||
resultwa = @inferred(suffstats(Gamma, a, wa)) | ||
resultwa = @inferred(suffstats(Gamma, a, wa)) | ||
|
||
b = OffsetArray(a, -5:5) | ||
wb = OffsetArray(wa, -5:5) | ||
b = OffsetArray(a, -5:5) | ||
wb = OffsetArray(wa, -5:5) | ||
|
||
resultb = @inferred(suffstats(Gamma, b)) | ||
@test resulta == resultb | ||
resultb = @inferred(suffstats(Gamma, b)) | ||
@test resulta == resultb | ||
|
||
resultwb = @inferred(suffstats(Gamma, b, wb)) | ||
@test resultwa == resultwb | ||
resultwb = @inferred(suffstats(Gamma, b, wb)) | ||
@test resultwa == resultwb | ||
|
||
@test_throws DimensionMismatch suffstats(Gamma, a, wb) | ||
@test_throws DimensionMismatch suffstats(Gamma, a, wb) | ||
end | ||
|
||
@test rand(Gamma(1.0, 1.0)) isa Float64 | ||
@test rand(Gamma(0.5, 1.0)) isa Float64 | ||
@test rand(Gamma(2.0, 1.0)) isa Float64 | ||
|
||
@test rand(Gamma(1.0f0, 1.0f0)) isa Float32 | ||
@test rand(Gamma(0.5f0, 1.0f0)) isa Float32 | ||
@test rand(Gamma(2.0f0, 1.0f0)) isa Float32 | ||
Red-Portal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a slightly different case and easily broken (e.g., when
T
is not a floating point number type). In theTDist
andGamma
case we just try to avoid promotions of a sample from anotherrand
call, whereas this case goes deeper into the question of howrand
should behave wrt parameters etc. (see also #1433 (comment)).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do this though, the return type of
rand(Gamma(Float32, Float32))
changes depending on the value of the shape parameter becauseshape == 1
samples fromExponential
. (This is why the tests are currently failing.) Should we let this happen? I imagine some people will be super surprised by such behavior.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @devmotion could you comment on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. I didn't realize that
GammaMTSampler
already respects the parameter types (but samples are not necessarily of the parameter type:Distributions.jl/src/samplers/gamma.jl
Lines 175 to 182 in 13029c0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's an argument for using the same approach as for
Normal
here, until we move to a better/different API: