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 consistency for FDist
- Loading branch information
Showing
1 changed file
with
26 additions
and
25 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,63 +1,64 @@ | ||
immutable FDist <: ContinuousUnivariateDistribution | ||
d1::Float64 | ||
d2::Float64 | ||
ν1::Float64 | ||
ν2::Float64 | ||
|
||
function FDist(d1::Real, d2::Real) | ||
d1 > zero(d1) && d2 > zero(d2) || error("Degrees of freedom must be positive") | ||
@compat new(Float64(d1), Float64(d2)) | ||
function FDist(ν1::Real, ν2::Real) | ||
ν1 > zero(ν1) && ν2 > zero(ν2) || | ||
throw(ArgumentError("FDist: ν1 and ν2 must be positive.")) | ||
@compat new(Float64(ν1), Float64(ν2)) | ||
end | ||
end | ||
|
||
@distr_support FDist 0.0 Inf | ||
|
||
#### Parameters | ||
|
||
params(d::FDist) = (d.d1, d.d2) | ||
params(d::FDist) = (d.ν1, d.ν2) | ||
|
||
|
||
#### Statistics | ||
|
||
mean(d::FDist) = (d2 = d.d2; d2 > 2.0 ? d2 / (d2 - 2.0) : NaN) | ||
mean(d::FDist) = (ν2 = d.ν2; ν2 > 2.0 ? ν2 / (ν2 - 2.0) : NaN) | ||
|
||
mode(d::FDist) = ((d1, d2) = params(d); d1 > 2.0 ? ((d1 - 2.0)/d1) * (d2 / (d2 + 2.0)) : 0.0) | ||
mode(d::FDist) = ((ν1, ν2) = params(d); ν1 > 2.0 ? ((ν1 - 2.0)/ν1) * (ν2 / (ν2 + 2.0)) : 0.0) | ||
|
||
function var(d::FDist) | ||
(d1, d2) = params(d) | ||
d2 > 4.0 ? 2.0 * d2^2 * (d1 + d2 - 2.0) / (d1 * (d2 - 2.0)^2 * (d2 - 4.0)) : NaN | ||
(ν1, ν2) = params(d) | ||
ν2 > 4.0 ? 2.0 * ν2^2 * (ν1 + ν2 - 2.0) / (ν1 * (ν2 - 2.0)^2 * (ν2 - 4.0)) : NaN | ||
end | ||
|
||
function skewness(d::FDist) | ||
(d1, d2) = params(d) | ||
if d2 > 6.0 | ||
return (2.0 * d1 + d2 - 2.0) * sqrt(8.0 * (d2 - 4.0)) / ((d2 - 6.0) * sqrt(d1 * (d1 + d2 - 2.0))) | ||
(ν1, ν2) = params(d) | ||
if ν2 > 6.0 | ||
return (2.0 * ν1 + ν2 - 2.0) * sqrt(8.0 * (ν2 - 4.0)) / ((ν2 - 6.0) * sqrt(ν1 * (ν1 + ν2 - 2.0))) | ||
else | ||
return NaN | ||
end | ||
end | ||
|
||
function kurtosis(d::FDist) | ||
(d1, d2) = params(d) | ||
if d2 > 8.0 | ||
a = d1 * (5. * d2 - 22.) * (d1 + d2 - 2.) + (d2 - 4.) * (d2 - 2.)^2 | ||
b = d1 * (d2 - 6.) * (d2 - 8.) * (d2 - 2.) | ||
(ν1, ν2) = params(d) | ||
if ν2 > 8.0 | ||
a = ν1 * (5. * ν2 - 22.) * (ν1 + ν2 - 2.) + (ν2 - 4.) * (ν2 - 2.)^2 | ||
b = ν1 * (ν2 - 6.) * (ν2 - 8.) * (ν2 - 2.) | ||
return 12. * a / b | ||
else | ||
return NaN | ||
end | ||
end | ||
|
||
function entropy(d::FDist) | ||
(d1, d2) = params(d) | ||
hd1 = d1 * 0.5 | ||
hd2 = d2 * 0.5 | ||
hs = (d1 + d2) * 0.5 | ||
return log(d2 / d1) + lgamma(hd1) + lgamma(hd2) - lgamma(hs) + | ||
(1.0 - hd1) * digamma(hd1) + (-1.0 - hd2) * digamma(hd2) + | ||
(ν1, ν2) = params(d) | ||
hν1 = ν1 * 0.5 | ||
hν2 = ν2 * 0.5 | ||
hs = (ν1 + ν2) * 0.5 | ||
return log(ν2 / ν1) + lgamma(hν1) + lgamma(hν2) - lgamma(hs) + | ||
(1.0 - hν1) * digamma(hν1) + (-1.0 - hν2) * digamma(hν2) + | ||
hs * digamma(hs) | ||
end | ||
|
||
#### Evaluation & Sampling | ||
|
||
@_delegate_statsfuns FDist fdist d1 d2 | ||
@_delegate_statsfuns FDist fdist ν1 ν2 | ||
|
||
rand(d::FDist) = StatsFuns.Rmath.fdistrand(d.d1, d.d2) | ||
rand(d::FDist) = StatsFuns.Rmath.fdistrand(d.ν1, d.ν2) |