Commit 33ba185c authored by Dahua Lin's avatar Dahua Lin

use sampleable as base of distributions

parent 084559e6
......@@ -14,17 +14,7 @@ export
Matrixvariate,
Discrete,
Continuous,
Sampler,
UnivariateSampler,
MultivariateSampler,
MatrixSampler,
DiscreteUnivariateSampler,
ContinuousUnivariateSampler,
DiscreteMultivariateSampler,
ContinuousMultivariateSampler,
DiscreteMatrixSampler,
ContinuousMatrixSampler,
Sampleable,
rand, rand!, # random sampling
sample, sample!, # sample from a source array
......@@ -197,7 +187,7 @@ import PDMats: dim, PDMat, invquad
#### Distribution type system
abstract Distribution{F<:VariateForm,S<:ValueSupport}
abstract Distribution{F<:VariateForm,S<:ValueSupport} <: Sampleable{F,S}
typealias UnivariateDistribution{S<:ValueSupport} Distribution{Univariate,S}
typealias MultivariateDistribution{S<:ValueSupport} Distribution{Multivariate,S}
......
......@@ -208,63 +208,6 @@ pmf(d::DiscreteDistribution, args::Any...) = pdf(d, args...)
# default: inverse transform sampling
rand(d::UnivariateDistribution) = quantile(d, rand())
function rand!(d::UnivariateDistribution, A::Array)
for i in 1:length(A)
@inbounds A[i] = rand(d)
end
return A
end
function rand(d::ContinuousUnivariateDistribution, dims::Dims)
return rand!(d, Array(Float64, dims))
end
function rand(d::DiscreteUnivariateDistribution, dims::Dims)
return rand!(d, Array(Int, dims))
end
function rand(d::UnivariateDistribution, dim1::Integer, dims::Integer...)
return rand(d, map(int, tuple(dim1,dims...)))
end
function rand(d::ContinuousMultivariateDistribution)
return rand!(d, Array(Float64, dim(d)))
end
function rand(d::DiscreteMultivariateDistribution)
return rand!(d, Array(Int, dim(d)))
end
function rand(d::ContinuousMultivariateDistribution, n::Integer)
return rand!(d, Array(Float64, dim(d), n))
end
function rand(d::DiscreteMultivariateDistribution, n::Integer)
return rand!(d, Array(Int, dim(d), n))
end
function rand(d::MatrixDistribution, n::Integer)
return rand!(d, Array(Matrix{Float64}, n))
end
function rand!(d::MultivariateDistribution, X::Matrix)
if size(X, 1) != dim(d)
error("Inconsistent argument dimensions")
end
for i in 1 : size(X, 2)
X[:, i] = rand(d)
end
X
end
function rand!(d::MatrixDistribution, X::Array{Matrix{Float64}})
for i in 1:length(X)
X[i] = rand(d)
end
return X
end
function sprand(m::Integer, n::Integer, density::Real, d::Distribution)
return sprand(m, n, density, n -> rand(d, n))
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