Skip to content

Commit

Permalink
enforce consistent naming for Epanechnikov & Exponential
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Aug 2, 2015
1 parent 9460f9d commit 0fb1cb9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
72 changes: 42 additions & 30 deletions src/univariate/continuous/epanechnikov.jl
Original file line number Diff line number Diff line change
@@ -1,51 +1,63 @@
immutable Epanechnikov <: ContinuousUnivariateDistribution
location::Float64
scale::Float64
function Epanechnikov(l::Real, s::Real)
s > zero(s) || error("scale must be positive")
@compat new(Float64(l), Float64(s))
μ::Float64
σ::Float64
function Epanechnikov::Real, σ::Real)
σ > zero(σ) ||
throw(ArgumentError("Epanechnikov: σ must be positive."))
@compat new(Float64(μ), Float64(σ))
end
end

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

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

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

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

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

var(d::Epanechnikov) = d.scale*d.scale/5
var(d::Epanechnikov) = d.σ^2 / 5
skewness(d::Epanechnikov) = 0.0
kurtosis(d::Epanechnikov) = 3/35-3
kurtosis(d::Epanechnikov) = -2.914285714285714 # 3/35-3

## Functions
function pdf(d::Epanechnikov, x::Real)
u = abs(x - d.location)/d.scale
u >= 1 ? 0.0 : 0.75*(1-u*u)/d.scale
function pdf(d::Epanechnikov, x::Float64)
u = abs(x - d.μ) / d.σ
u >= 1 ? 0.0 : 0.75 * (1 - u^2) / d.σ
end
function cdf(d::Epanechnikov, x::Real)
u = (x - d.location)/d.scale
u <= -1 ? 0.0 : u >= 1 ? 1.0 : 0.5+u*(0.75-0.25*u*u)

function cdf(d::Epanechnikov, x::Float64)
u = (x - d.μ) / d.σ
u <= -1 ? 0.0 :
u >= 1 ? 1.0 :
0.5 + u * (0.75 - 0.25 * u^2)
end
function ccdf(d::Epanechnikov, x::Real)
u = (d.location - x)/d.scale
u <= -1 ? 1.0 : u >= 1 ? 0.0 : 0.5+u*(0.75-0.25*u*u)

function ccdf(d::Epanechnikov, x::Float64)
u = (d.μ - x) / d.σ
u <= -1 ? 1.0 :
u >= 1 ? 0.0 :
0.5 + u * (0.75 - 0.25 * u^2)
end

@quantile_newton Epanechnikov

function mgf(d::Epanechnikov, t::Real)
a = d.scale*t
a == 0 ? one(a) : 3.0*exp(d.location*t)*(cosh(a)-sinh(a)/a)/(a*a)
function mgf(d::Epanechnikov, t::Float64)
a = d.σ * t
a == 0 ? 1.0 :
3.0 * exp(d.μ * t) * (cosh(a) - sinh(a) / a) / a^2
end

function cf(d::Epanechnikov, t::Real)
a = d.scale*t
a == 0 ? complex(one(a)) : -3.0*exp(im*d.location*t)*(cos(a)-sin(a)/a)/(a*a)
function cf(d::Epanechnikov, t::Float64)
a = d.σ * t
a == 0 ? 1.0+0.0im :
-3.0 * exp(im * d.μ * t) * (cos(a) - sin(a) / a) / a^2
end
28 changes: 14 additions & 14 deletions src/univariate/continuous/exponential.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
immutable Exponential <: ContinuousUnivariateDistribution
β::Float64 # note: scale not rate
θ::Float64 # note: scale not rate

function Exponential::Real)
β > zero(β) || error("scale must be positive")
@compat new(Float64(β))
function Exponential::Real)
θ > zero(θ) ||
throw(ArgumentError("Exponential: scale must be positive"))
@compat new(Float64(θ))
end

Exponential() = new(1.0)
end

Expand All @@ -14,33 +14,33 @@ end

#### Parameters

scale(d::Exponential) = d.β
rate(d::Exponential) = 1.0 / d.β
scale(d::Exponential) = d.θ
rate(d::Exponential) = 1.0 / d.θ

params(d::Exponential) = (d.β,)
params(d::Exponential) = (d.θ,)


#### Statistics

mean(d::Exponential) = scale(d)
mean(d::Exponential) = d.θ

median(d::Exponential) = logtwo * scale(d)
median(d::Exponential) = logtwo * d.θ

mode(d::Exponential) = 0.0

var(d::Exponential) = scale(d)^2
var(d::Exponential) = d.θ^2

skewness(d::Exponential) = 2.0

kurtosis(d::Exponential) = 6.0

entropy(d::Exponential) = 1.0 + log(scale(d))
entropy(d::Exponential) = 1.0 + log(d.θ)


#### Evaluation

zval(d::Exponential, x::Float64) = x / d.β
xval(d::Exponential, z::Float64) = z * d.β
zval(d::Exponential, x::Float64) = x / d.θ
xval(d::Exponential, z::Float64) = z * d.θ

pdf(d::Exponential, x::Float64) == rate(d); x < 0.0 ? 0.0 : λ * exp(-λ * x))
logpdf(d::Exponential, x::Float64) == rate(d); x < 0.0 ? -Inf : log(λ) - λ * x)
Expand Down

0 comments on commit 0fb1cb9

Please sign in to comment.