From 72e872d33b0d0d26284d757e6ba6d9bf67388c32 Mon Sep 17 00:00:00 2001 From: Dahua Lin Date: Sun, 2 Aug 2015 17:53:35 +0800 Subject: [PATCH] enforce naming consistencies for Frechet --- src/univariate/continuous/frechet.jl | 59 ++++++++++++++-------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/univariate/continuous/frechet.jl b/src/univariate/continuous/frechet.jl index f0faf2e84..37878467d 100644 --- a/src/univariate/continuous/frechet.jl +++ b/src/univariate/continuous/frechet.jl @@ -1,10 +1,11 @@ immutable Frechet <: ContinuousUnivariateDistribution α::Float64 - β::Float64 + θ::Float64 - function Frechet(α::Real, β::Real) - α > zero(α) && β > zero(β) || error("Both shape and scale must be positive") - @compat new(Float64(α), Float64(β)) + function Frechet(α::Real, θ::Real) + α > zero(α) && θ > zero(θ) || + throw(ArgumentError("Frechet: both α and θ must be positive.")) + @compat new(Float64(α), Float64(θ)) end Frechet(α::Real) = Frechet(α, 1.0) @@ -17,23 +18,23 @@ end #### Parameters shape(d::Frechet) = d.α -scale(d::Frechet) = d.β -params(d::Frechet) = (d.α, d.β) +scale(d::Frechet) = d.θ +params(d::Frechet) = (d.α, d.θ) #### Statistics -mean(d::Frechet) = (α = d.α; α > 1.0 ? d.β * gamma(1.0 - 1.0 / α) : Inf) +mean(d::Frechet) = (α = d.α; α > 1.0 ? d.θ * gamma(1.0 - 1.0 / α) : Inf) -median(d::Frechet) = d.β * logtwo^(-1.0 / d.α) +median(d::Frechet) = d.θ * logtwo^(-1.0 / d.α) -mode(d::Frechet) = (iα = -1.0/d.α; d.β * (1.0 - iα) ^ iα) +mode(d::Frechet) = (iα = -1.0/d.α; d.θ * (1.0 - iα) ^ iα) function var(d::Frechet) if d.α > 2.0 - iα = 1.0 / d.α - return d.β^2 * (gamma(1.0 - 2.0 * iα) - gamma(1.0 - iα)^2) - else + iα = 1.0 / d.α + return d.θ^2 * (gamma(1.0 - 2.0 * iα) - gamma(1.0 - iα)^2) + else return Inf end end @@ -60,22 +61,22 @@ function kurtosis(d::Frechet) return (g4 - 4.0 * g3 * g1 + 3 * g2^2) / ((g2 - g1^2)^2) - 6.0 else return Inf - end + end end function entropy(d::Frechet) const γ = 0.57721566490153286060 # γ is the Euler-Mascheroni constant - 1.0 + γ / d.α + γ + log(d.β / d.α) + 1.0 + γ / d.α + γ + log(d.θ / d.α) end #### Evaluation function logpdf(d::Frechet, x::Float64) - (α, β) = params(d) + (α, θ) = params(d) if x > 0.0 - z = β / x - return log(α / β) + (1.0 + α) * log(z) - z^α + z = θ / x + return log(α / θ) + (1.0 + α) * log(z) - z^α else return -Inf end @@ -83,23 +84,21 @@ end pdf(d::Frechet, x::Float64) = exp(logpdf(d, x)) -cdf(d::Frechet, x::Float64) = x > 0.0 ? exp(-((d.β / x) ^ d.α)) : 0.0 -ccdf(d::Frechet, x::Float64) = x > 0.0 ? -expm1(-((d.β / x) ^ d.α)) : 1.0 -logcdf(d::Frechet, x::Float64) = x > 0.0 ? -(d.β / x) ^ d.α : -Inf -logccdf(d::Frechet, x::Float64) = x > 0.0 ? log1mexp(-((d.β / x) ^ d.α)) : 0.0 +cdf(d::Frechet, x::Float64) = x > 0.0 ? exp(-((d.θ / x) ^ d.α)) : 0.0 +ccdf(d::Frechet, x::Float64) = x > 0.0 ? -expm1(-((d.θ / x) ^ d.α)) : 1.0 +logcdf(d::Frechet, x::Float64) = x > 0.0 ? -(d.θ / x) ^ d.α : -Inf +logccdf(d::Frechet, x::Float64) = x > 0.0 ? log1mexp(-((d.θ / x) ^ d.α)) : 0.0 -quantile(d::Frechet, p::Float64) = d.β * (-log(p)) ^ (-1.0 / d.α) -cquantile(d::Frechet, p::Float64) = d.β * (-log1p(-p)) ^ (-1.0 / d.α) -invlogcdf(d::Frechet, lp::Float64) = d.β * (-lp)^(-1.0 / d.α) -invlogccdf(d::Frechet, lp::Float64) = d.β * (-log1mexp(lp))^(-1.0 / d.α) +quantile(d::Frechet, p::Float64) = d.θ * (-log(p)) ^ (-1.0 / d.α) +cquantile(d::Frechet, p::Float64) = d.θ * (-log1p(-p)) ^ (-1.0 / d.α) +invlogcdf(d::Frechet, lp::Float64) = d.θ * (-lp)^(-1.0 / d.α) +invlogccdf(d::Frechet, lp::Float64) = d.θ * (-log1mexp(lp))^(-1.0 / d.α) function gradlogpdf(d::Frechet, x::Float64) - (α, β) = params(d) - insupport(Frechet, x) ? -(α + 1.0) / x + α * (β^α) * x^(-α-1.0) : 0.0 + (α, θ) = params(d) + insupport(Frechet, x) ? -(α + 1.0) / x + α * (θ^α) * x^(-α-1.0) : 0.0 end ## Sampling -rand(d::Frechet) = d.β * randexp() ^ (-1.0 / d.α) - - +rand(d::Frechet) = d.θ * randexp() ^ (-1.0 / d.α)