Skip to content

Commit

Permalink
add probs to lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Nov 8, 2014
1 parent b9d6e24 commit 2afa38d
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions src/univariate/poisson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@ end

@_jl_dist_1p Poisson pois

function entropy(d::Poisson)
λ = d.lambda
if λ < 50.0
s = 0.0
for k in 1:100
s += λ^k * lgamma(k + 1.0) / gamma(k + 1.0)
end
return λ * (1.0 - log(λ)) + exp(-λ) * s
else
return 0.5 * log(2 * pi * e * λ) -
(1 / (12 * λ)) -
(1 / (24 * λ * λ)) -
(19 / (360 * λ * λ * λ))
end
end

isupperbounded(::Union(Poisson, Type{Poisson})) = false
islowerbounded(::Union(Poisson, Type{Poisson})) = true
Expand All @@ -35,12 +20,52 @@ maximum(::Union(Poisson, Type{Poisson})) = Inf
insupport(::Poisson, x::Real) = isinteger(x) && zero(x) <= x
insupport(::Type{Poisson}, x::Real) = isinteger(x) && zero(x) <= x

kurtosis(d::Poisson) = 1.0 / d.lambda

function probs(d::Poisson, rgn::UnitRange)
λ = d.lambda
f, l = rgn[1], rgn[end]
0 <= f <= l || throw(BoundsError())
r = Array(Float64, l - f + 1)
v = r[1] = pdf(d, f)
if l > f
b = f - 1
for x = f+1:l
c = λ / x
r[x - b] = (v *= c)
end
end
return r
end

mean(d::Poisson) = d.lambda

median(d::Poisson) = quantile(d, 0.5)

mode(d::Poisson) = ifloor(d.lambda)
modes(d::Poisson) = isinteger(d.lambda) ? [int(d.lambda)-1,int(d.lambda)] : [ifloor(d.lambda)]

var(d::Poisson) = d.lambda

skewness(d::Poisson) = 1.0 / sqrt(d.lambda)

kurtosis(d::Poisson) = 1.0 / d.lambda

function entropy(d::Poisson)
λ = d.lambda
if λ < 50.0
s = 0.0
for k in 1:100
s += λ^k * lgamma(k + 1.0) / gamma(k + 1.0)
end
return λ * (1.0 - log(λ)) + exp(-λ) * s
else
return 0.5 * log(2 * pi * e * λ) -
(1 / (12 * λ)) -
(1 / (24 * λ * λ)) -
(19 / (360 * λ * λ * λ))
end
end

function mgf(d::Poisson, t::Real)
l = d.lambda
return exp(l * (exp(t) - 1.0))
Expand All @@ -51,13 +76,6 @@ function cf(d::Poisson, t::Real)
return exp(l * (exp(im * t) - 1.0))
end

mode(d::Poisson) = ifloor(d.lambda)
modes(d::Poisson) = isinteger(d.lambda) ? [int(d.lambda)-1,int(d.lambda)] : [ifloor(d.lambda)]

skewness(d::Poisson) = 1.0 / sqrt(d.lambda)

var(d::Poisson) = d.lambda

# model fitting

immutable PoissonStats <: SufficientStats
Expand Down

0 comments on commit 2afa38d

Please sign in to comment.