Skip to content

Commit

Permalink
Fix eachvariate with zero variates (#1819)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored Jan 5, 2024
1 parent 9e72f1f commit 28bf738
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Distributions"
uuid = "31c24e10-a181-5473-b8eb-7969acd0382f"
authors = ["JuliaStats"]
version = "0.25.104"
version = "0.25.105"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
2 changes: 1 addition & 1 deletion src/eachvariate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end

function EachVariate{V}(x::AbstractArray{<:Real,M}) where {V,M}
ax = ntuple(i -> axes(x, i + V), Val(M - V))
T = typeof(view(x, ntuple(i -> i <= V ? Colon() : firstindex(x, i), Val(M))...))
T = Base.promote_op(view, typeof(x), ntuple(i -> i <= V ? Colon : eltype(axes(x, i)), Val(M))...)
return EachVariate{V,typeof(x),typeof(ax),T,M-V}(x, ax)
end

Expand Down
27 changes: 27 additions & 0 deletions test/eachvariate.jl
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

2 comments on commit 28bf738

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

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.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

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:

git tag -a v0.25.105 -m "<description of version>" 28bf738139dd4dc5bca69671f699912aa978d5a8
git push origin v0.25.105

Please sign in to comment.