Skip to content

Commit

Permalink
enforce naming consistencies for Normal, Pareto, Rayleigh, and SymTri…
Browse files Browse the repository at this point in the history
…angularDist
  • Loading branch information
lindahua committed Aug 2, 2015
1 parent 983ccb7 commit 19e1618
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 64 deletions.
5 changes: 3 additions & 2 deletions src/univariate/continuous/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ immutable Normal <: ContinuousUnivariateDistribution
σ::Float64

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

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

Expand Down
49 changes: 23 additions & 26 deletions src/univariate/continuous/pareto.jl
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
immutable Pareto <: ContinuousUnivariateDistribution
α::Float64
β::Float64
θ::Float64

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

Pareto::Real) = Pareto(α, 1.0)
Pareto() = new(1.0, 1.0)
end

@distr_support Pareto d.β Inf
@distr_support Pareto d.θ Inf


#### Parameters

shape(d::Pareto) = d.α
scale(d::Pareto) = d.β
scale(d::Pareto) = d.θ

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


#### Statistics

mean(d::Pareto) = ((α, β) = params(d); α > 1.0 ? α * β /- 1.0) : Inf)
median(d::Pareto) = ((α, β) = params(d); β * 2.0 ^ (1.0 / α))
mode(d::Pareto) = d.β
mean(d::Pareto) = ((α, θ) = params(d); α > 1.0 ? α * θ /- 1.0) : Inf)
median(d::Pareto) = ((α, θ) = params(d); θ * 2.0 ^ (1.0 / α))
mode(d::Pareto) = d.θ

