forked from JuliaStats/Distributions.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce naming consistencies for Chisq & Cosine
- Loading branch information
Showing
2 changed files
with
33 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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.ν) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters