Commit 013643b7 authored by Dahua Lin's avatar Dahua Lin

support handling of all univariate distributions are all completed

parent 4f7f562e
......@@ -68,7 +68,6 @@ isupperbounded(d::Union(Binomial, Type{Binomial})) = true
islowerbounded(d::Union(Binomial, Type{Binomial})) = true
isbounded(d::Union(Binomial, Type{Binomial})) = true
hasfinitesupport(d::Union(Binomial, Type{Binomial})) = true
min(d::Union(Binomial, Type{Binomial})) = 0
max(d::Binomial) = d.size
support(d::Binomial) = 0:d.size
......
......@@ -148,7 +148,6 @@ isupperbounded(::Union(Categorical, Type{Categorical})) = true
islowerbounded(::Union(Categorical, Type{Categorical})) = true
isbounded(::Union(Categorical, Type{Categorical})) = true
hasfinitesupport(::Union(Categorical, Type{Categorical})) = true
min(::Union(Categorical, Type{Categorical})) = 1
max(d::Categorical) = d.K
support(d::Categorical) = 1:d.K
......
......@@ -59,7 +59,6 @@ isupperbounded(::Union(DiscreteUniform, Type{DiscreteUniform})) = true
islowerbounded(::Union(DiscreteUniform, Type{DiscreteUniform})) = true
isbounded(::Union(DiscreteUniform, Type{DiscreteUniform})) = true
hasfinitesupport(::Union(DiscreteUniform, Type{DiscreteUniform})) = true
min(d::DiscreteUniform) = d.a
max(d::DiscreteUniform) = d.b
support(d::DiscreteUniform) = d.a:d.b
......
......@@ -51,6 +51,17 @@ skewness(d::Geometric) = (2.0 - d.prob) / sqrt(1.0 - d.prob)
var(d::Geometric) = (1.0 - d.prob) / d.prob^2
### handling support
isupperbounded(d::Union(Geometric, Type{Geometric})) = false
islowerbounded(d::Union(Geometric, Type{Geometric})) = true
isbounded(d::Union(Geometric, Type{Geometric})) = false
min(d::Union(Geometric, Type{Geometric})) = 0
max(d::Geometric) = Inf
insupport(d::Geometric, x::Real) = isinteger(x) && x >= 0
## Fit model
immutable GeometricStats
......
......@@ -9,6 +9,8 @@ end
Gumbel() = Gumbel(0.0, 1.0)
@continuous_distr_support Gumbel -Inf Inf
const DoubleExponential = Gumbel
cdf(d::Gumbel, x::Real) = exp(-exp((d.mu - x) / d.beta))
......@@ -17,9 +19,6 @@ logcdf(d::Gumbel, x::Real) = -exp((d.mu - x) / d.beta)
entropy(d::Gumbel) = log(d.beta) - digamma(1.0) + 1.0
insupport(::Gumbel, x::Real) = isfinite(x)
insupport(::Type{Gumbel}, x::Real) = isfinite(x)
kurtosis(d::Gumbel) = 2.4
mean(d::Gumbel) = d.mu - d.beta * digamma(1.0)
......
......@@ -9,14 +9,13 @@ immutable InverseGamma <: ContinuousUnivariateDistribution
end
end
@continuous_distr_support InverseGamma 0.0 Inf
_inv(d::InverseGamma) = Gamma(d.shape, 1.0 / d.scale)
scale(d::InverseGamma) = d.scale
rate(d::InverseGamma) = 1.0 / d.scale
insupport(::InverseGamma, x::Real) = zero(x) <= x < Inf
insupport(::Type{InverseGamma}, x::Real) = zero(x) <= x < Inf
mean(d::InverseGamma) = d.shape > 1.0 ? d.scale / (d.shape - 1.0) : Inf
var(d::InverseGamma) = d.shape > 2.0 ? abs2(d.scale) / (abs2(d.shape - 1.0) * (d.shape - 2.0)) : Inf
skewness(d::InverseGamma) = d.shape > 3.0 ? (4.0 * sqrt(d.shape - 2.0)) / (d.shape - 3.0) : NaN
......
......@@ -10,8 +10,7 @@ end
InverseGaussian() = InverseGaussian(1.0, 1.0)
insupport(::InverseGaussian, x::Real) = zero(x) <= x < Inf
insupport(::Type{InverseGaussian}, x::Real) = zero(x) <= x < Inf
@continuous_distr_support InverseGaussian 0.0 Inf
mean(d::InverseGaussian) = d.mu
mode(d::InverseGaussian) = (r=d.mu/d.lambda; d.mu*(sqrt(1.0+2.25*r*r)-1.5*r))
......
......@@ -5,8 +5,7 @@
immutable Kolmogorov <: ContinuousUnivariateDistribution
end
insupport(::Kolmogorov, x::Real) = zero(x) <= x < Inf
insupport(::Type{Kolmogorov}, x::Real) = zero(x) <= x < Inf
@continuous_distr_support Kolmogorov 0.0 Inf
mean(d::Kolmogorov) = 0.5*√2π*log(2.0)
var(d::Kolmogorov) = pi*pi/12.0 - 0.5*pi*log(2.0)^2
......
......@@ -5,7 +5,15 @@ immutable KSDist <: ContinuousUnivariateDistribution
n::Int
end
insupport(d::KSDist, x::Real) = 1/(2*d.n) <= x <= 1.0
# support handling
isupperbounded(::Union(KSDist, Type{KSDist})) = true
islowerbounded(::Union(KSDist, Type{KSDist})) = true
isbounded(::Union(KSDist, Type{KSDist})) = true
min(d::KSDist) = 1 / (2 * d.n)
max(d::KSDist) = 1.0
insupport(d::KSDist, x::Real) = min(d) <= x <= 1.0
# TODO: implement Simard and L'Ecuyer (2011) meta-algorithm
# requires Pomeranz and Pelz-Good algorithms
......
......@@ -4,7 +4,7 @@ immutable KSOneSided <: ContinuousUnivariateDistribution
n::Int
end
insupport(d::KSOneSided, x::Real) = 0.0 <= x <= 1.0
@continuous_distr_support KSOneSided 0.0 1.0
# formula of Birnbaum and Tingey (1951)
function ccdf(d::KSOneSided,x::Real)
......
......@@ -10,6 +10,8 @@ end
Laplace(location::Real) = Laplace(location, 1.0)
Laplace() = Laplace(0.0, 1.0)
@continuous_distr_support Laplace -Inf Inf
const Biexponential = Laplace
function cdf(d::Laplace, q::Real)
......@@ -19,9 +21,6 @@ end
entropy(d::Laplace) = log(2.0 * d.scale) + 1.0
insupport(d::Laplace, x::Real) = isfinite(x)
insupport(d::Type{Laplace}, x::Real) = isfinite(x)
kurtosis(d::Laplace) = 3.0
mean(d::Laplace) = d.location
......
......@@ -17,6 +17,12 @@ function entropy(d::Levy)
(1.0 - 3.0 * digamma(1.0) + log(16.0 * pi * c * c)) / 2.0
end
isupperbounded(::Union(Levy, Type{Levy})) = false
islowerbounded(::Union(Levy, Type{Levy})) = true
isbounded(::Union(Levy, Type{Levy})) = false
min(d::Levy) = d.location
max(d::Levy) = Inf
insupport(d::Levy, x::Real) = d.location <= x && isfinite(x)
mean(d::Levy) = Inf
......
......@@ -11,10 +11,9 @@ end
@_jl_dist_2p Logistic logis
entropy(d::Logistic) = log(d.scale) + 2.0
@continuous_distr_support Logistic -Inf Inf
insupport(::Logistic, x::Real) = isfinite(x)
insupport(::Type{Logistic}, x::Real) = isfinite(x)
entropy(d::Logistic) = log(d.scale) + 2.0
kurtosis(d::Logistic) = 1.2
......
......@@ -12,10 +12,9 @@ LogNormal() = LogNormal(0.0, 1.0)
@_jl_dist_2p LogNormal lnorm
entropy(d::LogNormal) = 0.5 + 0.5 * log(2.0 * pi * d.sdlog^2) + d.meanlog
@continuous_distr_support LogNormal 0.0 Inf
insupport(::LogNormal, x::Real) = zero(x) < x < Inf
insupport(::Type{LogNormal}, x::Real) = zero(x) < x < Inf
entropy(d::LogNormal) = 0.5 + 0.5 * log(2.0 * pi * d.sdlog^2) + d.meanlog
function kurtosis(d::LogNormal)
exp(4.0 * d.sdlog^2) + 2.0 * exp(3.0 * d.sdlog^2) +
......
......@@ -19,6 +19,13 @@ end
@_jl_dist_2p NegativeBinomial nbinom
isupperbounded(::Union(NegativeBinomial, Type{NegativeBinomial})) = false
islowerbounded(::Union(NegativeBinomial, Type{NegativeBinomial})) = true
isbounded(::Union(NegativeBinomial, Type{NegativeBinomial})) = false
min(::Union(NegativeBinomial, Type{NegativeBinomial})) = 0
max(::Union(NegativeBinomial, Type{NegativeBinomial})) = Inf
insupport(::NegativeBinomial, x::Real) = isinteger(x) && zero(x) <= x
insupport(::Type{NegativeBinomial}, x::Real) = isinteger(x) && zero(x) <= x
......
......@@ -11,8 +11,7 @@ end
@_jl_dist_3p NoncentralBeta nbeta
insupport(::NoncentralBeta, x::Real) = zero(x) < x < one(x)
insupport(::Type{NoncentralBeta}, x::Real) = zero(x) < x < one(x)
@continuous_distr_support NoncentralBeta 0.0 1.0
function rand(d::NoncentralBeta)
a = rand(NoncentralChisq(2.0 * d.alpha, d.ncp))
......
......@@ -14,5 +14,5 @@ kurtosis(d::NoncentralChisq) = 12.0*(d.df + 4.0*d.ncp)/(d.df + 2.0*d.ncp)^2
@_jl_dist_2p NoncentralChisq nchisq
insupport(::NoncentralChisq, x::Real) = zero(x) < x < Inf
insupport(::Type{NoncentralChisq}, x::Real) = zero(x) < x < Inf
@continuous_distr_support NoncentralChisq 0.0 Inf
......@@ -11,17 +11,14 @@ end
@_jl_dist_3p NoncentralF nf
@continuous_distr_support NoncentralF 0.0 Inf
mean(d::NoncentralF) = d.ddf > 2.0 ? d.ddf / (d.ddf - 2.0) * (d.ndf + d.ncp) / d.ndf : NaN
var(d::NoncentralF) = d.ddf > 4.0 ? 2.0 * d.ddf^2 *
((d.ndf+d.ncp)^2 + (d.ddf - 2.0)*(d.ndf + 2.0*d.ncp)) /
(d.ndf * (d.ddf - 2.0)^2 * (d.ddf - 4.0)) : NaN
insupport(::NoncentralF, x::Number) = zero(x) <= x < Inf
insupport(::Type{NoncentralF}, x::Number) = zero(x) <= x < Inf
function rand(d::NoncentralF)
rn = rand(NoncentralChisq(d.ndf,d.ncp)) / d.ndf
rd = rand(Chisq(d.ddf)) / d.ddf
......
......@@ -9,12 +9,11 @@ end
@_jl_dist_2p NoncentralT nt
@continuous_distr_support NoncentralT -Inf Inf
mean(d::NoncentralT) = d.df > 1.0 ? sqrt(0.5*d.df)*d.ncp*gamma(0.5*(d.df-1))/gamma(0.5*d.df) : NaN
var(d::NoncentralT) = d.df > 2.0 ? d.df*(1+d.ncp^2)/(d.df-2.0) - mean(d)^2 : NaN
insupport(::NoncentralT, x::Real) = isfinite(x)
insupport(::Type{NoncentralT}, x::Real) = isfinite(x)
function rand(d::NoncentralT)
z = randn()
v = rand(Chisq(d.df))
......
......@@ -11,6 +11,8 @@ Normal() = Normal(0.0, 1.0)
@_jl_dist_2p Normal norm
@continuous_distr_support Normal -Inf Inf
const Gaussian = Normal
zval(d::Normal, x::Real) = (x - d.μ)/d.σ
......@@ -31,8 +33,6 @@ invlogccdf(d::Normal, p::Real) = xval(d, -logΦinv(p))
entropy(d::Normal) = 0.5 * (log2π + 1.) + log(d.σ)
insupport(d::Normal, x::Real) = isfinite(x)
kurtosis(d::Normal) = 0.0
mean(d::Normal) = d.μ
......
......@@ -10,7 +10,13 @@ end
Pareto(scale::Real) = Pareto(scale, 1.0)
insupport(d::Pareto, x::Number) = isreal(x) && isfinite(x) && x > d.scale
islowerbounded(::Union(Pareto, Type{Pareto})) = true
isupperbounded(::Union(Pareto, Type{Pareto})) = false
isbounded(::Union(Pareto, Type{Pareto})) = false
min(d::Pareto) = d.scale
max(d::Pareto) = Inf
insupport(d::Pareto, x::Number) = isfinite(x) && x >= d.scale
mean(d::Pareto) = d.shape > 1.0 ? (d.scale * d.shape) / (d.shape - 1.0) : Inf
......
......@@ -25,6 +25,13 @@ function entropy(d::Poisson)
end
end
isupperbounded(::Union(Poisson, Type{Poisson})) = false
islowerbounded(::Union(Poisson, Type{Poisson})) = true
isbounded(::Union(Poisson, Type{Poisson})) = false
min(::Union(Poisson, Type{Poisson})) = 0
max(::Union(Poisson, Type{Poisson})) = Inf
insupport(::Poisson, x::Real) = isinteger(x) && zero(x) <= x
insupport(::Type{Poisson}, x::Real) = isinteger(x) && zero(x) <= x
......
......@@ -13,13 +13,12 @@ immutable Rayleigh <: ContinuousUnivariateDistribution
Rayleigh() = new(1.0)
end
@continuous_distr_support Rayleigh 0.0 Inf
cdf(d::Rayleigh, x::Real) = 1.0 - exp(-x^2 / (2.0 * d.scale^2))
entropy(d::Rayleigh) = 1.0 + log(d.scale) - log(sqrt(2.0)) - digamma(1.0) / 2.0
insupport(::Rayleigh, x::Real) = zero(x) < x < Inf
insupport(::Type{Rayleigh}, x::Real) = zero(x) < x < Inf
kurtosis(d::Rayleigh) = -(6.0 * pi^2 - 24.0 * pi + 16.0) / (4.0 - pi)^2
mean(d::Rayleigh) = d.scale * sqrt(pi / 2.)
......
......@@ -4,8 +4,15 @@ immutable Skellam <: DiscreteUnivariateDistribution
Skellam(m1::Real, m2::Real) = new(float64(m1), float64(m2))
end
insupport(::Skellam, k::Real) = isinteger(k)
insupport(::Type{Skellam}, k::Real) = isinteger(k)
isupperbounded(::Union(Skellam, Type{Skellam})) = false
islowerbounded(::Union(Skellam, Type{Skellam})) = false
isbounded(::Union(Skellam, Type{Skellam})) = false
min(::Union(Skellam, Type{Skellam})) = -Inf
max(::Union(Skellam, Type{Skellam})) = Inf
insupport(::Skellam, x::Real) = isinteger(x) && zero(x) <= x
insupport(::Type{Skellam}, x::Real) = isinteger(x) && zero(x) <= x
kurtosis(d::Skellam) = 1.0 / (d.mu1 + d.mu2)
......
......@@ -8,6 +8,8 @@ end
@_jl_dist_1p TDist t
@continuous_distr_support TDist -Inf Inf
function entropy(d::TDist)
hdf = 0.5*d.df
hdfph = hdf + 0.5
......@@ -15,9 +17,6 @@ function entropy(d::TDist)
0.5*log(d.df) + lbeta(hdf,0.5)
end
insupport(::TDist, x::Real) = isfinite(x)
insupport(::Type{TDist}, x::Real) = isfinite(x)
mean(d::TDist) = d.df > 1.0 ? 0.0 : NaN
median(d::TDist) = 0.0
......
......@@ -31,10 +31,16 @@ end
entropy(d::Triangular) = 0.5 + log(d.scale)
function insupport(d::Triangular, x::Number)
return isreal(x) && isfinite(x) &&
d.location - d.scale <= x <= d.location + d.scale
end
# support handling
isupperbounded(::Union(Triangular, Type{Triangular})) = true
islowerbounded(::Union(Triangular, Type{Triangular})) = true
isbounded(::Union(Triangular, Type{Triangular})) = true
min(d::Triangular) = d.location - d.scale
max(d::Triangular) = d.location + d.scale
insupport(d::Triangular, x::Real) = min(d) <= x <= max(d)
kurtosis(d::Triangular) = -0.6
......
......@@ -10,14 +10,16 @@ end
@_jl_dist_2p Uniform unif
min(d::Uniform) = d.a
isupperbounded(::Union(Uniform, Type{Uniform})) = true
islowerbounded(::Union(Uniform, Type{Uniform})) = true
isbounded(::Union(Uniform, Type{Uniform})) = true
min(d::Uniform) = d.a
max(d::Uniform) = d.b
insupport(d::Uniform, x::Real) = d.a <= x <= d.b
entropy(d::Uniform) = log(d.b - d.a)
insupport(d::Uniform, x::Real) = d.a <= x <= d.b
kurtosis(d::Uniform) = -6.0 / 5.0
mean(d::Uniform) = (d.a + d.b) / 2.0
......
......@@ -11,6 +11,8 @@ Weibull(sh::Real) = Weibull(sh, 1.0)
@_jl_dist_2p Weibull weibull
@continuous_distr_support Weibull 0.0 Inf
function cdf(d::Weibull, x::Real)
if 0.0 < x
return 1.0 - exp(-((x / d.scale)^d.shape))
......@@ -24,9 +26,6 @@ function entropy(d::Weibull)
return ((k - 1.0) / k) * -digamma(1.0) + log(l / k) + 1.0
end
insupport(::Weibull, x::Real) = zero(x) <= x < Inf
insupport(::Type{Weibull}, x::Real) = zero(x) <= x < Inf
function kurtosis(d::Weibull)
λ, k = d.scale, d.shape
μ = mean(d)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment