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

support intervals #1809

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "0.25.104"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
DensityInterface = "b429d917-457f-4dbc-8f4c-0cc954292b1d"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand Down Expand Up @@ -39,6 +40,7 @@ Distributed = "<0.0.1, 1"
FillArrays = "0.9, 0.10, 0.11, 0.12, 0.13, 1"
FiniteDifferences = "0.12"
ForwardDiff = "0.10"
IntervalSets = "0.7"
JSON = "0.21"
LinearAlgebra = "<0.0.1, 1"
OffsetArrays = "1"
Expand Down
10 changes: 10 additions & 0 deletions src/Distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import PDMats: dim, PDMat, invquad
using SpecialFunctions
using Base.MathConstants: eulergamma

using IntervalSets

const RealInterval = Interval

export
# re-export Statistics
mean, median, quantile, std, var, cov, cor,
Expand Down Expand Up @@ -328,6 +332,12 @@ end

include("deprecates.jl")


Uniform(i::Interval) = Uniform(leftendpoint(i), rightendpoint(i))
LogUniform(i::Interval) = LogUniform(leftendpoint(i), rightendpoint(i))
truncated(d0, i::Interval) = truncated(d, leftendpoint(i), rightendpoint(i))
censored(d0, i::Interval) = censored(d, leftendpoint(i), rightendpoint(i))

"""
A Julia package for probability distributions and associated functions.

Expand Down
6 changes: 3 additions & 3 deletions src/univariate/locationscale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ minimum(d::AffineDistribution) =
maximum(d::AffineDistribution) =
d.σ > 0 ? d.μ + d.σ * maximum(d.ρ) : d.μ + d.σ * minimum(d.ρ)
support(d::AffineDistribution) = affinedistribution_support(d.μ, d.σ, support(d.ρ))
function affinedistribution_support(μ::Real, σ::Real, support::RealInterval)
function affinedistribution_support(μ::Real, σ::Real, support::Interval)
if σ > 0
return RealInterval(μ + σ * support.lb, μ + σ * support.ub)
return Interval(μ + σ * minimum(support), μ + σ * maximum(support))
else
return RealInterval(μ + σ * support.ub, μ + σ * support.lb)
return Interval(μ + σ * maximum(support), μ + σ * minimum(support))
end
end
affinedistribution_support(μ::Real, σ::Real, support) = σ > 0 ? μ .+ σ .* support : μ .+ σ .* reverse(support)
Expand Down
16 changes: 1 addition & 15 deletions src/univariates.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
#### Domain && Support

struct RealInterval{T<:Real}
lb::T
ub::T
end

RealInterval(lb::Real, ub::Real) = RealInterval(promote(lb, ub)...)

Base.minimum(r::RealInterval) = r.lb
Base.maximum(r::RealInterval) = r.ub
Base.extrema(r::RealInterval) = (r.lb, r.ub)
Base.in(x::Real, r::RealInterval) = r.lb <= x <= r.ub

isbounded(d::Union{D,Type{D}}) where {D<:UnivariateDistribution} = isupperbounded(d) && islowerbounded(d)

islowerbounded(d::Union{D,Type{D}}) where {D<:UnivariateDistribution} = minimum(d) > -Inf
Expand All @@ -20,8 +8,6 @@ isupperbounded(d::Union{D,Type{D}}) where {D<:UnivariateDistribution} = maximum(
hasfinitesupport(d::Union{D,Type{D}}) where {D<:DiscreteUnivariateDistribution} = isbounded(d)
hasfinitesupport(d::Union{D,Type{D}}) where {D<:ContinuousUnivariateDistribution} = false

Base.:(==)(r1::RealInterval, r2::RealInterval) = r1.lb == r2.lb && r1.ub == r2.ub

"""
params(d::UnivariateDistribution)

Expand Down Expand Up @@ -108,7 +94,7 @@ insupport(d::Union{D,Type{D}}, X::AbstractArray) where {D<:UnivariateDistributio
insupport(d::Union{D,Type{D}},x::Real) where {D<:ContinuousUnivariateDistribution} = minimum(d) <= x <= maximum(d)
insupport(d::Union{D,Type{D}},x::Real) where {D<:DiscreteUnivariateDistribution} = isinteger(x) && minimum(d) <= x <= maximum(d)

support(d::Union{D,Type{D}}) where {D<:ContinuousUnivariateDistribution} = RealInterval(minimum(d), maximum(d))
support(d::Union{D,Type{D}}) where {D<:ContinuousUnivariateDistribution} = Interval(minimum(d), maximum(d))
support(d::Union{D,Type{D}}) where {D<:DiscreteUnivariateDistribution} = round(Int, minimum(d)):round(Int, maximum(d))

# Type used for dispatch on finite support
Expand Down
2 changes: 1 addition & 1 deletion test/pdfnorm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using QuadGK
# norms of the distributions. These methods aren't very robust because can't
# deal with divergent norms, or discrete distributions with infinite support.
numeric_norm(d::ContinuousUnivariateDistribution) =
quadgk(x -> pdf(d, x) ^ 2, support(d).lb, support(d).ub)[1]
quadgk(x -> pdf(d, x) ^ 2, extrema(support(d))...)[1]

function numeric_norm(d::DiscreteUnivariateDistribution)
# When the distribution has infinite support, sum up to an arbitrary large
Expand Down