Skip to content

Commit

Permalink
Merge branch 'master' into namedtuplevariate
Browse files Browse the repository at this point in the history
  • Loading branch information
sethaxen committed Jan 5, 2025
2 parents 28a7c00 + ceb6343 commit f7ab7c0
Show file tree
Hide file tree
Showing 27 changed files with 324 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
Pkg.instantiate()'
- run: julia --project=perf perf/samplers.jl
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v4
- uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }} # required
fail_ci_if_error: true
Expand Down
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.111"
version = "0.25.115"

[deps]
AliasTables = "66dad0bd-aa9a-41b7-9441-69ab47430ed8"
Expand Down
1 change: 0 additions & 1 deletion src/Distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ export
MatrixBeta,
MatrixFDist,
MatrixNormal,
MatrixReshaped,
MatrixTDist,
MixtureModel,
Multinomial,
Expand Down
15 changes: 12 additions & 3 deletions src/deprecates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,20 @@ end
@deprecate expectation(distr::Union{UnivariateDistribution,MultivariateDistribution}, g::Function; kwargs...) expectation(g, distr; kwargs...) false

# Deprecate `MatrixReshaped`
# This is very similar to `Base.@deprecate_binding MatrixReshaped{...} ReshapedDistribution{...}`
# However, `Base.@deprecate_binding` does not support type parameters
export MatrixReshaped
const MatrixReshaped{S<:ValueSupport,D<:MultivariateDistribution{S}} = ReshapedDistribution{2,S,D}
Base.deprecate(@__MODULE__, :MatrixReshaped)
@deprecate MatrixReshaped(
d::MultivariateDistribution, n::Integer, p::Integer=n
) reshape(d, (n, p))
# This is very similar to `Base.@deprecate MatrixReshaped(...) reshape(...)`
# We use another (unexported!) alias here to not throw a deprecation warning/error
# Unexported aliases do not affect the type printing
# In Julia >= 1.6, instead of a new alias we could have defined a method for (ReshapedDistribution{2,S,D} where {S<:ValueSupport,D<:MultivariateDistribution{S}})
const _MatrixReshaped{S<:ValueSupport,D<:MultivariateDistribution{S}} = ReshapedDistribution{2,S,D}
function _MatrixReshaped(d::MultivariateDistribution, n::Integer, p::Integer=n)
Base.depwarn("`MatrixReshaped(d, n, p)` is deprecated, use `reshape(d, (n, p))` instead.", :MatrixReshaped)
return reshape(d, (n, p))
end

for D in (:InverseWishart, :LKJ, :MatrixBeta, :MatrixFDist, :Wishart)
@eval @deprecate dim(d::$D) size(d, 1)
Expand Down
5 changes: 5 additions & 0 deletions src/univariate/continuous/exponential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ cf(d::Exponential, t::Real) = 1/(1 - t * im * scale(d))
#### Sampling
rand(rng::AbstractRNG, d::Exponential{T}) where {T} = xval(d, randexp(rng, float(T)))

function rand!(rng::AbstractRNG, d::Exponential, A::AbstractArray{<:Real})
randexp!(rng, A)
map!(Base.Fix1(xval, d), A, A)
return A
end

#### Fit model

Expand Down
9 changes: 8 additions & 1 deletion src/univariate/continuous/logitnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,14 @@ end

#### Sampling

rand(rng::AbstractRNG, d::LogitNormal) = logistic(randn(rng) * d.σ + d.μ)
xval(d::LogitNormal, z::Real) = logistic(muladd(d.σ, z, d.μ))

rand(rng::AbstractRNG, d::LogitNormal) = xval(d, randn(rng))
function rand!(rng::AbstractRNG, d::LogitNormal, A::AbstractArray{<:Real})
randn!(rng, A)
map!(Base.Fix1(xval, d), A, A)
return A
end

## Fitting

Expand Down
9 changes: 8 additions & 1 deletion src/univariate/continuous/lognormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,14 @@ end

#### Sampling

rand(rng::AbstractRNG, d::LogNormal) = exp(randn(rng) * d.σ + d.μ)
xval(d::LogNormal, z::Real) = exp(muladd(d.σ, z, d.μ))

rand(rng::AbstractRNG, d::LogNormal) = xval(d, randn(rng))
function rand!(rng::AbstractRNG, d::LogNormal, A::AbstractArray{<:Real})
randn!(rng, A)
map!(Base.Fix1(xval, d), A, A)
return A
end

## Fitting

Expand Down
9 changes: 7 additions & 2 deletions src/univariate/continuous/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ Base.:*(c::Real, d::Normal) = Normal(c * d.μ, abs(c) * d.σ)

#### Sampling

rand(rng::AbstractRNG, d::Normal{T}) where {T} = d.μ + d.σ * randn(rng, float(T))
xval(d::Normal, z::Real) = muladd(d.σ, z, d.μ)

