Skip to content

Commit

Permalink
enforce naming consistency for TDist
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Aug 2, 2015
1 parent 19e1618 commit a8d4372
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions src/univariate/continuous/tdist.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
immutable TDist <: ContinuousUnivariateDistribution
df::Float64
function TDist(d::Real)
d > zero(d) || error("TDist: df must be positive")
@compat new(Float64(d))
ν::Float64

function TDist::Real)
ν > zero(ν) ||
throw(ArgumentError("TDist: ν must be positive."))
@compat new(Float64(ν))
end
end

Expand All @@ -11,49 +13,49 @@ end

#### Parameters

dof(d::TDist) = d.df
params(d::TDist) = (d.df,)
dof(d::TDist) = d.ν
params(d::TDist) = (d.ν,)


#### Statistics

mean(d::TDist) = d.df > 1.0 ? 0.0 : NaN
mean(d::TDist) = d.ν > 1.0 ? 0.0 : NaN
median(d::TDist) = 0.0
mode(d::TDist) = 0.0

function var(d::TDist)
df = d.df
df > 2.0 ? df / (df - 2.0) :
df > 1.0 ? Inf : NaN
ν = d.ν
ν > 2.0 ? ν / (ν - 2.0) :
ν > 1.0 ? Inf : NaN
end

skewness(d::TDist) = d.df > 3.0 ? 0.0 : NaN
skewness(d::TDist) = d.ν > 3.0 ? 0.0 : NaN

function kurtosis(d::TDist)
df = d.df
df > 4.0 ? 6.0 / (df - 4.0) :
df > 2.0 ? Inf : NaN
ν = d.ν
ν > 4.0 ? 6.0 / (ν - 4.0) :
ν > 2.0 ? Inf : NaN
end

function entropy(d::TDist)
hdf = 0.5 * d.df
hdfph = hdf + 0.5
hdfph * (digamma(hdfph) - digamma(hdf)) + 0.5 * log(d.df) + lbeta(hdf,0.5)
h = 0.5 * d.ν
h1 = h + 0.5
h1 * (digamma(h1) - digamma(h)) + 0.5 * log(d.ν) + lbeta(h, 0.5)
end


#### Evaluation & Sampling

@_delegate_statsfuns TDist tdist df
@_delegate_statsfuns TDist tdist ν

rand(d::TDist) = StatsFuns.Rmath.tdistrand(d.df)
rand(d::TDist) = StatsFuns.Rmath.tdistrand(d.ν)

function cf(d::TDist, t::Real)
t == 0 && return complex(1.0)
h = d.df/2
q = d.df/4
h = d.ν * 0.5
q = d.ν * 0.25
t2 = t*t
complex(2*(q*t2)^q*besselk(h,sqrt(d.df)*abs(t))/gamma(h))
complex(2*(q*t2)^q*besselk(h,sqrt(d.ν)*abs(t))/gamma(h))
end

gradlogpdf(d::TDist, x::Float64) = -((d.df + 1.0) * x) / (x^2 + d.df)
gradlogpdf(d::TDist, x::Float64) = -((d.ν + 1.0) * x) / (x^2 + d.ν)

0 comments on commit a8d4372

Please sign in to comment.