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 5 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
6 changes: 6 additions & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ value_support(::Type{T}) where {T<:Distribution} = value_support(supertype(T))
# to be decided: how to handle multivariate/matrixvariate distributions?
Broadcast.broadcastable(d::UnivariateDistribution) = Ref(d)

"""
extrema(d::Distribution)

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

## TODO: the following types need to be improved
abstract type SufficientStats end
Expand Down
2 changes: 2 additions & 0 deletions src/multivariate/mvnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ abstract type AbstractMvNormal <: ContinuousMultivariateDistribution end
insupport(d::AbstractMvNormal, x::AbstractVector) =
length(d) == length(x) && all(isfinite, x)

minimum(d::AbstractMvNormal) = fill(eltype(d)(-Inf), length(d))
maximum(d::AbstractMvNormal) = fill(eltype(d)(Inf), length(d))
mode(d::AbstractMvNormal) = mean(d)
modes(d::AbstractMvNormal) = [mean(d)]

Expand Down
2 changes: 2 additions & 0 deletions src/multivariate/product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var(d::Product) = var.(d.v)
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) = map(minimum, d.v)
maximum(d::Product) = map(maximum, d.v)

"""
product_distribution(dists::AbstractVector{<:UnivariateDistribution})
Expand Down
11 changes: 2 additions & 9 deletions src/univariates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,19 @@ Get the degrees of freedom.
dof(d::UnivariateDistribution)

"""
minimum(d::UnivariateDistribution)
minimum(d::Distribution)

Return the minimum of the support of `d`.
"""
minimum(d::UnivariateDistribution)

"""
maximum(d::UnivariateDistribution)
maximum(d::Distribution)

Return the maximum of the support of `d`.
"""
maximum(d::UnivariateDistribution)

"""
extrema(d::UnivariateDistribution)

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

"""
insupport(d::UnivariateDistribution, x::Any)

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