@@ -31,8 +31,10 @@ function fit!(
31
31
info > 0 && throw (" Design matrix X is rank deficient" )
32
32
LAPACK. potrs! (' U' , model. storage_p_p, copyto! (model. B, model. xty))
33
33
BLAS. gemm! (' N' , ' N' , - one (T), model. X, model. B, one (T), copyto! (model. R̃Φ, model. Y))
34
+ update_res! (model)
34
35
else
35
- copy! (model. R̃Φ, Y)
36
+ copy! (model. R̃Φ, model. Y)
37
+ copy! (model. R̃, model. Ỹ)
36
38
end
37
39
# initialization of variance components Σ[1], Σ[2]
38
40
# if R is MatrixNormal(0, V[i], Σ[i]), i.e., vec(R) is Normal(0, Σ[i]⊗V[i]),
@@ -42,10 +44,12 @@ function fit!(
42
44
model. Σ[k] .= inv (tr (model. V[k])) .* model. storage_d_d_1
43
45
end
44
46
update_Φ! (model)
45
- update_res! (model)
47
+ # update R̃Φ = (Ỹ - X̃B)Φ
48
+ mul! (model. R̃Φ, model. R̃, model. Φ)
46
49
elseif init == :user
50
+ update_res! (model)
47
51
update_Φ! (model)
48
- update_res ! (model)
52
+ mul ! (model. R̃Φ, model . R̃, model . Φ)
49
53
else
50
54
throw (" Cannot recognize initialization method $init " )
51
55
end
@@ -61,10 +65,13 @@ function fit!(
61
65
IterativeSolvers. nextiter! (history)
62
66
tic = time ()
63
67
# 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
65
72
update_Σ! (model)
66
73
update_Φ! (model)
67
- update_res ! (model)
74
+ mul ! (model. R̃Φ, model . R̃, model . Φ )
68
75
logl_prev = logl
69
76
logl = loglikelihood! (model)
70
77
toc = time ()
@@ -89,15 +96,9 @@ function fit!(
89
96
end
90
97
end
91
98
# 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')
100
99
# update_B_reml!(model)
100
+ # update_res_reml!(model)
101
+ # mul!(model.R̃Φ_reml, model.R̃_reml, model.Φ)
101
102
# copyto!(model.logl_reml, loglikelihood_reml!(model))
102
103
# model.se ? fisher_B_reml!(model) : nothing
103
104
# end
@@ -183,15 +184,14 @@ function update_Φ!(
183
184
copy! (model. Φ, Φ)
184
185
copyto! (model. logdetΣ2, logdet (model. Σ[2 ]))
185
186
mul! (model. ỸΦ, model. Ỹ, model. Φ)
186
- mul! (model. BΦ, model. B, model. Φ)
187
187
end
188
188
189
189
function update_res! (
190
190
model :: MRTVCModel{T}
191
191
) where T <: BlasReal
192
- # update R̃Φ = ( Ỹ - X̃B)Φ
193
- BLAS. gemm! (' N' , ' N' , - one (T), model. X̃, model. BΦ , one (T), copyto! (model. R̃Φ , model. ỸΦ ))
194
- model. R̃Φ
192
+ # update R̃ = Ỹ - X̃B
193
+ BLAS. gemm! (' N' , ' N' , - one (T), model. X̃, model. B , one (T), copyto! (model. R̃ , model. Ỹ ))
194
+ model. R̃
195
195
end
196
196
197
197
function loglikelihood! (
@@ -207,7 +207,7 @@ function loglikelihood!(
207
207
logl += log (tmp) + inv (tmp) * model. R̃Φ[i, j]^ 2
208
208
end
209
209
end
210
- logl /= - 2
210
+ logl /= - 2
211
211
end
212
212
213
213
function update_B! (
@@ -236,6 +236,32 @@ function update_B!(
236
236
model. B
237
237
end
238
238
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
+
239
265
function fisher_B! (
240
266
model :: MRTVCModel{T}
241
267
) where T <: BlasReal
0 commit comments