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 UniformScaling constructors for normals #182

Merged
merged 2 commits into from
Jan 29, 2024
Merged
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
7 changes: 7 additions & 0 deletions src/distributions/normal_family/mv_normal_mean_covariance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ function MvNormalMeanCovariance(μ::AbstractVector{T}) where {T}
return MvNormalMeanCovariance(μ, convert(AbstractArray{T}, ones(length(μ))))
end

function MvNormalMeanCovariance(μ::AbstractVector{T1}, Σ::UniformScaling{T2}) where {T1, T2}
T = promote_type(T1, T2)
μ_new = convert(AbstractArray{T}, μ)
Σ_new = convert(UniformScaling{T}, Σ)(length(μ))
return MvNormalMeanCovariance(μ_new, Σ_new)
end

Distributions.distrname(::MvNormalMeanCovariance) = "MvNormalMeanCovariance"

function BayesBase.weightedmean(dist::MvNormalMeanCovariance)
Expand Down
7 changes: 7 additions & 0 deletions src/distributions/normal_family/mv_normal_mean_precision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ function MvNormalMeanPrecision(μ::AbstractVector{T}) where {T}
return MvNormalMeanPrecision(μ, convert(AbstractArray{T}, ones(length(μ))))
end

function MvNormalMeanPrecision(μ::AbstractVector{T1}, Λ::UniformScaling{T2}) where {T1, T2}
T = promote_type(T1, T2)
μ_new = convert(AbstractArray{T}, μ)
Λ_new = convert(UniformScaling{T}, Λ)(length(μ))
return MvNormalMeanPrecision(μ_new, Λ_new)
end

Distributions.distrname(::MvNormalMeanPrecision) = "MvNormalMeanPrecision"

BayesBase.weightedmean(dist::MvNormalMeanPrecision) = precision(dist) * mean(dist)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ function MvNormalWeightedMeanPrecision(xi::AbstractVector{T}) where {T}
return MvNormalWeightedMeanPrecision(xi, convert(AbstractArray{T}, ones(length(xi))))
end

function MvNormalWeightedMeanPrecision(xi::AbstractVector{T1}, Λ::UniformScaling{T2}) where {T1, T2}
T = promote_type(T1, T2)
xi_new = convert(AbstractArray{T}, xi)
Λ_new = convert(UniformScaling{T}, Λ)(length(xi))
return MvNormalWeightedMeanPrecision(xi_new, Λ_new)
end

Distributions.distrname(::MvNormalWeightedMeanPrecision) = "MvNormalWeightedMeanPrecision"

