Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

Commit 4f7f562e authored by Dahua Lin's avatar Dahua Lin

introduce macro @continuous_distr_support to make it easier to add support-handling functions

parent 8ee4312c
......@@ -110,6 +110,10 @@ export
isleptokurtic, # Is excess kurtosis < 0.0?
ismesokurtic, # Is excess kurtosis = 0.0?
isprobvec, # Is a probability vector?
isupperbounded,
islowerbounded,
isbouned,
hasfinitesupport,
kde, # Kernel density estimator
kurtosis, # kurtosis of the distribution
logccdf, # ccdf returning log-probability
......
......@@ -4,6 +4,12 @@
#
##############################################################################
# support handling
isbounded(d::UnivariateDistribution) = isupperbounded(d) && islowerbounded(d)
hasfinitesupport(d::DiscreteUnivariateDistribution) = isbounded(d)
hasfinitesupport(d::ContinuousUnivariateDistribution) = false
# generic function to get number of samples
nsamples{D<:UnivariateDistribution}(dt::Type{D}, x::Array) = length(x)
......
......@@ -3,6 +3,8 @@
immutable Arcsine <: ContinuousUnivariateDistribution
end
@continuous_distr_support Arcsine 0.0 1.0
function cdf(d::Arcsine, x::Real)
x < zero(x) ? 0.0 : (x > one(x) ? 1.0 : (2.0 / pi) * asin(sqrt(x)))
end
......@@ -48,14 +50,3 @@ skewness(d::Arcsine) = 0.0
var(d::Arcsine) = 1.0 / 8.0
### handling support
isupperbounded(::Union(Arcsine, Type{Arcsine})) = true
islowerbounded(::Union(Arcsine, Type{Arcsine})) = true
isbounded(::Union(Arcsine, Type{Arcsine})) = true
hasfinitesupport(::Union(Arcsine, Type{Arcsine})) = false
min(::Union(Arcsine, Type{Arcsine})) = 0.0
max(::Union(Arcsine, Type{Arcsine})) = 1.0
insupport(::Union(Arcsine, Type{Arcsine}), x::Real) = 0.0 <= x <= 1.0
\ No newline at end of file
......@@ -59,7 +59,6 @@ isupperbounded(::Union(Bernoulli, Type{Bernoulli})) = true
islowerbounded(::Union(Bernoulli, Type{Bernoulli})) = true
isbounded(::Union(Bernoulli, Type{Bernoulli})) = true
hasfinitesupport(::Union(Bernoulli, Type{Bernoulli})) = true
min(::Union(Bernoulli, Type{Bernoulli})) = 0
max(::Union(Bernoulli, Type{Bernoulli})) = 1
support(::Union(Bernoulli, Type{Bernoulli})) = 0:1
......
......@@ -70,16 +70,7 @@ end
### handling support
isupperbounded(::Union(Beta, Type{Beta})) = true
islowerbounded(::Union(Beta, Type{Beta})) = true
isbounded(::Union(Beta, Type{Beta})) = true
hasfinitesupport(::Union(Beta, Type{Beta})) = false
min(::Union(Beta, Type{Beta})) = 0.0
max(::Union(Beta, Type{Beta})) = 1.0
insupport(::Union(Beta, Type{Beta}), x::Real) = 0.0 <= x <= 1.0
@continuous_distr_support Beta 0.0 1.0
## Fit model
......
......@@ -15,6 +15,8 @@ end
BetaPrime() = BetaPrime(1.0, 1.0)
@continuous_distr_support BetaPrime 0.0 Inf
function mean(d::BetaPrime)
d.beta > 1.0 ? d.alpha / (d.beta - 1.0) : NaN
end
......@@ -53,14 +55,3 @@ function var(d::BetaPrime)
α, β = d.alpha, d.beta
β > 2.0 ? (α * (α + β - 1.0)) / ((β - 2.0) * (β - 1.0)^2) : NaN
end
### handling support
isupperbounded(::Union(BetaPrime, Type{BetaPrime})) = true
islowerbounded(::Union(BetaPrime, Type{BetaPrime})) = false
isbounded(::Union(BetaPrime, Type{BetaPrime})) = false
hasfinitesupport(::Union(BetaPrime, Type{BetaPrime})) = false
min(::Union(BetaPrime, Type{BetaPrime})) = 0.0
max(::Union(BetaPrime, Type{BetaPrime})) = Inf
insupport(::Union(BetaPrime, Type{BetaPrime}), x::Real) = x >= 0.
\ No newline at end of file
......@@ -12,6 +12,8 @@ Cauchy() = Cauchy(0.0, 1.0)
@_jl_dist_2p Cauchy cauchy
@continuous_distr_support Cauchy -Inf Inf
entropy(d::Cauchy) = log(d.scale) + log(4.0 * pi)
kurtosis(d::Cauchy) = NaN
......@@ -33,19 +35,6 @@ skewness(d::Cauchy) = NaN
var(d::Cauchy) = NaN
### handling support
insupport(::Union(Cauchy, Type{Cauchy}), x::Real) = isfinite(x)
isupperbounded(d::Union(Cauchy, Type{Cauchy})) = false
islowerbounded(d::Union(Cauchy, Type{Cauchy})) = false
isbounded(d::Union(Cauchy, Type{Cauchy})) = false
hasfinitesupport(d::Union(Cauchy, Type{Cauchy})) = false
min(::Union(Cauchy, Type{Cauchy})) = -Inf
max(::Union(Cauchy, Type{Cauchy})) = Inf
# Note: this is not a Maximum Likelihood estimator
function fit{T <: Real}(::Type{Cauchy}, x::Array{T})
l, u = iqr(x)
......
......@@ -6,6 +6,8 @@ immutable Chi <: ContinuousUnivariateDistribution
end
end
@continuous_distr_support Chi 0.0 Inf
cdf(d::Chi, x::Real) = cdf(Chisq(d.df),x^2)
ccdf(d::Chi, x::Real) = ccdf(Chisq(d.df),x^2)
logcdf(d::Chi, x::Real) = logcdf(Chisq(d.df),x^2)
......@@ -52,14 +54,3 @@ function entropy(d::Chi)
end
rand(d::Chi) = sqrt(rand(Chisq(d.df)))
### handling support
isupperbounded(d::Union(Chi, Type{Chi})) = false
islowerbounded(d::Union(Chi, Type{Chi})) = true
isbounded(d::Union(Chi, Type{Chi})) = false
hasfinitesupport(d::Union(Chi, Type{Chi})) = false
min(d::Union(Chi, Type{Chi})) = 0.0
max(d::Union(Chi, Type{Chi})) = Inf
insupport(::Union(Chi, Type{Chi}), x::Real) = x >= 0.0
\ No newline at end of file
......@@ -8,6 +8,8 @@ end
@_jl_dist_1p Chisq chisq
@continuous_distr_support Chisq 0.0 Inf
function entropy(d::Chisq)
x = d.df / 2.0 + log(2.0) + lgamma(d.df / 2.0)
x + (1.0 - d.df / 2.0) * digamma(d.df / 2.0)
......@@ -61,15 +63,3 @@ end
skewness(d::Chisq) = sqrt(8.0 / d.df)
var(d::Chisq) = 2.0 * d.df
### handling support
isupperbounded(d::Union(Chisq, Type{Chisq})) = false
islowerbounded(d::Union(Chisq, Type{Chisq})) = true
isbounded(d::Union(Chisq, Type{Chisq})) = false
hasfinitesupport(d::Union(Chisq, Type{Chisq})) = false
min(d::Union(Chisq, Type{Chisq})) = 0.0
max(d::Union(Chisq, Type{Chisq})) = Inf
insupport(::Union(Chisq, Type{Chisq}), x::Real) = x >= 0.0
\ No newline at end of file
......@@ -4,6 +4,8 @@
immutable Cosine <: ContinuousUnivariateDistribution
end
@continuous_distr_support Cosine 0.0 1.0
rand(d::Cosine) = asin(2.0 * rand() - 1.0)
function cdf(d::Cosine, x::Real)
......@@ -33,15 +35,3 @@ rand(d::Cosine) = sin(rand() * pi / 2.0)^2
skewness(d::Cosine) = 0.0
var(d::Cosine) = (pi^2 - 8.0) / (4.0 * pi^2)
### handling support
isupperbounded(::Union(Cosine, Type{Cosine})) = true
islowerbounded(::Union(Cosine, Type{Cosine})) = true
isbounded(::Union(Cosine, Type{Cosine})) = true
hasfinitesupport(::Union(Cosine, Type{Cosine})) = false
min(::Union(Cosine, Type{Cosine})) = 0.0
max(::Union(Cosine, Type{Cosine})) = 1.0
insupport(::Union(Cosine, Type{Cosine}), x::Real) = 0.0 <= x <= 1.0
\ No newline at end of file
......@@ -8,14 +8,17 @@ immutable Erlang <: ContinuousUnivariateDistribution
shape::Int
scale::Float64
nested_gamma::Gamma
function Erlang(shape::Real, scale::Real)
isinteger(shape) || error("Erlang shape parameter must be an integer")
new(int(shape), float64(scale), Gamma(shape, scale))
end
Erlang(scale::Real) = Erlang(scale, 1.0)
Erlang() = Erlang(1, 1.0)
end
Erlang(scale::Real) = Erlang(scale, 1.0)
Erlang() = Erlang(1, 1.0)
@continuous_distr_support Erlang 0.0 Inf
cdf(d::Erlang, x::Real) = cdf(d.nested_gamma, x)
......@@ -52,15 +55,3 @@ end
skewness(d::Erlang) = skewness(d.nested_gamma)
var(d::Erlang) = d.scale^2 * d.shape
### handling support
isupperbounded(::Union(Erlang, Type{Erlang})) = false
islowerbounded(::Union(Erlang, Type{Erlang})) = true
isbounded(::Union(Erlang, Type{Erlang})) = false
hasfinitesupport(::Union(Erlang, Type{Erlang})) = false
min(::Union(Erlang, Type{Erlang})) = 0.0
max(::Union(Erlang, Type{Erlang})) = Inf
insupport(::Union(Erlang, Type{Erlang}), x::Real) = x >= 0.0
\ No newline at end of file
......@@ -8,6 +8,8 @@ end
Exponential() = Exponential(1.0)
@continuous_distr_support Exponential 0.0 Inf
scale(d::Exponential) = d.scale
rate(d::Exponential) = 1.0 / d.scale
......
......@@ -10,6 +10,8 @@ end
@_jl_dist_2p FDist f
@continuous_distr_support FDist 0.0 Inf
mean(d::FDist) = 2.0 < d.ddf ? d.ddf / (d.ddf - 2.0) : NaN
median(d::FDist) = quantile(d, 0.5)
......@@ -37,15 +39,3 @@ entropy(d::FDist) = (log(d.ddf) -log(d.ndf)
+lgamma(0.5*d.ndf) +lgamma(0.5*d.ddf) -lgamma(0.5*(d.ndf+d.ddf))
+(1.0-0.5*d.ndf)*digamma(0.5*d.ndf) +(-1.0-0.5*d.ddf)*digamma(0.5*d.ddf)
+0.5*(d.ndf+d.ddf)*digamma(0.5*(d.ndf+d.ddf)))
### handling support
isupperbounded(::Union(FDist, Type{FDist})) = false
islowerbounded(::Union(FDist, Type{FDist})) = true
isbounded(::Union(FDist, Type{FDist})) = false
hasfinitesupport(::Union(FDist, Type{FDist})) = false
min(::Union(FDist, Type{FDist})) = 0.0
max(::Union(FDist, Type{FDist})) = Inf
insupport(::Union(FDist, Type{FDist}), x::Real) = x >= 0.0
\ No newline at end of file
......@@ -16,6 +16,8 @@ rate(d::Gamma) = 1.0 / d.scale
@_jl_dist_2p Gamma gamma
@continuous_distr_support Gamma 0.0 Inf
function entropy(d::Gamma)
x = (1.0 - d.shape) * digamma(d.shape)
x + lgamma(d.shape) + log(d.scale) + d.shape
......@@ -91,17 +93,6 @@ skewness(d::Gamma) = 2.0 / sqrt(d.shape)
var(d::Gamma) = d.shape * d.scale * d.scale
### handling support
isupperbounded(::Union(Gamma, Type{Gamma})) = false
islowerbounded(::Union(Gamma, Type{Gamma})) = true
isbounded(::Union(Gamma, Type{Gamma})) = false
hasfinitesupport(::Union(Gamma, Type{Gamma})) = false
min(::Union(Gamma, Type{Gamma})) = 0.0
max(::Union(Gamma, Type{Gamma})) = Inf
insupport(::Union(Gamma, Type{Gamma}), x::Real) = x >= 0.0
## Fit model
immutable GammaStats
......
......@@ -52,3 +52,55 @@ immutable RandIntSampler
end
rand(s::RandIntSampler) = int(_randu(s.Ku, s.U)) + s.a
# macros for generating functions for support handling
#
# Both lb & ub should be compile-time constants
# otherwise, one should manually specify the methods
#
macro continuous_distr_support(D, lb, ub)
if isfinite(eval(lb)) && isfinite(eval(ub)) # [lb, ub]
esc(quote
isupperbounded(::Union($D, Type{$D})) = true
islowerbounded(::Union($D, Type{$D})) = true
isbounded(::Union($D, Type{$D})) = true
min(::Union($D, Type{$D})) = $lb
max(::Union($D, Type{$D})) = $ub
insupport(::Union($D, Type{$D}), x::Real) = ($lb <= x <= $ub)
end)
elseif isfinite(eval(lb)) # [lb, inf)
esc(quote
isupperbounded(::Union($D, Type{$D})) = false
islowerbounded(::Union($D, Type{$D})) = true
isbounded(::Union($D, Type{$D})) = false
min(::Union($D, Type{$D})) = $lb
max(::Union($D, Type{$D})) = $ub
insupport(::Union($D, Type{$D}), x::Real) = (isfinite(x) && x >= $lb)
end)
elseif isfinite(eval(ub)) # (-inf, ub]
esc(quote
isupperbounded(::Union($D, Type{$D})) = true
islowerbounded(::Union($D, Type{$D})) = false
isbounded(::Union($D, Type{$D})) = false
min(::Union($D, Type{$D})) = $lb
max(::Union($D, Type{$D})) = $ub
insupport(::Union($D, Type{$D}), x::Real) = (isfinite(x) && x <= $ub)
end)
else # (-inf, inf)
esc(quote
isupperbounded(::Union($D, Type{$D})) = false
islowerbounded(::Union($D, Type{$D})) = false
isbounded(::Union($D, Type{$D})) = false
min(::Union($D, Type{$D})) = $lb
max(::Union($D, Type{$D})) = $ub
insupport(::Union($D, Type{$D}), x::Real) = isfinite(x)
end)
end
end
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