Skip to content

Commit 7831193

Browse files
authored
Fix deprecations of dim (#1572)
* Fix deprecations of `dim` * Fix deprecation * Use `size` instead of forwarding `rank` for distributions of full-rank matrices
1 parent 1d9739a commit 7831193

14 files changed

+56
-57
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Distributions"
22
uuid = "31c24e10-a181-5473-b8eb-7969acd0382f"
33
authors = ["JuliaStats"]
4-
version = "0.25.62"
4+
version = "0.25.63"
55

66
[deps]
77
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/Distributions.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ export
191191
componentwise_logpdf, # component-wise logpdf for mixture models
192192
concentration, # the concentration parameter
193193
convolve, # convolve distributions of the same type
194-
dim, # sample dimension of multivariate distribution
195194
dof, # get the degree of freedom
196195
entropy, # entropy of distribution in nats
197196
failprob, # failing probability

src/deprecates.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ const MatrixReshaped{S<:ValueSupport,D<:MultivariateDistribution{S}} = ReshapedD
5757
@deprecate MatrixReshaped(
5858
d::MultivariateDistribution, n::Integer, p::Integer=n
5959
) reshape(d, (n, p))
60+
61+
for D in (:InverseWishart, :LKJ, :MatrixBeta, :MatrixFDist, :Wishart)
62+
@eval @deprecate dim(d::$D) size(d, 1)
63+
end

src/matrix/inversewishart.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ end
2929
# -----------------------------------------------------------------------------
3030

3131
function InverseWishart(df::T, Ψ::AbstractPDMat{T}) where T<:Real
32-
p = dim)
32+
p = size(Ψ, 1)
3333
df > p - 1 || throw(ArgumentError("df should be greater than dim - 1."))
3434
logc0 = invwishart_logc0(df, Ψ)
3535
R = Base.promote_eltype(T, logc0)
@@ -74,31 +74,31 @@ end
7474
insupport(::Type{InverseWishart}, X::Matrix) = isposdef(X)
7575
insupport(d::InverseWishart, X::Matrix) = size(X) == size(d) && isposdef(X)
7676

77-
dim(d::InverseWishart) = dim(d.Ψ)
78-
size(d::InverseWishart) = (p = dim(d); (p, p))
79-
rank(d::InverseWishart) = dim(d)
77+
size(d::InverseWishart) = size(d.Ψ)
78+
rank(d::InverseWishart) = rank(d.Ψ)
79+
8080
params(d::InverseWishart) = (d.df, d.Ψ)
8181
@inline partype(d::InverseWishart{T}) where {T<:Real} = T
8282

8383
function mean(d::InverseWishart)
8484
df = d.df
85-
p = dim(d)
85+
p = size(d, 1)
8686
r = df - (p + 1)
8787
r > 0.0 || throw(ArgumentError("mean only defined for df > p + 1"))
8888
return Matrix(d.Ψ) * (1.0 / r)
8989
end
9090

91-
mode(d::InverseWishart) = d.Ψ * inv(d.df + dim(d) + 1.0)
91+
mode(d::InverseWishart) = d.Ψ * inv(d.df + size(d, 1) + 1.0)
9292

9393
# https://en.wikipedia.org/wiki/Inverse-Wishart_distribution#Moments
9494
function cov(d::InverseWishart, i::Integer, j::Integer, k::Integer, l::Integer)
95-
p, ν, Ψ = (dim(d), d.df, Matrix(d.Ψ))
95+
p, ν, Ψ = (size(d, 1), d.df, Matrix(d.Ψ))
9696
ν > p + 3 || throw(ArgumentError("cov only defined for df > dim + 3"))
9797
inv((ν - p)*- p - 3)*- p - 1)^2)*(2Ψ[i,j]*Ψ[k,l] +-p-1)*(Ψ[i,k]*Ψ[j,l] + Ψ[i,l]*Ψ[k,j]))
9898
end
9999

100100
function var(d::InverseWishart, i::Integer, j::Integer)
101-
p, ν, Ψ = (dim(d), d.df, Matrix(d.Ψ))
101+
p, ν, Ψ = (size(d, 1), d.df, Matrix(d.Ψ))
102102
ν > p + 3 || throw(ArgumentError("var only defined for df > dim + 3"))
103103
inv((ν - p)*- p - 3)*- p - 1)^2)*((ν - p + 1)*Ψ[i,j]^2 +- p - 1)*Ψ[i,i]*Ψ[j,j])
104104
end
@@ -109,12 +109,12 @@ end
109109

