Skip to content

Commit

Permalink
Enforce naming consistencies for Gamma, Gumbel, InverseGamma, and Inv…
Browse files Browse the repository at this point in the history
…erseGaussian
  • Loading branch information
lindahua committed Aug 2, 2015
1 parent 72e872d commit fb5577f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 55 deletions.
39 changes: 20 additions & 19 deletions src/univariate/continuous/gamma.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
immutable Gamma <: ContinuousUnivariateDistribution
α::Float64
β::Float64
θ::Float64

function Gamma::Real, β::Real)
α > zero(α) && β > zero(β) || error("Gamma: both shape and scale must be positive")
@compat new(Float64(α), Float64(β))
function Gamma::Real, θ::Real)
α > zero(α) && θ > zero(θ) ||
throw(ArgumentError("Gamma: both α and Θ must be positive"))
@compat new(Float64(α), Float64(θ))
end

Gamma::Real) = Gamma(α, 1.0)
Gamma::Real) = @compat Gamma(Float64(α), 1.0)
Gamma() = new(1.0, 1.0)
end

Expand All @@ -17,45 +18,45 @@ end
#### Parameters

shape(d::Gamma) = d.α
scale(d::Gamma) = d.β
rate(d::Gamma) = 1.0 / d.β
scale(d::Gamma) = d.θ
rate(d::Gamma) = 1.0 / d.θ

params(d::Gamma) = (d.α, d.β)
params(d::Gamma) = (d.α, d.θ)


#### Statistics

mean(d::Gamma) = d.α * d.β
mean(d::Gamma) = d.α * d.θ

var(d::Gamma) = d.α * d.β^2
var(d::Gamma) = d.α * d.θ^2

skewness(d::Gamma) = 2.0 / sqrt(d.α)

kurtosis(d::Gamma) = 6.0 / d.α

function mode(d::Gamma)
(α, β) = params(d)
α >= 1.0 ? β *- 1.0) : error("Gamma has no mode when shape < 1.0")
(α, θ) = params(d)
α >= 1.0 ? θ *- 1.0) : error("Gamma has no mode when shape < 1.0")
end

function entropy(d::Gamma)
(α, β) = params(d)
α + lgamma(α) + (1.0 - α) * digamma(α) + log(β)
(α, θ) = params(d)
α + lgamma(α) + (1.0 - α) * digamma(α) + log(θ)
end

mgf(d::Gamma, t::Real) = (1.0 - t * d.β)^(-d.α)
mgf(d::Gamma, t::Real) = (1.0 - t * d.θ)^(-d.α)

cf(d::Gamma, t::Real) = (1.0 - im * t * d.β)^(-d.α)
cf(d::Gamma, t::Real) = (1.0 - im * t * d.θ)^(-d.α)


#### Evaluation & Sampling

@_delegate_statsfuns Gamma gamma α β
@_delegate_statsfuns Gamma gamma α θ

gradlogpdf(d::Gamma, x::Float64) =
insupport(Gamma, x) ? (d.α - 1.0) / x - 1.0 / d.β : 0.0
insupport(Gamma, x) ? (d.α - 1.0) / x - 1.0 / d.θ : 0.0

rand(d::Gamma) = StatsFuns.Rmath.gammarand(d.α, d.β)
rand(d::Gamma) = StatsFuns.Rmath.gammarand(d.α, d.θ)


#### Fit model
Expand Down
39 changes: 19 additions & 20 deletions src/univariate/continuous/gumbel.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
immutable Gumbel <: ContinuousUnivariateDistribution
μ::Float64 # location
β::Float64 # scale
μ::Float64 # location
θ::Float64 # scale

function Gumbel::Real, β::Real)
β > zero(β) || error("The scale of Gumbel must be positive")
@compat new(Float64(μ), Float64(β))
function Gumbel::Real, θ::Real)
θ > zero(θ) ||
throw(ArgumentError("Gumbel: Θ must be positive."))
@compat new(Float64(μ), Float64(θ))
end

Gumbel::Real) = Gumbel(μ, 1.0)
Gumbel::Real) = @compat Gumbel(Float64(μ), 1.0)
Gumbel() = new(0.0, 1.0)
end

Expand All @@ -19,52 +20,50 @@ const DoubleExponential = Gumbel
#### Parameters

location(d::Gumbel) = d.μ
scale(d::Gumbel) = d.β
params(d::Gumbel) = (d.μ, d.β)
scale(d::Gumbel) = d.θ
params(d::Gumbel) = (d.μ, d.θ)


#### Statistics

mean(d::Gumbel) = d.μ + d.β * 0.57721566490153286
mean(d::Gumbel) = d.μ + d.θ * 0.57721566490153286

median(d::Gumbel) = d.μ + d.β * 0.366512920581664327
median(d::Gumbel) = d.μ + d.θ * 0.366512920581664327

mode(d::Gumbel) = d.μ

var(d::Gumbel) = 1.6449340668482264 * d.β^2
var(d::Gumbel) = 1.6449340668482264 * d.θ^2

skewness(d::Gumbel) = 1.13954709940464866

kurtosis(d::Gumbel) = 2.4