function var(d::Pareto)
(α, β) = 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::Pareto)
Expand All @@ -43,42 +43,39 @@ function kurtosis(d::Pareto)
α > 4.0 ? (6.0 *^3 + α^2 - 6.0 * α - 2.0)) /*- 3.0) *- 4.0)) : NaN
end

entropy(d::Pareto) = ((α, β) = params(d); log(β / α) + 1.0 / α + 1.0)
entropy(d::Pareto) = ((α, θ) = params(d); log(θ / α) + 1.0 / α + 1.0)


#### Evaluation

function pdf(d::Pareto, x::Float64)
(α, β) = params(d)
x >= β ? α * (β / x)^α * (1.0 / x) : 0.0
(α, θ) = params(d)
x >= θ ? α * (θ / x)^α * (1.0 / x) : 0.0
end

function logpdf(d::Pareto, x::Float64)
(α, β) = params(d)
x >= β ? log(α) + α * log(β) -+ 1.0) * log(x) : -Inf
(α, θ) = params(d)
x >= θ ? log(α) + α * log(θ) -+ 1.0) * log(x) : -Inf
end

function ccdf(d::Pareto, x::Float64)
(α, β) = params(d)
x >= β ? (β / x)^α : 1.0
(α, θ) = params(d)
x >= θ ? (θ / x)^α : 1.0
end

cdf(d::Pareto, x::Float64) = 1.0 - ccdf(d, x)

function logccdf(d::Pareto, x::Float64)
(α, β) = params(d)
x >= β ? α * log(β / x) : 0.0
(α, θ) = params(d)
x >= θ ? α * log(θ / x) : 0.0
end

logcdf(d::Pareto, x::Float64) = log1p(-ccdf(d, x))

cquantile(d::Pareto, p::Float64) = d.β / p^(1.0 / d.α)
cquantile(d::Pareto, p::Float64) = d.θ / p^(1.0 / d.α)
quantile(d::Pareto, p::Float64) = cquantile(d, 1.0 - p)


#### Sampling

rand(d::Pareto) = d.β * exp(randexp() / d.α)



rand(d::Pareto) = d.θ * exp(randexp() / d.α)
4 changes: 2 additions & 2 deletions src/univariate/continuous/rayleigh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ immutable Rayleigh <: ContinuousUnivariateDistribution
σ::Float64

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

Expand Down Expand Up @@ -57,4 +58,3 @@ quantile(d::Rayleigh, p::Float64) = sqrt(-2.0 * d.σ^2 * log1p(-p))
#### Sampling

rand(d::Rayleigh) = d.σ * sqrt(2.0 * randexp())

67 changes: 33 additions & 34 deletions src/univariate/continuous/symtriangular.jl
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
immutable SymTriangularDist <: ContinuousUnivariateDistribution
μ::Float64
s::Float64
σ::Float64

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

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

@distr_support SymTriangularDist d.μ - d.s d.μ + d.s
@distr_support SymTriangularDist d.μ - d.σ d.μ + d.σ


#### Parameters

location(d::SymTriangularDist) = d.μ
scale(d::SymTriangularDist) = d.s
scale(d::SymTriangularDist) = d.σ

params(d::SymTriangularDist) = (d.μ, d.s)
params(d::SymTriangularDist) = (d.μ, d.σ)


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

var(d::SymTriangularDist) = d.s^2 / 6.0
var(d::SymTriangularDist) = d.σ^2 / 6.0
skewness(d::SymTriangularDist) = 0.0
kurtosis(d::SymTriangularDist) = -0.6

entropy(d::SymTriangularDist) = 0.5 + log(d.s)
entropy(d::SymTriangularDist) = 0.5 + log(d.σ)


#### Evaluation

zval(d::SymTriangularDist, x::Float64) = (x - d.μ) / d.s
xval(d::SymTriangularDist, z::Float64) = d.μ + z * d.s
zval(d::SymTriangularDist, x::Float64) = (x - d.μ) / d.σ
xval(d::SymTriangularDist, z::Float64) = d.μ + z * d.σ


pdf(d::SymTriangularDist, x::Float64) = insupport(d, x) ? (1.0 - abs(zval(d, x))) / scale(d) : 0.0

logpdf(d::SymTriangularDist, x::Float64) = insupport(d, x) ? log((1.0 - abs(zval(d, x))) / scale(d)) : -Inf

function cdf(d::SymTriangularDist, x::Float64)
(μ, s) = params(d)
x <= μ - s ? 0.0 :
(μ, σ) = params(d)
x <= μ - σ ? 0.0 :
x <= μ ? 0.5 * (1.0 + zval(d, x))^2 :
x < μ + s ? 1.0 - 0.5 * (1.0 - zval(d, x))^2 : 1.0
x < μ + σ ? 1.0 - 0.5 * (1.0 - zval(d, x))^2 : 1.0
end

function ccdf(d::SymTriangularDist, x::Float64)
(μ, s) = params(d)
x <= μ - s ? 1.0 :
(μ, σ) = params(d)
x <= μ - σ ? 1.0 :
x <= μ ? 1.0 - 0.5 * (1.0 + zval(d, x))^2 :
x < μ + s ? 0.5 * (1.0 - zval(d, x))^2 : 0.0
x < μ + σ ? 0.5 * (1.0 - zval(d, x))^2 : 0.0
end

function logcdf(d::SymTriangularDist, x::Float64)
(μ, s) = params(d)
x <= μ - s ? -Inf :
(μ, σ) = params(d)
x <= μ - σ ? -Inf :
x <= μ ? loghalf + 2.0 * log1p(zval(d, x)) :
x < μ + s ? log1p(-0.5 * (1.0 - zval(d, x))^2) : 0.0
x < μ + σ ? log1p(-0.5 * (1.0 - zval(d, x))^2) : 0.0
end

function logccdf(d::SymTriangularDist, x::Float64)
(μ, s) = params(d)
x <= μ - s ? 0.0 :
(μ, σ) = params(d)
x <= μ - σ ? 0.0 :
x <= μ ? log1p(-0.5 * (1.0 + zval(d, x))^2) :
x < μ + s ? loghalf + 2.0 * log1p(-zval(d, x)) : -Inf
x < μ + σ ? loghalf + 2.0 * log1p(-zval(d, x)) : -Inf
end

quantile(d::SymTriangularDist, p::Float64) = p < 0.5 ? xval(d, sqrt(2.0 * p) - 1.0) :
quantile(d::SymTriangularDist, p::Float64) = p < 0.5 ? xval(d, sqrt(2.0 * p) - 1.0) :
xval(d, 1.0 - sqrt(2.0 * (1.0 - p)))

cquantile(d::SymTriangularDist, p::Float64) = p > 0.5 ? xval(d, sqrt(2.0 * (1.0-p)) - 1.0) :
xval(d, 1.0 - sqrt(2.0 * p))
xval(d, 1.0 - sqrt(2.0 * p))

invlogcdf(d::SymTriangularDist, lp::Float64) = lp < loghalf ? xval(d, expm1(0.5*(lp - loghalf))) :
xval(d, 1.0 - sqrt(-2.0 * expm1(lp)))
Expand All @@ -86,16 +87,16 @@ invlogccdf(d::SymTriangularDist, lp::Float64) = lp > loghalf ? xval(d, sqrt(-2.0
xval(d, -(expm1(0.5 * (lp - loghalf))))


function mgf(d::SymTriangularDist, t::Real)
(μ, s) = params(d)
a = s * t
function mgf(d::SymTriangularDist, t::Float64)
(μ, σ) = params(d)
a = σ * t
a == zero(a) && return one(a)
4.0 * exp* t) * (sinh(0.5 * a) / a)^2
end

function cf(d::SymTriangularDist, t::Real)
(μ, s) = params(d)
a = s * t
function cf(d::SymTriangularDist, t::Float64)
(μ, σ) = params(d)
a = σ * t
a == zero(a) && return complex(one(a))
4.0 * cis* t) * (sin(0.5 * a) / a)^2
end
Expand All @@ -104,5 +105,3 @@ end
#### Sampling

rand(d::SymTriangularDist) = xval(d, rand() - rand())


0 comments on commit 19e1618

Please sign in to comment.