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

Replace broadcasting over distributions with broadcasting with partially applied functions #1818

Merged
merged 2 commits into from
Jan 5, 2024

Conversation

devmotion
Copy link
Member

Broadcasting treats UnivariateDistributions as scalars due to

Broadcast.broadcastable(d::UnivariateDistribution) = Ref(d)
. However, such Refs are suboptimal since

Unfortunately, replacing

Broadcast.broadcastable(d::UnivariateDistribution) = Ref(d)
with broadcastable(d::UnivariateDistribution) = (d,) is not an option since broadcasting with tuples is not identical to broadcasting with Refs (see, e.g., https://discourse.julialang.org/t/add-scalar-macro-to-base/50255/5).

However, broadcasting over distributions is actually not needed as one can just broadcast with a partially applied function that captures the distribution instead. Therefore this PR changes all broadcasts over distributions in Distributions.jl to broadcasts with such partially applied functions (and quite often this implies that broadcasting is not needed anymore and a simple map would be sufficient).

Fixes #1815.


As a side note, IMO it would be more convenient for users and downstream packages that would like to follow the same pattern if we would define pdf(d::Distribution) = Base.Fix1(pdf, d) etc. (similar to ==(x) etc. in Base) (ref #966). Unfortunately, pdf(d::DiscreteUnivariateDistribution) is already defined in a different way:

@deprecate pdf(d::DiscreteUnivariateDistribution) pdf.(Ref(d), support(d))

@codecov-commenter
Copy link

codecov-commenter commented Jan 4, 2024

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (9e72f1f) 85.93% compared to head (b48ca27) 85.93%.
Report is 1 commits behind head on master.

Files Patch % Lines
src/univariate/discrete/betabinomial.jl 50.00% 3 Missing ⚠️
src/mixtures/mixturemodel.jl 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1818      +/-   ##
==========================================
- Coverage   85.93%   85.93%   -0.01%     
==========================================
  Files         144      144              
  Lines        8655     8658       +3     
==========================================
+ Hits         7438     7440       +2     
- Misses       1217     1218       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@devmotion devmotion merged commit d58adb6 into master Jan 5, 2024
12 of 15 checks passed
@devmotion devmotion deleted the dw/partially_applied branch January 5, 2024 11:20
@theogf
Copy link
Contributor

theogf commented Apr 12, 2024

Unfortunately, pdf(d::DiscreteUnivariateDistribution) is already defined in a different way

I find your proposal for the use of the single argument pdf to be far more useful.
Since it's already deprecated, does that mean we could introduce the new definition in the next breaking release?

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.

rand! with Uniform distribution does not work with CUDA arrays
4 participants