-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathpoisson.jl
44 lines (31 loc) · 1.02 KB
/
poisson.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Poisson distribution
export Poisson
import Base
using SpecialFunctions: loggamma
@parameterized Poisson()
@kwstruct Poisson()
@kwstruct Poisson(λ)
Poisson(λ) = Poisson((λ = λ,))
basemeasure(::Poisson) = Counting(BoundedInts(static(0), static(Inf)))
@inline function logdensity_def(d::Poisson{()}, y::T) where {T}
return y - one(T) - loggamma(one(T) + y)
end
@inline function logdensity_def(d::Poisson{(:λ,)}, y)
λ = d.λ
return y * log(λ) - λ - loggamma(1 + y)
end
@kwstruct Poisson(logλ)
@inline function logdensity_def(d::Poisson{(:logλ,)}, y)
return y * d.logλ - exp(d.logλ) - loggamma(1 + y)
end
gentype(::Poisson) = Int
Base.rand(rng::AbstractRNG, T::Type, d::Poisson{(:λ,)}) = rand(rng, Dists.Poisson(d.λ))
function Base.rand(rng::AbstractRNG, T::Type, d::Poisson{(:logλ,)})
rand(rng, Dists.Poisson(exp(d.logλ)))
end
mean(d::Poisson{(:λ,)}) = d.λ
std(d::Poisson{(:λ,)}) = sqrt(d.λ)
var(d::Poisson{(:λ,)}) = d.λ
@inline function insupport(::Poisson, x)
isinteger(x) && x ≥ 0
end