rand!(rng::AbstractRNG, d::Normal, A::AbstractArray{<:Real}) = A .= muladd.(d.σ, randn!(rng, A), d.μ)
rand(rng::AbstractRNG, d::Normal{T}) where {T} = xval(d, randn(rng, float(T)))
function rand!(rng::AbstractRNG, d::Normal, A::AbstractArray{<:Real})
randn!(rng, A)
map!(Base.Fix1(xval, d), A, A)
return A
end

#### Fitting

Expand Down
8 changes: 7 additions & 1 deletion src/univariate/continuous/normalcanon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ invlogccdf(d::NormalCanon, lp::Real) = xval(d, norminvlogccdf(lp))

#### Sampling

rand(rng::AbstractRNG, cf::NormalCanon) = cf.μ + randn(rng) / sqrt(cf.λ)
rand(rng::AbstractRNG, cf::NormalCanon) = xval(cf, randn(rng))

function rand!(rng::AbstractRNG, cf::NormalCanon, A::AbstractArray{<:Real})
randn!(rng, A)
map!(Base.Fix1(xval, cf), A, A)
return A
end

#### Affine transformations

Expand Down
9 changes: 8 additions & 1 deletion src/univariate/continuous/pareto.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@ quantile(d::Pareto, p::Real) = cquantile(d, 1 - p)

#### Sampling

rand(rng::AbstractRNG, d::Pareto) = d.θ * exp(randexp(rng) / d.α)
xval(d::Pareto, z::Real) = d.θ * exp(z / d.α)

rand(rng::AbstractRNG, d::Pareto) = xval(d, randexp(rng))
function rand!(rng::AbstractRNG, d::Pareto, A::AbstractArray{<:Real})
randexp!(rng, A)
map!(Base.Fix1(xval, d), A, A)
return A
end

## Fitting

Expand Down
2 changes: 1 addition & 1 deletion src/univariate/continuous/pgeneralizedgaussian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function rand(rng::AbstractRNG, d::PGeneralizedGaussian)
inv_p = inv(d.p)
g = Gamma(inv_p, 1)
z = d.α * rand(rng, g)^inv_p
if rand(rng) < 0.5
if rand(rng, Bool)
return d.μ - z
else
return d.μ + z
Expand Down
31 changes: 30 additions & 1 deletion src/univariate/discrete/binomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,36 @@ function mode(d::Binomial{T}) where T<:Real
end
modes(d::Binomial) = Int[mode(d)]

median(d::Binomial) = round(Int,mean(d))
function median(dist::Binomial)
# The median is floor(Int, mean) or ceil(Int, mean)
# As shown in https://doi.org/10.1016/0167-7152(94)00090-U,
# |median - mean| <= 1 - bound
# where the equality is strict except for the case p = 1/2 and n odd.
# Thus if |k - mean| < bound for one of the two candidates if p = 1/2 and n odd
# or |k - mean| <= bound for one of the two candidates otherwise,
# the other candidate can't satisfy the condition and hence k must be the median
bound = max(min(dist.p, 1-dist.p), loghalf)
dist_mean = mean(dist)

floor_mean = floor(Int, dist_mean)
difference = dist_mean - floor_mean

if difference <= bound
# The only case where the median satisfies |median - mean| <= 1 - bound with equality
# is p = 1/2 and n odd
# However, in that case we also want to return floor(mean)
floor_mean
elseif difference >= 1 - bound
# The case p = 1/2 and n odd was already covered above,
# thus only cases with |median - mean| < 1 - bound are left here
# Therefore difference >= 1 - bound implies that floor(mean) cannot be the median
floor_mean + 1
elseif cdf(dist, floor_mean) >= 0.5
floor_mean
else
floor_mean + 1
end
end

function skewness(d::Binomial)
n, p1 = params(d)
Expand Down
2 changes: 1 addition & 1 deletion src/univariate/discrete/geometric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Geometric{T<:Real} <: DiscreteUnivariateDistribution
end

function Geometric(p::Real; check_args::Bool=true)
@check_args Geometric (p, zero(p) < p < one(p))
@check_args Geometric (p, zero(p) < p <= one(p))
return Geometric{typeof(p)}(p)
end

Expand Down
2 changes: 1 addition & 1 deletion test/fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ end
for func in funcs, dist in (Laplace, Laplace{Float64})
d = fit(dist, func[2](dist(5.0, 3.0), N + 1))
@test isa(d, dist)
@test isapprox(location(d), 5.0, atol=0.02)
@test isapprox(location(d), 5.0, atol=0.03)
@test isapprox(scale(d) , 3.0, atol=0.03)
end
end
Expand Down
Loading

0 comments on commit f7ab7c0

Please sign in to comment.