Skip to content

Commit

Permalink
rename Dense -> Abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed Feb 11, 2016
1 parent 73758ec commit 9ae90a6
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Distributions.jl
[![Build Status](https://travis-ci.org/JuliaStats/Distributions.jl.svg?branch=master)](https://travis-ci.org/JuliaStats/Distributions.jl)
[![Coverage Status](https://coveralls.io/repos/JuliaStats/Distributions.jl/badge.svg?branch=master)](https://coveralls.io/r/JuliaStats/Distributions.jl?branch=master)

[![Distributions](http://pkg.julialang.org/badges/Distributions_0.3.svg)](http://pkg.julialang.org/?pkg=Distributions&ver=0.3)
[![Distributions](http://pkg.julialang.org/badges/Distributions_0.4.svg)](http://pkg.julialang.org/?pkg=Distributions&ver=0.4)
[![Distributions](http://pkg.julialang.org/badges/Distributions_0.5.svg)](http://pkg.julialang.org/?pkg=Distributions&ver=0.5)

A Julia package for probability distributions and associated functions. Particularly, Distributions implements:

Expand Down
4 changes: 2 additions & 2 deletions src/genericrand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ rand(s::Sampleable{Univariate}, dims::Int...) =

# multivariate

function _rand!(s::Sampleable{Multivariate}, A::DenseMatrix)
function _rand!(s::Sampleable{Multivariate}, A::AbstractMatrix)
for i = 1:size(A,2)
_rand!(s, slice(A,:,i))
end
Expand All @@ -32,7 +32,7 @@ function rand!(s::Sampleable{Multivariate}, A::AbstractVector)
_rand!(s, A)
end

function rand!(s::Sampleable{Multivariate}, A::DenseMatrix)
function rand!(s::Sampleable{Multivariate}, A::AbstractMatrix)
size(A,1) == length(s) ||
throw(DimensionMismatch("Output size inconsistent with sample length."))
_rand!(s, A)
Expand Down
4 changes: 2 additions & 2 deletions src/matrix/inversewishart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mode(d::InverseWishart) = d.Ψ * inv(d.df + dim(d) + 1.0)

#### Evaluation

function _logpdf(d::InverseWishart, X::DenseMatrix{Float64})
function _logpdf(d::InverseWishart, X::AbstractMatrix{Float64})
p = dim(d)
df = d.df
Xcf = cholfact(X)
Expand All @@ -69,7 +69,7 @@ function _logpdf(d::InverseWishart, X::DenseMatrix{Float64})
-0.5 * ((df + p + 1) * logdet(Xcf) + trace(Xcf \ Ψ)) - d.c0
end

_logpdf{T<:Real}(d::InverseWishart, X::DenseMatrix{T}) = _logpdf(d, convert(Matrix{Float64}, X))
_logpdf{T<:Real}(d::InverseWishart, X::AbstractMatrix{T}) = _logpdf(d, convert(Matrix{Float64}, X))


#### Sampling
Expand Down
4 changes: 2 additions & 2 deletions src/matrix/wishart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ end

#### Evaluation

function _logpdf(d::Wishart, X::DenseMatrix{Float64})
function _logpdf(d::Wishart, X::AbstractMatrix{Float64})
df = d.df
p = dim(d)
Xcf = cholfact(X)
0.5 * ((df - (p + 1)) * logdet(Xcf) - trace(d.S \ X)) - d.c0
end

_logpdf{T<:Real}(d::Wishart, X::DenseMatrix{T}) = _logpdf(d, convert(Matrix{Float64}, X))
_logpdf{T<:Real}(d::Wishart, X::AbstractMatrix{T}) = _logpdf(d, convert(Matrix{Float64}, X))

#### Sampling

Expand Down
6 changes: 3 additions & 3 deletions src/matrixvariates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ rand(d::MatrixDistribution, n::Int) = _rand!(sampler(d), Array(Matrix{eltype(d)}

# pdf & logpdf

_pdf{T<:Real}(d::MatrixDistribution, x::DenseMatrix{T}) = exp(_logpdf(d, x))
_pdf{T<:Real}(d::MatrixDistribution, x::AbstractMatrix{T}) = exp(_logpdf(d, x))

function logpdf{T<:Real}(d::MatrixDistribution, x::DenseMatrix{T})
function logpdf{T<:Real}(d::MatrixDistribution, x::AbstractMatrix{T})
size(x) == size(d) ||
throw(DimensionMismatch("Inconsistent array dimensions."))
_logpdf(d, x)
end

function pdf{T<:Real}(d::MatrixDistribution, x::DenseMatrix{T})
function pdf{T<:Real}(d::MatrixDistribution, x::AbstractMatrix{T})
size(x) == size(d) ||
throw(DimensionMismatch("Inconsistent array dimensions."))
_pdf(d, x)
Expand Down
39 changes: 20 additions & 19 deletions src/mixtures/mixturemodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function _mixpdf1(d::AbstractMixtureModel, x)
return v
end

function _mixpdf!(r::DenseArray, d::AbstractMixtureModel, x)
function _mixpdf!(r::AbstractArray, d::AbstractMixtureModel, x)
K = ncomponents(d)
p = probs(d)
@assert length(p) == K
Expand Down Expand Up @@ -260,7 +260,7 @@ function _mixlogpdf1(d::AbstractMixtureModel, x)
return m + log(v)
end

function _mixlogpdf!(r::DenseArray, d::AbstractMixtureModel, x)
function _mixlogpdf!(r::AbstractArray, d::AbstractMixtureModel, x)
K = ncomponents(d)
p = probs(d)
@assert length(p) == K
Expand Down Expand Up @@ -307,18 +307,19 @@ pdf(d::UnivariateMixture{Discrete}, x::Int) = _mixpdf1(d, x)
logpdf(d::UnivariateMixture{Continuous}, x::Real) = _mixlogpdf1(d, x)
logpdf(d::UnivariateMixture{Discrete}, x::Int) = _mixlogpdf1(d, x)

_pdf!(r::AbstractArray, d::UnivariateMixture, x::DenseArray) = _mixpdf!(r, d, x)
_logpdf!(r::AbstractArray, d::UnivariateMixture, x::DenseArray) = _mixlogpdf!(r, d, x)
_pdf!(r::AbstractArray, d::UnivariateMixture{Discrete}, x::UnitRange) = _mixpdf!(r, d, x)
_pdf!(r::AbstractArray, d::UnivariateMixture, x::AbstractArray) = _mixpdf!(r, d, x)
_logpdf!(r::AbstractArray, d::UnivariateMixture, x::AbstractArray) = _mixlogpdf!(r, d, x)

_pdf(d::MultivariateMixture, x::AbstractVector) = _mixpdf1(d, x)
_logpdf(d::MultivariateMixture, x::AbstractVector) = _mixlogpdf1(d, x)
_pdf!(r::AbstractArray, d::MultivariateMixture, x::DenseMatrix) = _mixpdf!(r, d, x)
_lodpdf!(r::AbstractArray, d::MultivariateMixture, x::DenseMatrix) = _mixlogpdf!(r, d, x)
_pdf!(r::AbstractArray, d::MultivariateMixture, x::AbstractMatrix) = _mixpdf!(r, d, x)
_lodpdf!(r::AbstractArray, d::MultivariateMixture, x::AbstractMatrix) = _mixlogpdf!(r, d, x)


## component-wise pdf and logpdf

function _cwise_pdf1!(r::StridedVector, d::AbstractMixtureModel, x)
function _cwise_pdf1!(r::AbstractVector, d::AbstractMixtureModel, x)
K = ncomponents(d)
length(r) == K || error("The length of r should match the number of components.")
for i = 1:K
Expand All @@ -327,7 +328,7 @@ function _cwise_pdf1!(r::StridedVector, d::AbstractMixtureModel, x)
r
end

function _cwise_logpdf1!(r::StridedVector, d::AbstractMixtureModel, x)
function _cwise_logpdf1!(r::AbstractVector, d::AbstractMixtureModel, x)
K = ncomponents(d)
length(r) == K || error("The length of r should match the number of components.")
for i = 1:K
Expand All @@ -336,7 +337,7 @@ function _cwise_logpdf1!(r::StridedVector, d::AbstractMixtureModel, x)
r
end

function _cwise_pdf!(r::StridedMatrix, d::AbstractMixtureModel, X)
function _cwise_pdf!(r::AbstractMatrix, d::AbstractMixtureModel, X)
K = ncomponents(d)
n = size(X, ndims(X))
size(r) == (n, K) || error("The size of r is incorrect.")
Expand All @@ -346,7 +347,7 @@ function _cwise_pdf!(r::StridedMatrix, d::AbstractMixtureModel, X)
r
end

function _cwise_logpdf!(r::StridedMatrix, d::AbstractMixtureModel, X)
function _cwise_logpdf!(r::AbstractMatrix, d::AbstractMixtureModel, X)
K = ncomponents(d)
n = size(X, ndims(X))
size(r) == (n, K) || error("The size of r is incorrect.")
Expand All @@ -356,15 +357,15 @@ function _cwise_logpdf!(r::StridedMatrix, d::AbstractMixtureModel, X)
r
end

componentwise_pdf!(r::StridedVector, d::UnivariateMixture, x::Real) = _cwise_pdf1!(r, d, x)
componentwise_pdf!(r::StridedVector, d::MultivariateMixture, x::AbstractVector) = _cwise_pdf1!(r, d, x)
componentwise_pdf!(r::StridedMatrix, d::UnivariateMixture, x::AbstractVector) = _cwise_pdf!(r, d, x)
componentwise_pdf!(r::StridedMatrix, d::MultivariateMixture, x::AbstractMatrix) = _cwise_pdf!(r, d, x)
componentwise_pdf!(r::AbstractVector, d::UnivariateMixture, x::Real) = _cwise_pdf1!(r, d, x)
componentwise_pdf!(r::AbstractVector, d::MultivariateMixture, x::AbstractVector) = _cwise_pdf1!(r, d, x)
componentwise_pdf!(r::AbstractMatrix, d::UnivariateMixture, x::AbstractVector) = _cwise_pdf!(r, d, x)
componentwise_pdf!(r::AbstractMatrix, d::MultivariateMixture, x::AbstractMatrix) = _cwise_pdf!(r, d, x)

componentwise_logpdf!(r::StridedVector, d::UnivariateMixture, x::Real) = _cwise_logpdf1!(r, d, x)
componentwise_logpdf!(r::StridedVector, d::MultivariateMixture, x::AbstractVector) = _cwise_logpdf1!(r, d, x)
componentwise_logpdf!(r::StridedMatrix, d::UnivariateMixture, x::AbstractVector) = _cwise_logpdf!(r, d, x)
componentwise_logpdf!(r::StridedMatrix, d::MultivariateMixture, x::AbstractMatrix) = _cwise_logpdf!(r, d, x)
componentwise_logpdf!(r::AbstractVector, d::UnivariateMixture, x::Real) = _cwise_logpdf1!(r, d, x)
componentwise_logpdf!(r::AbstractVector, d::MultivariateMixture, x::AbstractVector) = _cwise_logpdf1!(r, d, x)
componentwise_logpdf!(r::AbstractMatrix, d::UnivariateMixture, x::AbstractVector) = _cwise_logpdf!(r, d, x)
componentwise_logpdf!(r::AbstractMatrix, d::MultivariateMixture, x::AbstractMatrix) = _cwise_logpdf!(r, d, x)

componentwise_pdf(d::UnivariateMixture, x::Real) = componentwise_pdf!(Array(Float64, ncomponents(d)), d, x)
componentwise_pdf(d::UnivariateMixture, x::AbstractVector) = componentwise_pdf!(Array(Float64, length(x), ncomponents(d)), d, x)
Expand Down Expand Up @@ -394,6 +395,6 @@ end
rand(d::MixtureModel) = rand(component(d, rand(d.prior)))

rand(s::MixtureSampler) = rand(s.csamplers[rand(s.psampler)])
_rand!(s::MixtureSampler{Multivariate}, x::DenseVector) = _rand!(s.csamplers[rand(s.psampler)], x)
_rand!(s::MixtureSampler{Multivariate}, x::AbstractVector) = _rand!(s.csamplers[rand(s.psampler)], x)

sampler(d::MixtureModel) = MixtureSampler(d)
16 changes: 8 additions & 8 deletions src/multivariate/mvnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ entropy(d::AbstractMvNormal) = 0.5 * (length(d) * (Float64(log2π) + 1.0) + logd

mvnormal_c0(g::AbstractMvNormal) = -0.5 * (length(g) * Float64(log2π) + logdetcov(g))

sqmahal{T<:Real}(d::AbstractMvNormal, x::DenseMatrix{T}) = sqmahal!(Array(Float64, size(x, 2)), d, x)
sqmahal{T<:Real}(d::AbstractMvNormal, x::AbstractMatrix{T}) = sqmahal!(Array(Float64, size(x, 2)), d, x)

_logpdf{T<:Real}(d::AbstractMvNormal, x::DenseVector{T}) = mvnormal_c0(d) - 0.5 * sqmahal(d, x)
_logpdf{T<:Real}(d::AbstractMvNormal, x::AbstractVector{T}) = mvnormal_c0(d) - 0.5 * sqmahal(d, x)

function _logpdf!{T<:Real}(r::DenseArray, d::AbstractMvNormal, x::AbstractMatrix{T})
function _logpdf!{T<:Real}(r::AbstractArray, d::AbstractMvNormal, x::AbstractMatrix{T})
sqmahal!(r, d, x)
c0::Float64 = mvnormal_c0(d)
for i = 1:size(x, 2)
Expand All @@ -49,7 +49,7 @@ function _logpdf!{T<:Real}(r::DenseArray, d::AbstractMvNormal, x::AbstractMatrix
r
end

_pdf!{T<:Real}(r::DenseArray, d::AbstractMvNormal, x::AbstractMatrix{T}) = exp!(_logpdf!(r, d, x))
_pdf!{T<:Real}(r::AbstractArray, d::AbstractMvNormal, x::AbstractMatrix{T}) = exp!(_logpdf!(r, d, x))


###########################################################
Expand Down Expand Up @@ -118,9 +118,9 @@ logdetcov(d::MvNormal) = logdet(d.Σ)

### Evaluation

sqmahal{T<:Real}(d::MvNormal, x::DenseVector{T}) = invquad(d.Σ, x - d.μ)
sqmahal{T<:Real}(d::MvNormal, x::AbstractVector{T}) = invquad(d.Σ, x - d.μ)

sqmahal!{T<:Real}(r::DenseVector, d::MvNormal, x::DenseMatrix{T}) =
sqmahal!{T<:Real}(r::AbstractVector, d::MvNormal, x::AbstractMatrix{T}) =
invquad!(r, d.Σ, x .- d.μ)

gradlogpdf(d::MvNormal, x::Vector{Float64}) = -(d.Σ \ (x - d.μ))
Expand All @@ -129,8 +129,8 @@ gradlogpdf(d::MvNormal, x::Vector{Float64}) = -(d.Σ \ (x - d.μ))

_rand!(d::MvNormal, x::VecOrMat{Float64}) = add!(unwhiten!(d.Σ, randn!(x)), d.μ)

# Workaround: randn! only works for Array, but not generally for DenseArray
function _rand!(d::MvNormal, x::DenseVecOrMat{Float64})
# Workaround: randn! only works for Array, but not generally for AbstractArray
function _rand!(d::MvNormal, x::AbstractVecOrMat{Float64})
for i = 1:length(x)
@inbounds x[i] = randn()
end
Expand Down
12 changes: 6 additions & 6 deletions src/multivariate/mvnormalcanon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ logdetcov(d::MvNormalCanon) = -logdet(d.J)

### Evaluation

sqmahal(d::MvNormalCanon, x::DenseVector{Float64}) = quad(d.J, x - d.μ)
sqmahal!(r::DenseVector{Float64}, d::MvNormalCanon, x::DenseMatrix{Float64}) = quad!(r, d.J, x .- d.μ)
sqmahal(d::MvNormalCanon, x::AbstractVector{Float64}) = quad(d.J, x - d.μ)
sqmahal!(r::AbstractVector{Float64}, d::MvNormalCanon, x::AbstractMatrix{Float64}) = quad!(r, d.J, x .- d.μ)


# Sampling (for GenericMvNormal)

unwhiten_winv!(J::AbstractPDMat, x::DenseVecOrMat) = unwhiten!(inv(J), x)
unwhiten_winv!(J::PDiagMat, x::DenseVecOrMat) = whiten!(J, x)
unwhiten_winv!(J::ScalMat, x::DenseVecOrMat) = whiten!(J, x)
unwhiten_winv!(J::AbstractPDMat, x::AbstractVecOrMat) = unwhiten!(inv(J), x)
unwhiten_winv!(J::PDiagMat, x::AbstractVecOrMat) = whiten!(J, x)
unwhiten_winv!(J::ScalMat, x::AbstractVecOrMat) = whiten!(J, x)

_rand!(d::MvNormalCanon, x::DenseVecOrMat) = add!(unwhiten_winv!(d.J, randn!(x)), d.μ)
_rand!(d::MvNormalCanon, x::AbstractVecOrMat) = add!(unwhiten_winv!(d.J, randn!(x)), d.μ)
18 changes: 9 additions & 9 deletions src/multivariate/mvtdist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ end
insupport{T<:Real}(d::AbstractMvTDist, x::AbstractVector{T}) =
length(d) == length(x) && allfinite(x)

function sqmahal{T<:Real}(d::GenericMvTDist, x::DenseVector{T})
function sqmahal{T<:Real}(d::GenericMvTDist, x::AbstractVector{T})
z::Vector{Float64} = d.zeromean ? x : x - d.μ
invquad(d.Σ, z)
end

function sqmahal!{T<:Real}(r::DenseArray, d::GenericMvTDist, x::DenseMatrix{T})
function sqmahal!{T<:Real}(r::AbstractArray, d::GenericMvTDist, x::AbstractMatrix{T})
z::Matrix{Float64} = d.zeromean ? x : x .- d.μ
invquad!(r, d.Σ, z)
end

sqmahal{T<:Real}(d::AbstractMvTDist, x::DenseMatrix{T}) = sqmahal!(Array(Float64, size(x, 2)), d, x)
sqmahal{T<:Real}(d::AbstractMvTDist, x::AbstractMatrix{T}) = sqmahal!(Array(Float64, size(x, 2)), d, x)


function mvtdist_consts(d::AbstractMvTDist)
Expand All @@ -115,12 +115,12 @@ function mvtdist_consts(d::AbstractMvTDist)
return (shdfhdim, v)
end

function _logpdf{T<:Real}(d::AbstractMvTDist, x::DenseVector{T})
function _logpdf{T<:Real}(d::AbstractMvTDist, x::AbstractVector{T})
shdfhdim, v = mvtdist_consts(d)
v - shdfhdim * log1p(sqmahal(d, x) / d.df)
end

function _logpdf!{T<:Real}(r::DenseArray, d::AbstractMvTDist, x::DenseMatrix{T})
function _logpdf!{T<:Real}(r::AbstractArray, d::AbstractMvTDist, x::AbstractMatrix{T})
sqmahal!(r, d, x)
shdfhdim, v = mvtdist_consts(d)
for i = 1:size(x, 2)
Expand All @@ -129,17 +129,17 @@ function _logpdf!{T<:Real}(r::DenseArray, d::AbstractMvTDist, x::DenseMatrix{T})
return r
end

_pdf!{T<:Real}(r::DenseArray, d::AbstractMvNormal, x::DenseMatrix{T}) = exp!(_logpdf!(r, d, x))
_pdf!{T<:Real}(r::AbstractArray, d::AbstractMvNormal, x::AbstractMatrix{T}) = exp!(_logpdf!(r, d, x))

function gradlogpdf{T<:Real}(d::GenericMvTDist, x::DenseVector{T})
function gradlogpdf{T<:Real}(d::GenericMvTDist, x::AbstractVector{T})
z::Vector{Float64} = d.zeromean ? x : x - d.μ
prz = invscale(d)*z
-((d.df + d.dim) / (d.df + dot(z, prz))) * prz
end

# Sampling (for GenericMvTDist)

function _rand!{T<:Real}(d::GenericMvTDist, x::DenseVector{T})
function _rand!{T<:Real}(d::GenericMvTDist, x::AbstractVector{T})
chisqd = Chisq(d.df)
y = sqrt(rand(chisqd)/(d.df))
unwhiten!(d.Σ, randn!(x))
Expand All @@ -150,7 +150,7 @@ function _rand!{T<:Real}(d::GenericMvTDist, x::DenseVector{T})
x
end

function _rand!{T<:Real}(d::GenericMvTDist, x::DenseMatrix{T})
function _rand!{T<:Real}(d::GenericMvTDist, x::AbstractMatrix{T})
cols = size(x,2)
chisqd = Chisq(d.df)
y = Array(Float64, 1, cols)
Expand Down
8 changes: 4 additions & 4 deletions src/multivariate/vonmisesfisher.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ length(d::VonMisesFisher) = length(d.μ)
meandir(d::VonMisesFisher) = d.μ
concentration(d::VonMisesFisher) = d.κ

insupport{T<:Real}(d::VonMisesFisher, x::DenseVector{T}) = isunitvec(x)
insupport{T<:Real}(d::VonMisesFisher, x::AbstractVector{T}) = isunitvec(x)
params(d::VonMisesFisher) = (d.μ, d.κ)

### Evaluation
Expand All @@ -54,15 +54,15 @@ end
_vmflck3(κ) = log(κ) - log2π - κ - log1mexp(-2.0 * κ)
vmflck(p, κ) = (p == 3 ? _vmflck3(κ) : _vmflck(p, κ))::Float64

_logpdf{T<:Real}(d::VonMisesFisher, x::DenseVector{T}) = d.logCκ + d.κ * dot(d.μ, x)
_logpdf{T<:Real}(d::VonMisesFisher, x::AbstractVector{T}) = d.logCκ + d.κ * dot(d.μ, x)


### Sampling

sampler(d::VonMisesFisher) = VonMisesFisherSampler(d.μ, d.κ)

_rand!(d::VonMisesFisher, x::DenseVector) = _rand!(sampler(d), x)
_rand!(d::VonMisesFisher, x::DenseMatrix) = _rand!(sampler(d), x)
_rand!(d::VonMisesFisher, x::AbstractVector) = _rand!(sampler(d), x)
_rand!(d::VonMisesFisher, x::AbstractMatrix) = _rand!(sampler(d), x)


### Estimation
Expand Down
Loading

0 comments on commit 9ae90a6

Please sign in to comment.