Commit e3c71bd8 authored by Dahua Lin's avatar Dahua Lin

Deprecate dim(::MultivariateDistribution) in favor of length

parent f14ddc20
......@@ -219,4 +219,6 @@ include("estimators.jl")
# mixture distributions (TODO: moveout)
include("mixturemodel.jl")
include("deprecates.jl")
end # module
......@@ -53,11 +53,6 @@ variate_form{T<:Distribution}(::Type{T}) = variate_form(super(T))
value_support{VF<:VariateForm,VS<:ValueSupport}(::Type{Distribution{VF,VS}}) = VS
value_support{T<:Distribution}(::Type{T}) = value_support(super(T))
## TODO: replace dim with length in specialized methods
Base.length(d::MultivariateDistribution) = dim(d)
Base.size(d::MatrixDistribution) = (dim(d), dim(d)) # override if the matrix isn't square
## TODO: the following types need to be improved
abstract SufficientStats
abstract IncompleteDistribution
......
......@@ -24,17 +24,15 @@ end
posterior_canon(pri::Dirichlet, ss::MultinomialStats) = DirichletCanon(pri.alpha + ss.scnts)
function posterior_canon{T<:Real}(pri::Dirichlet, G::Type{Multinomial}, x::Matrix{T})
d = dim(pri)
d = length(pri)
size(x,1) == d || throw(ArgumentError("Inconsistent argument dimensions."))
a = add!(sum(x, 2), pri.alpha)
DirichletCanon(vec(a))
end
function posterior_canon{T<:Real}(pri::Dirichlet, G::Type{Multinomial}, x::Matrix{T}, w::Array{Float64})
d = dim(pri)
if !(size(x,1) == d && size(x,2) == length(w))
throw(ArgumentError("Inconsistent argument dimensions."))
end
d = length(pri)
size(x) == (d, length(w)) || throw(ArgumentError("Inconsistent argument dimensions."))
a = copy(pri.alpha)
Base.LinAlg.BLAS.gemv!('N', 1.0, float64(x), vec(w), 1.0, a)
DirichletCanon(a)
......
#### Deprecate on 0.5 (to be removed on 0.6)
function dim(d::MultivariateDistribution)
Base.depwarn("dim(d::MultivariateDistribution) is deprecated. Please use length(d).", :dim)
return length(d)
end
......@@ -19,6 +19,7 @@ immutable InverseWishart <: ContinuousMatrixDistribution
end
end
dim(d::InverseWishart) = size(d.Psichol, 1)
size(d::InverseWishart) = size(d.Psichol)
function InverseWishart(nu::Real, Psi::Matrix{Float64})
......
......@@ -23,6 +23,9 @@ end
Wishart(nu::Real, S::Matrix{Float64}) = Wishart(nu, cholfact(S))
dim(W::Wishart) = size(W.Schol, 1)
size(W::Wishart) = size(W.Schol)
function insupport(W::Wishart, X::Matrix{Float64})
return size(X, 1) == size(X, 2) && isApproxSymmmetric(X) &&
size(X, 1) == size(W.Schol, 1) && hasCholesky(X)
......@@ -36,9 +39,6 @@ mean(w::Wishart) = w.nu * (w.Schol[:U]' * w.Schol[:U])
pdf(W::Wishart, X::Matrix{Float64}) = exp(logpdf(W, X))
size(W::Wishart) = size(W.Schol)
dim(W::Wishart) = size(W.Schol, 1)
function expected_logdet(W::Wishart)
logd = 0.
d = dim(W)
......
......@@ -28,14 +28,13 @@ immutable DirichletCanon
alpha::Vector{Float64}
end
dim(d::DirichletCanon) = length(d.alpha)
length(d::DirichletCanon) = length(d.alpha)
Base.convert(::Type{Dirichlet}, cf::DirichletCanon) = Dirichlet(cf.alpha)
# Properties
dim(d::Dirichlet) = length(d.alpha)
length(d::Dirichlet) = length(d.alpha)
mean(d::Dirichlet) = d.alpha .* inv(d.alpha0)
function var(d::Dirichlet)
......@@ -193,7 +192,7 @@ immutable DirichletStats <: SufficientStats
DirichletStats(slogp::Vector{Float64}, tw::Real) = new(slogp, float64(tw))
end
dim(ss::DirichletStats) = length(s.slogp)
length(ss::DirichletStats) = length(s.slogp)
mean_logp(ss::DirichletStats) = ss.slogp * inv(ss.tw)
......
......@@ -18,7 +18,7 @@ end
# Properties
dim(d::Multinomial) = length(d.prob)
length(d::Multinomial) = length(d.prob)
mean(d::Multinomial) = d.n .* d.prob
......@@ -173,7 +173,7 @@ end
function rand!{T <: Real}(s::MultinomialSampler, x::Vector{T})
d::Multinomial = s.d
n::Int = d.n
k = dim(s)
k = length(s)
if n^2 > k
d = s.d
......@@ -189,10 +189,10 @@ function rand!{T <: Real}(s::MultinomialSampler, x::Vector{T})
return x
end
dim(s::MultinomialSampler) = length(s.d.prob)
length(s::MultinomialSampler) = length(s.d.prob)
sampler(d::Multinomial) = MultinomialSampler(d)
rand(s::MultinomialSampler) = rand!(s, zeros(Int, dim(s)))
rand(s::MultinomialSampler) = rand!(s, zeros(Int, length(s)))
## Fit model
......
......@@ -63,7 +63,7 @@ gmvnormal(Σ::Matrix{Float64}) = MvNormal(Σ)
# Basic statistics
dim(d::GenericMvNormal) = d.dim
length(d::GenericMvNormal) = d.dim
mean(d::GenericMvNormal) = d.μ
mode(d::GenericMvNormal) = d.μ
......@@ -74,7 +74,7 @@ cov(d::GenericMvNormal) = full(d.Σ)
invcov(d::GenericMvNormal) = full(inv(d.Σ))
logdet_cov(d::GenericMvNormal) = logdet(d.Σ)
entropy(d::GenericMvNormal) = 0.5 * (dim(d) * (float64(log2π) + 1.0) + logdet_cov(d))
entropy(d::GenericMvNormal) = 0.5 * (length(d) * (float64(log2π) + 1.0) + logdet_cov(d))
# evaluation (for GenericMvNormal)
......@@ -85,7 +85,7 @@ function sqmahal(d::GenericMvNormal, x::Vector{Float64})
end
function sqmahal!(r::Array{Float64}, d::GenericMvNormal, x::Matrix{Float64})
if !(size(x, 1) == dim(d) && size(x, 2) == length(r))
if !(size(x, 1) == length(d) && size(x, 2) == length(r))
throw(ArgumentError("Inconsistent argument dimensions."))
end
z::Matrix{Float64} = d.zeromean ? x : x .- d.μ
......@@ -95,10 +95,10 @@ end
# generic PDF evaluation (appliable to AbstractMvNormal)
insupport{T<:Real}(d::AbstractMvNormal, x::Vector{T}) = dim(d) == length(x) && allfinite(x)
insupport{T<:Real}(d::AbstractMvNormal, x::Vector{T}) = length(d) == length(x) && allfinite(x)
insupport{G<:AbstractMvNormal,T<:Real}(::Type{G}, x::Vector{T}) = allfinite(x)
mvnormal_c0(g::AbstractMvNormal) = -0.5 * (dim(g) * float64(log2π) + logdet_cov(g))
mvnormal_c0(g::AbstractMvNormal) = -0.5 * (length(g) * float64(log2π) + logdet_cov(g))
sqmahal(d::AbstractMvNormal, x::Matrix{Float64}) = sqmahal!(Array(Float64, size(x, 2)), d, x)
......@@ -156,7 +156,7 @@ IsoNormalKnownSigma(dimension::Int, σ::Float64) =
DiagNormalKnownSigma(σ::Vector{Float64}) = DiagNormalKnownSigma(abs2(σ))
MvNormalKnownSigma(C::Matrix{Float64}) = MvNormalKnownSigma(PDMat(C))
dim(g::GenericMvNormalKnownSigma) = dim(g.Σ)
length(g::GenericMvNormalKnownSigma) = dim(g.Σ)
immutable GenericMvNormalKnownSigmaStats{Cov<:AbstractPDMat}
......@@ -170,7 +170,7 @@ typealias DiagNormalKnownSigmaStats GenericMvNormalKnownSigmaStats{PDiagMat}
typealias IsoNormalKnownSigmaStats GenericMvNormalKnownSigmaStats{ScalMat}
function suffstats{Cov<:AbstractPDMat}(g::GenericMvNormalKnownSigma{Cov}, x::Matrix{Float64})
size(x,1) == dim(g) || throw(ArgumentError("Invalid argument dimensions."))
size(x,1) == length(g) || throw(ArgumentError("Invalid argument dimensions."))
invΣ = inv(g.Σ)
sx = vec(sum(x, 2))
tw = float64(size(x, 2))
......@@ -178,7 +178,7 @@ function suffstats{Cov<:AbstractPDMat}(g::GenericMvNormalKnownSigma{Cov}, x::Mat
end
function suffstats{Cov<:AbstractPDMat}(g::GenericMvNormalKnownSigma{Cov}, x::Matrix{Float64}, w::Array{Float64})
if !(size(x,1) == dim(g) && size(x,2) == length(w))
if !(size(x,1) == length(g) && size(x,2) == length(w))
throw(ArgumentError("Inconsistent argument dimensions."))
end
invΣ = inv(g.Σ)
......@@ -195,14 +195,14 @@ function fit_mle{C<:AbstractPDMat}(g::GenericMvNormalKnownSigma{C}, ss::GenericM
end
function fit_mle(g::GenericMvNormalKnownSigma, x::Matrix{Float64})
d = dim(g)
d = length(g)
size(x,1) == d || throw(ArgumentError("Invalid argument dimensions."))
μ = multiply!(vec(sum(x,2)), 1.0 / size(x,2))
GenericMvNormal(μ, g.Σ)
end
function fit_mle(g::GenericMvNormalKnownSigma, x::Matrix{Float64}, w::Array{Float64})
d = dim(g)
d = length(g)
if !(size(x,1) == d && size(x,2) == length(w))
throw(ArgumentError("Inconsistent argument dimensions."))
end
......
......@@ -77,7 +77,7 @@ canonform{C<:AbstractPDMat}(d::GenericMvNormal{C}) = convert(GenericMvNormalCano
# Basic statistics
dim(d::GenericMvNormalCanon) = d.dim
length(d::GenericMvNormalCanon) = d.dim
mean(d::GenericMvNormalCanon) = d.μ
mode(d::GenericMvNormalCanon) = d.μ
......@@ -88,7 +88,7 @@ cov(d::GenericMvNormalCanon) = full(inv(d.J))
invcov(d::GenericMvNormalCanon) = full(d.J)
logdet_cov(d::GenericMvNormalCanon) = -logdet(d.J)
entropy(d::GenericMvNormalCanon) = 0.5 * (dim(d) * (float64(log2π) + 1.0) - logdet(d.J))
entropy(d::GenericMvNormalCanon) = 0.5 * (length(d) * (float64(log2π) + 1.0) - logdet(d.J))
# PDF evaluation
......@@ -99,7 +99,7 @@ function sqmahal(d::GenericMvNormalCanon, x::Vector{Float64})
end
function sqmahal!(r::Array{Float64}, d::GenericMvNormalCanon, x::Matrix{Float64})
if !(size(x, 1) == dim(d) && size(x, 2) == length(r))
if !(size(x, 1) == length(d) && size(x, 2) == length(r))
throw(ArgumentError("Inconsistent argument dimensions."))
end
z::Matrix{Float64} = d.zeromean ? x : bsubtract(x, d.μ, 1)
......
......@@ -67,7 +67,7 @@ mvtdist(df::Float64, Σ::Matrix{Float64}) = MvTDist(df, Σ)
# Basic statistics
dim(d::GenericMvTDist) = d.dim
length(d::GenericMvTDist) = d.dim
mean(d::GenericMvTDist) = d.df>1 ? d.μ : NaN
mode(d::GenericMvTDist) = d.μ
......@@ -95,7 +95,7 @@ function sqmahal(d::GenericMvTDist, x::Vector{Float64})
end
function sqmahal!(r::Array{Float64}, d::GenericMvTDist, x::Matrix{Float64})
if !(size(x, 1) == dim(d) && size(x, 2) == length(r))
if !(size(x, 1) == length(d) && size(x, 2) == length(r))
throw(ArgumentError("Inconsistent argument dimensions."))
end
z::Matrix{Float64} = d.zeromean ? x : bsubtract(x, d.μ, 1)
......@@ -104,7 +104,7 @@ end
# generic PDF evaluation (appliable to AbstractMvTDist)
insupport{T<:Real}(d::AbstractMvTDist, x::Vector{T}) = dim(d) == length(x) && allfinite(x)
insupport{T<:Real}(d::AbstractMvTDist, x::Vector{T}) = length(d) == length(x) && allfinite(x)
insupport{G<:AbstractMvTDist,T<:Real}(::Type{G}, x::Vector{T}) = allfinite(x)
......
......@@ -18,7 +18,7 @@ immutable VonMisesFisher <: ContinuousMultivariateDistribution
end
end
dim(d::VonMisesFisher) = length(d.mu)
length(d::VonMisesFisher) = length(d.mu)
mean(d::VonMisesFisher) = d.mu
......@@ -37,7 +37,7 @@ function logpdf(d::VonMisesFisher, x::Vector{Float64}; stable=true)
return d.kappa * dot(d.mu, x) - d.kappa + log(d.kappa) - log(2*pi) - log(1-exp(-2*d.kappa))
else
# As described on Wikipedia
p = dim(d)
p = length(d)
logCpk = 0.0
if p == 3
logCpk = log(d.kappa) - log(2 * pi * (exp(kappa) - exp(-kappa)))
......
......@@ -5,7 +5,7 @@ using Base.Test
d = Dirichlet(3, 2.0)
@test dim(d) == 3
@test length(d) == 3
@test d.alpha == [2.0, 2.0, 2.0]
@test d.alpha0 == 6.0
......@@ -30,7 +30,7 @@ end
v = [2.0, 1.0, 3.0]
d = Dirichlet(v)
@test dim(d) == length(v)
@test length(d) == length(v)
@test d.alpha == v
@test d.alpha0 == sum(v)
......
......@@ -9,7 +9,7 @@ d = Multinomial(nt, p)
# Basics
@test dim(d) == 3
@test length(d) == 3
@test d.n == nt
@test_approx_eq mean(d) [2., 5., 3.]
@test_approx_eq var(d) [1.6, 2.5, 2.1]
......@@ -74,16 +74,16 @@ ss = suffstats(Multinomial, x, w)
# fit
x = rand(d0, 10^5)
@test size(x) == (dim(d0), 10^5)
@test size(x) == (length(d0), 10^5)
@test all(sum(x, 1) .== nt)
r = fit(Multinomial, x)
@test r.n == nt
@test dim(r) == length(p)
@test length(r) == length(p)
@test_approx_eq_eps r.prob p 0.02
r = fit_mle(Multinomial, x, fill(2.0, size(x,2)))
@test r.n == nt
@test dim(r) == length(p)
@test length(r) == length(p)
@test_approx_eq_eps r.prob p 0.02
......@@ -18,7 +18,7 @@ x = rand(3, 100)
gs = gmvnormal(mu, sqrt(2.0))
@test isa(gs, IsoNormal)
@test dim(gs) == 3
@test length(gs) == 3
@test mean(gs) == mode(gs) == mu
@test_approx_eq cov(gs) diagm(fill(2.0, 3))
@test var(gs) == diag(cov(gs))
......@@ -26,7 +26,7 @@ gs = gmvnormal(mu, sqrt(2.0))
gsz = gmvnormal(3, sqrt(2.0))
@test isa(gsz, IsoNormal)
@test dim(gsz) == 3
@test length(gsz) == 3
@test mean(gsz) == zeros(3)
@test gsz.zeromean
......@@ -34,7 +34,7 @@ gsz = gmvnormal(3, sqrt(2.0))
gd = gmvnormal(mu, sqrt(va))
@test isa(gd, DiagNormal)
@test dim(gd) == 3
@test length(gd) == 3
@test mean(gd) == mode(gd) == mu
@test_approx_eq cov(gd) diagm(va)
@test var(gd) == diag(cov(gd))
......@@ -42,7 +42,7 @@ gd = gmvnormal(mu, sqrt(va))
gdz = gmvnormal(PDiagMat(va))
@test isa(gdz, DiagNormal)
@test dim(gdz) == 3
@test length(gdz) == 3
@test mean(gdz) == zeros(3)
@test gdz.zeromean
......@@ -50,7 +50,7 @@ gdz = gmvnormal(PDiagMat(va))
gf = MvNormal(mu, C)
@test isa(gf, MvNormal)
@test dim(gf) == 3
@test length(gf) == 3
@test mean(gf) == mode(gf) == mu
@test cov(gf) == C
@test var(gf) == diag(cov(gf))
......@@ -58,7 +58,7 @@ gf = MvNormal(mu, C)
gfz = MvNormal(C)
@test isa(gfz, MvNormal)
@test dim(gfz) == 3
@test length(gfz) == 3
@test mean(gfz) == zeros(3)
@test gfz.zeromean
......@@ -106,52 +106,52 @@ for i = 1:n; r[i] = logpdf(gfz, x[:,i]); end
x = rand(gs)
@test isa(x, Vector{Float64})
@test length(x) == dim(gs)
@test length(x) == length(gs)
x = rand(gd)
@test isa(x, Vector{Float64})
@test length(x) == dim(gd)
@test length(x) == length(gd)
x = rand(gf)
@test isa(x, Vector{Float64})
@test length(x) == dim(gf)
@test length(x) == length(gf)
x = rand(gsz)
@test isa(x, Vector{Float64})
@test length(x) == dim(gsz)
@test length(x) == length(gsz)
x = rand(gdz)
@test isa(x, Vector{Float64})
@test length(x) == dim(gdz)
@test length(x) == length(gdz)
x = rand(gfz)
@test isa(x, Vector{Float64})
@test length(x) == dim(gfz)
@test length(x) == length(gfz)
n = 10
x = rand(gs, n)
@test isa(x, Matrix{Float64})
@test size(x) == (dim(gs), n)
@test size(x) == (length(gs), n)
x = rand(gd, n)
@test isa(x, Matrix{Float64})
@test size(x) == (dim(gd), n)
@test size(x) == (length(gd), n)
x = rand(gf, n)
@test isa(x, Matrix{Float64})
@test size(x) == (dim(gf), n)
@test size(x) == (length(gf), n)
x = rand(gsz, n)
@test isa(x, Matrix{Float64})
@test size(x) == (dim(gsz), n)
@test size(x) == (length(gsz), n)
x = rand(gdz, n)
@test isa(x, Matrix{Float64})
@test size(x) == (dim(gdz), n)
@test size(x) == (length(gdz), n)
x = rand(gfz, n)
@test isa(x, Matrix{Float64})
@test size(x) == (dim(gfz), n)
@test size(x) == (length(gfz), n)
##### MLE
......
......@@ -11,7 +11,7 @@ d = VonMisesFisher(mu, kappa)
# Basics
@test dim(d) == D
@test length(d) == D
@test d.kappa == kappa
@test_approx_eq d.mu mean(d)
@test_approx_eq norm(d.mu) 1.0
......
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