Skip to content

Commit

Permalink
direct implementation of Erlang
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Sep 5, 2015
1 parent e55560e commit 9c891f0
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions src/univariate/continuous/erlang.jl
Original file line number Diff line number Diff line change
@@ -1,57 +1,47 @@
immutable Erlang <: ContinuousUnivariateDistribution
gammad::Gamma
α::Int
θ::Float64

function Erlang::Real, θ::Real)
@check_args(Erlang, isinteger(α) && α >= zero(α))
new(Gamma(α, θ))
new(α, θ)
end
Erlang::Real) = Erlang(α, 1.0)
Erlang() = new(Gamma())
Erlang() = new(1, 1.0)
end

@distr_support Erlang 0.0 Inf

show(io::IO, d::Erlang) = ((α, β) = params(d); show_oneline(io, d, [(, α), (, β)]))

#### Parameters

shape(d::Erlang) = round(Int, shape(d.gammad))
scale(d::Erlang) = scale(d.gammad)
rate(d::Erlang) = rate(d.gammad)
params(d::Erlang) = ((α, β) = params(d.gammad); (round(Int, α), β))

shape(d::Erlang) = d.α
scale(d::Erlang) = d.θ
rate(d::Erlang) = inv(d.θ)
params(d::Erlang) = (d.α, d.θ)

#### Statistics

mean(d::Erlang) = mean(d.gammad)
var(d::Erlang) = var(d.gammad)
median(d::Erlang) = median(d.gammad)
skewness(d::Erlang) = skewness(d.gammad)
kurtosis(d::Erlang) = kurtosis(d.gammad)

mode(d::Erlang) = mode(d.gammad)
entropy(d::Erlang) = entropy(d.gammad)

mean(d::Erlang) = d.α * d.θ
var(d::Erlang) = d.α * d.θ^2
skewness(d::Erlang) = 2.0 / sqrt(d.α)
kurtosis(d::Erlang) = 6.0 / d.α

#### Evaluation

pdf(d::Erlang, x::Float64) = pdf(d.gammad, x)
logpdf(d::Erlang, x::Float64) = logpdf(d.gammad, x)
function mode(d::Erlang)
(α, θ) = params(d)
α >= 1.0 ? θ *- 1.0) : error("Erlang has no mode when α < 1.0")
end

cdf(d::Erlang, x::Float64) = cdf(d.gammad, x)
ccdf(d::Erlang, x::Float64) = ccdf(d.gammad, x)
logcdf(d::Erlang, x::Float64) = logcdf(d.gammad, x)
logccdf(d::Erlang, x::Float64) = logccdf(d.gammad, x)
function entropy(d::Erlang)
(α, θ) = params(d)
α + lgamma(α) + (1.0 - α) * digamma(α) + log)
end

quantile(d::Erlang, p::Float64) = quantile(d.gammad, p)
cquantile(d::Erlang, p::Float64) = cquantile(d.gammad, p)
invlogcdf(d::Erlang, lp::Float64) = invlogcdf(d.gammad, lp)
invlogccdf(d::Erlang, lp::Float64) = invlogccdf(d.gammad, lp)
mgf(d::Erlang, t::Real) = (1.0 - t * d.θ)^(-d.α)
cf(d::Erlang, t::Real) = (1.0 - im * t * d.θ)^(-d.α)

mgf(d::Erlang, t::Real) = mgf(d.gammad, t)
cf(d::Erlang, t::Real) = cf(d.gammad, t)

#### Evaluation & Sampling

#### Sampling
@_delegate_statsfuns Erlang gamma α θ

rand(d::Erlang) = rand(d.gammad)
rand(d::Erlang) = StatsFuns.Rmath.gammarand(d.α, d.θ)

0 comments on commit 9c891f0

Please sign in to comment.