Commit 15173b84 authored by Dahua Lin's avatar Dahua Lin

clean up APIs for matrix-variates

parent 7150d1ed
......@@ -293,11 +293,9 @@ Following methods need to be implemented for each univariate distribution type (
Return the size of each sample.
.. function:: _rand!{T<:Real}(d::D, x::AbstractMatrix{T})
.. function:: rand{T<:Real}(d::D)
Generate a matrix sample to ``x``.
This function does not need to perform dimension checking.
Generate a matrix sample.
.. function:: sampler(d::D)
......
......@@ -206,8 +206,7 @@ include("genericfit.jl")
include("samplers.jl")
include("univariates.jl")
include("multivariates.jl")
include(joinpath("matrix", "inversewishart.jl"))
include(joinpath("matrix", "wishart.jl"))
include("matrixvariates.jl")
# others
include("truncate.jl")
......
......@@ -63,10 +63,3 @@ rand(s::Sampleable{Matrixvariate}, n::Int) =
# for certain distributions
sampler(d::Distribution) = d
rand!{M<:Matrix}(d::MatrixDistribution, A::AbstractArray{M}) = _rand!(sampler(d), A)
......@@ -44,9 +44,7 @@ function mean(IW::InverseWishart)
end
end
pdf(IW::InverseWishart, X::Matrix{Float64}) = exp(logpdf(IW, X))
function logpdf(IW::InverseWishart, X::Matrix{Float64})
function _logpdf{T<:Real}(IW::InverseWishart, X::DenseMatrix{T})
if !insupport(IW, X)
return -Inf
else
......
......@@ -37,8 +37,6 @@ end
mean(w::Wishart) = w.nu * (w.Schol[:U]' * w.Schol[:U])
pdf(W::Wishart, X::Matrix{Float64}) = exp(logpdf(W, X))
function expected_logdet(W::Wishart)
logd = 0.
d = dim(W)
......@@ -58,7 +56,7 @@ function lognorm(W::Wishart)
return (W.nu / 2) * logdet(W.Schol) + (d * W.nu / 2) * log(2) + lpgamma(d, W.nu / 2)
end
function logpdf(W::Wishart, X::Matrix{Float64})
function _logpdf{T<:Real}(W::Wishart, X::DenseMatrix{T})
if !insupport(W, X)
return -Inf
else
......@@ -84,7 +82,7 @@ function rand(w::Wishart)
end
end
Z = X * w.Schol[:U]
return Z' * Z
return At_mul_B(Z, Z)
end
function entropy(W::Wishart)
......
##### Generic methods #####
# sampling
rand!{M<:Matrix}(d::MatrixDistribution, A::AbstractArray{M}) = _rand!(sampler(d), A)
rand(d::MatrixDistribution, n::Int) = _rand!(sampler(d), Array(Matrix{eltype(d)}, n))
# pdf & logpdf
_pdf{T<:Real}(d::MatrixDistribution, x::DenseMatrix{T}) = exp(_logpdf(d, x))
function logpdf{T<:Real}(d::MatrixDistribution, x::DenseMatrix{T})
size(x) == size(d) ||
throw(DimensionMismatch("Inconsistent array dimensions."))
_logpdf(d, x)
end
function pdf{T<:Real}(d::MatrixDistribution, x::DenseMatrix{T})
size(x) == size(d) ||
throw(DimensionMismatch("Inconsistent array dimensions."))
_pdf(d, x)
end
function _logpdf!{M<:Matrix}(r::AbstractArray, d::MatrixDistribution, X::AbstractArray{M})
for i = 1:length(X)
r[i] = logpdf(r, X[i])
end
return r
end
function _pdf!{M<:Matrix}(r::AbstractArray, d::MatrixDistribution, X::AbstractArray{M})
for i = 1:length(X)
r[i] = pdf(r, X[i])
end
return r
end
function logpdf!{M<:Matrix}(r::AbstractArray, d::MatrixDistribution, X::AbstractArray{M})
length(X) == length(r) ||
throw(DimensionMismatch("Inconsistent array dimensions."))
_logpdf!(r, d, X)
end
function pdf!{M<:Matrix}(r::AbstractArray, d::MatrixDistribution, X::AbstractArray{M})
length(X) == length(r) ||
throw(DimensionMismatch("Inconsistent array dimensions."))
_pdf!(r, d, X)
end
logpdf{M<:Matrix}(d::MatrixDistribution, X::AbstractArray{M}) =
_logpdf!(Array(Float64, size(X)), d, X)
pdf{M<:Matrix}(d::MatrixDistribution, X::AbstractArray{M}) =
_pdf!(Array(Float64, size(X)), d, X)
##### Specific distributions #####
for fname in ["wishart.jl", "inversewishart.jl"]
include(joinpath("matrix", fname))
end
......@@ -3,6 +3,12 @@
## sampling
function rand!(d::MultivariateDistribution, x::DenseVector)
length(x) == length(d) ||
throw(DimensionMismatch("Output size inconsistent with sample length."))
_rand!(d, x)
end
function rand!(d::MultivariateDistribution, A::DenseMatrix)
size(A,1) == length(d) ||
throw(DimensionMismatch("Output size inconsistent with sample length."))
......
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