Skip to content

Commit

Permalink
Enforce naming consistencies for Chisq & Cosine
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Aug 2, 2015
1 parent 42b4ea5 commit 9460f9d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
40 changes: 20 additions & 20 deletions src/univariate/continuous/chisq.jl
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
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

@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.ν)
30 changes: 13 additions & 17 deletions src/univariate/continuous/cosine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 9460f9d

Please sign in to comment.