Skip to content

Commit

Permalink
Merge pull request JuliaStats#400 from JuliaStats/dh/paramsym
Browse files Browse the repository at this point in the history
Enforce consistency of parameter naming
  • Loading branch information
lindahua committed Aug 3, 2015
2 parents 0439487 + fe4cba0 commit 2457917
Show file tree
Hide file tree
Showing 53 changed files with 635 additions and 708 deletions.
11 changes: 5 additions & 6 deletions src/edgeworth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ kurtosis(d::EdgeworthAbstract) = kurtosis(d.dist) / d.n
immutable EdgeworthZ{D<:UnivariateDistribution} <: EdgeworthAbstract
dist::D
n::Float64

function EdgeworthZ{T<:UnivariateDistribution}(d::T, n::Real)
n > zero(n) ||
error("n must be positive")
@compat new(d, Float64(n))
@check_args(EdgeworthZ, n > zero(n))
new(d, n)
end
end
EdgeworthZ(d::UnivariateDistribution,n::Real) = EdgeworthZ{typeof(d)}(d,n)
Expand Down Expand Up @@ -78,9 +78,8 @@ immutable EdgeworthSum{D<:UnivariateDistribution} <: EdgeworthAbstract
dist::D
n::Float64
function EdgeworthSum{T<:UnivariateDistribution}(d::T, n::Real)
n > zero(n) ||
error("n must be positive")
@compat new(d, Float64(n))
@check_args(EdgeworthSum, n > zero(n))
new(d, n)
end
end
EdgeworthSum(d::UnivariateDistribution, n::Real) = EdgeworthSum{typeof(d)}(d,n)
Expand Down
2 changes: 1 addition & 1 deletion src/matrix/inversewishart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function _logpdf(d::InverseWishart, X::DenseMatrix{Float64})
-0.5 * ((df + p + 1) * logdet(Xcf) + trace(Xcf \ Ψ)) - d.c0
end

@compat _logpdf{T<:Real}(d::InverseWishart, X::DenseMatrix{T}) = _logpdf(d, Float64(X))
_logpdf{T<:Real}(d::InverseWishart, X::DenseMatrix{T}) = _logpdf(d, convert(Matrix{Float64}, X))


#### Sampling
Expand Down
4 changes: 2 additions & 2 deletions src/matrix/wishart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function mode(d::Wishart)
end
end

function meanlogdet(d::Wishart)
function meanlogdet(d::Wishart)
p = dim(d)
df = d.df
v = logdet(d.S) + p * logtwo
Expand All @@ -81,7 +81,7 @@ function _logpdf(d::Wishart, X::DenseMatrix{Float64})
0.5 * ((df - (p + 1)) * logdet(Xcf) - trace(d.S \ X)) - d.c0
end

@compat _logpdf{T<:Real}(d::Wishart, X::DenseMatrix{T}) = _logpdf(d, Float64(X))
_logpdf{T<:Real}(d::Wishart, X::DenseMatrix{T}) = _logpdf(d, convert(Matrix{Float64}, X))

#### Sampling

Expand Down
2 changes: 1 addition & 1 deletion src/multivariate/dirichlet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ immutable Dirichlet <: ContinuousMultivariateDistribution
lmnB::Float64 = 0.0
for i in 1:length(alpha)
ai = alpha[i]
ai > 0 || throw(ArgumentError("alpha must be a positive vector."))
ai > 0 || throw(ArgumentError("Dirichlet: alpha must be a positive vector."))
alpha0 += ai
lmnB += lgamma(ai)
end
Expand Down
2 changes: 1 addition & 1 deletion src/testutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ end
function test_stats(d::DiscreteUnivariateDistribution, vs::AbstractVector)
# using definition (or an approximation)

@compat vf = Float64[Float64(v) for v in vs]
vf = Float64[v for v in vs]
p = pdf(d, vf)
xmean = dot(p, vf)
xvar = dot(p, abs2(vf .- xmean))
Expand Down
9 changes: 2 additions & 7 deletions src/univariate/continuous/arcsine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ immutable Arcsine <: ContinuousUnivariateDistribution
a::Float64
b::Float64

function Arcsine(a::Float64, b::Float64)
a < b || throw(ArgumentError("a must be less than b."))
new(a, b)
end

@compat Arcsine(a::Real, b::Real) = Arcsine(Float64(a), Float64(b))
@compat Arcsine(b::Real) = Arcsine(0.0, Float64(b))
Arcsine(a::Real, b::Real) = (@check_args(Arcsine, a < b); new(a, b))
Arcsine(b::Real) = (@check_args(Arcsine, b > zero(b)); new(0.0, b))
Arcsine() = new(0.0, 1.0)
end

Expand Down
6 changes: 2 additions & 4 deletions src/univariate/continuous/beta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ immutable Beta <: ContinuousUnivariateDistribution
β::Float64

function Beta::Real, β::Real)
> zero(α) && β > zero(β)) ||
throw(ArgumentError("α and β must be positive"))
@compat new(Float64(α), Float64(β))
@check_args(Beta, α > zero(α) && β > zero(β))
new(α, β)
end

Beta::Real) = Beta(α, α)
Beta() = new(1.0, 1.0)
end
Expand Down
9 changes: 4 additions & 5 deletions src/univariate/continuous/betaprime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ immutable BetaPrime <: ContinuousUnivariateDistribution
α::Float64
β::Float64

function BetaPrime::Float64, β::Float64)
(α > zero(α) && β > zero(β)) || error("α and β must be positive")
@compat new(Float64(α), Float64(β))
function BetaPrime::Real, β::Real)
@check_args(BetaPrime, α > zero(α) && β > zero(β))
new(α, β)
end

BetaPrime::Float64) = BetaPrime(α)
BetaPrime::Real) = BetaPrime(α)
BetaPrime() = new(1.0, 1.0)
end

Expand Down
71 changes: 38 additions & 33 deletions src/univariate/continuous/biweight.jl
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
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))
end
end
μ::Float64
σ::Float64

Biweight(location::Real) = Biweight(location, 1.0)
Biweight() = Biweight(0.0, 1.0)
Biweight::Real, σ::Real) = (@check_args(Biweight, σ > zero(σ)); new(μ, σ))
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
46 changes: 21 additions & 25 deletions src/univariate/continuous/cauchy.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
immutable Cauchy <: ContinuousUnivariateDistribution
μ::Float64
β::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)
Cauchy::Real, σ::Real) = (@check_args(Cauchy, σ > zero(σ)); new(μ, σ))
Cauchy::Real) = new(μ, 1.0)
Cauchy() = new(0.0, 1.0)
end

Expand All @@ -16,54 +12,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
6 changes: 1 addition & 5 deletions src/univariate/continuous/chi.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
immutable Chi <: ContinuousUnivariateDistribution
ν::Float64

function Chi::Real)
ν > zero(ν) || throw(ArgumentError("Chi: ν must be positive."))
@compat new(Float64(ν))
end
Chi::Real) = (@check_args(Chi, ν > zero(ν)); new(ν))
end

@distr_support Chi 0.0 Inf


#### Parameters

dof(d::Chi) = d.ν
Expand Down
38 changes: 17 additions & 21 deletions src/univariate/continuous/chisq.jl
Original file line number Diff line number Diff line change
@@ -1,59 +1,55 @@
immutable Chisq <: ContinuousUnivariateDistribution
df::Float64
ν::Float64

function Chisq(k::Real)
k > zero(k) || error("The degree of freedom k must be positive")
@compat new(Float64(k))
end
Chisq::Real) = (@check_args(Chisq, ν > zero(ν)); new(ν))
end

@distr_support Chisq 0.0 Inf

#### Parameters

dof(d::Chisq) = d.df
params(d::Chisq) = (d.df,)
dof(d::Chisq) = d.ν
params(d::Chisq) = (d.ν,)


#### Statistics

mean(d::Chisq) = dof(d)
mean(d::Chisq) = d.ν

var(d::Chisq) = 2.0 * dof(d)
var(d::Chisq) = 2.0 * d.ν

skewness(d::Chisq) = sqrt(8.0 / dof(d))
skewness(d::Chisq) = sqrt(8.0 / d.ν)

kurtosis(d::Chisq) = 12.0 / dof(d)
kurtosis(d::Chisq) = 12.0 / d.ν

mode(d::Chisq) = d.df > 2.0 ? d.df - 2.0 : 0.0
mode(d::Chisq) = d.ν > 2.0 ? d.ν - 2.0 : 0.0

function median(d::Chisq; approx::Bool=false)
if approx
k = dof(d)
return k * (1.0 - 2.0 / (9.0 * k))^3
return d.ν * (1.0 - 2.0 / (9.0 * d.ν))^3
else
return quantile(d, 0.5)
end
end

function entropy(d::Chisq)
hk = 0.5 * dof(d)
hk + logtwo + lgamma(hk) + (1.0 - hk) * digamma(hk)
= 0.5 * d.ν
+ logtwo + lgamma() + (1.0 - ) * digamma()
end


#### Evaluation

@_delegate_statsfuns Chisq chisq df
@_delegate_statsfuns Chisq chisq ν

mgf(d::Chisq, t::Real) = (1.0 - 2.0 * t)^(-dof(d) / 2.0)
mgf(d::Chisq, t::Real) = (1.0 - 2.0 * t)^(-d.ν * 0.5)

cf(d::Chisq, t::Real) = (1.0 - 2.0 * im * t)^(-dof(d) / 2.0)
cf(d::Chisq, t::Real) = (1.0 - 2.0 * im * t)^(-d.ν * 0.5)

gradlogpdf(d::Chisq, x::Float64) = x >= 0.0 ? (dof(d) / 2.0 - 1) / x - 0.5 : 0.0
gradlogpdf(d::Chisq, x::Float64) = x > 0.0 ? (d.ν * 0.5 - 1) / x - 0.5 : 0.0


#### Sampling

_chisq_rand::Float64) = StatsFuns.Rmath.chisqrand(ν)
rand(d::Chisq) = _chisq_rand(d.df)
rand(d::Chisq) = _chisq_rand(d.ν)
Loading

0 comments on commit 2457917

Please sign in to comment.