diff --git a/src/univariate/continuous/gamma.jl b/src/univariate/continuous/gamma.jl index 16d96088c..5b60b6811 100644 --- a/src/univariate/continuous/gamma.jl +++ b/src/univariate/continuous/gamma.jl @@ -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 @@ -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 diff --git a/src/univariate/continuous/gumbel.jl b/src/univariate/continuous/gumbel.jl index 55de73416..2e6128eab 100644 --- a/src/univariate/continuous/gumbel.jl +++ b/src/univariate/continuous/gumbel.jl @@ -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 @@ -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()) - - diff --git a/src/univariate/continuous/inversegamma.jl b/src/univariate/continuous/inversegamma.jl index 4aa6b98ee..5403b660f 100644 --- a/src/univariate/continuous/inversegamma.jl +++ b/src/univariate/continuous/inversegamma.jl @@ -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 @@ -17,7 +18,7 @@ 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)) @@ -25,13 +26,13 @@ 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) @@ -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 @@ -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) @@ -92,4 +93,3 @@ function _rand!(d::InverseGamma, A::AbstractArray) end A end - diff --git a/src/univariate/continuous/inversegaussian.jl b/src/univariate/continuous/inversegaussian.jl index ad51b0998..27be1c222 100644 --- a/src/univariate/continuous/inversegaussian.jl +++ b/src/univariate/continuous/inversegaussian.jl @@ -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