Skip to content

Commit 6d3abdd

Browse files
committed
fix funky simulate
1 parent b48fe6c commit 6d3abdd

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

docs/src/examples.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ model = MRVCModel(Y, X, V; se = false) # or model = MRVCModel(Y, X, V; se = fals
8282
@timev fit!(model)
8383
```
8484

85-
# Missing response
85+
# Special case: missing response
8686
You can also fit data with missing response. For example:
8787
```julia
8888
Y_miss = Matrix{Union{eltype(Y), Missing}}(missing, size(Y))
@@ -94,32 +94,25 @@ model = MRVCModel(Y_miss, X, V; se = false)
9494
```
9595

9696
# Special case: ``m = 2``
97-
When there are __two__ variance components, you can accelerate fitting by avoiding large matrix inversion per iteration with the generalized eigenvalue decomposition of kernel matrices and variance components.
98-
99-
You can first simulate data with a slightly more memory-efficient code.
97+
When there are __two__ variance components, you can accelerate fitting by avoiding large matrix inversion per iteration. To illustrate this, you can first simulate data as we did previously but with larger ``n \cdot d`` and ``m = 2``.
10098
```@repl 1
10199
function simulate(n, d, p, m)
102-
Y = zeros(n, d)
103100
X = rand(n, p)
104101
B = rand(p, d)
105-
V = [zeros(n, n) for _ in 1:m] # kernel matrices
106-
Σ = [zeros(d, d) for _ in 1:m] # variance components
107-
Ω = zeros(n * d, n * d) # overall nd-by-nd covariance matrix Ω
108-
storage = rand(n * d)
109-
@inbounds for i in 1:m
102+
V = [zeros(n, n) for _ in 1:m]
103+
Σ = [zeros(d, d) for _ in 1:m]
104+
Ω = zeros(n * d, n * d)
105+
for i in 1:m
110106
Vi = randn(n, n)
111107
mul!(V[i], transpose(Vi), Vi)
112108
Σi = randn(d, d)
113109
mul!(Σ[i], transpose(Σi), Σi)
114110
kron_axpy!(Σ[i], V[i], Ω) # Ω = Σ[1]⊗V[1] + ... + Σ[m]⊗V[m]
115111
end
116-
LAPACK.potrf!('L', Ω) # lower Cholesky factor of Ω
117-
BLAS.trmv!('L', 'N', 'N', Ω, storage)
118-
copyto!(Y, storage)
119-
mul!(Y, X, B, one(eltype(Ω)), one(eltype(Ω)))
112+
Ωchol = cholesky(Ω)
113+
Y = X * B + reshape(Ωchol.L * randn(n * d), n, d)
120114
Y, X, V, B, Σ
121115
end
122-
123116
Y, X, V, B, Σ = simulate(5000, 4, 10, 2)
124117
```
125118

0 commit comments

Comments
 (0)