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 convolve for DiscreteNonParametric #1523

Closed
wants to merge 7 commits into from

Conversation

iampritishpatil
Copy link
Contributor

DiscreteNonParametric convolution has a very nice trivial closed form. It was not implemented.

This pull request implements it.

DiscreteNonParametric convolution has a very nice trivial closed form. It was not implemented.

This pull request implements it.
Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! I made some suggestions. Can you also add some tests, also of more numerically tricky edge cases?

src/convolution.jl Outdated Show resolved Hide resolved
@@ -12,13 +12,16 @@ and one of
* [`NegativeBinomial`](@ref)
* [`Geometric`](@ref)
* [`Poisson`](@ref)
* [`DiscreteNonParametric`](@ref)
* [`Normal`](@ref)
* [`Cauchy`](@ref)
* [`Chisq`](@ref)
* [`Exponential`](@ref)
* [`Gamma`](@ref)
* [`MvNormal`](@ref)

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

src/convolution.jl Outdated Show resolved Hide resolved
src/convolution.jl Outdated Show resolved Hide resolved
src/convolution.jl Outdated Show resolved Hide resolved
src/convolution.jl Show resolved Hide resolved
src/convolution.jl Outdated Show resolved Hide resolved
src/convolution.jl Outdated Show resolved Hide resolved
iampritishpatil and others added 5 commits March 23, 2022 09:29
Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
Doesn't preserve the type of the Vector, but perhaps this is better ....

Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
use functions to access the support and probabilities, and write as one loop.

Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
Removed check args:
We know the convovultion is a proper distribution.
@iampritishpatil
Copy link
Contributor Author

Thanks for the suggestions, they were, all on the point.

I think I addressed all of it. If I run into any more floating-point tricky cases, I'll add them, but as far as I can tell, it shouldn't cause errors, apart from any unexpected compiler optimization which causes different things to happen in the two loops.

Let me know if anything more is needed.

@codecov-commenter
Copy link

codecov-commenter commented Mar 25, 2022

Codecov Report

Merging #1523 (f85407a) into master (1c44a2a) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1523      +/-   ##
==========================================
+ Coverage   85.45%   85.47%   +0.02%     
==========================================
  Files         128      128              
  Lines        7818     7829      +11     
==========================================
+ Hits         6681     6692      +11     
  Misses       1137     1137              
Impacted Files Coverage Δ
src/convolution.jl 100.00% <100.00%> (ø)

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 1c44a2a...f85407a. Read the comment docs.

@testset "DiscreteNonParametric" begin
d1 = DiscreteNonParametric([0,1],[0.5,0.5])
d2 = DiscreteNonParametric([1,2],[0.5,0.5])
d_eps= DiscreteNonParametric([-1* eps(Float64),0.0,eps(Float64),1.0],[1//4,1//4,1//4,1//4])
Copy link
Member

Choose a reason for hiding this comment

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

Maybe use

Suggested change
d_eps= DiscreteNonParametric([-1* eps(Float64),0.0,eps(Float64),1.0],[1//4,1//4,1//4,1//4])
d_eps= DiscreteNonParametric([prevfloat(0.0),0.0,nextfloat(0.0),1.0], fill(1//4, 4))

d1 = DiscreteNonParametric([0,1],[0.5,0.5])
d2 = DiscreteNonParametric([1,2],[0.5,0.5])
d_eps= DiscreteNonParametric([-1* eps(Float64),0.0,eps(Float64),1.0],[1//4,1//4,1//4,1//4])
d10= DiscreteNonParametric(collect(1:10)//10,ones(Int,10)//10)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
d10= DiscreteNonParametric(collect(1:10)//10,ones(Int,10)//10)
d10= DiscreteNonParametric(1//10:1//10:1, fill(1//10, 10))

@@ -47,6 +48,20 @@ end
convolve(d1::Poisson, d2::Poisson) = Poisson(d1.λ + d2.λ)



Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

@test probs(d_int_simple) == [0.25,0.5,0.25]

d_rat = convolve(d10, d10)
@test support(d_rat)[1] == 1//5
Copy link
Member

Choose a reason for hiding this comment

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

I guess this could be made more precise?

Suggested change
@test support(d_rat)[1] == 1//5
@test support(d_rat) == 1//5:1//10:2

@test probs(d_rat)[1] == 1//100

d_float_supp = convolve(d_eps, d_eps)
@test support(d_float_supp)[3] == 0.0
Copy link
Member

Choose a reason for hiding this comment

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

I think this will be

Suggested change
@test support(d_float_supp)[3] == 0.0
@test support(d_float_supp) == [2 * prevfloat(0.0), prevfloat(0.0), 0.0, nextfloat(0.0), 2 * nextfloat(0.0), 1.0, 2.0]

since numerically prevfloat(0.0) + 1 = 1 = nextfloat(0.0) + 1.

@mharradon
Copy link
Contributor

I think this would be a nice PR to merge. I implemented the latest suggestions from @devmotion and PR'd at #1850. Happy to either see this PR fixed up or merge that.

Thank you!

@devmotion
Copy link
Member

Fixed by #1850.

@devmotion devmotion closed this May 30, 2024
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.

None yet

4 participants