/
matrixbeta.jl
145 lines (116 loc) · 5.1 KB
/
matrixbeta.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
MatrixBeta(p, n1, n2)
```julia
p::Int dimension
n1::Real degrees of freedom (greater than p - 1)
n2::Real degrees of freedom (greater than p - 1)
```
The [matrix beta distribution](https://en.wikipedia.org/wiki/Matrix_variate_beta_distribution)
generalizes the beta distribution to ``p\\times p`` real matrices ``\\mathbf{U}``
for which ``\\mathbf{U}`` and ``\\mathbf{I}_p-\\mathbf{U}`` are both positive definite.
If ``\\mathbf{U}\\sim \\textrm{MB}_p(n_1/2, n_2/2)``, then its probability density function is
```math
f(\\mathbf{U}; n_1,n_2) = \\frac{\\Gamma_p(\\frac{n_1+n_2}{2})}{\\Gamma_p(\\frac{n_1}{2})\\Gamma_p(\\frac{n_2}{2})}
|\\mathbf{U}|^{(n_1-p-1)/2}\\left|\\mathbf{I}_p-\\mathbf{U}\\right|^{(n_2-p-1)/2}.
```
If ``\\mathbf{S}_1\\sim \\textrm{W}_p(n_1,\\mathbf{I}_p)`` and
``\\mathbf{S}_2\\sim \\textrm{W}_p(n_2,\\mathbf{I}_p)``
are independent, and we use ``\\mathcal{L}(\\cdot)`` to denote the lower Cholesky factor, then
```math
\\mathbf{U}=\\mathcal{L}(\\mathbf{S}_1+\\mathbf{S}_2)^{-1}\\mathbf{S}_1\\mathcal{L}(\\mathbf{S}_1+\\mathbf{S}_2)^{-\\rm{T}}
```
has ``\\mathbf{U}\\sim \\textrm{MB}_p(n_1/2, n_2/2)``.
"""
struct MatrixBeta{T <: Real, TW} <: ContinuousMatrixDistribution
W1::TW
W2::TW
logc0::T
end
# -----------------------------------------------------------------------------
# Constructors
# -----------------------------------------------------------------------------
function MatrixBeta(p::Int, n1::Real, n2::Real)
p > 0 || throw(ArgumentError("dim must be positive: got $(p)."))
logc0 = matrixbeta_logc0(p, n1, n2)
T = Base.promote_eltype(n1, n2, logc0)
Ip = ScalMat(p, one(T))
W1 = Wishart(T(n1), Ip)
W2 = Wishart(T(n2), Ip)
MatrixBeta{T, typeof(W1)}(W1, W2, T(logc0))
end
# -----------------------------------------------------------------------------
# REPL display
# -----------------------------------------------------------------------------
show(io::IO, d::MatrixBeta) = show_multline(io, d, [(:n1, d.W1.df), (:n2, d.W2.df)])
# -----------------------------------------------------------------------------
# Conversion
# -----------------------------------------------------------------------------
function convert(::Type{MatrixBeta{T}}, d::MatrixBeta) where T <: Real
W1 = convert(Wishart{T}, d.W1)
W2 = convert(Wishart{T}, d.W2)
MatrixBeta{T, typeof(W1)}(W1, W2, T(d.logc0))
end
Base.convert(::Type{MatrixBeta{T}}, d::MatrixBeta{T}) where {T<:Real} = d
function convert(::Type{MatrixBeta{T}}, W1::Wishart, W2::Wishart, logc0) where T <: Real
WW1 = convert(Wishart{T}, W1)
WW2 = convert(Wishart{T}, W2)
MatrixBeta{T, typeof(WW1)}(WW1, WW2, T(logc0))
end
# -----------------------------------------------------------------------------
# Properties
# -----------------------------------------------------------------------------
size(d::MatrixBeta) = size(d.W1)
rank(d::MatrixBeta) = size(d, 1)
insupport(d::MatrixBeta, U::AbstractMatrix) = isreal(U) && size(U) == size(d) && isposdef(U) && isposdef(I - U)
params(d::MatrixBeta) = (size(d, 1), d.W1.df, d.W2.df)
mean(d::MatrixBeta) = ((p, n1, n2) = params(d); Matrix((n1 / (n1 + n2)) * I, p, p))
@inline partype(d::MatrixBeta{T}) where {T <: Real} = T
# Konno (1988 JJSS) Corollary 3.3.i
function cov(d::MatrixBeta, i::Integer, j::Integer, k::Integer, l::Integer)
p, n1, n2 = params(d)
n = n1 + n2
Ω = Matrix{partype(d)}(I, p, p)
n1*n2*inv(n*(n - 1)*(n + 2))*(-(2/n)*Ω[i,j]*Ω[k,l] + Ω[j,l]*Ω[i,k] + Ω[i,l]*Ω[k,j])
end
function var(d::MatrixBeta, i::Integer, j::Integer)
p, n1, n2 = params(d)
n = n1 + n2
Ω = Matrix{partype(d)}(I, p, p)
n1*n2*inv(n*(n - 1)*(n + 2))*((1 - (2/n))*Ω[i,j]^2 + Ω[j,j]*Ω[i,i])
end
# -----------------------------------------------------------------------------
# Evaluation
# -----------------------------------------------------------------------------
function matrixbeta_logc0(p::Int, n1::Real, n2::Real)
# returns the natural log of the normalizing constant for the pdf
return -logmvbeta(p, n1 / 2, n2 / 2)
end
function logkernel(d::MatrixBeta, U::AbstractMatrix)
p, n1, n2 = params(d)
((n1 - p - 1) / 2) * logdet(U) + ((n2 - p - 1) / 2) * logdet(I - U)
end
# -----------------------------------------------------------------------------
# Sampling
# -----------------------------------------------------------------------------
# Mitra (1970 Sankhyā)
function _rand!(rng::AbstractRNG, d::MatrixBeta, A::AbstractMatrix)
S1 = PDMat( rand(rng, d.W1) )
S2 = PDMat( rand(rng, d.W2) )
S = S1 + S2
invL = Matrix( inv(S.chol.L) )
A .= X_A_Xt(S1, invL)
end
# -----------------------------------------------------------------------------
# Test utils
# -----------------------------------------------------------------------------
function _univariate(d::MatrixBeta)
check_univariate(d)
p, n1, n2 = params(d)
return Beta(n1 / 2, n2 / 2)
end
function _rand_params(::Type{MatrixBeta}, elty, n::Int, p::Int)
n == p || throw(ArgumentError("dims must be equal for MatrixBeta"))
n1 = elty( n + 1 + abs(10randn()) )
n2 = elty( n + 1 + abs(10randn()) )
return n, n1, n2
end