110110
function invwishart_logc0(df::Real, Ψ::AbstractPDMat)
111111
h_df = df / 2
112-
p = dim)
112+
p = size(Ψ, 1)
113113
-h_df * (p * typeof(df)(logtwo) - logdet(Ψ)) - logmvgamma(p, h_df)
114114
end
115115

116116
function logkernel(d::InverseWishart, X::AbstractMatrix)
117-
p = dim(d)
117+
p = size(d, 1)
118118
df = d.df
119119
Xcf = cholesky(X)
120120
# we use the fact: tr(Ψ * inv(X)) = tr(inv(X) * Ψ) = tr(X \ Ψ)

src/matrix/lkj.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,14 @@ end
6666
# Properties
6767
# -----------------------------------------------------------------------------
6868

69-
dim(d::LKJ) = d.d
7069

71-
size(d::LKJ) = (dim(d), dim(d))
70+
size(d::LKJ) = (d.d, d.d)
7271

73-
rank(d::LKJ) = dim(d)
72+
rank(d::LKJ) = d.d
7473

7574
insupport(d::LKJ, R::AbstractMatrix) = isreal(R) && size(R) == size(d) && isone(Diagonal(R)) && isposdef(R)
7675

77-
mean(d::LKJ) = Matrix{partype(d)}(I, dim(d), dim(d))
76+
mean(d::LKJ) = Matrix{partype(d)}(I, d.d, d.d)
7877

