Skip to content

Instantly share code, notes, and snippets.

@bicycle1885
Last active November 30, 2021 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bicycle1885/375d63d9c089e233b5bdfd677579f85d to your computer and use it in GitHub Desktop.
Save bicycle1885/375d63d9c089e233b5bdfd677579f85d to your computer and use it in GitHub Desktop.
Sparse inverse covariance estimation (graphical lasso)
# Fattahi, Salar, and Somayeh Sojoudi. 2019. “Graphical Lasso and Thresholding:
# Equivalence and Closed-Form Solutions.” Journal of Machine Learning Research:
# JMLR. https://www.jmlr.org/papers/volume20/17-501/17-501.pdf.
function approxsol(Σ, λ)
# Residue of Σ relative to λ (see def. 11).
Σres = zeros(size(Σ))
for j in axes(Σ, 2), i in axes(Σ, 1)
if i ≠ j && abs(Σ[i,j]) > λ
Σres[i,j] = Σ[i,j] - λ*sign(Σ[i,j])
end
end
# Approximated solution (see eq. 13).
v = zeros(size(Σ, 1))
for j in axes(Σ, 2), i in 1:j-1
if !iszero(Σres[i,j])
v[i] += Σres[i,j]^2/(Σ[i,i]*Σ[j,j] - Σres[i,j]^2)
end
end
A = zeros(size(Σ))
for j in axes(Σ, 2), i in axes(Σ, 1)
if i == j
A[i,j] = (1 + v[i])/Σ[i,i]
elseif !iszero(Σres[i,j])
A[i,j] = -Σres[i,j]/(Σ[i,i]*Σ[j,j] - Σres[i,j]^2)
end
end
return A
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment