Skip to content

Commit

Permalink
Enforce consistent naming for Biweight & Cauchy
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Aug 2, 2015
1 parent 816090c commit d88ea43
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 54 deletions.
73 changes: 41 additions & 32 deletions src/univariate/continuous/biweight.jl
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
immutable Biweight <: ContinuousUnivariateDistribution
location::Float64
scale::Float64
function Biweight(l::Real, s::Real)
s > zero(s) || error("scale must be positive")
@compat new(Float64(l), Float64(s))
μ::Float64
σ::Float64

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

Biweight(location::Real) = Biweight(location, 1.0)
Biweight() = Biweight(0.0, 1.0)
Biweight::Real) = new(μ, 1.0)
Biweight() = new(0.0, 1.0)
end

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

## Parameters
params(d::Biweight) = (d.location, d.scale)
params(d::Biweight) = (d.μ, d.σ)

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

var(d::Biweight) = d.scale*d.scale/7.0
var(d::Biweight) = d.σ^2 / 7.0
skewness(d::Biweight) = 0.0
kurtosis(d::Biweight) = 1/21-3
kurtosis(d::Biweight) = -2.9523809523809526 # = 1/21-3

## Functions
function pdf(d::Biweight, x::Real)
u = abs(x - d.location)/d.scale
u >= 1 ? 0.0 : 0.9375*(1-u*u)^2/d.scale
function pdf(d::Biweight, x::Float64)
u = abs(x - d.μ) / d.σ
u >= 1.0 ? 0.0 : 0.9375 * (1 - u^2)^2 / d.σ
end

function cdf(d::Biweight, x::Real)
u = (x - d.location)/d.scale
u <= -1 ? 0.0 : u >= 1 ? 1.0 : 0.0625*(1+u)^3*@horner(u,8.0,-9.0,3.0)
function cdf(d::Biweight, x::Float64)
u = (x - d.μ) / d.σ
u <= -1.0 ? 0.0 :
u >= 1.0 ? 1.0 :
0.0625 * (u + 1.0)^3 * @horner(u,8.0,-9.0,3.0)
end
function ccdf(d::Biweight, x::Real)
u = (d.location - x)/d.scale
u <= -1 ? 1.0 : u >= 1 ? 0.0 : 0.0625*(1+u)^3*@horner(u,8.0,-9.0,3.0)

function ccdf(d::Biweight, x::Float64)
u = (d.μ - x) / d.σ
u <= -1.0 ? 1.0 :
u >= 1.0 ? 0.0 :
0.0625 * (u + 1.0)^3 * @horner(u,8.0,-9.0,3.0)
end

@quantile_newton Biweight

function mgf(d::Biweight, t::Real)
a = d.scale*t
a2 = a*a
a == 0 ? one(a) : 15.0*exp(d.location*t)*(-3.0*cosh(a)+(a+3.0/a)*sinh(a))/(a2*a2)
function mgf(d::Biweight, t::Float64)
a = d.σ*t
a2 = a^2
a == 0 ? 1.0 :
15.0 * exp(d.μ * t) * (-3.0 * cosh(a) + (a + 3.0/a) * sinh(a)) / (a2^2)
end
function cf(d::Biweight, t::Real)
a = d.scale*t
a2 = a*a
a == 0 ? complex(one(a)) : -15.0*cis(d.location*t)*(3.0*cos(a)+(a-3.0/a)*sin(a))/(a2*a2)

function cf(d::Biweight, t::Float64)
a = d.σ * t
a2 = a^2
a == 0 ? 1.0+0.0im :
-15.0 * cis(d.μ * t) * (3.0 * cos(a) + (a - 3.0/a) * sin(a)) / (a2^2)
end
44 changes: 22 additions & 22 deletions src/univariate/continuous/cauchy.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
immutable Cauchy <: ContinuousUnivariateDistribution
μ::Float64
β::Float64
σ::Float64

function Cauchy::Real, β::Real)
β > zero(β) || error("Cauchy: scale must be positive")
@compat new(Float64(μ), Float64(β))
function Cauchy::Real, σ::Real)
σ > zero(σ) || error("Cauchy: scale must be positive")
@compat new(Float64(μ), Float64(σ))
end

@compat Cauchy::Real) = new(Float64(μ), 1.0)
Expand All @@ -16,54 +16,54 @@ end
#### Parameters

location(d::Cauchy) = d.μ
scale(d::Cauchy) = d.β
scale(d::Cauchy) = d.σ

params(d::Cauchy) = (d.μ, d.β)
params(d::Cauchy) = (d.μ, d.σ)


#### Statistics

mean(d::Cauchy) = NaN
median(d::Cauchy) = location(d)
mode(d::Cauchy) = location(d)
median(d::Cauchy) = d.μ
mode(d::Cauchy) = d.μ

var(d::Cauchy) = NaN
skewness(d::Cauchy) = NaN
kurtosis(d::Cauchy) = NaN

entropy(d::Cauchy) = log(scale(d)) + log4π
entropy(d::Cauchy) = log4π + log(d.σ)


#### Functions

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

pdf(d::Cauchy, x::Float64) = 1.0 /* scale(d) * (1 + zval(d, x)^2))
logpdf(d::Cauchy, x::Float64) = - (logπ + log(scale(d)) + log1psq(zval(d, x)))
pdf(d::Cauchy, x::Float64) = 1.0 /* scale(d) * (1.0 + zval(d, x)^2))
logpdf(d::Cauchy, x::Float64) = - (log1psq(zval(d, x)) + logπ + log(d.σ))

function cdf(d::Cauchy, x::Float64)
μ, β = params(d)
invπ * atan2(x - μ, β) + 0.5
μ, σ = params(d)
invπ * atan2(x - μ, σ) + 0.5
end

function ccdf(d::Cauchy, x::Float64)
μ, β = params(d)
invπ * atan2- x, β) + 0.5
μ, σ = params(d)
invπ * atan2- x, σ) + 0.5
end

function quantile(d::Cauchy, p::Float64)
μ, β = params(d)
μ + β * tan* (p - 0.5))
μ, σ = params(d)
μ + σ * tan* (p - 0.5))
end

function cquantile(d::Cauchy, p::Float64)
μ, β = params(d)
μ + β * tan* (0.5 - p))
μ, σ = params(d)
μ + σ * tan* (0.5 - p))
end

mgf(d::Cauchy, t::Real) = t == zero(t) ? 1.0 : NaN
cf(d::Cauchy, t::Real) = exp(im * (t * d.μ) - d.β * abs(t))
cf(d::Cauchy, t::Real) = exp(im * (t * d.μ) - d.σ * abs(t))


#### Fitting
Expand Down

0 comments on commit d88ea43

Please sign in to comment.