7978
function mode(d::LKJ; check_args::Bool=true)
8079
@check_args(
@@ -86,7 +85,7 @@ function mode(d::LKJ; check_args::Bool=true)
8685
end
8786

8887
function var(lkj::LKJ)
89-
d = dim(lkj)
88+
d = lkj.d
9089
d > 1 || return zeros(d, d)
9190
σ² = var(_marginal(lkj))
9291
σ² * (ones(partype(lkj), d, d) - I)

src/matrix/matrixbeta.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,13 @@ end
7272
# Properties
7373
# -----------------------------------------------------------------------------
7474

75-
dim(d::MatrixBeta) = dim(d.W1)
76-
7775
size(d::MatrixBeta) = size(d.W1)
7876

79-
rank(d::MatrixBeta) = dim(d)
77+
rank(d::MatrixBeta) = size(d, 1)
8078

8179
insupport(d::MatrixBeta, U::AbstractMatrix) = isreal(U) && size(U) == size(d) && isposdef(U) && isposdef(I - U)
8280

83-
params(d::MatrixBeta) = (dim(d), d.W1.df, d.W2.df)
81+
params(d::MatrixBeta) = (size(d, 1), d.W1.df, d.W2.df)
8482

8583
mean(d::MatrixBeta) = ((p, n1, n2) = params(d); Matrix((n1 / (n1 + n2)) * I, p, p))
8684

src/matrix/matrixfdist.jl

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ end
4141
# -----------------------------------------------------------------------------
4242

4343
function MatrixFDist(n1::Real, n2::Real, B::AbstractPDMat)
44-
p = dim(B)
44+
p = size(B, 1)
4545
n1 > p - 1 || throw(ArgumentError("first degrees of freedom must be larger than $(p - 1)"))
4646
n2 > p - 1 || throw(ArgumentError("second degrees of freedom must be larger than $(p - 1)"))
4747
logc0 = matrixfdist_logc0(n1, n2, B)
@@ -78,18 +78,16 @@ end
7878
# Properties
7979
# -----------------------------------------------------------------------------
8080

81-
dim(d::MatrixFDist) = dim(d.W)
82-
8381
size(d::MatrixFDist) = size(d.W)
8482

85-
rank(d::MatrixFDist) = dim(d)
83+
rank(d::MatrixFDist) = size(d, 1)
8684

8785
insupport(d::MatrixFDist, Σ::AbstractMatrix) = isreal(Σ) && size(Σ) == size(d) && isposdef(Σ)
8886

8987
params(d::MatrixFDist) = (d.W.df, d.n2, d.W.S)
9088

9189
function mean(d::MatrixFDist)
92-
p = dim(d)
90+
p = size(d, 1)
9391
n1, n2, B = params(d)
9492
n2 > p + 1 || throw(ArgumentError("mean only defined for df2 > dim + 1"))
9593
return (n1 / (n2 - p - 1)) * Matrix(B)
@@ -99,7 +97,7 @@ end
9997

10098
# Konno (1988 JJSS) Corollary 2.4.i
10199
function cov(d::MatrixFDist, i::Integer, j::Integer, k::Integer, l::Integer)
102-
p = dim(d)
100+
p = size(d, 1)
103101
n1, n2, PDB = params(d)
104102
n2 > p + 3 || throw(ArgumentError("cov only defined for df2 > dim + 3"))
105103
n = n1 + n2
@@ -108,7 +106,7 @@ function cov(d::MatrixFDist, i::Integer, j::Integer, k::Integer, l::Integer)
108106
end
109107

110108
function var(d::MatrixFDist, i::Integer, j::Integer)
111-
p = dim(d)
109+
p = size(d, 1)
112110
n1, n2, PDB = params(d)
113111
n2 > p + 3 || throw(ArgumentError("var only defined for df2 > dim + 3"))
114112
n = n1 + n2
@@ -122,14 +120,14 @@ end
122120

123121
function matrixfdist_logc0(n1::Real, n2::Real, B::AbstractPDMat)
124122
# returns the natural log of the normalizing constant for the pdf
125-
p = dim(B)
123+
p = size(B, 1)
126124
term1 = -logmvbeta(p, n1 / 2, n2 / 2)
127125
term2 = (n2 / 2) * logdet(B)
128126
return term1 + term2
129127
end
130128

131129
function logkernel(d::MatrixFDist, Σ::AbstractMatrix)
132-
p = dim(d)
130+
p = size(d, 1)
133131
n1, n2, B = params(d)
134132
((n1 - p - 1) / 2) * logdet(Σ) - ((n1 + n2) / 2) * logdet(pdadd(Σ, B))
135133
end

src/matrix/matrixnormal.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ end
3030

3131
function MatrixNormal(M::AbstractMatrix{T}, U::AbstractPDMat{T}, V::AbstractPDMat{T}) where T <: Real
3232
n, p = size(M)
33-
n == dim(U) || throw(ArgumentError("Number of rows of M must equal dim of U."))
34-
p == dim(V) || throw(ArgumentError("Number of columns of M must equal dim of V."))
33+
n == size(U, 1) || throw(ArgumentError("Number of rows of M must equal dim of U."))
34+
p == size(V, 1) || throw(ArgumentError("Number of columns of M must equal dim of V."))
3535
logc0 = matrixnormal_logc0(U, V)
3636
R = Base.promote_eltype(T, logc0)
3737
prom_M = convert(AbstractArray{R}, M)
@@ -105,8 +105,8 @@ params(d::MatrixNormal) = (d.M, d.U, d.V)
105105
# -----------------------------------------------------------------------------
106106

107107
function matrixnormal_logc0(U::AbstractPDMat, V::AbstractPDMat)
108-
n = dim(U)
109-
p = dim(V)
108+
n = size(U, 1)
109+
p = size(V, 1)
110110
-(n * p / 2) * (logtwo + logπ) - (n / 2) * logdet(V) - (p / 2) * logdet(U)
111111
end
112112

src/matrix/matrixtdist.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ end
5050
function MatrixTDist::T, M::AbstractMatrix{T}, Σ::AbstractPDMat{T}, Ω::AbstractPDMat{T}) where T <: Real
5151
n, p = size(M)
5252
0 < ν < Inf || throw(ArgumentError("degrees of freedom must be positive and finite."))
53-
n == dim) || throw(ArgumentError("Number of rows of M must equal dim of Σ."))
54-
p == dim) || throw(ArgumentError("Number of columns of M must equal dim of Ω."))
53+
n == size(Σ, 1) || throw(ArgumentError("Number of rows of M must equal dim of Σ."))
54+
p == size(Ω, 1) || throw(ArgumentError("Number of columns of M must equal dim of Ω."))
5555
logc0 = matrixtdist_logc0(Σ, Ω, ν)
5656
R = Base.promote_eltype(T, logc0)
5757
prom_M = convert(AbstractArray{R}, M)
@@ -128,8 +128,8 @@ params(d::MatrixTDist) = (d.ν, d.M, d.Σ, d.Ω)
128128

