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 eachvariate with zero variates #1819

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading