From 9460f9db0325baf88aff381d6756d6318cdfc2b6 Mon Sep 17 00:00:00 2001 From: Dahua Lin Date: Sun, 2 Aug 2015 17:29:32 +0800 Subject: [PATCH] Enforce naming consistencies for Chisq & Cosine --- src/univariate/continuous/chisq.jl | 40 ++++++++++++++--------------- src/univariate/continuous/cosine.jl | 30 ++++++++++------------ 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/univariate/continuous/chisq.jl b/src/univariate/continuous/chisq.jl index 60bb2ffd7..e1bbbd142 100644 --- a/src/univariate/continuous/chisq.jl +++ b/src/univariate/continuous/chisq.jl @@ -1,9 +1,10 @@ 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)) + function Chisq(ν::Real) + ν > zero(ν) || + throw(ArgumentError("Chisq: ν must be positive.")) + @compat new(Float64(ν)) end end @@ -11,49 +12,48 @@ end #### 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) + hν = 0.5 * d.ν + hν + logtwo + lgamma(hν) + (1.0 - hν) * digamma(hν) 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.ν) diff --git a/src/univariate/continuous/cosine.jl b/src/univariate/continuous/cosine.jl index 2142b1e73..3198fe8ac 100644 --- a/src/univariate/continuous/cosine.jl +++ b/src/univariate/continuous/cosine.jl @@ -5,26 +5,26 @@ immutable Cosine <: ContinuousUnivariateDistribution μ::Float64 - s::Float64 + σ::Float64 - function Cosine(μ::Real, s::Real) - s > 0.0 || error("s must be positive.") - @compat new(Float64(μ), Float64(s)) + function Cosine(μ::Real, σ::Real) + σ > 0.0 || error("Cosine: σ must be positive.") + @compat new(Float64(μ), Float64(σ)) end - @compat Cosine(μ::Real) = new(Float64(μ), 1.0) + Cosine(μ::Real) = @compat new(Float64(μ), 1.0) Cosine() = new(0.0, 1.0) end -@distr_support Cosine d.μ - d.s d.μ + d.s +@distr_support Cosine d.μ - d.σ d.μ + d.σ #### Parameters location(d::Cosine) = d.μ -scale(d::Cosine) = d.s +scale(d::Cosine) = d.σ -params(d::Cosine) = (d.μ, d.s) +params(d::Cosine) = (d.μ, d.σ) #### Statistics @@ -35,8 +35,7 @@ median(d::Cosine) = d.μ mode(d::Cosine) = d.μ -const _cosined_varcoef = 0.13069096604865779 # 1 / 3 - 2 / π^2 -var(d::Cosine) = d.s^2 * _cosined_varcoef +var(d::Cosine) = d.σ^2 * 0.13069096604865779 # 0.130... = 1/3 - 2 / π^2 skewness(d::Cosine) = 0.0 @@ -47,9 +46,8 @@ kurtosis(d::Cosine) = -0.59376287559828102362 function pdf(d::Cosine, x::Float64) if insupport(d, x) - μ, s = params(d) - z = (x - μ) / s - return (1.0 + cospi(z)) / (2 * s) + z = (x - d.μ) / d.σ + return (1.0 + cospi(z)) / (2 * d.σ) else return 0.0 end @@ -58,14 +56,12 @@ end logpdf(d::Cosine, x::Float64) = insupport(d, x) ? log(pdf(d, x)) : -Inf function cdf(d::Cosine, x::Float64) - μ, s = params(d) - z = (x - μ) / s + z = (x - d.μ) / d.σ 0.5 * (1.0 + z + sinpi(z) * invπ) end function ccdf(d::Cosine, x::Float64) - μ, s = params(d) - nz = (μ - x) / s + nz = (d.μ - x) / d.σ 0.5 * (1.0 + nz + sinpi(nz) * invπ) end