-
Notifications
You must be signed in to change notification settings - Fork 430
Improve performance of logpdf
for DiagNormal
and IsoNormal
#1991
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
sum = zero(T) | ||
for i in eachindex(x) | ||
@inbounds sum += abs2(x[i] - d.μ[i]) / d.Σ[i, i] | ||
Comment on lines
+268
to
+269
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
sum = zero(T) | ||
for i in eachindex(x) | ||
@inbounds sum += abs2(x[i] - d.μ[i]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indices might not be compatible |
||
end | ||
return sum / d.Σ[1, 1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.μ) | ||
|
||
|
There was a problem hiding this comment.
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. withmapreduce
andBase.Broadcast.broadcasted(...)
).There was a problem hiding this comment.
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.