Skip to content

Commit

Permalink
enforce naming consistencies for Frechet
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Aug 2, 2015
1 parent 6bef4ba commit 72e872d
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions src/univariate/continuous/frechet.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
= 1.0 / d.α
return d.β^2 * (gamma(1.0 - 2.0 * iα) - gamma(1.0 - iα)^2)
else
= 1.0 / d.α
return d.θ^2 * (gamma(1.0 - 2.0 * iα) - gamma(1.0 - iα)^2)
else
return Inf
end
end
Expand All @@ -60,46 +61,44 @@ 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
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.α)

0 comments on commit 72e872d

Please sign in to comment.