Skip to content

Commit

Permalink
Enforce naming consistencies for TriangularDist, Triweight
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Aug 2, 2015
1 parent a8d4372 commit 8d8d5e5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
9 changes: 5 additions & 4 deletions src/univariate/continuous/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ immutable TriangularDist <: ContinuousUnivariateDistribution
c::Float64

function TriangularDist(a::Real, b::Real, c::Real)
a < b || error("TriangularDist: a < b must be true")
a < b ||
throw(ArgumentError("TriangularDist: a < b must be true"))
a <= c <= b || error("a <= c <= b must be true")
@compat new(Float64(a), Float64(b), Float64(c))
end

function TriangularDist(a::Real, b::Real)
a < b || error("TriangularDist: a < b must be true")
a < b ||
throw(ArgumentError("TriangularDist: a < b must be true"))
@compat a_ = Float64(a)
@compat b_ = Float64(b)
c_ = middle(a_, b_)
Expand Down Expand Up @@ -113,7 +115,6 @@ function rand(d::TriangularDist)
(a, b, c) = params(d)
b_m_a = b - a
u = rand()
b_m_a * u < (c - a) ? d.a + sqrt(u * b_m_a * (c - a)) :
b_m_a * u < (c - a) ? d.a + sqrt(u * b_m_a * (c - a)) :
d.b - sqrt((1.0 - u) * b_m_a * (b - c))
end

53 changes: 29 additions & 24 deletions src/univariate/continuous/triweight.jl
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
immutable Triweight <: ContinuousUnivariateDistribution
location::Float64
scale::Float64
function Triweight(l::Real, s::Real)
s > zero(s) || error("scale must be positive")
@compat new(Float64(l), Float64(s))
μ::Float64
σ::Float64
function Triweight::Real, σ::Real)
σ > zero(σ) ||
throw(ArgumentError("Triweight: σ must be positive."))
@compat new(Float64(μ), Float64(σ))
end
end

Triweight(location::Real) = Triweight(location, 1.0)
Triweight(μ::Real) = Triweight(μ, 1.0)
Triweight() = Triweight(0.0, 1.0)

@distr_support Triweight d.location-d.scale d.location+d.scale
@distr_support Triweight d.μ - d.σ d.μ + d.σ

## Parameters
params(d::Triweight) = (d.location, d.scale)

location(d::Triweight) = d.μ
scale(d::Triweight) = d.σ
params(d::Triweight) = (d.μ, d.σ)


## Properties
mean(d::Triweight) = d.location
median(d::Triweight) = d.location
mode(d::Triweight) = d.location
mean(d::Triweight) = d.μ
median(d::Triweight) = d.μ
mode(d::Triweight) = d.μ

var(d::Triweight) = d.scale*d.scale/9.0
var(d::Triweight) = d.σ^2 / 9.0
skewness(d::Triweight) = 0.0
kurtosis(d::Triweight) = 1/33-3
kurtosis(d::Triweight) = -2.9696969696969697 # 1/33-3

## Functions
function pdf(d::Triweight, x::Real)
u = abs(x - d.location)/d.scale
u >= 1 ? 0.0 : 1.09375*(1-u*u)^3/d.scale
u = abs(x - d.μ)/d.σ
u >= 1 ? 0.0 : 1.09375*(1-u*u)^3/d.σ
end

function cdf(d::Triweight, x::Real)
u = (x - d.location)/d.scale
u = (x - d.μ)/d.σ
u <= -1 ? 0.0 : u >= 1 ? 1.0 : 0.03125*(1+u)^4*@horner(u,16.0,-29.0,20.0,-5.0)
end

function ccdf(d::Triweight, x::Real)
u = (d.location - x)/d.scale
u = (d.μ - x)/d.σ
u <= -1 ? 1.0 : u >= 1 ? 0.0 : 0.03125*(1+u)^4*@horner(u,16.0,-29.0,20.0,-5.0)
end

@quantile_newton Triweight


function mgf(d::Triweight, t::Real)
a = d.scale*t
function mgf(d::Triweight, t::Float64)
a = d.σ*t
a2 = a*a
a == 0 ? one(a) : 105.0*exp(d.location*t)*((15.0/a2+1.0)*cosh(a)-(15.0/a2-6.0)/a*sinh(a))/(a2*a2)
a == 0 ? one(a) : 105.0*exp(d.μ*t)*((15.0/a2+1.0)*cosh(a)-(15.0/a2-6.0)/a*sinh(a))/(a2*a2)
end

function cf(d::Triweight, t::Real)
a = d.scale*t
function cf(d::Triweight, t::Float64)
a = d.σ*t
a2 = a*a
a == 0 ? complex(one(a)) : 105.0*cis(d.location*t)*((1.0-15.0/a2)*cos(a)+(15.0/a2-6.0)/a*sin(a))/(a2*a2)
a == 0 ? complex(one(a)) : 105.0*cis(d.μ*t)*((1.0-15.0/a2)*cos(a)+(15.0/a2-6.0)/a*sin(a))/(a2*a2)
end
6 changes: 3 additions & 3 deletions src/univariate/continuous/uniform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ immutable Uniform <: ContinuousUnivariateDistribution
b::Float64

function Uniform(a::Real, b::Real)
a < b || error("Uniform: a must be less than b")
@compat new(Float64(a), Float64(b))
a < b ||
throw(ArgumentError("Uniform: a must be less than b"))
@compat new(Float64(a), Float64(b))
end

Uniform() = new(0.0, 1.0)
end

Expand Down

0 comments on commit 8d8d5e5

Please sign in to comment.