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 Unicode alias \oplus for convolve #1445

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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.34"
version = "0.25.35"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ makedocs(
"reshape.md",
"cholesky.md",
"mixture.md",
"convolution.md",
"fit.md",
"extends.md",
"density_interface.md",
Expand Down
12 changes: 12 additions & 0 deletions docs/src/convolution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Convolutions

A [convolution of two probability distributions](https://en.wikipedia.org/wiki/List_of_convolutions_of_probability_distributions)
is the probability distribution of the sum of two independent random variables that are
distributed according to these distributions.

The convolution of two distributions can be constructed with [`convolve`](@ref) or its
Unicode alias [`⊕`](@ref).

```@docs
convolve
```
1 change: 1 addition & 0 deletions src/Distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export
componentwise_logpdf, # component-wise logpdf for mixture models
concentration, # the concentration parameter
convolve, # convolve distributions of the same type
⊕, # unicode alias of convolve
dim, # sample dimension of multivariate distribution
dof, # get the degree of freedom
entropy, # entropy of distribution in nats
Expand Down
68 changes: 29 additions & 39 deletions src/convolution.jl
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
"""
convolve(d1::T, d2::T) where T<:Distribution -> Distribution

Convolve two distributions of the same type to yield the distribution corresponding to the
sum of independent random variables drawn from the underlying distributions.

The function is only defined in the cases where the convolution has a closed form as
defined here https://en.wikipedia.org/wiki/List_of_convolutions_of_probability_distributions

* `Bernoulli`
* `Binomial`
* `NegativeBinomial`
* `Geometric`
* `Poisson`
* `Normal`
* `Cauchy`
* `Chisq`
* `Exponential`
* `Gamma`
* `MultivariateNormal`
convolve(d1::Distribution, d2::Distribution)
d1 ⊕ d2

Convolve two distributions and return the distribution corresponding to the sum of
independent random variables drawn from the underlying distributions.

The Unicode operator `⊕` can be typed by `\\oplus<tab>`.

Currently, the function is only defined in cases where the convolution has a closed form.
More precisely, the function is defined if the distributions of `d1` and `d2` are the same
and one of
* [`Bernoulli`](@ref)
* [`Binomial`](@ref)
* [`NegativeBinomial`](@ref)
* [`Geometric`](@ref)
* [`Poisson`](@ref)
* [`Normal`](@ref)
* [`Cauchy`](@ref)
* [`Chisq`](@ref)
* [`Exponential`](@ref)
* [`Gamma`](@ref)
* [`MvNormal`](@ref)

External links: [List of convolutions of probability distributions on Wikipedia](https://en.wikipedia.org/wiki/List_of_convolutions_of_probability_distributions)
"""
function convolve end
convolve(::Distribution, ::Distribution)

# define Unicode alias
const ⊕ = convolve
Copy link
Member

Choose a reason for hiding this comment

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

That's better, yes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure what I did yesterday, I guess I was too tired 😂😂


# discrete univariate
function convolve(d1::Bernoulli, d2::Bernoulli)
Expand Down Expand Up @@ -61,27 +69,9 @@ function convolve(d1::Gamma, d2::Gamma)
end

# continuous multivariate
# The first two methods exist for performance reasons to avoid unnecessarily converting
# PDMats to a Matrix
function convolve(
d1::Union{IsoNormal, ZeroMeanIsoNormal, DiagNormal, ZeroMeanDiagNormal},
d2::Union{IsoNormal, ZeroMeanIsoNormal, DiagNormal, ZeroMeanDiagNormal},
)
_check_convolution_shape(d1, d2)
return MvNormal(d1.μ .+ d2.μ, d1.Σ + d2.Σ)
end

function convolve(
d1::Union{FullNormal, ZeroMeanFullNormal},
d2::Union{FullNormal, ZeroMeanFullNormal},
)
_check_convolution_shape(d1, d2)
return MvNormal(d1.μ .+ d2.μ, d1.Σ.mat + d2.Σ.mat)
end

function convolve(d1::MvNormal, d2::MvNormal)
_check_convolution_shape(d1, d2)
return MvNormal(d1.μ .+ d2.μ, Matrix(d1.Σ) + Matrix(d2.Σ))
return MvNormal(d1.μ + d2.μ, d1.Σ + d2.Σ)
end


Expand Down
Loading