GraphicalLasso.jl
A package for fitting the graphical lasso and some diagnostics for tuning parameter selection. This package follows the work of Friedman et al. (2008) and the extended BIC criterion of Foygel and Drton (2010). We gain inspiration from the glasso
package in R, and aim to provide a similar user experience in Julia.
Graphical Lasso Main Functions
GraphicalLasso.glasso
— Functionglasso(s::Matrix{Float64}, obs::Int, λ::Real; penalizediag::Bool=true, γ::Real=0.0, tol::Float64=1e-05, verbose::Bool=true, maxiter::Int=100, winit::Matrix{Float64}=zeros(size(s)))
Applies the graphical lasso (glasso) algorithm to estimate a sparse inverse covariance matrix.
Arguments
s::Matrix{Float64}
: Empirical covariance matrix.obs::Int
: Number of observations.λ::Real
: Regularization parameter for the lasso penalty.penalizediag::Bool=true
: Whether to penalize the diagonal entries. (optional)γ::Real=0.0
: EBIC tuning parameter. (optional)tol::Float64=1e-05
: Tolerance for the convergence criteria. (optional)verbose::Bool=true
: If true, prints convergence information. (optional)maxiter::Int=100
: Maximum number of iterations. (optional)winit::Matrix{Float64}=zeros(size(s))
: Initial value of the precision matrix. (optional)
Returns
NamedTuple
: A named tuple with fields:W::Matrix{Float64}
: Estimated precision matrix.θ::Matrix{Float64}
: Estimated inverse covariance matrix.ll::Float64
: Log-likelihood of the estimate.bicval::Float64
: EBIC value of the estimate.
GraphicalLasso.cdlasso
— Functioncdlasso(W11::Matrix{T}, s12::Vector{T}, λ::Real; max_iter::Int=100, tol::T=1e-5) where {T<:Real}
Solves the coordinate descent Lasso problem.
Arguments
W11::Matrix{T}
: A square matrix used in the coordinate descent update.s12::Vector{T}
: A vector used in the coordinate descent update.λ::Real
: Regularization parameter.max_iter::Int=100
: Maximum number of iterations. (optional)tol::T=1e-5
: Tolerance for the convergence criteria. (optional)
Returns
Vector{T}
: Solution vectorβ
.
Information Criteria
GraphicalLasso.ebic
— Functionebic(θ, ll, obs, thr, γ)
Calculates the Extended Bayesian Information Criterion (EBIC) for a given precision matrix θ
. From the paper by Foygel and Drton (2010), the EBIC is defined as:
$\text{EBIC} = -2 \times \text{Log-likelihood} + \log(n) \times \mathbf{E} + 4 \times \gamma \times \mathbf{E} \times \log(p)$
where:
- n is the number of observations.
- p is the number of variables.
- gamma is a tuning parameter.
- The number of edges is calculated as the count of entries in theta that exceed a given threshold.
Arguments
θ::AbstractMatrix
: Precision matrix.ll::Real
: Log-likelihood.obs::Int
: Number of observations.thr::Real
: Threshold value for counting edges.γ::Real
: EBIC tuning parameter.
Returns
Real
: EBIC value.
GraphicalLasso.critfunc
— Functioncritfunc(s, θ, rho; penalizediag=true)
Calculates the objective function value for the graphical lasso.
Arguments
s::AbstractMatrix
: Empirical covariance matrix.θ::AbstractMatrix
: Precision matrix.rho::Real
: Regularization parameter.penalizediag::Bool=true
: Whether to penalize the diagonal entries. (optional)
Returns
Real
: Value of the objective function.
GraphicalLasso.tuningselect
— Functiontuningselect(s::Matrix{Float64}, obs::Int, λ::AbstractVector{T}; γ::Real=0.0) where {T}
Selects the optimal regularization parameter λ
for the graphical lasso using EBIC.
Arguments
s::Matrix{Float64}
: Empirical covariance matrix.obs::Int
: Number of observations.λ::AbstractVector{T}
: Vector of regularization parameters to be tested.γ::Real=0.0
: EBIC tuning parameter. (optional)
Returns
T
: The optimal regularization parameter from the input vectorλ
.
Utility Functions
GraphicalLasso.randsparsecov
— Functionrandsparsecov(p, thr)
Generates a random sparse covariance matrix of size p x p
with a specified threshold for sparsity.
Arguments
p::Int
: The dimension of the covariance matrix.thr::Real
: Threshold value for sparsity. Values below this threshold will be set to zero.
Returns
Hermitian{Float64}
: A sparse covariance matrix.
GraphicalLasso.iscov
— Functioniscov(x::AbstractMatrix{T}) where {T<:Real}
Checks if a given matrix is a valid covariance matrix. A valid covariance matrix must be square, symmetric, and positive semi-definite.
Arguments
x::AbstractMatrix{T}
: Input matrix to check.
Returns
Bool
:true
if the matrix is a valid covariance matrix,false
otherwise.