-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
eachvariate
with zero variates (#1819)
- Loading branch information
Showing
3 changed files
with
29 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,44 @@ | ||
using Distributions | ||
using ChainRulesTestUtils | ||
using ChainRulesTestUtils: FiniteDifferences | ||
|
||
using Random | ||
using Test | ||
|
||
# Without this, `to_vec` will also include the `axes` field of `EachVariate`. | ||
function FiniteDifferences.to_vec(xs::Distributions.EachVariate{V}) where {V} | ||
vals, vals_from_vec = FiniteDifferences.to_vec(xs.parent) | ||
return vals, x -> Distributions.EachVariate{V}(vals_from_vec(x)) | ||
end | ||
|
||
# MWE in #1817 | ||
struct FooEachvariate <: Sampleable{Multivariate, Continuous} end | ||
Base.length(::FooEachvariate) = 3 | ||
Base.eltype(::FooEachvariate) = Float64 | ||
function Distributions._rand!(rng::AbstractRNG, ::FooEachvariate, x::AbstractVector{<:Real}) | ||
return rand!(rng, x) | ||
end | ||
|
||
@testset "eachvariate.jl" begin | ||
@testset "ChainRules" begin | ||
xs = randn(2, 3, 4, 5) | ||
test_rrule(Distributions.EachVariate{1}, xs) | ||
test_rrule(Distributions.EachVariate{2}, xs) | ||
test_rrule(Distributions.EachVariate{3}, xs) | ||
end | ||
|
||
@testset "No variates (#1817)" begin | ||
@test size(Distributions.eachvariate(rand(0), Univariate)) == (0,) | ||
@test size(Distributions.eachvariate(rand(3, 0, 1), Univariate)) == (3, 0, 1) | ||
@test size(Distributions.eachvariate(rand(3, 2, 0), Univariate)) == (3, 2, 0) | ||
|
||
@test size(Distributions.eachvariate(rand(4, 0), Multivariate)) == (0,) | ||
@test size(Distributions.eachvariate(rand(4, 0, 2), Multivariate)) == (0, 2) | ||
@test size(Distributions.eachvariate(rand(4, 5, 0), Multivariate)) == (5, 0) | ||
@test size(Distributions.eachvariate(rand(4, 5, 0, 2), Multivariate)) == (5, 0, 2) | ||
|
||
draws = @inferred(rand(FooEachvariate(), 0)) | ||
@test draws isa Matrix{Float64} | ||
@test size(draws) == (3, 0) | ||
end | ||
end |
28bf738
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.
@JuliaRegistrator register
28bf738
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.
Registration pull request created: JuliaRegistries/General/98274
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
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: