-
Notifications
You must be signed in to change notification settings - Fork 414
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
Change to ValueSupport interface #951
Changes from 32 commits
20872be
c6173e5
5a399cc
0d99f67
f4e5156
f8e9918
6642ae9
42477c6
6c1c76e
bb9c2ac
acad538
ef25d5c
0c148ef
c557036
d42c02a
1924394
aface29
cea3814
bb9a6de
b2f15d7
c916f95
133a7a2
865f23f
ab6494f
0d66c20
115be40
3677204
8528488
c9ff468
2db64ba
1b35567
e6bfa4c
c72dc35
c774daa
3ee46f1
383527f
180e88f
23277cf
46bfea4
d5d599b
955c861
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -10,24 +10,33 @@ struct Multivariate <: VariateForm end | |||||
struct Matrixvariate <: VariateForm end | ||||||
|
||||||
""" | ||||||
`S <: ValueSupport` specifies the support of sample elements, | ||||||
either discrete or continuous. | ||||||
""" | ||||||
abstract type ValueSupport end | ||||||
struct Discrete <: ValueSupport end | ||||||
struct Continuous <: ValueSupport end | ||||||
`S <: Support{T}` specifies the support of sample elements as T, | ||||||
either discrete, continuous or other. | ||||||
""" | ||||||
abstract type Support{N} end | ||||||
struct ContinuousSupport{N <: Number} <: Support{N} end | ||||||
abstract type CountableSupport{C} <: Support{C} end | ||||||
struct ContiguousSupport{C <: Integer} <: CountableSupport{C} end | ||||||
struct UnionSupport{N1, N2, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document |
||||||
S1 <: Support{N1}, | ||||||
S2 <: Support{N2}} <: | ||||||
Support{Union{N1, N2}} end | ||||||
|
||||||
const DiscontinuousSupport{I, F} = | ||||||
UnionSupport{I, F, <: CountableSupport{I}, | ||||||
ContinuousSupport{F}} where {I <: Number, F <: Number} | ||||||
|
||||||
## Sampleable | ||||||
|
||||||
""" | ||||||
Sampleable{F<:VariateForm,S<:ValueSupport} | ||||||
Sampleable{F<:VariateForm,S<:Support} | ||||||
|
||||||
`Sampleable` is any type able to produce random values. | ||||||
Parametrized by a `VariateForm` defining the dimension of samples | ||||||
and a `ValueSupport` defining the domain of possibly sampled values. | ||||||
and a `Support` defining the domain of possibly sampled values. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
Any `Sampleable` implements the `Base.rand` method. | ||||||
""" | ||||||
abstract type Sampleable{F<:VariateForm,S<:ValueSupport} end | ||||||
abstract type Sampleable{F<:VariateForm,S<:Support} end | ||||||
|
||||||
""" | ||||||
length(s::Sampleable) | ||||||
|
@@ -50,13 +59,15 @@ Base.size(s::Sampleable{Multivariate}) = (length(s),) | |||||
|
||||||
""" | ||||||
eltype(s::Sampleable) | ||||||
eltype(::Support) | ||||||
|
||||||
The default element type of a sample. This is the type of elements of the samples generated | ||||||
by the `rand` method. However, one can provide an array of different element types to | ||||||
store the samples using `rand!`. | ||||||
""" | ||||||
Base.eltype(s::Sampleable{F,Discrete}) where {F} = Int | ||||||
Base.eltype(s::Sampleable{F,Continuous}) where {F} = Float64 | ||||||
Base.eltype(::Sampleable{F, <: Support{N}}) where {F, N} = N | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to be the type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Below it is |
||||||
Base.eltype(::Type{<:Sampleable{F, <: Support{N}}}) where {F, N} = N | ||||||
Base.eltype(::Type{<:Support{N}}) where {N} = N | ||||||
|
||||||
""" | ||||||
nsamples(s::Sampleable) | ||||||
|
@@ -67,41 +78,28 @@ into an array, depending on the variate form. | |||||
nsamples(t::Type{Sampleable}, x::Any) | ||||||
nsamples(::Type{D}, x::Number) where {D<:Sampleable{Univariate}} = 1 | ||||||
nsamples(::Type{D}, x::AbstractArray) where {D<:Sampleable{Univariate}} = length(x) | ||||||
nsamples(::Type{D}, x::AbstractVector) where {D<:Sampleable{Multivariate}} = 1 | ||||||
nsamples(::Type{D}, x::AbstractArray{<:AbstractVector}) where {D<:Sampleable{Multivariate}} = length(x) | ||||||
nsamples(::Type{D}, x::AbstractVector{<:Number}) where {D<:Sampleable{Multivariate}} = 1 | ||||||
nsamples(::Type{D}, x::AbstractMatrix) where {D<:Sampleable{Multivariate}} = size(x, 2) | ||||||
nsamples(::Type{D}, x::Number) where {D<:Sampleable{Matrixvariate}} = 1 | ||||||
nsamples(::Type{D}, x::Array{Matrix{T}}) where {D<:Sampleable{Matrixvariate},T<:Number} = length(x) | ||||||
nsamples(::Type{D}, x::AbstractMatrix{<:Number}) where {D<:Sampleable{Matrixvariate}} = 1 | ||||||
nsamples(::Type{D}, x::AbstractArray{<:AbstractMatrix{T}}) where {D<:Sampleable{Matrixvariate},T<:Number} = length(x) | ||||||
|
||||||
""" | ||||||
Distribution{F<:VariateForm,S<:ValueSupport} <: Sampleable{F,S} | ||||||
Distribution{F<:VariateForm,S<:Support} <: Sampleable{F,S} | ||||||
|
||||||
`Distribution` is a `Sampleable` generating random values from a probability | ||||||
distribution. Distributions define a Probability Distribution Function (PDF) | ||||||
to implement with `pdf` and a Cumulated Distribution Function (CDF) to implement | ||||||
with `cdf`. | ||||||
""" | ||||||
abstract type Distribution{F<:VariateForm,S<:ValueSupport} <: Sampleable{F,S} end | ||||||
|
||||||
const UnivariateDistribution{S<:ValueSupport} = Distribution{Univariate,S} | ||||||
const MultivariateDistribution{S<:ValueSupport} = Distribution{Multivariate,S} | ||||||
const MatrixDistribution{S<:ValueSupport} = Distribution{Matrixvariate,S} | ||||||
const NonMatrixDistribution = Union{UnivariateDistribution, MultivariateDistribution} | ||||||
|
||||||
const DiscreteDistribution{F<:VariateForm} = Distribution{F,Discrete} | ||||||
const ContinuousDistribution{F<:VariateForm} = Distribution{F,Continuous} | ||||||
|
||||||
const DiscreteUnivariateDistribution = Distribution{Univariate, Discrete} | ||||||
const ContinuousUnivariateDistribution = Distribution{Univariate, Continuous} | ||||||
const DiscreteMultivariateDistribution = Distribution{Multivariate, Discrete} | ||||||
const ContinuousMultivariateDistribution = Distribution{Multivariate, Continuous} | ||||||
const DiscreteMatrixDistribution = Distribution{Matrixvariate, Discrete} | ||||||
const ContinuousMatrixDistribution = Distribution{Matrixvariate, Continuous} | ||||||
abstract type Distribution{F<:VariateForm,S<:Support} <: Sampleable{F,S} end | ||||||
|
||||||
variate_form(::Type{Distribution{VF,VS}}) where {VF<:VariateForm,VS<:ValueSupport} = VF | ||||||
variate_form(::Type{T}) where {T<:Distribution} = variate_form(supertype(T)) | ||||||
const UnivariateDistribution{S<:Support} = Distribution{Univariate,S} | ||||||
const MultivariateDistribution{S<:Support} = Distribution{Multivariate,S} | ||||||
const MatrixDistribution{S<:Support} = Distribution{Matrixvariate,S} | ||||||
|
||||||
value_support(::Type{Distribution{VF,VS}}) where {VF<:VariateForm,VS<:ValueSupport} = VS | ||||||
value_support(::Type{T}) where {T<:Distribution} = value_support(supertype(T)) | ||||||
variate_form(::Type{<:Sampleable{VF, <:Support}}) where {VF<:VariateForm} = VF | ||||||
value_support(::Type{<:Sampleable{<:VariateForm,VS}}) where {VS<:Support} = VS | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be called |
||||||
|
||||||
# allow broadcasting over distribution objects | ||||||
# to be decided: how to handle multivariate/matrixvariate distributions? | ||||||
|
@@ -116,18 +114,18 @@ const DistributionType{D<:Distribution} = Type{D} | |||||
const IncompleteFormulation = Union{DistributionType,IncompleteDistribution} | ||||||
|
||||||
""" | ||||||
succprob(d::DiscreteUnivariateDistribution) | ||||||
succprob(d::UnivariateDistribution{ContiguousSupport{T}}) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Get the probability of success. | ||||||
""" | ||||||
succprob(d::DiscreteUnivariateDistribution) | ||||||
succprob(d::UnivariateDistribution{<:ContiguousSupport}) | ||||||
|
||||||
""" | ||||||
failprob(d::DiscreteUnivariateDistribution) | ||||||
failprob(d::UnivariateDistribution{ContiguousSupport{T}}) | ||||||
|
||||||
Get the probability of failure. | ||||||
""" | ||||||
failprob(d::DiscreteUnivariateDistribution) | ||||||
failprob(d::UnivariateDistribution{<:ContiguousSupport}) | ||||||
|
||||||
# Temporary fix to handle RFunctions dependencies | ||||||
""" | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
# Discrete univariate | ||
# Integer univariate | ||
|
||
convert(::Type{Binomial}, d::Bernoulli) = Binomial(1, d.p) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
@Base.deprecate expected_logdet meanlogdet | ||
|
||
function probs(d::DiscreteUnivariateDistribution) | ||
function probs(d::UnivariateDistribution{<:ContiguousSupport}) | ||
Base.depwarn("probs(d::$(typeof(d))) is deprecated. Please use pdf(d) instead.", :probs) | ||
return probs(d) | ||
end | ||
|
@@ -40,4 +40,19 @@ for fun in [:pdf, :logpdf, | |
end | ||
end | ||
|
||
@deprecate pdf(d::DiscreteUnivariateDistribution) pdf.(Ref(d), support(d)) | ||
@deprecate pdf(d::UnivariateDistribution{<:ContiguousSupport}) pdf.(Ref(d), support(d)) | ||
|
||
# No longer proposing use of many const aliases | ||
const ValueSupport = Support{Float64} | ||
const Discrete = ContiguousSupport{Int} | ||
const Continuous = ContinuousSupport{Float64} | ||
|
||
const DiscreteDistribution{F<:VariateForm} = Distribution{F,Discrete} | ||
const ContinuousDistribution{F<:VariateForm} = Distribution{F,Continuous} | ||
|
||
const DiscreteUnivariateDistribution = Distribution{Univariate, Discrete} | ||
const ContinuousUnivariateDistribution = Distribution{Univariate, Continuous} | ||
const DiscreteMultivariateDistribution = Distribution{Multivariate, Discrete} | ||
const ContinuousMultivariateDistribution = Distribution{Multivariate, Continuous} | ||
const DiscreteMatrixDistribution = Distribution{Matrixvariate, Discrete} | ||
const ContinuousMatrixDistribution = Distribution{Matrixvariate, Continuous} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea thanks, it preserves compatibility but signals what is changing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool. I had no idea that existed - I see it isn't exported in fact, so I don't feel too bad. Anyway, I'll definitely do that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I notice that it doesn't handle parametric types, so I can't do it for all of the deprecated types: const DiscreteDistribution{F<:VariateForm} = Distribution{F,Discrete}
const ContinuousDistribution{F<:VariateForm} = Distribution{F,Continuous} but I've covered most of them now. However, testing them is another matter: julia> @test_deprecated DiscreteMatrixDistribution
WARNING: Distributions.DiscreteMatrixDistribution is deprecated, use Distribution{Matrixvariate, Discrete} instead.
likely near REPL[6]:1
Log Test Failed at REPL[6]:1
Expression: $(Expr(:escape, :DiscreteMatrixDistribution))
Log Pattern: (:warn, r"deprecated"i, Ignored(), :depwarn) match_mode = :any
Captured Logs:
ERROR: There was an error during testing Is the message returned by |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,17 +5,28 @@ function getEndpoints(distr::UnivariateDistribution, epsilon::Real) | |||||
(leftEnd, rightEnd) | ||||||
end | ||||||
|
||||||
function expectation(distr::ContinuousUnivariateDistribution, g::Function, epsilon::Real) | ||||||
function expectation(distr::UnivariateDistribution{<:ContinuousSupport}, | ||||||
g::Function, epsilon::Real) | ||||||
f = x->pdf(distr,x) | ||||||
(leftEnd, rightEnd) = getEndpoints(distr, epsilon) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
integrate(x -> f(x)*g(x), leftEnd, rightEnd) | ||||||
end | ||||||
|
||||||
## Assuming that discrete distributions only take integer values. | ||||||
function expectation(distr::DiscreteUnivariateDistribution, g::Function, epsilon::Real) | ||||||
f = x->pdf(distr,x) | ||||||
## Assuming that contiguous integer distributions don't have gaps in support | ||||||
function expectation(distr::UnivariateDistribution{<:ContiguousSupport}, | ||||||
g::Function, epsilon::Real) | ||||||
(leftEnd, rightEnd) = getEndpoints(distr, epsilon) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please use a new function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not all these different implementations are needed then |
||||||
sum(x -> f(x)*g(x), leftEnd:rightEnd) | ||||||
sum(leftEnd:rightEnd) do x | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
pdf(distr,x) * g(x) | ||||||
end | ||||||
end | ||||||
|
||||||
## Any countable distribution that is finite | ||||||
function expectation(distr::UnivariateDistribution{<:CountableSupport}, | ||||||
g::Function, epsilon::Real) | ||||||
sum(support(distr)) do x | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
pdf(distr,x) * g(x) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
end | ||||||
end | ||||||
|
||||||
function expectation(distr::UnivariateDistribution, g::Function) | ||||||
|
@@ -29,6 +40,7 @@ end | |||||
# expectation(distr, x -> -log(f(x))) | ||||||
# end | ||||||
|
||||||
function kldivergence(P::UnivariateDistribution, Q::UnivariateDistribution) | ||||||
function kldivergence(P::UnivariateDistribution{S}, | ||||||
Q::UnivariateDistribution{S}) where {S <: Support} | ||||||
expectation(P, x -> let p = pdf(P,x); (p > 0)*log(p/pdf(Q,x)) end) | ||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
support
of a measure has a specific conflicting definition (https://en.wikipedia.org/wiki/Support_(measure_theory)) we might want to call this differently.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What definition are we going to use. What is the support of a
Bernoulli(1.0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we go with
T
being theeltype
of samples (see below), we should specify this here.