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

Use pairwise summation in loglikelihood #1409

Closed
wants to merge 5 commits into from
Closed

Conversation

devmotion
Copy link
Member

This PR changes the implementation of loglikelihood such that it uses pairwise summation. This approach is based on JuliaLang/julia#31020 and uses a fast path in recent Julia versions. It is already used in e.g. StatsBase successfully (xref: JuliaStats/StatsBase.jl#518).

@andreasnoack
Copy link
Member

It almost feels like we should change the Base method for sum(f, ::Array) to do this. Would there be any drawbacks?

@devmotion
Copy link
Member Author

I'm not sure, I think this could be a good idea. However, even if sum(f, x) would use pairwise summation the advantage of the explicit Broadcast.broadcasted etc. expressions in this PR here is that we don't have to use Base.Fix1 or anonynomous functions.

@codecov-commenter
Copy link

codecov-commenter commented Nov 10, 2021

Codecov Report

Merging #1409 (3c03200) into master (837d312) will decrease coverage by 0.04%.
The diff coverage is 100.00%.

❗ Current head 3c03200 differs from pull request most recent head e74ba97. Consider uploading reports for the commit e74ba97 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1409      +/-   ##
==========================================
- Coverage   83.81%   83.76%   -0.05%     
==========================================
  Files         122      120       -2     
  Lines        6993     6960      -33     
==========================================
- Hits         5861     5830      -31     
+ Misses       1132     1130       -2     
Impacted Files Coverage Δ
src/matrixvariates.jl 77.77% <100.00%> (+0.96%) ⬆️
src/multivariates.jl 71.42% <100.00%> (+0.76%) ⬆️
src/univariates.jl 74.60% <100.00%> (+0.27%) ⬆️
src/truncated/loguniform.jl
src/univariate/continuous/loguniform.jl

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 837d312...e74ba97. Read the comment docs.

@devmotion
Copy link
Member Author

@andreasnoack would you like to wait for a potential change in base? While the implementation is more verbose, I still think it has an advantage even if sum(f, x) is redefined in base since no anonymous functions or Base.Fix1 are needed.

end
function loglikelihood(d::MatrixDistribution, X::AbstractArray{<:AbstractMatrix{<:Real}})
return sum(x -> logpdf(d, x), X)
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this call already use pairwise summation?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I thought it doesn't but seems you're right (was it always the case or did it change at some point?):

julia> A = vcat([Float32(1E0)], fill(Float32(1E-8), 10^8));

julia> sum(identity, A)
1.9999989f0

julia> sum(x -> x, A)
1.9999989f0

julia> sum(Broadcast.instantiate(Broadcast.broadcasted(x -> x, A)))
1.9999989f0

In any case, I'll close this issue in favour of #1391 which will generalize logpdf and loglikelihood.

Copy link
Member

Choose a reason for hiding this comment

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

No idea. That said, I get slightly different results in some cases:

julia> x = vcat([Float32(1E0)], fill(Float32(1E-8), 10^8));

julia> sum(sin, x) - sum(sin.(x))
-1.41859055f-5

@devmotion
Copy link
Member Author

In any case, I'll close this issue in favour of #1391 which will generalize logpdf and loglikelihood.

@devmotion devmotion closed this Nov 21, 2021
@devmotion devmotion deleted the dw/pairwise branch November 21, 2021 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants