-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathfdist.jl
More file actions
50 lines (45 loc) · 1.93 KB
/
fdist.jl
File metadata and controls
50 lines (45 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# functions related to F distribution
# Julia implementations
fdistpdf(ν1::Real, ν2::Real, x::Real) = exp(fdistlogpdf(ν1, ν2, x))
fdistlogpdf(ν1::Real, ν2::Real, x::Real) = fdistlogpdf(promote(ν1, ν2, x)...)
function fdistlogpdf(ν1::T, ν2::T, x::T) where {T <: Real}
# we ensure that `log(x)` does not error if `x < 0`
ν1ν2 = ν1 / ν2
y = max(x, 0)
val = (xlogy(ν1, ν1ν2) + xlogy(ν1 - 2, y) - xlogy(ν1 + ν2, 1 + ν1ν2 * y)) / 2 - logbeta(ν1 / 2, ν2 / 2)
return x < 0 ? oftype(val, -Inf) : val
end
fdistlogupdf(ν1::Real, ν2::Real, x::Real) = fdistlogupdf(promote(ν1, ν2, x)...)
function fdistlogupdf(ν1::T, ν2::T, x::T) where {T <: Real}
# we ensure that `log(x)` does not error if `x < 0`
y = max(x, 0)
val = (xlogy(ν1 - 2, y) - xlogy(ν1 + ν2, ν1 * y + ν2)) / 2
return x < 0 ? oftype(val, -Inf) : val
end
fdistloguloglikelihood(ν1::Real, ν2::Real, x::Real) = fdistlogulikelihood(promote(ν1, ν2, x)...)
function fdistlogulikelihood(ν1::T, ν2::T, x::T) where {T}
# we ensure that `log(x)` does not error if `x < 0`
y = max(x, 0)
tmp = ν1 * y + ν2
a = ν1 / tmp
b = ν2 / tmp
halfν1 = ν1 / 2
halfν2 = ν2 / 2
val = (xlogy(halfν1, a) + xlogy(halfν2, b)) - logbeta(halfν1, halfν2)
return x < 0 ? oftype(val, -Inf) : val
end
for f in ("cdf", "ccdf", "logcdf", "logccdf")
ff = Symbol("fdist" * f)
bf = Symbol("beta" * f)
@eval $ff(ν1::T, ν2::T, x::T) where {T <: Real} = $bf(ν1 / 2, ν2 / 2, inv(1 + ν2 / (ν1 * max(0, x))))
@eval $ff(ν1::Real, ν2::Real, x::Real) = $ff(promote(ν1, ν2, x)...)
end
for f in ("invcdf", "invccdf", "invlogcdf", "invlogccdf")
ff = Symbol("fdist" * f)
bf = Symbol("beta" * f)
@eval function $ff(ν1::T, ν2::T, y::T) where {T <: Real}
x = $bf(ν1 / 2, ν2 / 2, y)
return x / (1 - x) * ν2 / ν1
end
@eval $ff(ν1::Real, ν2::Real, y::Real) = $ff(promote(ν1, ν2, y)...)
end