Skip to content

Commit

Permalink
Fix deprecations (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
femtocleaner[bot] authored and ararslan committed Aug 16, 2017
1 parent ebeab79 commit c13f0b6
Show file tree
Hide file tree
Showing 95 changed files with 920 additions and 926 deletions.
16 changes: 8 additions & 8 deletions perf/samplers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ using Distributions

### define benchmark tasks

@compat abstract type UnivariateSamplerRun{Spl} <: Proc end
type BatchSamplerRun{Spl} <: UnivariateSamplerRun{Spl} end
type IndivSamplerRun{Spl} <: UnivariateSamplerRun{Spl} end
abstract type UnivariateSamplerRun{Spl} <: Proc end
mutable struct BatchSamplerRun{Spl} <: UnivariateSamplerRun{Spl} end
mutable struct IndivSamplerRun{Spl} <: UnivariateSamplerRun{Spl} end

const batch_unit = 1000

Base.isvalid(::UnivariateSamplerRun, cfg) = true
Base.length(p::UnivariateSamplerRun, cfg) = batch_unit
Base.string{Spl}(p::UnivariateSamplerRun{Spl}) = getname(Spl)
Base.string(p::UnivariateSamplerRun{Spl}) where {Spl} = getname(Spl)

Base.start{Spl}(p::BatchSamplerRun{Spl}, cfg) = getsampler(Spl, cfg)
Base.start{Spl}(p::IndivSamplerRun{Spl}, cfg) = ()
Base.start(p::BatchSamplerRun{Spl}, cfg) where {Spl} = getsampler(Spl, cfg)
Base.start(p::IndivSamplerRun{Spl}, cfg) where {Spl} = ()
Base.done(p::UnivariateSamplerRun, cfg, s) = nothing

getsampler{Spl<:Sampleable}(::Type{Spl}, cfg) = Spl(cfg...)
getsampler(::Type{Spl}, cfg) where {Spl<:Sampleable} = Spl(cfg...)

function Base.run(p::BatchSamplerRun, cfg, s)
for i = 1:batch_unit
rand(s)
end
end

function Base.run{Spl}(p::IndivSamplerRun{Spl}, cfg, s)
function Base.run(p::IndivSamplerRun{Spl}, cfg, s) where Spl
for i = 1:batch_unit
rand(getsampler(Spl,cfg))
end
Expand Down
60 changes: 30 additions & 30 deletions src/common.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
## sample space/domain

@compat abstract type VariateForm end
type Univariate <: VariateForm end
type Multivariate <: VariateForm end
type Matrixvariate <: VariateForm end
abstract type VariateForm end
mutable struct Univariate <: VariateForm end
mutable struct Multivariate <: VariateForm end
mutable struct Matrixvariate <: VariateForm end

@compat abstract type ValueSupport end
type Discrete <: ValueSupport end
type Continuous <: ValueSupport end
abstract type ValueSupport end
mutable struct Discrete <: ValueSupport end
mutable struct Continuous <: ValueSupport end

Base.eltype(::Type{Discrete}) = Int
Base.eltype(::Type{Continuous}) = Float64

## Sampleable

@compat abstract type Sampleable{F<:VariateForm,S<:ValueSupport} end
abstract type Sampleable{F<:VariateForm,S<:ValueSupport} end

"""
length(s::Sampleable)
Expand Down Expand Up @@ -42,9 +42,9 @@ The default element type of a sample. This is the type of elements of the sample
by the `rand` method. However, one can provide an array of different element types to
store the samples using `rand!`.
"""
Base.eltype{F,S}(s::Sampleable{F,S}) = eltype(S)
Base.eltype{F}(s::Sampleable{F,Discrete}) = Int
Base.eltype{F}(s::Sampleable{F,Continuous}) = Float64
Base.eltype(s::Sampleable{F,S}) where {F,S} = eltype(S)
Base.eltype(s::Sampleable{F,Discrete}) where {F} = Int
Base.eltype(s::Sampleable{F,Continuous}) where {F} = Float64

"""
nsamples(s::Sampleable)
Expand All @@ -53,24 +53,24 @@ The number of samples contained in `A`. Multiple samples are often organized int
depending on the variate form.
"""
nsamples(t::Type{Sampleable}, x::Any)
nsamples{D<:Sampleable{Univariate}}(::Type{D}, x::Number) = 1
nsamples{D<:Sampleable{Univariate}}(::Type{D}, x::AbstractArray) = length(x)
nsamples{D<:Sampleable{Multivariate}}(::Type{D}, x::AbstractVector) = 1
nsamples{D<:Sampleable{Multivariate}}(::Type{D}, x::AbstractMatrix) = size(x, 2)
nsamples{D<:Sampleable{Matrixvariate}}(::Type{D}, x::Number) = 1
nsamples{D<:Sampleable{Matrixvariate},T<:Number}(::Type{D}, x::Array{Matrix{T}}) = length(x)
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::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)

## Distribution <: Sampleable

@compat abstract type Distribution{F<:VariateForm,S<:ValueSupport} <: Sampleable{F,S} end
abstract type Distribution{F<:VariateForm,S<:ValueSupport} <: Sampleable{F,S} end

@compat const UnivariateDistribution{S<:ValueSupport} = Distribution{Univariate,S}
@compat const MultivariateDistribution{S<:ValueSupport} = Distribution{Multivariate,S}
@compat const MatrixDistribution{S<:ValueSupport} = Distribution{Matrixvariate,S}
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}

@compat const DiscreteDistribution{F<:VariateForm} = Distribution{F,Discrete}
@compat const ContinuousDistribution{F<:VariateForm} = Distribution{F,Continuous}
const DiscreteDistribution{F<:VariateForm} = Distribution{F,Discrete}
const ContinuousDistribution{F<:VariateForm} = Distribution{F,Continuous}

const DiscreteUnivariateDistribution = Distribution{Univariate, Discrete}
const ContinuousUnivariateDistribution = Distribution{Univariate, Continuous}
Expand All @@ -79,15 +79,15 @@ const ContinuousMultivariateDistribution = Distribution{Multivariate, Continuou
const DiscreteMatrixDistribution = Distribution{Matrixvariate, Discrete}
const ContinuousMatrixDistribution = Distribution{Matrixvariate, Continuous}

variate_form{VF<:VariateForm,VS<:ValueSupport}(::Type{Distribution{VF,VS}}) = VF
variate_form{T<:Distribution}(::Type{T}) = variate_form(supertype(T))
variate_form(::Type{Distribution{VF,VS}}) where {VF<:VariateForm,VS<:ValueSupport} = VF
variate_form(::Type{T}) where {T<:Distribution} = variate_form(supertype(T))

value_support{VF<:VariateForm,VS<:ValueSupport}(::Type{Distribution{VF,VS}}) = VS
value_support{T<:Distribution}(::Type{T}) = value_support(supertype(T))
value_support(::Type{Distribution{VF,VS}}) where {VF<:VariateForm,VS<:ValueSupport} = VS
value_support(::Type{T}) where {T<:Distribution} = value_support(supertype(T))

## TODO: the following types need to be improved
@compat abstract type SufficientStats end
@compat abstract type IncompleteDistribution end
abstract type SufficientStats end
abstract type IncompleteDistribution end

@compat const DistributionType{D<:Distribution} = Type{D}
const DistributionType{D<:Distribution} = Type{D}
const IncompleteFormulation = Union{DistributionType,IncompleteDistribution}
14 changes: 7 additions & 7 deletions src/edgeworth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

# Edgeworth approximation of the Z statistic
# EdgeworthSum and EdgeworthMean are both defined in terms of this
@compat abstract type EdgeworthAbstract <: ContinuousUnivariateDistribution end
abstract type EdgeworthAbstract <: ContinuousUnivariateDistribution end

skewness(d::EdgeworthAbstract) = skewness(d.dist) / sqrt(d.n)
kurtosis(d::EdgeworthAbstract) = kurtosis(d.dist) / d.n

immutable EdgeworthZ{D<:UnivariateDistribution} <: EdgeworthAbstract
struct EdgeworthZ{D<:UnivariateDistribution} <: EdgeworthAbstract
dist::D
n::Float64

function (::Type{EdgeworthZ{D}}){D<:UnivariateDistribution,T<:UnivariateDistribution}(d::T, n::Real)
function EdgeworthZ{D}(d::T, n::Real) where {D<:UnivariateDistribution,T<:UnivariateDistribution}
@check_args(EdgeworthZ, n > zero(n))
new{D}(d, n)
end
Expand Down Expand Up @@ -74,10 +74,10 @@ end


# Edgeworth approximation of the sum
immutable EdgeworthSum{D<:UnivariateDistribution} <: EdgeworthAbstract
struct EdgeworthSum{D<:UnivariateDistribution} <: EdgeworthAbstract
dist::D
n::Float64
function (::Type{EdgeworthSum{D}}){D<:UnivariateDistribution,T<:UnivariateDistribution}(d::T, n::Real)
function EdgeworthSum{D}(d::T, n::Real) where {D<:UnivariateDistribution,T<:UnivariateDistribution}
@check_args(EdgeworthSum, n > zero(n))
new{D}(d, n)
end
Expand All @@ -88,10 +88,10 @@ mean(d::EdgeworthSum) = d.n*mean(d.dist)
var(d::EdgeworthSum) = d.n*var(d.dist)

# Edgeworth approximation of the mean
immutable EdgeworthMean{D<:UnivariateDistribution} <: EdgeworthAbstract
struct EdgeworthMean{D<:UnivariateDistribution} <: EdgeworthAbstract
dist::D
n::Float64
function (::Type{EdgeworthMean{D}}){D<:UnivariateDistribution,T<:UnivariateDistribution}(d::T, n::Real)
function EdgeworthMean{D}(d::T, n::Real) where {D<:UnivariateDistribution,T<:UnivariateDistribution}
# although n would usually be an integer, no methods are require this
n > zero(n) ||
error("n must be positive")
Expand Down
6 changes: 3 additions & 3 deletions src/empirical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
##############################################################################

immutable EmpiricalUnivariateDistribution <: ContinuousUnivariateDistribution
struct EmpiricalUnivariateDistribution <: ContinuousUnivariateDistribution
values::Vector{Float64}
support::Vector{Float64}
cdf::Function
Expand Down Expand Up @@ -70,7 +70,7 @@ end

### fit model

function fit_mle{T <: Real}(::Type{EmpiricalUnivariateDistribution},
x::Vector{T})
function fit_mle(::Type{EmpiricalUnivariateDistribution},
x::Vector{T}) where T <: Real
EmpiricalUnivariateDistribution(x)
end
14 changes: 7 additions & 7 deletions src/estimators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
export Estimator, MLEstimator
export nsamples, estimate

@compat abstract type Estimator{D<:Distribution} end
abstract type Estimator{D<:Distribution} end

nsamples{D<:UnivariateDistribution}(e::Estimator{D}, x::Array) = length(x)
nsamples{D<:MultivariateDistribution}(e::Estimator{D}, x::Matrix) = size(x, 2)
nsamples(e::Estimator{D}, x::Array) where {D<:UnivariateDistribution} = length(x)
nsamples(e::Estimator{D}, x::Matrix) where {D<:MultivariateDistribution} = size(x, 2)

type MLEstimator{D<:Distribution} <: Estimator{D} end
MLEstimator{D<:Distribution}(::Type{D}) = MLEstimator{D}()
mutable struct MLEstimator{D<:Distribution} <: Estimator{D} end
MLEstimator(::Type{D}) where {D<:Distribution} = MLEstimator{D}()

estimate{D<:Distribution}(e::MLEstimator{D}, x) = fit_mle(D, x)
estimate{D<:Distribution}(e::MLEstimator{D}, x, w) = fit_mle(D, x, w)
estimate(e::MLEstimator{D}, x) where {D<:Distribution} = fit_mle(D, x)
estimate(e::MLEstimator{D}, x, w) where {D<:Distribution} = fit_mle(D, x, w)
14 changes: 7 additions & 7 deletions src/genericfit.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generic functions for distribution fitting

function suffstats{D<:Distribution}(dt::Type{D}, xs...)
function suffstats(dt::Type{D}, xs...) where D<:Distribution
argtypes = tuple(D, map(typeof, xs)...)
error("suffstats is not implemented for $argtypes.")
end
Expand All @@ -24,11 +24,11 @@ Here, `w` should be an array with length `n`, where `n` is the number of samples
"""
fit_mle(D, x, w)

fit_mle{D<:UnivariateDistribution}(dt::Type{D}, x::AbstractArray) = fit_mle(D, suffstats(D, x))
fit_mle{D<:UnivariateDistribution}(dt::Type{D}, x::AbstractArray, w::AbstractArray) = fit_mle(D, suffstats(D, x, w))
fit_mle(dt::Type{D}, x::AbstractArray) where {D<:UnivariateDistribution} = fit_mle(D, suffstats(D, x))
fit_mle(dt::Type{D}, x::AbstractArray, w::AbstractArray) where {D<:UnivariateDistribution} = fit_mle(D, suffstats(D, x, w))

fit_mle{D<:MultivariateDistribution}(dt::Type{D}, x::AbstractMatrix) = fit_mle(D, suffstats(D, x))
fit_mle{D<:MultivariateDistribution}(dt::Type{D}, x::AbstractMatrix, w::AbstractArray) = fit_mle(D, suffstats(D, x, w))
fit_mle(dt::Type{D}, x::AbstractMatrix) where {D<:MultivariateDistribution} = fit_mle(D, suffstats(D, x))
fit_mle(dt::Type{D}, x::AbstractMatrix, w::AbstractArray) where {D<:MultivariateDistribution} = fit_mle(D, suffstats(D, x, w))

fit{D<:Distribution}(dt::Type{D}, x) = fit_mle(D, x)
fit{D<:Distribution}(dt::Type{D}, args...) = fit_mle(D, args...)
fit(dt::Type{D}, x) where {D<:Distribution} = fit_mle(D, x)
fit(dt::Type{D}, args...) where {D<:Distribution} = fit_mle(D, args...)
4 changes: 2 additions & 2 deletions src/genericrand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ rand(s::Sampleable{Multivariate}, n::Int) =

# matrix-variate

function _rand!{M<:Matrix}(s::Sampleable{Matrixvariate}, X::AbstractArray{M})
function _rand!(s::Sampleable{Matrixvariate}, X::AbstractArray{M}) where M<:Matrix
for i in 1:length(X)
X[i] = rand(s)
end
return X
end

rand!{M<:Matrix}(s::Sampleable{Matrixvariate}, X::AbstractArray{M}) =
rand!(s::Sampleable{Matrixvariate}, X::AbstractArray{M}) where {M<:Matrix} =
_rand!(s, X)

rand(s::Sampleable{Matrixvariate}, n::Int) =
Expand Down
12 changes: 6 additions & 6 deletions src/matrix/inversewishart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ The [Inverse Wishart distribution](http://en.wikipedia.org/wiki/Inverse-Wishart_
is usually used a the conjugate prior for the covariance matrix of a multivariate normal
distribution, which is characterized by a degree of freedom ν, and a base matrix Φ.
"""
immutable InverseWishart{T<:Real, ST<:AbstractPDMat} <: ContinuousMatrixDistribution
struct InverseWishart{T<:Real, ST<:AbstractPDMat} <: ContinuousMatrixDistribution
df::T # degree of freedom
Ψ::ST # scale matrix
c0::T # log of normalizing constant
end

#### Constructors

function InverseWishart{T<:Real}(df::T, Ψ::AbstractPDMat{T})
function InverseWishart(df::T, Ψ::AbstractPDMat{T}) where T<:Real
p = dim(Ψ)
df > p - 1 || error("df should be greater than dim - 1.")
c0 = _invwishart_c0(df, Ψ)
Expand Down Expand Up @@ -46,14 +46,14 @@ insupport(d::InverseWishart, X::Matrix) = size(X) == size(d) && isposdef(X)
dim(d::InverseWishart) = dim(d.Ψ)
size(d::InverseWishart) = (p = dim(d); (p, p))
params(d::InverseWishart) = (d.df, d.Ψ, d.c0)
@inline partype{T<:Real}(d::InverseWishart{T}) = T
@inline partype(d::InverseWishart{T}) where {T<:Real} = T

### Conversion
function convert{T<:Real}(::Type{InverseWishart{T}}, d::InverseWishart)
function convert(::Type{InverseWishart{T}}, d::InverseWishart) where T<:Real
P = Wishart{T}(d.Ψ)
InverseWishart{T, typeof(P)}(T(d.df), P, T(d.c0))
end
function convert{T<:Real}(::Type{InverseWishart{T}}, df, Ψ::AbstractPDMat, c0)
function convert(::Type{InverseWishart{T}}, df, Ψ::AbstractPDMat, c0) where T<:Real
P = Wishart{T}(Ψ)
InverseWishart{T, typeof(P)}(T(df), P, T(c0))
end
Expand Down Expand Up @@ -95,7 +95,7 @@ end

rand(d::InverseWishart) = inv(cholfact!(rand(Wishart(d.df, inv(d.Ψ)))))

function _rand!{M<:Matrix}(d::InverseWishart, X::AbstractArray{M})
function _rand!(d::InverseWishart, X::AbstractArray{M}) where M<:Matrix
wd = Wishart(d.df, inv(d.Ψ))
for i in 1:length(X)
X[i] = inv(cholfact!(rand(wd)))
Expand Down
10 changes: 5 additions & 5 deletions src/matrix/wishart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ The [Wishart distribution](http://en.wikipedia.org/wiki/Wishart_distribution) is
multidimensional generalization of the Chi-square distribution, which is characterized by
a degree of freedom ν, and a base matrix S.
"""
immutable Wishart{T<:Real, ST<:AbstractPDMat} <: ContinuousMatrixDistribution
struct Wishart{T<:Real, ST<:AbstractPDMat} <: ContinuousMatrixDistribution
df::T # degree of freedom
S::ST # the scale matrix
c0::T # the logarithm of normalizing constant in pdf
end

#### Constructors

function Wishart{T<:Real}(df::T, S::AbstractPDMat{T})
function Wishart(df::T, S::AbstractPDMat{T}) where T<:Real
p = dim(S)
df > p - 1 || error("dpf should be greater than dim - 1.")
c0 = _wishart_c0(df, S)
Expand Down Expand Up @@ -50,14 +50,14 @@ insupport(d::Wishart, X::Matrix) = size(X) == size(d) && isposdef(X)
dim(d::Wishart) = dim(d.S)
size(d::Wishart) = (p = dim(d); (p, p))
params(d::Wishart) = (d.df, d.S, d.c0)
@inline partype{T<:Real}(d::Wishart{T}) = T
@inline partype(d::Wishart{T}) where {T<:Real} = T

### Conversion
function convert{T<:Real}(::Type{Wishart{T}}, d::Wishart)
function convert(::Type{Wishart{T}}, d::Wishart) where T<:Real
P = AbstractMatrix{T}(d.S)
Wishart{T, typeof(P)}(T(d.df), P, T(d.c0))
end
function convert{T<:Real}(::Type{Wishart{T}}, df, S::AbstractPDMat, c0)
function convert(::Type{Wishart{T}}, df, S::AbstractPDMat, c0) where T<:Real
P = AbstractMatrix{T}(S)
Wishart{T, typeof(P)}(T(df), P, T(c0))
end
Expand Down
Loading

0 comments on commit c13f0b6

Please sign in to comment.