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 2ac51ca8 authored by Dahua Lin's avatar Dahua Lin

new distribution type system

This implements the proposal by Simon in #119.

Through typealias, I retain all existing types, such as
UnivariateDistribution, MultivariateDistribution, DiscreteDistribution,
etc.  I created a script test/types.jl to ensure that all sub-typing
relations are preserved.

This new type system breaks nothing (all tests passed easily) and makes
it easier to write generic types and methods (the Truncated type now
works perfectly).
parent b1cbdc67
tests = [
"types",
"utils",
"sample",
"fit",
......
......@@ -3,7 +3,15 @@ module Distributions
using NumericExtensions
using Stats
export # types
export
# types
VariateForm,
ValueSupport,
Univariate,
Multivariate,
Matrixvariate,
Discrete,
Continuous,
Distribution,
UnivariateDistribution,
MultivariateDistribution,
......@@ -72,7 +80,8 @@ export # types
Weibull,
Wishart,
QQPair,
# methods
# methods
binaryentropy, # entropy of distribution in bits
ccdf, # complementary cdf, i.e. 1 - cdf
cdf, # cumulative distribution function
......@@ -136,27 +145,41 @@ import Base.show, Base.sprand
import NumericExtensions.dim, NumericExtensions.entropy
import Stats.kurtosis, Stats.skewness, Stats.mode, Stats.modes
abstract Distribution
abstract UnivariateDistribution <: Distribution
abstract MultivariateDistribution <: Distribution
abstract MatrixDistribution <: Distribution
abstract DiscreteUnivariateDistribution <: UnivariateDistribution
abstract ContinuousUnivariateDistribution <: UnivariateDistribution
#### Distribution type system
abstract ValueSupport
type Discrete <: ValueSupport end
type Continuous <: ValueSupport end
abstract DiscreteMultivariateDistribution <: MultivariateDistribution
abstract ContinuousMultivariateDistribution <: MultivariateDistribution
abstract VariateForm
type Univariate <: VariateForm end
type Multivariate <: VariateForm end
type Matrixvariate <: VariateForm end
abstract ContinuousMatrixDistribution <: MatrixDistribution
abstract DiscreteMatrixDistribution <: MatrixDistribution
abstract Distribution{F<:VariateForm,S<:ValueSupport}
typealias UnivariateDistribution{S<:ValueSupport} Distribution{Univariate,S}
typealias MultivariateDistribution{S<:ValueSupport} Distribution{Multivariate,S}
typealias MatrixDistribution{S<:ValueSupport} Distribution{Matrixvariate,S}
typealias NonMatrixDistribution Union(UnivariateDistribution, MultivariateDistribution)
typealias DiscreteDistribution Union(DiscreteUnivariateDistribution, DiscreteMultivariateDistribution)
typealias ContinuousDistribution Union(ContinuousUnivariateDistribution, ContinuousMultivariateDistribution)
typealias DiscreteDistribution{F<:VariateForm} Distribution{F,Discrete}
typealias ContinuousDistribution{F<:VariateForm} Distribution{F,Continuous}
typealias DiscreteUnivariateDistribution Distribution{Univariate, Discrete}
typealias ContinuousUnivariateDistribution Distribution{Univariate, Continuous}
typealias DiscreteMultivariateDistribution Distribution{Multivariate, Discrete}
typealias ContinuousMultivariateDistribution Distribution{Multivariate, Continuous}
typealias DiscreteMatrixDistribution Distribution{Matrixvariate, Discrete}
typealias ContinuousMatrixDistribution Distribution{Matrixvariate, Continuous}
abstract SufficientStats
abstract GenerativeFormulation
#### Include files
include("constants.jl")
include("fallbacks.jl")
......
immutable Truncated{D<:UnivariateDistribution} <: UnivariateDistribution
immutable Truncated{D<:UnivariateDistribution,S<:ValueSupport} <: Distribution{Univariate,S}
untruncated::D
lower::Float64
upper::Float64
......@@ -11,9 +11,14 @@ immutable Truncated{D<:UnivariateDistribution} <: UnivariateDistribution
new(d, float64(l), float64(u), float64(nc))
end
end
Truncated(d::UnivariateDistribution, l::Real, u::Real, nc::Real) = Truncated{typeof(d)}(d,l,u,nc)
Truncated(d::UnivariateDistribution, l::Real, u::Real) = Truncated{typeof(d)}(d,l,u, cdf(d, u) - cdf(d, l))
function Truncated{S<:ValueSupport}(d::UnivariateDistribution{S}, l::Real, u::Real, nc::Real)
Truncated{typeof(d),S}(d,l,u,nc)
end
function Truncated{S<:ValueSupport}(d::UnivariateDistribution{S}, l::Real, u::Real)
Truncated{typeof(d),S}(d,l,u, cdf(d, u) - cdf(d, l))
end
function insupport(d::Truncated, x::Number)
return x >= d.lower && x <= d.upper && insupport(d.untruncated, x)
......
# Test type relations
using Distributions
@assert UnivariateDistribution <: Distribution
@assert MultivariateDistribution <: Distribution
@assert MatrixDistribution <: Distribution
@assert DiscreteDistribution <: Distribution
@assert ContinuousDistribution <: Distribution
@assert DiscreteUnivariateDistribution <: DiscreteDistribution
@assert DiscreteUnivariateDistribution <: UnivariateDistribution
@assert ContinuousUnivariateDistribution <: ContinuousDistribution
@assert ContinuousUnivariateDistribution <: UnivariateDistribution
@assert DiscreteMultivariateDistribution <: DiscreteDistribution
@assert DiscreteMultivariateDistribution <: MultivariateDistribution
@assert ContinuousMultivariateDistribution <: ContinuousDistribution
@assert ContinuousMultivariateDistribution <: MultivariateDistribution
@assert DiscreteMatrixDistribution <: DiscreteDistribution
@assert DiscreteMatrixDistribution <: MatrixDistribution
@assert ContinuousMatrixDistribution <: ContinuousDistribution
@assert ContinuousMatrixDistribution <: MatrixDistribution
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