Skip to content
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: 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.120"
version = "0.25.121"

[deps]
AliasTables = "66dad0bd-aa9a-41b7-9441-69ab47430ed8"
Expand Down
19 changes: 19 additions & 0 deletions src/multivariate/mvnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,25 @@ logdetcov(d::MvNormal) = logdet(d.Σ)

sqmahal(d::MvNormal, x::AbstractVector) = invquad(d.Σ, x .- d.μ)

function sqmahal(d::DiagNormal, x::AbstractVector)
# Faster than above as this avoids calculating (x .- d.µ)
T = promote_type(partype(d), eltype(x))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct eg if the parameters and x are arrays of integers. One could either compute the first element outside of the loop or possibly use a functional approach (e.g. with mapreduce and Base.Broadcast.broadcasted(...)).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functional approach probably also works better with GPU arrays.

sum = zero(T)
for i in eachindex(x)
@inbounds sum += abs2(x[i] - d.μ[i]) / d.Σ[i, i]
Comment on lines +268 to +269
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indices might not be compatible.

end
return sum
end

function sqmahal(d::IsoNormal, x::AbstractVector)
T = promote_type(partype(d), eltype(x))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very artificial, but this is also not guaranteed to be correct e.g. for Boolean values.

sum = zero(T)
for i in eachindex(x)
@inbounds sum += abs2(x[i] - d.μ[i])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indices might not be compatible

end
return sum / d.Σ[1, 1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might not use one-based indexing.

end

sqmahal!(r::AbstractVector, d::MvNormal, x::AbstractMatrix) =
invquad!(r, d.Σ, x .- d.μ)

Expand Down
Loading