129129
function matrixtdist_logc0::AbstractPDMat, Ω::AbstractPDMat, ν::Real)
130130
# returns the natural log of the normalizing constant for the pdf
131-
n = dim)
132-
p = dim)
131+
n = size(Σ, 1)
132+
p = size(Ω, 1)
133133
term1 = logmvgamma(p, (ν + n + p - 1) / 2)
134134
term2 = - (n * p / 2) * logπ
135135
term3 = - logmvgamma(p, (ν + p - 1) / 2)

src/matrix/wishart.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ end
4444

4545
function Wishart(df::T, S::AbstractPDMat{T}) where T<:Real
4646
df > 0 || throw(ArgumentError("df must be positive. got $(df)."))
47-
p = dim(S)
47+
p = size(S, 1)
4848
singular = df <= p - 1
4949
if singular
5050
isinteger(df) || throw(
@@ -100,23 +100,23 @@ function insupport(d::Wishart, X::AbstractMatrix)
100100
end
101101
end
102102

103-
dim(d::Wishart) = dim(d.S)
104-
size(d::Wishart) = (p = dim(d); (p, p))
103+
size(d::Wishart) = size(d.S)
104+
105105
rank(d::Wishart) = d.rank
106106
params(d::Wishart) = (d.df, d.S)
107107
@inline partype(d::Wishart{T}) where {T<:Real} = T
108108

109109
mean(d::Wishart) = d.df * Matrix(d.S)
110110

111111
function mode(d::Wishart)
112-
r = d.df - dim(d) - 1
112+
r = d.df - size(d, 1) - 1
113113
r > 0 || throw(ArgumentError("mode is only defined when df > p + 1"))
114114
return Matrix(d.S) * r
115115
end
116116

117117
function meanlogdet(d::Wishart)
118118
logdet_S = logdet(d.S)
119-
p = dim(d)
119+
p = size(d, 1)
120120
v = logdet_S + p * oftype(logdet_S, logtwo)
121121
df = oftype(logdet_S, d.df)
122122
for i in 0:(p - 1)
@@ -127,7 +127,7 @@ end
127127

128128
function entropy(d::Wishart)
129129
d.singular && throw(ArgumentError("entropy not defined for singular Wishart."))
130-
p = dim(d)
130+
p = size(d, 1)
131131
df = d.df
132132
return -d.logc0 - ((df - p - 1) * meanlogdet(d) - df * p) / 2
133133
end
@@ -148,7 +148,7 @@ end
148148
# -----------------------------------------------------------------------------
149149

150150
function wishart_logc0(df::T, S::AbstractPDMat{T}, rnk::Integer) where {T<:Real}
151-
p = dim(S)
151+
p = size(S, 1)
152152
if df <= p - 1
153153
return singular_wishart_logc0(p, df, S, rnk)
154154
else
@@ -172,7 +172,7 @@ function singular_wishart_logc0(p::Integer, df::T, S::AbstractPDMat{T}, rnk::Int
172172
end
173173

174174
function singular_wishart_logkernel(d::Wishart, X::AbstractMatrix)
175-
p = dim(d)
175+
p = size(d, 1)
176176
r = rank(d)
177177
L = eigvals(Hermitian(X), (p - r + 1):p)
178178
return ((d.df - (p + 1)) * sum(log, L) - tr(d.S \ X)) / 2
@@ -186,7 +186,7 @@ function nonsingular_wishart_logc0(p::Integer, df::T, S::AbstractPDMat{T}) where
186186
end
187187

188188
function nonsingular_wishart_logkernel(d::Wishart, X::AbstractMatrix)
189-
return ((d.df - (dim(d) + 1)) * logdet(cholesky(X)) - tr(d.S \ X)) / 2
189+
return ((d.df - (size(d, 1) + 1)) * logdet(cholesky(X)) - tr(d.S \ X)) / 2
190190
end
191191

192192
# -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)