entropy(d::Gumbel) = 1.57721566490153286 + log(d.β)
entropy(d::Gumbel) = 1.57721566490153286 + log(d.θ)


#### Evaluation

zval(d::Gumbel, x::Float64) = (x - d.μ) / d.β
xval(d::Gumbel, z::Float64) = x * d.β + d.μ
zval(d::Gumbel, x::Float64) = (x - d.μ) / d.θ
xval(d::Gumbel, z::Float64) = x * d.θ + d.μ

function pdf(d::Gumbel, x::Float64)
z = zval(d, x)
exp(-z - exp(-z)) / d.β
exp(-z - exp(-z)) / d.θ
end

function logpdf(d::Gumbel, x::Float64)
z = zval(d, x)
- (z + exp(-z) + log(d.β))
- (z + exp(-z) + log(d.θ))
end

cdf(d::Gumbel, x::Float64) = exp(-exp(-zval(d, x)))
logcdf(d::Gumbel, x::Float64) = -exp(-zval(d, x))

quantile(d::Gumbel, p::Float64) = d.μ - d.β * log(-log(p))
quantile(d::Gumbel, p::Float64) = d.μ - d.θ * log(-log(p))

gradlogpdf(d::Gumbel, x::Float64) = - (1.0 + exp((d.μ - x) / d.β)) / d.β
gradlogpdf(d::Gumbel, x::Float64) = - (1.0 + exp((d.μ - x) / d.θ)) / d.θ


#### Sampling

rand(d::Gumbel) = quantile(d, rand())


28 changes: 14 additions & 14 deletions src/univariate/continuous/inversegamma.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
immutable InverseGamma <: ContinuousUnivariateDistribution
invd::Gamma
β::Float64
θ::Float64

function InverseGamma::Real, β::Real)
> zero(α) && β > zero(β)) || error("Both shape and scale must be positive.")
@compat new(Gamma(α, 1.0 / β), Float64(β))
function InverseGamma::Real, θ::Real)
> zero(α) && θ > zero(θ)) ||
throw(ArgumentError("InverseGamma: both α and θ must be positive."))
@compat new(Gamma(α, 1.0 / θ), Float64(θ))
end

InverseGamma::Real) = InverseGamma(α, 1.0)
InverseGamma::Real) = @compat InverseGamma(Float64(α), 1.0)
InverseGamma() = InverseGamma(1.0, 1.0)
end

Expand All @@ -17,21 +18,21 @@ end
#### Parameters

shape(d::InverseGamma) = shape(d.invd)
scale(d::InverseGamma) = d.β
scale(d::InverseGamma) = d.θ
rate(d::InverseGamma) = scale(d.invd)

params(d::InverseGamma) = (shape(d), scale(d))


#### Parameters

mean(d::InverseGamma) = ((α, β) = params(d); α > 1.0 ? β /- 1.0) : Inf)
mean(d::InverseGamma) = ((α, θ) = params(d); α > 1.0 ? θ /- 1.0) : Inf)

mode(d::InverseGamma) = scale(d) / (shape(d) + 1.0)

function var(d::InverseGamma)
(α, β) = params(d)
α > 2.0 ? β^2 / ((α - 1.0)^2 *- 2.0)) : Inf
(α, θ) = params(d)
α > 2.0 ? θ^2 / ((α - 1.0)^2 *- 2.0)) : Inf
end

function skewness(d::InverseGamma)
Expand All @@ -45,8 +46,8 @@ function kurtosis(d::InverseGamma)
end

function entropy(d::InverseGamma)
(α, β) = params(d)
α + lgamma(α) - (1.0 + α) * digamma(α) + log(β)
(α, θ) = params(d)
α + lgamma(α) - (1.0 + α) * digamma(α) + log(θ)
end


Expand All @@ -55,8 +56,8 @@ end
pdf(d::InverseGamma, x::Float64) = exp(logpdf(d, x))

function logpdf(d::InverseGamma, x::Float64)
(α, β) = params(d)
α * log(β) - lgamma(α) -+ 1.0) * log(x) - β / x
(α, θ) = params(d)
α * log(θ) - lgamma(α) -+ 1.0) * log(x) - θ / x
end

cdf(d::InverseGamma, x::Float64) = ccdf(d.invd, 1.0 / x)
Expand Down Expand Up @@ -92,4 +93,3 @@ function _rand!(d::InverseGamma, A::AbstractArray)
end
A
end

5 changes: 3 additions & 2 deletions src/univariate/continuous/inversegaussian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ immutable InverseGaussian <: ContinuousUnivariateDistribution
λ::Float64

function InverseGaussian::Real, λ::Real)
> zero(μ) && λ > zero(λ)) || error("InverseGaussian's μ and λ must be positive")
> zero(μ) && λ > zero(λ)) ||
throw(ArgumentError("InverseGaussian: μ and λ must be positive."))
@compat new(Float64(μ), Float64(λ))
end

InverseGaussian::Real) = InverseGaussian(μ, 1.0)
InverseGaussian::Real) = @compat InverseGaussian(Float64(μ), 1.0)
InverseGaussian() = new(1.0, 1.0)
end

Expand Down

0 comments on commit fb5577f

Please sign in to comment.