The "graphlasso" package implements the so-called graphical Lasso to estimate a covariance matrix with sparsity, setting some elements to zero.

This is a port from the publicly available Matlab code by Xiaohui Chen. The main reference for the graphical Lasso is:
Jerome Friedman, Trevor Hastie, Robert Tibshirani
Sparse inverse covariance estimation with the graphical lasso
Biostatistics, Volume 9, Issue 3, July 2008, Pages 432–441, 
https://doi.org/10.1093/biostatistics/kxm045
Working paper: http://www-stat.stanford.edu/~tibs/ftp/graph.pdf

# graphlasso (public function)

*Arguments:*

1. matrix Sigma - sample covariance matrix

2. scalar rho - regularization parameter (non-negative)

3. int algo - lasso algorithm chooser:

--- 0: included hansl-based shooting algorithm (slow),

--- 1: use gretl's regls with ADMM

--- 2: use gretl's regls with CCD (faster)

--- 3: use gretl's native C-level implementation with CCD (default, fastest)

4. scalar *iter - (maximum) number of iterations (pointer form, default 1000)

5. scalar tol - convergence tolerance level (default 1e-6)

*Returns:* a matrix with the sparse estimate corresponding to Sigma.

All arguments except the first two can be omitted. 

The Sigma matrix is expected to be symmetric and numerically positive-definite. Currently no specialized algorithm is implemented to obtain the (sparse) inverse, i.e. the corresponding precision matrix. Simply use the invpd() function afterwards in case you need it. 

With respect to 'rho', it might be useful to know that in Friedman et al. (2007/2008) values between 0.01 and 0.6 are employed. The appropriate choice depends on the application and is up to the caller.

As for the 'algo' switch, using the included shooting algorithm (also ported from Xiaohui Chen's code) should normally not be necessary. The ADMM algorithm is the default in gretl's regls, see the documentation for the regls add-on; however, the graphical lasso estimation must call the underlying lasso/ADMM routine repeatedly. (For purposes of experimentation and comparisons the lassoshooting function is also declared public but not documented here; see the hansl source code which should be fairly self-explanatory.) The CCD algorithm is somewhat faster than ADMM. Finally, there exists (since gretl 2023c) a native C-based implementation in gretl for the whole graphical lasso problem, so this approach also avoids two nested scripting loops and should yield a considerable speedup. 

The parameters 'iter' and 'tol' can typically be omitted; on entry 'iter' determines the max number of iterations before stopping, but on exit 'iter' will be set to the actual number of iterations that were needed, so if you're interested in that number you can pass a scalar variable in pointer form as the fourth argument to retrieve that information. (However, this does not work for the default native algorithm; see section 14.4 of the gretl user guide about using pointers.)

For comparison with R's glasso package, the sample script contains a commented-out example how to embed that package using the 'foreign' block apparatus in gretl. (Note that the defaults for iter and tol are different there, namely 10000 and 0.0001.)

## Changelog

- December 2024, v0.5: incorporate a C-level hidden gretl function for much faster execution (this speedup requires gretl 2023c), and because of this switch the default algorithm from ADMM to CCD; change maximum iteration default value from 100 to 1000; switch help text to markdown format. 

- February 2023, v0.2: add a check for NaN in input

- January  2023, v0.1: first release

## To do:

- Possibly implement faster algorithm from Witten et al. 2011.

- Offer the inverse of the covariance estimate with a specialized algorithm.