BayesBase.weightedmean(dist::MvNormalWeightedMeanPrecision) = dist.xi
Expand Down
7 changes: 7 additions & 0 deletions src/distributions/normal_family/normal_mean_precision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ NormalMeanPrecision(μ::Integer, w::Integer) = NormalMeanPrecision(float(μ), fl
NormalMeanPrecision(μ::Real) = NormalMeanPrecision(μ, one(μ))
NormalMeanPrecision() = NormalMeanPrecision(0.0, 1.0)

function NormalMeanPrecision(μ::T1, w::UniformScaling{T2}) where {T1 <: Real, T2}
T = promote_type(T1, T2)
μ_new = convert(T, μ)
w_new = convert(T, w.λ)
return NormalMeanPrecision(μ_new, w_new)
end

Distributions.@distr_support NormalMeanPrecision -Inf Inf

BayesBase.support(dist::NormalMeanPrecision) = Distributions.RealInterval(minimum(dist), maximum(dist))
Expand Down
7 changes: 7 additions & 0 deletions src/distributions/normal_family/normal_mean_variance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ NormalMeanVariance(μ::Integer, v::Integer) = NormalMeanVariance(float(μ), floa
NormalMeanVariance(μ::T) where {T <: Real} = NormalMeanVariance(μ, one(T))
NormalMeanVariance() = NormalMeanVariance(0.0, 1.0)

function NormalMeanVariance(μ::T1, v::UniformScaling{T2}) where {T1 <: Real, T2}
T = promote_type(T1, T2)
μ_new = convert(T, μ)
v_new = convert(T, v.λ)
return NormalMeanVariance(μ_new, v_new)
end

Distributions.@distr_support NormalMeanVariance -Inf Inf

BayesBase.support(dist::NormalMeanVariance) = Distributions.RealInterval(minimum(dist), maximum(dist))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ NormalWeightedMeanPrecision(xi::Integer, w::Integer) = NormalWeightedMeanPrecisi
NormalWeightedMeanPrecision(xi::Real) = NormalWeightedMeanPrecision(xi, one(xi))
NormalWeightedMeanPrecision() = NormalWeightedMeanPrecision(0.0, 1.0)

function NormalWeightedMeanPrecision(xi::T1, w::UniformScaling{T2}) where {T1 <: Real, T2}
T = promote_type(T1, T2)
xi_new = convert(T, xi)
w_new = convert(T, w.λ)
return NormalWeightedMeanPrecision(xi_new, w_new)
end

Distributions.@distr_support NormalWeightedMeanPrecision -Inf Inf

BayesBase.support(dist::NormalWeightedMeanPrecision) = Distributions.RealInterval(minimum(dist), maximum(dist))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
@test eltype(MvNormalMeanCovariance([1, 1], [1, 1])) === Float64
@test eltype(MvNormalMeanCovariance([1.0f0, 1.0f0])) === Float32
@test eltype(MvNormalMeanCovariance([1.0f0, 1.0f0], [1.0f0, 1.0f0])) === Float32

@test MvNormalMeanCovariance(ones(3), 5I) == MvNormalMeanCovariance(ones(3), Diagonal(5 * ones(3)))
@test MvNormalMeanCovariance([1, 2, 3, 4], 7.0I) == MvNormalMeanCovariance([1.0, 2.0, 3.0, 4.0], Diagonal(7.0 * ones(4)))
end

@testitem "MvNormalMeanCovariance: distrname" begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
@test eltype(MvNormalMeanPrecision([1, 1], [1, 1])) === Float64
@test eltype(MvNormalMeanPrecision([1.0f0, 1.0f0])) === Float32
@test eltype(MvNormalMeanPrecision([1.0f0, 1.0f0], [1.0f0, 1.0f0])) === Float32

@test MvNormalMeanPrecision(ones(3), 5I) == MvNormalMeanPrecision(ones(3), Diagonal(5 * ones(3)))
@test MvNormalMeanPrecision([1, 2, 3, 4], 7.0I) == MvNormalMeanPrecision([1.0, 2.0, 3.0, 4.0], Diagonal(7.0 * ones(4)))
end

@testitem "MvNormalMeanPrecision: distrname" begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
@test eltype(MvNormalWeightedMeanPrecision([1, 1], [1, 1])) === Float64
@test eltype(MvNormalWeightedMeanPrecision([1.0f0, 1.0f0])) === Float32
@test eltype(MvNormalWeightedMeanPrecision([1.0f0, 1.0f0], [1.0f0, 1.0f0])) === Float32

@test MvNormalWeightedMeanPrecision(ones(3), 5I) == MvNormalWeightedMeanPrecision(ones(3), Diagonal(5 * ones(3)))
@test MvNormalWeightedMeanPrecision([1, 2, 3, 4], 7.0I) == MvNormalWeightedMeanPrecision([1.0, 2.0, 3.0, 4.0], Diagonal(7.0 * ones(4)))
end

@testitem "MvNormalWeightedMeanPrecision: distrname" begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
@test eltype(NormalMeanPrecision(0.0f0)) === Float32
@test eltype(NormalMeanPrecision(0.0f0, 1.0f0)) === Float32
@test eltype(NormalMeanPrecision(0.0f0, 1.0)) === Float64

@test NormalMeanPrecision(3, 5I) == NormalMeanPrecision(3, 5)
@test NormalMeanPrecision(2, 7.0I) == NormalMeanPrecision(2.0, 7.0)
end

@testitem "NormalMeanPrecision: Stats methods" begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
@test eltype(NormalMeanVariance(0.0f0)) === Float32
@test eltype(NormalMeanVariance(0.0f0, 1.0f0)) === Float32
@test eltype(NormalMeanVariance(0.0f0, 1.0)) === Float64

@test NormalMeanVariance(3, 5I) == NormalMeanVariance(3, 5)
@test NormalMeanVariance(2, 7.0I) == NormalMeanVariance(2.0, 7.0)
end

@testitem "NormalMeanVariance: Stats methods" begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
@test eltype(NormalWeightedMeanPrecision(0.0f0)) === Float32
@test eltype(NormalWeightedMeanPrecision(0.0f0, 1.0f0)) === Float32
@test eltype(NormalWeightedMeanPrecision(0.0f0, 1.0)) === Float64

@test NormalWeightedMeanPrecision(3, 5I) == NormalWeightedMeanPrecision(3, 5)
@test NormalWeightedMeanPrecision(2, 7.0I) == NormalWeightedMeanPrecision(2.0, 7.0)
end

@testitem "NormalWeightedMeanPrecision: Stats methods" begin
Expand Down
Loading