versioninfo()
Biostat/Biomath M257 Homework 7
Due June 16 @ 11:59PM
System information (for reproducibility):
Load packages:
using Pkg
Pkg.activate(pwd())
Pkg.instantiate()
Pkg.status()
Again we continue with the linear mixed effects model (LMM) \[
\mathbf{Y}_i = \mathbf{X}_i \boldsymbol{\beta} + \mathbf{Z}_i \boldsymbol{\gamma}_i + \boldsymbol{\epsilon}_i, \quad i=1,\ldots,n,
\] where
- \(\mathbf{Y}_i \in \mathbb{R}^{n_i}\) is the response vector of \(i\)-th individual,
- \(\mathbf{X}_i \in \mathbb{R}^{n_i \times p}\) is the fixed effects predictor matrix of \(i\)-th individual,
- \(\mathbf{Z}_i \in \mathbb{R}^{n_i \times q}\) is the random effects predictor matrix of \(i\)-th individual,
- \(\boldsymbol{\epsilon}_i \in \mathbb{R}^{n_i}\) are multivariate normal \(N(\mathbf{0}_{n_i},\sigma^2 \mathbf{I}_{n_i})\),
- \(\boldsymbol{\beta} \in \mathbb{R}^p\) are fixed effects, and
- \(\boldsymbol{\gamma}_i \in \mathbb{R}^q\) are random effects assumed to be \(N(\mathbf{0}_q, \boldsymbol{\Sigma}_{q \times q}\)) independent of \(\boldsymbol{\epsilon}_i\).
The log-likelihood of the \(i\)-th datum \((\mathbf{y}_i, \mathbf{X}_i, \mathbf{Z}_i)\) is \[ \ell_i(\boldsymbol{\beta}, \mathbf{L}, \sigma_0^2) = - \frac{n_i}{2} \log (2\pi) - \frac{1}{2} \log \det \boldsymbol{\Omega}_i - \frac{1}{2} (\mathbf{y} - \mathbf{X}_i \boldsymbol{\beta})^T \boldsymbol{\Omega}_i^{-1} (\mathbf{y} - \mathbf{X}_i \boldsymbol{\beta}), \] where \[ \boldsymbol{\Omega}_i = \sigma^2 \mathbf{I}_{n_i} + \mathbf{Z}_i \boldsymbol{\Sigma} \mathbf{Z}_i^T. \] Given \(m\) independent data points \((\mathbf{y}_i, \mathbf{X}_i, \mathbf{Z}_i)\), \(i=1,\ldots,m\), we seek the maximum likelihood estimate (MLE) by maximizing the log-likelihood \[ \ell(\boldsymbol{\beta}, \boldsymbol{\Sigma}, \sigma_0^2) = \sum_{i=1}^m \ell_i(\boldsymbol{\beta}, \boldsymbol{\Sigma}, \sigma_0^2). \]
In HW6, we used the nonlinear programming (NLP) approach (Newton type algorithms) for optimization. In this assignment, we derive and implement an expectation-maximization (EM) algorithm for the same problem.
# load necessary packages; make sure install them first
using BenchmarkTools, Distributions, LinearAlgebra, Random
1 Q1. (10 pts) Refresher on normal-normal model
Assume the conditional distribution \[ \mathbf{y} \mid \boldsymbol{\gamma} \sim N(\mathbf{X} \boldsymbol{\beta} + \mathbf{Z} \boldsymbol{\gamma}, \sigma^2 \mathbf{I}_n) \] and the prior distribution \[ \boldsymbol{\gamma} \sim N(\mathbf{0}_q, \boldsymbol{\Sigma}). \] By the Bayes theorem, the posterior distribution is \[\begin{eqnarray*} f(\boldsymbol{\gamma} \mid \mathbf{y}) &=& \frac{f(\mathbf{y} \mid \boldsymbol{\gamma}) \times f(\boldsymbol{\gamma})}{f(\mathbf{y})}, \end{eqnarray*}\] where \(f\) denotes corresponding density.
Show that the posterior distribution of random effects \(\boldsymbol{\gamma}\) is a multivariate normal with mean \[\begin{eqnarray*} \mathbb{E} (\boldsymbol{\gamma} \mid \mathbf{y}) &=& \sigma^{-2} (\sigma^{-2} \mathbf{Z}^T \mathbf{Z} + \boldsymbol{\Sigma}^{-1})^{-1 } \mathbf{Z}^T (\mathbf{y} - \mathbf{X} \boldsymbol{\beta}) \\ &=& \boldsymbol{\Sigma} \mathbf{Z}^T (\mathbf{Z} \boldsymbol{\Sigma} \mathbf{Z}^T + \sigma^2 \mathbf{I})^{-1} (\mathbf{y} - \mathbf{X} \boldsymbol{\beta}) \end{eqnarray*}\] and covariance \[\begin{eqnarray*} \text{Var} (\boldsymbol{\gamma} \mid \mathbf{y}) &=& (\sigma^{-2} \mathbf{Z}^T \mathbf{Z} + \boldsymbol{\Sigma}^{-1})^{-1} \\ &=& \boldsymbol{\Sigma} - \boldsymbol{\Sigma} \mathbf{Z}^T (\mathbf{Z} \boldsymbol{\Sigma} \mathbf{Z}^T + \sigma^2 \mathbf{I})^{-1} \mathbf{Z} \boldsymbol{\Sigma}. \end{eqnarray*}\]
2 Q2. (20 pts) Derive EM algorithm
Write down the complete log-likelihood \[ \sum_{i=1}^m \log f(\mathbf{y}_i, \boldsymbol{\gamma}_i \mid \boldsymbol{\beta}, \boldsymbol{\Sigma}, \sigma^2) \]
Derive the \(Q\) function (E-step). \[ Q(\boldsymbol{\beta}, \boldsymbol{\Sigma}, \sigma^2 \mid \boldsymbol{\beta}^{(t)}, \boldsymbol{\Sigma}^{(t)}, \sigma^{2(t)}). \]
Derive the EM (or ECM) update of \(\boldsymbol{\beta}\), \(\boldsymbol{\Sigma}\), and \(\sigma^2\) (M-step).
3 Q3. (20 pts) Objective of a single datum
We modify the code from HW6 to evaluate the objective, the conditional mean of \(\boldsymbol{\gamma}\), and the conditional variance of \(\boldsymbol{\gamma}\). Start-up code is provided below. You do not have to use this code.
# define a type that holds an LMM datum
struct LmmObs{T <: AbstractFloat}
# data
:: Vector{T}
y :: Matrix{T}
X :: Matrix{T}
Z # posterior mean and variance of random effects γ
:: Vector{T} # posterior mean of random effects
μγ :: Matrix{T} # posterior variance of random effects
νγ # TODO: add whatever intermediate arrays you may want to pre-allocate
:: T
yty :: Vector{T}
rtr :: Vector{T}
xty :: Vector{T}
zty :: Vector{T}
ztr :: Vector{T}
ltztr :: Vector{T}
xtr :: Vector{T}
storage_p :: Vector{T}
storage_q :: Matrix{T}
xtx :: Matrix{T}
ztx :: Matrix{T}
ztz :: Matrix{T}
ltztzl :: Matrix{T}
storage_qq end
"""
LmmObs(y::Vector, X::Matrix, Z::Matrix)
Create an LMM datum of type `LmmObs`.
"""
function LmmObs(
::Vector{T},
y::Matrix{T},
X::Matrix{T}) where T <: AbstractFloat
Z= size(X, 1), size(X, 2), size(Z, 2)
n, p, q = Vector{T}(undef, q)
μγ = Matrix{T}(undef, q, q)
νγ = abs2(norm(y))
yty = Vector{T}(undef, 1)
rtr = transpose(X) * y
xty = transpose(Z) * y
zty = similar(zty)
ztr = similar(zty)
ltztr = Vector{T}(undef, p)
xtr = similar(xtr)
storage_p = Vector{T}(undef, q)
storage_q = transpose(X) * X
xtx = transpose(Z) * X
ztx = transpose(Z) * Z
ztz = similar(ztz)
ltztzl = similar(ztz)
storage_qq LmmObs(y, X, Z, μγ, νγ,
yty, rtr, xty, zty, ztr, ltztr, xtr,
storage_p, storage_q,
xtx, ztx, ztz, ltztzl, storage_qq)end
"""
logl!(obs::LmmObs, β, Σ, L, σ², updater = false)
Evaluate the log-likelihood of a single LMM datum at parameter values `β`, `Σ`,
and `σ²`. The lower triangular Cholesky factor `L` of `Σ` must be supplied too.
The fields `obs.μγ` and `obs.νγ` are overwritten by the posterior mean and
posterior variance of random effects. If `updater==true`, fields `obs.ztr`,
`obs.xtr`, and `obs.rtr` are updated according to input parameter values.
Otherwise, it assumes these three fields are pre-computed.
"""
function logl!(
:: LmmObs{T},
obs :: Vector{T},
β :: Matrix{T},
Σ :: Matrix{T},
L :: T,
σ² :: Bool = false
updater where T <: AbstractFloat
) = size(obs.X, 1), size(obs.X, 2), size(obs.Z, 2)
n, p, q = inv(σ²)
σ²inv ####################
# Evaluate objective
####################
# form the q-by-q matrix: Lt Zt Z L
copy!(obs.ltztzl, obs.ztz)
trmm!('L', 'L', 'T', 'N', T(1), L, obs.ltztzl) # O(q^3)
BLAS.trmm!('R', 'L', 'N', 'N', T(1), L, obs.ltztzl) # O(q^3)
BLAS.# form the q-by-q matrix: M = σ² I + Lt Zt Z L
copy!(obs.storage_qq, obs.ltztzl)
@inbounds for j in 1:q
+= σ²
obs.storage_qq[j, j] end
potrf!('U', obs.storage_qq) # O(q^3)
LAPACK.# Zt * res
&& BLAS.gemv!('N', T(-1), obs.ztx, β, T(1), copy!(obs.ztr, obs.zty)) # O(pq)
updater # Lt * (Zt * res)
trmv!('L', 'T', 'N', L, copy!(obs.ltztr, obs.ztr)) # O(q^2)
BLAS.# storage_q = (Mchol.U') \ (Lt * (Zt * res))
trsv!('U', 'T', 'N', obs.storage_qq, copy!(obs.storage_q, obs.ltztr)) # O(q^3)
BLAS.# Xt * res = Xt * y - Xt * X * β
&& BLAS.gemv!('N', T(-1), obs.xtx, β, T(1), copy!(obs.xtr, obs.xty))
updater # l2 norm of residual vector
&& (obs.rtr[1] = obs.yty - dot(obs.xty, β) - dot(obs.xtr, β))
updater # assemble pieces
::T = n * log(2π) + (n - q) * log(σ²) # constant term
logl@inbounds for j in 1:q # log det term
+= 2log(obs.storage_qq[j, j])
logl end
= abs2(norm(obs.storage_q)) # quadratic form term
qf += (obs.rtr[1] - qf) * σ²inv
logl /= -2
logl ######################################
# TODO: Evaluate posterior mean and variance
######################################
# TODO
###################
# Return
###################
return logl
end
It is a good idea to test correctness and efficiency of the single datum objective/posterior mean/var evaluator here. It’s the same test datum as in HW3 and HW6.
Random.seed!(257)
# dimension
= 2000, 5, 3
n, p, q # predictors
= [ones(n) randn(n, p - 1)]
X = [ones(n) randn(n, q - 1)]
Z # parameter values
= [2.0; -1.0; rand(p - 2)]
β = 1.5
σ² = fill(0.1, q, q) + 0.9I # compound symmetry
Σ = Matrix(cholesky(Symmetric(Σ)).L)
L # generate y
= X * β + Z * rand(MvNormal(Σ)) + sqrt(σ²) * randn(n)
y
# form the LmmObs object
= LmmObs(y, X, Z); obs
3.1 Correctness
@show logl = logl!(obs, β, Σ, L, σ², true)
@show obs.μγ
@show obs.νγ;
You will lose all 20 points if following statement throws AssertionError
.
@assert abs(logl - (-3256.1793358058258)) < 1e-4
@assert norm(obs.μγ - [0.10608689301333621,
-0.25104190602577225, -1.4653979409855415]) < 1e-4
@assert norm(obs.νγ - [
0.0007494356395909563 -1.2183420093769967e-6 -2.176783643112221e-6;
-1.2183420282298223e-6 0.0007542331467601107 2.1553464632686345e-5;
-2.1767836636008638e-6 2.1553464641863096e-5 0.0007465271342535443
< 1e-4 ])
3.2 Efficiency
Benchmark for efficiency.
= @benchmark logl!($obs, $β, $Σ, $L, $σ², true) bm_obj
My median runt time is 800ns. You will get full credit if the median run time is within 10μs. The points you will get are
clamp(10 / (median(bm_obj).time / 1e3) * 10, 0, 10)
# # check for type stability
# @code_warntype logl!(obs, β, Σ, L, σ²)
# using Profile
# Profile.clear()
# @profile for i in 1:10000; logl!(obs, β, Σ, L, σ²); end
# Profile.print(format=:flat)
4 Q4. LmmModel type
We modify the LmmModel
type in HW6 to hold all data points, model parameters, and intermediate arrays.
# define a type that holds LMM model (data + parameters)
struct LmmModel{T <: AbstractFloat}
# data
:: Vector{LmmObs{T}}
data # parameters
:: Vector{T}
β :: Matrix{T}
Σ :: Matrix{T}
L :: Vector{T}
σ² # TODO: add whatever intermediate arrays you may want to pre-allocate
:: Vector{T}
xty :: Vector{T}
xtr :: Vector{T}
ztr2 :: Matrix{T}
xtxinv :: Matrix{T}
ztz2 end
"""
LmmModel(data::Vector{LmmObs})
Create an LMM model that contains data and parameters.
"""
function LmmModel(obsvec::Vector{LmmObs{T}}) where T <: AbstractFloat
# dims
= size(obsvec[1].X, 2)
p = size(obsvec[1].Z, 2)
q # parameters
= Vector{T}(undef, p)
β = Matrix{T}(undef, q, q)
Σ = Matrix{T}(undef, q, q)
L = Vector{T}(undef, 1)
σ² # intermediate arrays
= zeros(T, p)
xty = similar(xty)
xtr = Vector{T}(undef, abs2(q))
ztr2 = zeros(T, p, p)
xtxinv # pre-calculate \sum_i Xi^T Xi and \sum_i Xi^T y_i
@inbounds for i in eachindex(obsvec)
= obsvec[i]
obs axpy!(T(1), obs.xtx, xtxinv)
BLAS.axpy!(T(1), obs.xty, xty)
BLAS.end
# invert X'X
potrf!('U', xtxinv)
LAPACK.potri!('U', xtxinv)
LAPACK.LinearAlgebra.copytri!(xtxinv, 'U')
= Matrix{T}(undef, abs2(q), abs2(q))
ztz2 LmmModel(obsvec, β, Σ, L, σ², xty, xtr, ztr2, xtxinv, ztz2)
end
5 Q5. Implement EM update
Let’s write the key function update_em!
that performs one iteration of EM update.
"""
update_em!(m::LmmModel, updater::Bool = false)
Perform one iteration of EM update. It returns the log-likelihood calculated
from input `m.β`, `m.Σ`, `m.L`, and `m.σ²`. These fields are then overwritten
by the next EM iterate. The fields `m.data[i].xtr`, `m.data[i].ztr`, and
`m.data[i].rtr` are updated according to the resultant `m.β`. If `updater==true`,
the function first updates `m.data[i].xtr`, `m.data[i].ztr`, and
`m.data[i].rtr` according to `m.β`. If `updater==false`, it assumes these fields
are pre-computed.
"""
function update_em!(m::LmmModel{T}, updater::Bool = false) where T <: AbstractFloat
= zero(T)
logl # TODO: update m.β
# TODO: update m.data[i].ztr, m.data[i].xtr, m.data[i].rtr
# TODO: update m.σ²
# update m.Σ and m.L
# return log-likelihood at input parameter values
loglend
6 Q6. (30 pts) Test data
Let’s generate a synthetic longitudinal data set (same as HW6) to test our algorithm.
Random.seed!(257)
# dimension
= 1000 # number of individuals
m = rand(1500:2000, m) # numbers of observations per individual
ns = 5 # number of fixed effects, including intercept
p = 3 # number of random effects, including intercept
q = Vector{LmmObs{Float64}}(undef, m)
obsvec # true parameter values
= [0.1; 6.5; -3.5; 1.0; 5]
βtrue = 1.5
σ²true = sqrt(σ²true)
σtrue = Matrix(Diagonal([2.0; 1.2; 1.0]))
Σtrue = Matrix(cholesky(Symmetric(Σtrue)).L)
Ltrue # generate data
for i in 1:m
# first column intercept, remaining entries iid std normal
= Matrix{Float64}(undef, ns[i], p)
X :, 1] .= 1
X[@views Distributions.rand!(Normal(), X[:, 2:p])
# first column intercept, remaining entries iid std normal
= Matrix{Float64}(undef, ns[i], q)
Z :, 1] .= 1
Z[@views Distributions.rand!(Normal(), Z[:, 2:q])
# generate y
= X * βtrue .+ Z * (Ltrue * randn(q)) .+ σtrue * randn(ns[i])
y # form a LmmObs instance
= LmmObs(y, X, Z)
obsvec[i] end
# form a LmmModel instance
= LmmModel(obsvec); lmm
6.1 Correctness
Evaluate log-likelihood and gradient at the true parameter values.
copy!(lmm.β, βtrue)
copy!(lmm.Σ, Σtrue)
copy!(lmm.L, Ltrue)
1] = σ²true
lmm.σ²[@show obj1 = update_em!(lmm, true)
@show lmm.β
@show lmm.Σ
@show lmm.L
@show lmm.σ²
println()
@show obj2 = update_em!(lmm, false)
@show lmm.β
@show lmm.Σ
@show lmm.L
@show lmm.σ²
Test correctness. You will loss all 30 points if following code throws AssertError
.
@assert abs(obj1 - (-2.840068438369969e6)) < 1e-4
@assert abs(obj2 - (-2.84006046054206e6)) < 1e-4
6.2 Efficiency
Test efficiency of EM update.
= @benchmark update_em!($lmm, true) setup=(
bm_emupdate copy!(lmm.β, βtrue);
copy!(lmm.Σ, Σtrue);
copy!(lmm.L, Ltrue);
1] = σ²true) lmm.σ²[
My median run time is 1ms. You will get full credit if your median run time is within 10ms. The points you will get are
clamp(10 / (median(bm_emupdate).time / 1e6) * 10, 0, 10)
6.3 Memory
You will lose 1 point for each 100 bytes memory allocation. So the points you will get is
clamp(10 - median(bm_emupdate).memory / 100, 0, 10)
7 Q7. Starting point
We use the same least squares estimates as in HW6 as starting point.
"""
init_ls!(m::LmmModel)
Initialize parameters of a `LmmModel` object from the least squares estimate.
`m.β`, `m.L`, and `m.σ²` are overwritten with the least squares estimates.
"""
function init_ls!(m::LmmModel{T}) where T <: AbstractFloat
= size(m.data[1].X, 2), size(m.data[1].Z, 2)
p, q # LS estimate for β
mul!(m.β, m.xtxinv, m.xty)
# LS etimate for σ2 and Σ
= zero(T), 0
rss, ntotal fill!(m.ztz2, 0)
fill!(m.ztr2, 0)
@inbounds for i in eachindex(m.data)
= m.data[i]
obs += length(obs.y)
ntotal # update Xt * res
gemv!('N', T(-1), obs.xtx, m.β, T(1), copy!(obs.xtr, obs.xty))
BLAS.# rss of i-th individual
+= obs.yty - dot(obs.xty, m.β) - dot(obs.xtr, m.β)
rss # update Zi' * res
gemv!('N', T(-1), obs.ztx, m.β, T(1), copy!(obs.ztr, obs.zty))
BLAS.# Zi'Zi ⊗ Zi'Zi
kron_axpy!(obs.ztz, obs.ztz, m.ztz2)
# Zi'res ⊗ Zi'res
kron_axpy!(obs.ztr, obs.ztr, m.ztr2)
end
1] = rss / ntotal
m.σ²[# LS estimate for Σ = LLt
potrf!('U', m.ztz2)
LAPACK.trsv!('U', 'T', 'N', m.ztz2, m.ztr2)
BLAS.trsv!('U', 'N', 'N', m.ztz2, m.ztr2)
BLAS.copyto!(m.Σ, m.ztr2)
copy!(m.L, m.Σ)
potrf!('L', m.L)
LAPACK.for j in 2:q, i in 1:j-1
= 0
m.L[i, j] end
mend
"""
kron_axpy!(A, X, Y)
Overwrite `Y` with `A ⊗ X + Y`. Same as `Y += kron(A, X)` but
more memory efficient.
"""
function kron_axpy!(
::AbstractVecOrMat{T},
A::AbstractVecOrMat{T},
X::AbstractVecOrMat{T}
Ywhere T <: Real
) = size(A, 1), size(A, 2)
m, n = size(X, 1), size(X, 2)
p, q @assert size(Y, 1) == m * p
@assert size(Y, 2) == n * q
@inbounds for j in 1:n
= (j - 1) * q
coffset for i in 1:m
= A[i, j]
a = (i - 1) * p
roffset for l in 1:q
= roffset + 1
r = coffset + l
c for k in 1:p
+= a * X[k, l]
Y[r, c] += 1
r end
end
end
end
Yend
init_ls!(lmm)
@show lmm.β
@show lmm.Σ
@show lmm.L
@show lmm.σ²
8 Q8. Estimation by EM
We write a function fit!
that implements the EM algorithm for estimating LMM.
"""
fit!(m::LmmModel)
Fit an `LmmModel` object by MLE using a EM algorithm. Start point
should be provided in `m.β`, `m.σ²`, `m.L`.
"""
function fit!(
:: LmmModel;
m :: Integer = 10_000,
maxiter :: AbstractFloat = 1e-12,
ftolrel :: Integer = 0
prtfreq
)= update_em!(m, true)
obj for iter in 0:maxiter
= obj
obj_old # EM update
= update_em!(m, false)
obj # print obj
> 0 && rem(iter, prtfreq) == 0 && println("iter=$iter, obj=$obj")
prtfreq # check monotonicity
< obj_old && (@warn "monotoniciy violated")
obj # check convergence criterion
- obj_old) < ftolrel * (abs(obj_old) + 1) && break
(obj # warning about non-convergence
== maxiter && (@warn "maximum iterations reached")
iter end
mend
9 Q9. (20 pts) Test drive
Now we can run our EM algorithm to compute the MLE.
# initialize from least squares
init_ls!(lmm)
@time fit!(lmm, prtfreq = 1);
println("objective value at solution: ", update_em!(lmm)); println()
println("solution values:")
@show lmm.β
@show lmm.σ²
@show lmm.L * transpose(lmm.L)
9.1 Correctness
You get 10 points if the following code does not throw AssertError
.
# objective at solution should be close enough to the optimal
@assert update_em!(lmm) > -2.840059e6
9.2 Efficiency
My median run time 5ms. You get 10 points if your median run time is within 1s.
= @benchmark fit!($lmm) setup = (init_ls!(lmm)) bm_em
# this is the points you get
clamp(1 / (median(bm_em).time / 1e9) * 10, 0, 10)
10 Q10. (10 pts) EM vs Newton type algorithms
Contrast EM algorithm to the Newton type algorithms (gradient free, gradient based, using Hessian) in HW6, in terms of the stability, convergence rate (how fast the algorithm is converging), final objective value, total run time, derivation, and implementation efforts.