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

Add minimum, maximum, extrema for AbstractMvNormal and Product #1319

Merged
merged 6 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions docs/src/multivariate.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ invcov(::Distributions.AbstractMvNormal)
logdetcov(::Distributions.AbstractMvNormal)
sqmahal(::Distributions.AbstractMvNormal, ::AbstractArray)
rand(::AbstractRNG, ::Distributions.AbstractMvNormal)
minimum(::Distributions.AbstractMvNormal)
maximum(::Distributions.AbstractMvNormal)
extrema(::Distributions.AbstractMvNormal)
```

### MvLogNormal
Expand Down
21 changes: 21 additions & 0 deletions src/multivariate/mvnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ abstract type AbstractMvNormal <: ContinuousMultivariateDistribution end
insupport(d::AbstractMvNormal, x::AbstractVector) =
length(d) == length(x) && all(isfinite, x)

"""
minimum(d::AbstractMvNormal)

Return the minimum of the support of each dimension of `d`.
"""
minimum(d::AbstractMvNormal) = fill(eltype(d)(-Inf), length(d))

"""
maximum(d::AbstractMvNormal)

Return the maximum of the support of each dimension of `d`.
"""
maximum(d::AbstractMvNormal) = fill(eltype(d)(Inf), length(d))

"""
extrema(d::AbstractMvNormal)

Return the minimum and maximum of the support of each dimenison of `d` as a 2-tuple.
"""
extrema(d::AbstractMvNormal) = minimum(d), maximum(d)

mode(d::AbstractMvNormal) = mean(d)
modes(d::AbstractMvNormal) = [mean(d)]

Expand Down
21 changes: 21 additions & 0 deletions src/multivariate/product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ cov(d::Product) = Diagonal(var(d))
entropy(d::Product) = sum(entropy, d.v)
insupport(d::Product, x::AbstractVector) = all(insupport.(d.v, x))

"""
minimum(d::Product)

Return the minimum of the support of each dimension of `d`.
"""
minimum(d::Product) = minimum.(d.v)

"""
maximum(d::Product)

Return the maximum of the support of each dimension of `d`.
"""
maximum(d::Product) = maximum.(d.v)

"""
extrema(d::Product)

Return the minimum and maximum of the support of each dimension of `d` as a 2-tuple.
"""
extrema(d::Product) = minimum(d), maximum(d)

"""
product_distribution(dists::AbstractVector{<:UnivariateDistribution})

Expand Down
4 changes: 4 additions & 0 deletions test/mvnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ function test_mvnormal(g::AbstractMvNormal, n_tsamples::Int=10^6,
vs = diag(Σ)
@test g == typeof(g)(params(g)...)
@test g == deepcopy(g)
@test minimum(g) == fill(-Inf, d)
@test maximum(g) == fill(Inf, d)
@test extrema(g) == (minimum(g), maximum(g))
@test isless(extrema(g)...)

# test sampling for AbstractMatrix (here, a SubArray):
if ismissing(rng)
Expand Down
6 changes: 5 additions & 1 deletion test/product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ end
N = 11
# Construct independent distributions and `Product` distribution from these.
ubound = rand(N)
ds = Uniform.(0.0, ubound)
ds = Uniform.(-ubound, ubound)
x = rand.(ds)
d_product = product_distribution(ds)
@test d_product isa Product
Expand All @@ -43,6 +43,10 @@ end
@test entropy(d_product) == sum(entropy.(ds))
@test insupport(d_product, ubound) == true
@test insupport(d_product, ubound .+ 1) == false
@test minimum(d_product) == -ubound
@test maximum(d_product) == ubound
@test extrema(d_product) == (-ubound, ubound)
@test isless(extrema(d_product)...)

y = rand(d_product)
@test y isa typeof(x)
Expand Down