Skip to content

Commit 424b3d4

Browse files
emfelthamdevmotion
andauthored
Apply suggestions from code review
Co-authored-by: David Widmann <[email protected]>
1 parent 2619337 commit 424b3d4

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

src/univariate/discrete/zeroinflatedpoisson.jl

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ end
6464

6565
#### Parameters
6666

67-
import StatsBase:params
68-
6967
params(d::ZeroInflatedPoisson) = (d.λ, d.p,)
7068
partype(::ZeroInflatedPoisson{T}) where {T} = T
7169

@@ -75,16 +73,12 @@ excessprob(d::ZeroInflatedPoisson) = d.p
7573
#### Evaluation
7674

7775
function logpdf(d::ZeroInflatedPoisson, y::Real)
78-
LL = 0.0
79-
if y == 0
80-
LLs = zeros(typeof(log(d.λ)), 2)
81-
LLs[1] = log(d.p)
82-
LLs[2] = log1p(-d.p) - d.λ
83-
LL = logsumexp(LLs)
84-
else
85-
LL = log1p(-d.p) + logpdf(Poisson(d.λ), y)
86-
end
87-
return LL
76+
lp = if iszero(y)
77+
logaddexp(log(d.p), log1p(-d.p) - d.λ)
78+
else
79+
log1p(-d.p) + logpdf(Poisson(d.λ), y)
80+
end
81+
return lp
8882
end
8983

9084
function cdf(d::ZeroInflatedPoisson, x::Real)
@@ -119,13 +113,6 @@ function quantile(d::ZeroInflatedPoisson, q::Real)
119113
return out
120114
end
121115

122-
## Sampling
123-
124-
function rand(rng::AbstractRNG, d::ZeroInflatedPoisson)
125-
zo = rand(rng, Uniform(0, 1))
126-
return quantile(d, zo)
127-
end
128-
129116
#### Fitting
130117

131118
struct ZeroInflatedPoissonStats <: SufficientStats
@@ -136,7 +123,7 @@ end
136123

137124
suffstats(::Type{<:ZeroInflatedPoisson}, x::AbstractArray{T}) where {T<:Integer} = ZeroInflatedPoissonStats(
138125
sum(x),
139-
sum(x .== 0) / length(x),
126+
mean(iszero, x),
140127
length(x)
141128
)
142129

0 commit comments

Comments
 (0)