covariance 0.1.0.3 → 0.1.0.4
raw patch · 5 files changed
+84/−10 lines, 5 filesdep +glasso
Dependencies added: glasso
Files
- CHANGELOG.md +5/−0
- covariance.cabal +6/−4
- src/Statistics/Covariance.hs +9/−5
- src/Statistics/Covariance/GraphicalLasso.hs +62/−0
- test/Test.hs +2/−1
CHANGELOG.md view
@@ -5,6 +5,11 @@ ## Unreleased changes +## 0.1.0.4++- Graphical lasso (for now only re-export from `glasso` library).++ ## 0.1.0.3 - Fix tests and docs.
covariance.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.4 name: covariance-version: 0.1.0.3+version: 0.1.0.4 synopsis: Well-conditioned estimation of large-dimensional covariance matrices @@ -26,13 +26,15 @@ exposed-modules: Statistics.Covariance other-modules:- Statistics.Covariance.Types+ Statistics.Covariance.GraphicalLasso+ Statistics.Covariance.Internal.Tools Statistics.Covariance.LedoitWolf- Statistics.Covariance.RaoBlackwellLedoitWolf Statistics.Covariance.OracleApproximatingShrinkage- Statistics.Covariance.Internal.Tools+ Statistics.Covariance.RaoBlackwellLedoitWolf+ Statistics.Covariance.Types ghc-options: -Wall -Wunused-packages build-depends: base ^>=4.14.3.0+ , glasso , hmatrix , statistics , vector
src/Statistics/Covariance.hs view
@@ -21,6 +21,9 @@ module Statistics.Covariance.RaoBlackwellLedoitWolf, module Statistics.Covariance.OracleApproximatingShrinkage, + -- * Gaussian graphical model based estimators+ module Statistics.Covariance.GraphicalLasso,+ -- * Misc DoCenter (..), @@ -33,6 +36,7 @@ import qualified Data.Vector.Storable as VS import qualified Numeric.LinearAlgebra as L import qualified Numeric.LinearAlgebra.Devel as L+import Statistics.Covariance.GraphicalLasso import Statistics.Covariance.LedoitWolf import Statistics.Covariance.OracleApproximatingShrinkage import Statistics.Covariance.RaoBlackwellLedoitWolf@@ -72,8 +76,8 @@ -- 1.0. The estimated covariance matrix of a scaled data matrix is a correlation -- matrix, which is easier to estimate. scale ::- -- | Data matrix of dimension NxP, where N is the number of observations, and- -- P is the number of parameters.+ -- | Sample data matrix of dimension \(n \times p\), where \(n\) is the number+ -- of samples (rows), and \(p\) is the number of parameters (columns). L.Matrix Double -> -- | (Means, Standard deviations, Centered and scaled matrix) (L.Vector Double, L.Vector Double, L.Matrix Double)@@ -86,10 +90,10 @@ -- | Convert a correlation matrix with given standard deviations to original -- scale. rescaleWith ::- -- | Vector of standard deviations (length P)+ -- | Vector of standard deviations of length \(p\). L.Vector Double ->- -- | Correlation matrix.+ -- | Correlation matrix of dimension \(p \times p\). L.Matrix Double ->- -- | Covariance matrix.+ -- | Covariance matrix of dimension \(p \times p\). L.Matrix Double rescaleWith ss = L.mapMatrixWithIndex (\(i, j) x -> x * (ss VS.! i) * (ss VS.! j))
+ src/Statistics/Covariance/GraphicalLasso.hs view
@@ -0,0 +1,62 @@+-- |+-- Module : Statistics.Covariance.GraphicalLasso+-- Description : Graphical lasso+-- Copyright : (c) 2021 Dominik Schrempf+-- License : GPL-3.0-or-later+--+-- Maintainer : dominik.schrempf@gmail.com+-- Stability : experimental+-- Portability : portable+--+-- Creation date: Wed Sep 15 09:23:19 2021.+module Statistics.Covariance.GraphicalLasso+ ( graphicalLasso,+ )+where++import Algorithms.GLasso+import Data.Bifunctor+import qualified Numeric.LinearAlgebra as L++-- | Gaussian graphical model based estimator.+--+-- This function estimates both, the covariance and the precision matrices. It+-- is best suited for sparse covariance matrices.+--+-- For now, this is just a wrapper around 'glasso'.+--+-- See Friedman, J., Hastie, T., & Tibshirani, R., Sparse inverse covariance+-- estimation with the graphical lasso, Biostatistics, 9(3), 432–441 (2007).+-- http://dx.doi.org/10.1093/biostatistics/kxm045.+--+-- Return 'Left' if+--+-- - the regularization parameter is out of bounds \([0, \infty)\).+--+-- - only one sample is available.+--+-- - no parameters are available.+--+-- NOTE: This function may call 'error' due to partial library functions.+graphicalLasso ::+ -- | Regularization or lasso parameter; penalty for non-zero covariances. The+ -- higher the lasso parameter, the sparser the estimated inverse covariance+ -- matrix. Must be non-negative.+ Double ->+ -- | Sample data matrix of dimension \(n \times p\), where \(n\) is the number+ -- of samples (rows), and \(p\) is the number of parameters (columns).+ L.Matrix Double ->+ -- | @Either ErrorString (Covariance matrix, Precision matrix)@.+ Either String (L.Herm Double, L.Herm Double)+graphicalLasso l xs+ | l < 0 = Left "graphicalLasso: Regularization parameter is negative."+ | n < 2 = Left "graphicalLasso: Need more than one sample."+ | p < 1 = Left "graphicalLasso: Need at least one parameter."+ | otherwise =+ Right $+ bimap convert convert $ glasso p (L.flatten $ L.unSym sigma) l+ where+ n = L.rows xs+ p = L.cols xs+ (_, sigma) = L.meanCov xs+ convert = L.trustSym . L.reshape p
test/Test.hs view
@@ -46,7 +46,8 @@ estimators = [ (ledoitWolf DoCenter, "ledoitWolf"), (raoBlackwellLedoitWolf, "raoBlackwellLedoitWolf"),- (oracleApproximatingShrinkage, "oracleApproximatingShrinkage")+ (oracleApproximatingShrinkage, "oracleApproximatingShrinkage"),+ (fmap fst . graphicalLasso 1.0, "graphicalLasso") ] unitTests :: TestTree