@@ -41,7 +41,7 @@ function get_observed(rowind, data, semobserved; args = (), kwargs = NamedTuple(
4141 return observed_vec
4242end
4343
44- skipmissing_mean (mat:: AbstractMatrix ) =
44+ skipmissing_mean (mat:: AbstractMatrix ) =
4545 [mean (skipmissing (coldata)) for coldata in eachcol (mat)]
4646
4747function F_one_person (imp_mean, meandiff, inverse, data, logdet)
@@ -111,143 +111,34 @@ function cov_and_mean(rows; corrected = false)
111111 return obs_cov, vec (obs_mean)
112112end
113113
114- function duplication_matrix (nobs)
115- nobs = Int (nobs)
116- n1 = Int (nobs * (nobs + 1 ) * 0.5 )
117- n2 = Int (nobs^ 2 )
118- Dt = zeros (n1, n2)
119-
120- for j in 1 : nobs
121- for i in j: nobs
122- u = zeros (n1)
123- u[Int ((j - 1 ) * nobs + i - 0.5 * j * (j - 1 ))] = 1
124- T = zeros (nobs, nobs)
125- T[j, i] = 1
126- T[i, j] = 1
127- Dt += u * transpose (vec (T))
114+ # n²×(n(n+1)/2) matrix to transform a vector of lower
115+ # triangular entries into a vectorized form of a n×n symmetric matrix,
116+ # opposite of elimination_matrix()
117+ function duplication_matrix (n:: Integer )
118+ ntri = div (n * (n + 1 ), 2 )
119+ D = zeros (n^ 2 , ntri)
120+ for j in 1 : n
121+ for i in j: n
122+ tri_ix = (j - 1 ) * n + i - div (j * (j - 1 ), 2 )
123+ D[j+ n* (i- 1 ), tri_ix] = 1
124+ D[i+ n* (j- 1 ), tri_ix] = 1
128125 end
129126 end
130- D = transpose (Dt)
131127 return D
132128end
133129
134- function elimination_matrix (nobs)
135- nobs = Int (nobs)
136- n1 = Int (nobs * (nobs + 1 ) * 0.5 )
137- n2 = Int (nobs^ 2 )
138- L = zeros (n1, n2)
139-
140- for j in 1 : nobs
141- for i in j: nobs
142- u = zeros (n1)
143- u[Int ((j - 1 ) * nobs + i - 0.5 * j * (j - 1 ))] = 1
144- T = zeros (nobs, nobs)
145- T[i, j] = 1
146- L += u * transpose (vec (T))
130+ # (n(n+1)/2)×n² matrix to transform a
131+ # vectorized form of a n×n symmetric matrix
132+ # into vector of its lower triangular entries,
133+ # opposite of duplication_matrix()
134+ function elimination_matrix (n:: Integer )
135+ ntri = div (n * (n + 1 ), 2 )
136+ L = zeros (ntri, n^ 2 )
137+ for j in 1 : n
138+ for i in j: n
139+ tri_ix = (j - 1 ) * n + i - div (j * (j - 1 ), 2 )
140+ L[tri_ix, i+ n* (j- 1 )] = 1
147141 end
148142 end
149143 return L
150144end
151-
152- function commutation_matrix (n; tosparse = false )
153- M = zeros (n^ 2 , n^ 2 )
154-
155- for i in 1 : n
156- for j in 1 : n
157- M[i+ n* (j- 1 ), j+ n* (i- 1 )] = 1.0
158- end
159- end
160-
161- if tosparse
162- M = sparse (M)
163- end
164-
165- return M
166- end
167-
168- function commutation_matrix_pre_square (A)
169- n2 = size (A, 1 )
170- n = Int (sqrt (n2))
171-
172- ind = repeat (1 : n, inner = n)
173- indadd = (0 : (n- 1 )) * n
174- for i in 1 : n
175- ind[((i- 1 )* n+ 1 ): i* n] .+ = indadd
176- end
177-
178- A_post = A[ind, :]
179-
180- return A_post
181- end
182-
183- function commutation_matrix_pre_square_add! (B, A) # comuptes B + KₙA
184- n2 = size (A, 1 )
185- n = Int (sqrt (n2))
186-
187- ind = repeat (1 : n, inner = n)
188- indadd = (0 : (n- 1 )) * n
189- for i in 1 : n
190- ind[((i- 1 )* n+ 1 ): i* n] .+ = indadd
191- end
192-
193- @views @inbounds B .+ = A[ind, :]
194-
195- return B
196- end
197-
198- function get_commutation_lookup (n2:: Int64 )
199- n = Int (sqrt (n2))
200- ind = repeat (1 : n, inner = n)
201- indadd = (0 : (n- 1 )) * n
202- for i in 1 : n
203- ind[((i- 1 )* n+ 1 ): i* n] .+ = indadd
204- end
205-
206- lookup = Dict {Int64, Int64} ()
207-
208- for i in 1 : n2
209- j = findall (x -> (x == i), ind)[1 ]
210- push! (lookup, i => j)
211- end
212-
213- return lookup
214- end
215-
216- function commutation_matrix_pre_square! (A:: SparseMatrixCSC , lookup) # comuptes B + KₙA
217- for (i, rowind) in enumerate (A. rowval)
218- A. rowval[i] = lookup[rowind]
219- end
220- end
221-
222- function commutation_matrix_pre_square! (A:: SparseMatrixCSC ) # computes KₙA
223- lookup = get_commutation_lookup (size (A, 2 ))
224- commutation_matrix_pre_square! (A, lookup)
225- end
226-
227- function commutation_matrix_pre_square (A:: SparseMatrixCSC )
228- B = copy (A)
229- commutation_matrix_pre_square! (B)
230- return B
231- end
232-
233- function commutation_matrix_pre_square (A:: SparseMatrixCSC , lookup)
234- B = copy (A)
235- commutation_matrix_pre_square! (B, lookup)
236- return B
237- end
238-
239- function commutation_matrix_pre_square_add_mt! (B, A) # comuptes B + KₙA # 0 allocations but slower
240- n2 = size (A, 1 )
241- n = Int (sqrt (n2))
242-
243- indadd = (0 : (n- 1 )) * n
244-
245- Threads. @threads for i in 1 : n
246- for j in 1 : n
247- row = i + indadd[j]
248- @views @inbounds B[row, :] .+ = A[row, :]
249- end
250- end
251-
252- return B
253- end
0 commit comments