Skip to content

Commit

Permalink
Fix rand for Product(Fill(...)) distributions (#1521)
Browse files Browse the repository at this point in the history
* Fix rand for Product(Fill(...)) distributions, useful for representing iids

* Bump version
  • Loading branch information
chriselrod committed Mar 17, 2022
1 parent f9c2060 commit 1c44a2a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 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.51"
version = "0.25.52"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
2 changes: 1 addition & 1 deletion src/multivariate/product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function Base.eltype(::Type{<:Product{S,T}}) where {S<:ValueSupport,
end

_rand!(rng::AbstractRNG, d::Product, x::AbstractVector{<:Real}) =
broadcast!(dn->rand(rng, dn), x, d.v)
map!(Base.Fix1(rand, rng), x, d.v)
_logpdf(d::Product, x::AbstractVector{<:Real}) =
sum(n->logpdf(d.v[n], x[n]), 1:length(d))

Expand Down
12 changes: 11 additions & 1 deletion test/product.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Distributions, Test, Random, LinearAlgebra
using Distributions, Test, Random, LinearAlgebra, FillArrays
using Distributions: Product

@testset "Testing normal product distributions" begin
Expand Down Expand Up @@ -80,3 +80,13 @@ end
@test length(y) == N
end
end

@testset "Testing iid product distributions" begin
Random.seed!(123456)
N = 11
d = Product(Fill(Laplace(0.0, 2.3), N))
@test N == length(unique(rand(d)));
@test mean(d) === Fill(0.0, N)
@test cov(d) === Diagonal(Fill(var(Laplace(0.0, 2.3)), N))
end

2 comments on commit 1c44a2a

@devmotion
Copy link
Member

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/56781

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.52 -m "<description of version>" 1c44a2ae49e2e31b46b64557b0aa924d216aaf93
git push origin v0.25.52

Please sign in to comment.