Skip to content

Commit

Permalink
Fix pdf and logpdf of PoissonBinomial (#1345)
Browse files Browse the repository at this point in the history
* Fix `pdf` and `logpdf`  of `PoissonBinomial`

* Bump version
  • Loading branch information
devmotion authored Jun 16, 2021
1 parent 6c25150 commit 0ac7c42
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
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.3"
version = "0.25.4"

[deps]
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
Expand Down
8 changes: 4 additions & 4 deletions src/univariate/discrete/poissonbinomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ function cf(d::PoissonBinomial, t::Real)
end
end

pdf(d::PoissonBinomial, k::Real) = insupport(d, k) ? d.pmf[k+1] : zero(eltype(d.pmf))
pdf(d::PoissonBinomial, k::Real) = insupport(d, k) ? d.pmf[Int(k+1)] : zero(eltype(d.pmf))
logpdf(d::PoissonBinomial, k::Real) = log(pdf(d, k))

# Computes the pdf of a poisson-binomial random variable using
# simple, fast recursive formula
#
# Marlin A. Thomas & Audrey E. Taub (1982)
# Calculating binomial probabilities when the trial probabilities are unequal,
# Journal of Statistical Computation and Simulation, 14:2, 125-131, DOI: 10.1080/00949658208810534
# Marlin A. Thomas & Audrey E. Taub (1982)
# Calculating binomial probabilities when the trial probabilities are unequal,
# Journal of Statistical Computation and Simulation, 14:2, 125-131, DOI: 10.1080/00949658208810534
#
function poissonbinomial_pdf(p)
S = zeros(eltype(p), length(p) + 1)
Expand Down
13 changes: 10 additions & 3 deletions test/poissonbinomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ for (p, n) in [(0.8, 6), (0.5, 10), (0.04, 20)]
@test @inferred(quantile(d, i)) quantile(dref, i)
end
for i=0:n
@test isapprox(@inferred(cdf(d, i)), cdf(dref, i), atol=1e-15)
@test isapprox(@inferred(pdf(d, i)), pdf(dref, i), atol=1e-15)
@test @inferred(cdf(d, i)) cdf(dref, i) atol=1e-15
@test @inferred(cdf(d, i//1)) cdf(dref, i) atol=1e-15
@test @inferred(pdf(d, i)) pdf(dref, i) atol=1e-15
@test @inferred(pdf(d, i//1)) pdf(dref, i) atol=1e-15
@test @inferred(logpdf(d, i)) logpdf(dref, i)
@test @inferred(logpdf(d, i//1)) logpdf(dref, i)
end

end
Expand Down Expand Up @@ -105,7 +109,10 @@ for (n₁, n₂, n₃, p₁, p₂, p₃) in [(10, 10, 10, 0.1, 0.5, 0.9),
end
m += pmf1[i+1] * mc
end
@test isapprox(@inferred(pdf(d, k)), m, atol=1e-15)
@test @inferred(pdf(d, k)) m atol=1e-15
@test @inferred(pdf(d, k//1)) m atol=1e-15
@test @inferred(logpdf(d, k)) log(m)
@test @inferred(logpdf(d, k//1)) log(m)
end
end

Expand Down

0 comments on commit 0ac7c42

Please sign in to comment.