Skip to content

Commit

Permalink
Enforce naming consistency for Laplace, Levy, and Logistic
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Aug 2, 2015
1 parent fb5577f commit d5aa106
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 68 deletions.
34 changes: 16 additions & 18 deletions src/univariate/continuous/laplace.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
immutable Laplace <: ContinuousUnivariateDistribution
μ::Float64
β::Float64
θ::Float64

function Laplace::Real, β::Real)
β > zero(β) || error("Laplace's scale must be positive")
@compat new(Float64(μ), Float64(β))
function Laplace::Real, θ::Real)
θ > zero(θ) || error("Laplace's scale must be positive")
@compat new(Float64(μ), Float64(θ))
end

@compat Laplace::Real) = new(Float64(μ), 1.0)
Expand All @@ -19,8 +19,8 @@ typealias Biexponential Laplace
#### Parameters

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


#### Statistics
Expand All @@ -29,18 +29,18 @@ mean(d::Laplace) = d.μ
median(d::Laplace) = d.μ
mode(d::Laplace) = d.μ

var(d::Laplace) = 2.0 * d.β^2
std(d::Laplace) = sqrt2 * d.β
var(d::Laplace) = 2.0 * d.θ^2
std(d::Laplace) = sqrt2 * d.θ
skewness(d::Laplace) = 0.0
kurtosis(d::Laplace) = 3.0

entropy(d::Laplace) = log(2.0 * d.β) + 1.0
entropy(d::Laplace) = log(2.0 * d.θ) + 1.0


#### Evaluations

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

pdf(d::Laplace, x::Float64) = 0.5 * exp(-abs(zval(d, x))) / scale(d)
logpdf(d::Laplace, x::Float64) = - (abs(zval(d, x)) + log(2.0 * scale(d)))
Expand All @@ -56,25 +56,25 @@ invlogcdf(d::Laplace, lp::Float64) = lp < loghalf ? xval(d, logtwo + lp) : xval(
invlogccdf(d::Laplace, lp::Float64) = lp > loghalf ? xval(d, logtwo + log1mexp(lp)) : xval(d, -(logtwo + lp))

function gradlogpdf(d::Laplace, x::Float64)
μ, β = params(d)
μ, θ = params(d)
x == μ && error("Gradient is undefined at the location point")
g = 1.0 / β
g = 1.0 / θ
x > μ ? -g : g
end

function mgf(d::Laplace, t::Real)
st = d.β * t
st = d.θ * t
exp(t * d.μ) / ((1.0 - st) * (1.0 + st))
end
function cf(d::Laplace, t::Real)
st = d.β * t
st = d.θ * t
cis(t * d.μ) / (1+st*st)
end


#### Sampling

rand(d::Laplace) = d.μ + d.β*randexp()*ifelse(rand(Bool), 1, -1)
rand(d::Laplace) = d.μ + d.θ*randexp()*ifelse(rand(Bool), 1, -1)


#### Fitting
Expand All @@ -83,5 +83,3 @@ function fit_mle(::Type{Laplace}, x::Array)
a = median(x)
Laplace(a, mad(x, a))
end


50 changes: 22 additions & 28 deletions src/univariate/continuous/levy.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
immutable Levy <: ContinuousUnivariateDistribution
μ::Float64
c::Float64
σ::Float64

function Levy::Real, c::Real)
c >= zero(c) || error("scale must be non-negative")
@compat new(Float64(μ), Float64(c))
function Levy::Real, σ::Real)
σ > zero(σ) ||
throw(ArgumentError("Levy: σ must be positive."))
@compat new(Float64(μ), Float64(σ))
end

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

@distr_support Levy location(d) Inf
@distr_support Levy d.μ Inf


#### Parameters

location(d::Levy) = d.μ
params(d::Levy) = (d.μ, d.c)
params(d::Levy) = (d.μ, d.σ)


#### Statistics
Expand All @@ -27,48 +28,41 @@ var(d::Levy) = Inf
skewness(d::Levy) = NaN
kurtosis(d::Levy) = NaN

mode(d::Levy) = d.c / 3.0 + d.μ
mode(d::Levy) = d.σ / 3.0 + d.μ

function entropy(d::Levy)
c = scale(d)
(1.0 - 3.0 * digamma(1.0) + log(16.0 * pi * c * c)) / 2.0
end
entropy(d::Levy) = (1.0 - 3.0 * digamma(1.0) + log(16π * d.σ^2)) / 2.0

function median(d::Levy)
μ, c = params(d)
μ + c / (2.0 * erfcinv(0.5)^2)
end
median(d::Levy) = d.μ + d.σ / 0.4549364231195728 # 0.454... = (2.0 * erfcinv(0.5)^2)


#### Evaluation

function pdf(d::Levy, x::Float64)
μ, c = params(d)
μ, σ = params(d)
z = x - μ
(sqrt(c) / sqrt2π) * exp((-c) / (2.0 * z)) / z^1.5
(sqrt(σ) / sqrt2π) * exp((-σ) / (2.0 * z)) / z^1.5
end

function logpdf(d::Levy, x::Float64)
μ, c = params(d)
μ, σ = params(d)
z = x - μ
0.5 * (log(c) - log2π - c / z - 3.0 * log(z))
0.5 * (log(σ) - log2π - σ / z - 3.0 * log(z))
end

