Skip to content

Commit edf6493

Browse files
committed
reorder some stuff
1 parent ddc5239 commit edf6493

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

src/MultiResponseVarianceComponentModels.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ struct MRTVCModel{T <: BlasReal} <: VCModel
305305
xtx :: Matrix{T} # Gram matrix X'X
306306
xty :: Matrix{T} # X'Y
307307
ỸΦ :: Matrix{T}
308-
:: Matrix{T}
308+
:: Matrix{T}
309309
R̃Φ :: Matrix{T}
310310
N1tN1 :: Matrix{T}
311311
N2tN2 :: Matrix{T}
@@ -417,7 +417,7 @@ function MRTVCModel(
417417
xtx = transpose(Xmat) * Xmat
418418
xty = transpose(Xmat) * Y
419419
ỸΦ = Matrix{T}(undef, n, d)
420-
= Matrix{T}(undef, p, d)
420+
= Matrix{T}(undef, n, d)
421421
R̃Φ = Matrix{T}(undef, n, d)
422422
N1tN1 = Matrix{T}(undef, d, d)
423423
N2tN2 = Matrix{T}(undef, d, d)
@@ -435,7 +435,7 @@ function MRTVCModel(
435435
MRTVCModel{T}(
436436
Y, Ỹ, Xmat, X̃, V, U, D, logdetV2,
437437
B, Σ, Φ, Λ, logdetΣ2,
438-
xtx, xty, ỸΦ, , R̃Φ, N1tN1, N2tN2,
438+
xtx, xty, ỸΦ, , R̃Φ, N1tN1, N2tN2,
439439
storage_d_1, storage_d_2, storage_d_d_1, storage_d_d_2,
440440
storage_p_p, storage_pd, storage_pd_pd,
441441
storage_nd_1, storage_nd_2, storage_nd_pd, logl, Bcov, Σcov,

src/eigen.jl

+44-18
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ function fit!(
3131
info > 0 && throw("Design matrix X is rank deficient")
3232
LAPACK.potrs!('U', model.storage_p_p, copyto!(model.B, model.xty))
3333
BLAS.gemm!('N', 'N', -one(T), model.X, model.B, one(T), copyto!(model.R̃Φ, model.Y))
34+
update_res!(model)
3435
else
35-
copy!(model.R̃Φ, Y)
36+
copy!(model.R̃Φ, model.Y)
37+
copy!(model.R̃, model.Ỹ)
3638
end
3739
# initialization of variance components Σ[1], Σ[2]
3840
# if R is MatrixNormal(0, V[i], Σ[i]), i.e., vec(R) is Normal(0, Σ[i]⊗V[i]),
@@ -42,10 +44,12 @@ function fit!(
4244
model.Σ[k] .= inv(tr(model.V[k])) .* model.storage_d_d_1
4345
end
4446
update_Φ!(model)
45-
update_res!(model)
47+
# update R̃Φ = (Ỹ - X̃B)Φ
48+
mul!(model.R̃Φ, model.R̃, model.Φ)
4649
elseif init == :user
50+
update_res!(model)
4751
update_Φ!(model)
48-
update_res!(model)
52+
mul!(model.R̃Φ, model.R̃, model.Φ)
4953
else
5054
throw("Cannot recognize initialization method $init")
5155
end
@@ -61,10 +65,13 @@ function fit!(
6165
IterativeSolvers.nextiter!(history)
6266
tic = time()
6367
# initial estiamte of Σ[i] can be lousy, so we update Σ[i] first in the 1st round
64-
p > 0 && iter > 1 && update_B!(model)
68+
if p > 0 && iter > 1
69+
update_B!(model)
70+
update_res!(model)
71+
end
6572
update_Σ!(model)
6673
update_Φ!(model)
67-
update_res!(model)
74+
mul!(model.R̃Φ, model.R̃, model.Φ)
6875
logl_prev = logl
6976
logl = loglikelihood!(model)
7077
toc = time()
@@ -89,15 +96,9 @@ function fit!(
8996
end
9097
end
9198
# if model.reml
92-
# update_Ω_reml!(model)
93-
# # need Ω⁻¹ for B
94-
# copyto!(model.storage_nd_nd_reml, model.Ω_reml)
95-
# # Cholesky of covariance Ω = U'U
96-
# _, info = LAPACK.potrf!('U', model.storage_nd_nd_reml)
97-
# info > 0 && throw("Covariance matrix Ω is singular")
98-
# LAPACK.potri!('U', model.storage_nd_nd_reml)
99-
# copytri!(model.storage_nd_nd_reml, 'U')
10099
# update_B_reml!(model)
100+
# update_res_reml!(model)
101+
# mul!(model.R̃Φ_reml, model.R̃_reml, model.Φ)
101102
# copyto!(model.logl_reml, loglikelihood_reml!(model))
102103
# model.se ? fisher_B_reml!(model) : nothing
103104
# end
@@ -183,15 +184,14 @@ function update_Φ!(
183184
copy!(model.Φ, Φ)
184185
copyto!(model.logdetΣ2, logdet(model.Σ[2]))
185186
mul!(model.ỸΦ, model.Ỹ, model.Φ)
186-
mul!(model.BΦ, model.B, model.Φ)
187187
end
188188

189189
function update_res!(
190190
model :: MRTVCModel{T}
191191
) where T <: BlasReal
192-
# update R̃Φ = (Ỹ - X̃B
193-
BLAS.gemm!('N', 'N', -one(T), model.X̃, model., one(T), copyto!(model.R̃Φ, model.ỸΦ))
194-
model.R̃Φ
192+
# update = Ỹ - X̃B
193+
BLAS.gemm!('N', 'N', -one(T), model.X̃, model.B, one(T), copyto!(model., model.))
194+
model.
195195
end
196196

197197
function loglikelihood!(
@@ -207,7 +207,7 @@ function loglikelihood!(
207207
logl += log(tmp) + inv(tmp) * model.R̃Φ[i, j]^2
208208
end
209209
end
210-
logl /= -2
210+
logl /= -2
211211
end
212212

213213
function update_B!(
@@ -236,6 +236,32 @@ function update_B!(
236236
model.B
237237
end
238238

239+
# function update_B_reml!(
240+
# model :: MRTVCModel{T}
241+
# ) where T <: BlasReal
242+
# # Gram matrix G = (Φ'⊗X̃)'(Λ⊗D + Ind)⁻¹(Φ'⊗X̃)
243+
# G = model.storage_pd_pd
244+
# fill!(model.storage_nd_pd_reml, zero(T))
245+
# kron_axpy!(transpose(model.Φ), model.X̃_reml, model.storage_nd_pd_reml)
246+
# fill!(model.storage_nd_1_reml, zero(T))
247+
# kron_axpy!(model.Λ, model.D_reml, model.storage_nd_1_reml)
248+
# @inbounds @simd for i in eachindex(model.storage_nd_1_reml)
249+
# model.storage_nd_1_reml[i] = one(T) / sqrt(model.storage_nd_1_reml[i] + one(T))
250+
# end
251+
# lmul!(Diagonal(model.storage_nd_1_reml), model.storage_nd_pd_reml)
252+
# mul!(G, transpose(model.storage_nd_pd_reml), model.storage_nd_pd_reml)
253+
# # (Φ'⊗X̃)'(Λ⊗D + Ind)⁻¹vec(ỸΦ)
254+
# copyto!(model.storage_nd_2_reml, model.ỸΦ_reml)
255+
# model.storage_nd_2_reml .= model.storage_nd_1_reml .* model.storage_nd_2_reml
256+
# mul!(model.storage_pd, transpose(model.storage_nd_pd_reml), model.storage_nd_2_reml)
257+
# # Cholesky solve
258+
# _, info = LAPACK.potrf!('U', G)
259+
# info > 0 && throw("Gram matrix (Φ'⊗X̃)'(Λ⊗D + Ind)⁻¹(Φ'⊗X̃) is singular")
260+
# LAPACK.potrs!('U', G, model.storage_pd)
261+
# copyto!(model.B, model.storage_pd)
262+
# model.B
263+
# end
264+
239265
function fisher_B!(
240266
model :: MRTVCModel{T}
241267
) where T <: BlasReal

0 commit comments

Comments
 (0)