Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the T-distribution via the F-distribution #149

Merged
merged 6 commits into from
Nov 29, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 64 additions & 14 deletions src/distrs/tdist.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
# functions related to student's T distribution

# R implementations
using .RFunctions:
# tdistpdf,
# tdistlogpdf,
tdistcdf,
tdistccdf,
tdistlogcdf,
tdistlogccdf,
tdistinvcdf,
tdistinvccdf,
tdistinvlogcdf,
tdistinvlogccdf

# Julia implementations
tdistpdf(ν::Real, x::Real) = exp(tdistlogpdf(ν, x))

tdistlogpdf(ν::Real, x::Real) = tdistlogpdf(promote(ν, x)...)
Expand All @@ -22,3 +8,67 @@ function tdistlogpdf(ν::T, x::T) where {T<:Real}
νp12 = (ν + 1) / 2
return loggamma(νp12) - (logπ + log(ν)) / 2 - loggamma(ν / 2) - νp12 * log1p(x^2 / ν)
end

function tdistcdf(ν::T, x::T) where T<:Real
if isinf(ν)
return normcdf(x)
elseif x < 0
return fdistccdf(one(ν), ν, x*x)/2
andreasnoack marked this conversation as resolved.
Show resolved Hide resolved
else
return 1 - fdistccdf(one(ν), ν, x*x)/2
end
end
tdistcdf(ν::Real, x::Real) = tdistcdf(map(float, promote(ν, x))...)

tdistccdf(ν::Real, x::Real) = tdistcdf(ν, -x)

function tdistlogcdf(ν::T, x::T) where T<:Real
if isinf(ν)
return normlogcdf(x)
elseif x < 0
ret = fdistlogccdf(one(ν), ν, x*x)
return ret - log(2*one(ret))
andreasnoack marked this conversation as resolved.
Show resolved Hide resolved
else
return log1p(-fdistccdf(one(ν), ν, x*x)/2)
end
end
tdistlogcdf(ν::Real, x::Real) = tdistlogcdf(map(float, promote(ν, x))...)

tdistlogccdf(ν::Real, x::Real) = tdistlogcdf(ν, -x)

function tdistinvcdf(ν::T, p::T) where T<:Real
if isinf(ν)
return norminvcdf(p)
elseif p < 0.5
return -sqrt(fdistinvccdf(one(ν), ν, 2*p))
else
sqrt(fdistinvccdf(one(ν), ν, 2*(1 - p)))
andreasnoack marked this conversation as resolved.
Show resolved Hide resolved
end
end
tdistinvcdf(ν::Real, p::Real) = tdistinvcdf(map(float, promote(ν, p))...)

tdistinvccdf(ν::Real, p::Real) = -tdistinvcdf(ν, p)

if VERSION < v"1.7.0-beta1"
andreasnoack marked this conversation as resolved.
Show resolved Hide resolved
function _expm1(x::Float16)
if -0.2 < x < 0.1
return @evalpoly(x, Float16(0), Float16(1), Float16(1/2), Float16(1/6), Float16(1/24), Float16(1/120))
else
return exp(x) - 1
end
end
end
_expm1(x::Number) = expm1(x)

function tdistinvlogcdf(ν::T, logp::T) where T<:Real
if isinf(ν)
return norminvlogcdf(logp)
elseif logp < -log(2)
return -sqrt(fdistinvlogccdf(one(ν), ν, logp + log(2*one(logp))))
else
sqrt(fdistinvccdf(one(ν), ν, -2*_expm1(logp)))
andreasnoack marked this conversation as resolved.
Show resolved Hide resolved
end
end
tdistinvlogcdf(ν::Real, logp::Real) = tdistinvlogcdf(map(float, promote(ν, logp))...)

tdistinvlogccdf(ν::Real, logp::Real) = -tdistinvlogcdf(ν, logp)