cdf(d::Levy, x::Float64) = erfc(sqrt(d.c / (2.0 * (x - d.μ))))
ccdf(d::Levy, x::Float64) = erf(sqrt(d.c / (2.0 * (x - d.μ))))
cdf(d::Levy, x::Float64) = erfc(sqrt(d.σ / (2.0 * (x - d.μ))))
ccdf(d::Levy, x::Float64) = erf(sqrt(d.σ / (2.0 * (x - d.μ))))

quantile(d::Levy, p::Float64) = d.μ + d.c / (2.0 * erfcinv(p)^2)
cquantile(d::Levy, p::Float64) = d.μ + d.c / (2.0 * erfinv(p)^2)
quantile(d::Levy, p::Float64) = d.μ + d.σ / (2.0 * erfcinv(p)^2)
cquantile(d::Levy, p::Float64) = d.μ + d.σ / (2.0 * erfinv(p)^2)

mgf(d::Levy, t::Real) = t == zero(t) ? 1.0 : NaN

function cf(d::Levy, t::Real)
μ, c = params(d)
exp(im * μ * t - sqrt(-2.0 * im * c * t))
μ, σ = params(d)
exp(im * μ * t - sqrt(-2.0 * im * σ * t))
end


#### Sampling

rand(d::Levy) = d.μ + d.c / randn()^2

rand(d::Levy) = d.μ + d.σ / randn()^2
42 changes: 21 additions & 21 deletions src/univariate/continuous/logistic.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
immutable Logistic <: ContinuousUnivariateDistribution
μ::Float64
β::Float64
θ::Float64

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

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

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

location(d::Logistic) = d.μ
scale(d::Logistic) = d.β
scale(d::Logistic) = d.θ

params(d::Logistic) = (d.μ, d.β)
params(d::Logistic) = (d.μ, d.θ)


#### Statistics
Expand All @@ -28,46 +29,45 @@ mean(d::Logistic) = d.μ
median(d::Logistic) = d.μ
mode(d::Logistic) = d.μ

std(d::Logistic) = π * d.β / sqrt3
var(d::Logistic) =* d.β)^2 / 3.0
std(d::Logistic) = π * d.θ / sqrt3
var(d::Logistic) =* d.θ)^2 / 3.0
skewness(d::Logistic) = 0.0
kurtosis(d::Logistic) = 1.2

entropy(d::Logistic) = log(d.β) + 2.0
entropy(d::Logistic) = log(d.θ) + 2.0


#### Evaluation

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

pdf(d::Logistic, x::Float64) = (e = exp(-zval(d, x)); e / (d.β * (1.0 + e)^2))
logpdf(d::Logistic, x::Float64) = (u = -abs(zval(d, x)); u - 2.0 * log1pexp(u) - log(d.β))
pdf(d::Logistic, x::Float64) = (e = exp(-zval(d, x)); e / (d.θ * (1.0 + e)^2))
logpdf(d::Logistic, x::Float64) = (u = -abs(zval(d, x)); u - 2.0 * log1pexp(u) - log(d.θ))

cdf(d::Logistic, x::Float64) = logistic(zval(d, x))
ccdf(d::Logistic, x::Float64) = logistic(-zval(d, x))
logcdf(d::Logistic, x::Float64) = -log1pexp(-zval(d, x))
logccdf(d::Logistic, x::Float64) = -log1pexp(zval(d, x))

quantile(d::Logistic, p::Float64) = xval(d, logit(p))
cquantile(d::Logistic, p::Float64) = xval(d, -logit(p))
invlogcdf(d::Logistic, lp::Float64) = xval(d, -logexpm1(-lp))
invlogccdf(d::Logistic, lp::Float64) = xval(d, logexpm1(-lp))
cquantile(d::Logistic, p::Float64) = xval(d, -logit(p))
invlogcdf(d::Logistic, lp::Float64) = xval(d, -logexpm1(-lp))
invlogccdf(d::Logistic, lp::Float64) = xval(d, logexpm1(-lp))

function gradlogpdf(d::Logistic, x::Float64)
e = exp(-zval(d, x))
((2.0 * e) / (1.0 + e) - 1.0) / d.β
((2.0 * e) / (1.0 + e) - 1.0) / d.θ
end

mgf(d::Logistic, t::Real) = exp(t * d.μ) / sinc(d.β * t)
mgf(d::Logistic, t::Real) = exp(t * d.μ) / sinc(d.θ * t)

function cf(d::Logistic, t::Real)
a =* t) * d.β
a =* t) * d.θ
a == zero(a) ? complex(one(a)) : cis(t * d.μ) * (a / sinh(a))
end


#### Sampling

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

3 changes: 2 additions & 1 deletion src/univariate/continuous/lognormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ immutable LogNormal <: ContinuousUnivariateDistribution
σ::Float64

function LogNormal::Real, σ::Real)
σ > zero(σ) || error("σ must be positive")
σ > zero(σ) ||
throw(ArgumentError("LogNormal: σ must be positive."))
@compat new(Float64(μ), Float64(σ))
end

Expand Down

0 comments on commit d5aa106

Please sign in to comment.