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
Closed
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions src/convolution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and one of
* [`NegativeBinomial`](@ref)
* [`Geometric`](@ref)
* [`Poisson`](@ref)
* [`DiscreteNonParametric`](@ref)
* [`Normal`](@ref)
* [`Cauchy`](@ref)
* [`Chisq`](@ref)
Expand Down Expand Up @@ -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

function convolve(d1::DiscreteNonParametric, d2::DiscreteNonParametric)
support_conv = collect(Set(s1 + s2 for s1 in support(d1), s2 in support(d2)))
sort!(support_conv) #for fast index finding below
probs1 = probs(d1)
probs2 = probs(d2)
p_conv = zeros(Base.promote_eltype(probs1, probs2), length(support_conv))
for (s1, p1) in zip(support(d1), probs(d1)), (s2, p2) in zip(support(d2), probs(d2))
idx = searchsortedfirst(support_conv, s1+s2)
iampritishpatil marked this conversation as resolved.
Show resolved Hide resolved
p_conv[idx] += p1*p2
end
DiscreteNonParametric(support_conv, p_conv,check_args=false)
end

# continuous univariate
convolve(d1::Normal, d2::Normal) = Normal(d1.μ + d2.μ, hypot(d1.σ, d2.σ))
convolve(d1::Cauchy, d2::Cauchy) = Cauchy(d1.μ + d2.μ, d1.σ + d2.σ)
Expand Down
21 changes: 21 additions & 0 deletions test/convolution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ using Test
@test d3 isa Poisson
@test d3.λ == 0.5
end

@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))

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))


d_int_simple = @inferred(convolve(d1, d2))
@test d_int_simple isa DiscreteNonParametric
@test support(d_int_simple) == [1,2,3]
@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.

@test probs(d_float_supp)[3] == 3//16
end

end

@testset "continuous univariate" begin
Expand Down