covariance 0.1.0.1 → 0.1.0.2
raw patch · 5 files changed
+93/−6 lines, 5 filesdep +statistics
Dependencies added: statistics
Files
- CHANGELOG.md +6/−0
- covariance.cabal +3/−1
- src/Statistics/Covariance.hs +54/−3
- src/Statistics/Covariance/LedoitWolf.hs +6/−2
- src/Statistics/Covariance/Types.hs +24/−0
CHANGELOG.md view
@@ -5,6 +5,12 @@ ## Unreleased changes +## 0.1.0.2++- `scale`, `rescaleWith` functions.+- Avoid duplicate centering.++ ## 0.1.0.1 - Fix tests, remove debug output.
covariance.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.4 name: covariance-version: 0.1.0.1+version: 0.1.0.2 synopsis: Well-conditioned estimation of large-dimensional covariance matrices @@ -26,6 +26,7 @@ exposed-modules: Statistics.Covariance other-modules:+ Statistics.Covariance.Types Statistics.Covariance.LedoitWolf Statistics.Covariance.RaoBlackwellLedoitWolf Statistics.Covariance.OracleApproximatingShrinkage@@ -33,6 +34,7 @@ ghc-options: -Wall -Wunused-packages build-depends: base ^>=4.14.3.0 , hmatrix+ , statistics , vector hs-source-dirs: src default-language: Haskell2010
src/Statistics/Covariance.hs view
@@ -10,22 +10,30 @@ -- -- Creation date: Tue Sep 14 13:02:15 2021. module Statistics.Covariance- ( empiricalCovariance,+ ( -- * Empirical estimator+ empiricalCovariance, -- * Shrinkage based estimators- --+ -- | See the overview on shrinkage estimators provided by -- [scikit-learn](https://scikit-learn.org/dev/modules/covariance.html#shrunk-covariance). module Statistics.Covariance.LedoitWolf, module Statistics.Covariance.RaoBlackwellLedoitWolf, module Statistics.Covariance.OracleApproximatingShrinkage,++ -- * Helper functions+ scale,+ rescaleWith, ) where +import qualified Data.Vector.Storable as VS import qualified Numeric.LinearAlgebra as L+import qualified Numeric.LinearAlgebra.Devel as L import Statistics.Covariance.LedoitWolf import Statistics.Covariance.OracleApproximatingShrinkage import Statistics.Covariance.RaoBlackwellLedoitWolf+import qualified Statistics.Sample as S -- | Empirical or sample covariance. --@@ -36,5 +44,48 @@ -- [hmatrix](https://hackage.haskell.org/package/hmatrix). -- -- NOTE: This function may call 'error'.-empiricalCovariance :: L.Matrix Double -> L.Herm Double+empiricalCovariance ::+ -- | Data matrix of dimension NxP, where N is the number of observations, and+ -- P is the number of parameters.+ L.Matrix Double ->+ L.Herm Double empiricalCovariance = snd . L.meanCov++scaleWith ::+ -- Vector of means (length P).+ L.Vector Double ->+ -- Vector of standard deviations (length P)+ L.Vector Double ->+ -- Data matrix of dimension N x P.+ L.Matrix Double ->+ -- Data matrix with means 0 and variance 1.0.+ L.Matrix Double+scaleWith ms ss = L.mapMatrixWithIndex (\(_, j) x -> (x - ms VS.! j) / (ss VS.! j))++-- | Center and scales columns.+--+-- Normalize a data matrix to have means 0 and standard deviations/variances+-- 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.+ L.Matrix Double ->+ -- | (Means, Standard deviations, Centered and scaled matrix)+ (L.Vector Double, L.Vector Double, L.Matrix Double)+scale xs = (ms, ss, scaleWith ms ss xs)+ where+ msVs = map S.meanVariance $ L.toColumns xs+ ms = L.fromList $ map fst msVs+ ss = L.fromList $ map (sqrt . snd) msVs++-- | Convert a correlation matrix with given standard deviations to original+-- scale.+rescaleWith ::+ -- | Vector of standard deviations (length P)+ L.Vector Double ->+ -- | Correlation matrix.+ L.Matrix Double ->+ -- | Covariance matrix.+ L.Matrix Double+rescaleWith ss = L.mapMatrixWithIndex (\(i, j) x -> x * (ss VS.! i) * (ss VS.! j))
src/Statistics/Covariance/LedoitWolf.hs view
@@ -17,6 +17,7 @@ import Data.Foldable import qualified Numeric.LinearAlgebra as L import Statistics.Covariance.Internal.Tools+import Statistics.Covariance.Types -- | Shrinkage based covariance estimator by Ledoit and Wolf. --@@ -32,11 +33,12 @@ -- -- NOTE: This function may call 'error' due to partial library functions. ledoitWolf ::+ DoCenter -> -- | 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 String (L.Herm Double)-ledoitWolf xs+ledoitWolf c xs | n < 2 = Left "ledoitWolf: Need more than one sample." | p < 1 = Left "ledoitWolf: Need at least one parameter." -- The Ledoit and Wolf shrinkage estimator of the covariance matrix@@ -47,7 +49,9 @@ n = L.rows xs p = L.cols xs (means, sigma) = L.meanCov xs- xsCentered = centerWith means xs+ xsCentered = case c of+ DoCenter -> centerWith means xs+ AssumeCentered -> xs im = L.trustSym $ L.ident p mu = muE sigma d2 = d2E im sigma mu
+ src/Statistics/Covariance/Types.hs view
@@ -0,0 +1,24 @@+-- |+-- Module : Statistics.Covariance.Types+-- Description : Common types+-- 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:03:01 2021.+module Statistics.Covariance.Types+ ( DoCenter (..),+ )+where++-- | For some methods, data matrices have to be centered before estimation of+-- the covariance matrix. Sometimes, data matrices are already centered, and in+-- this case, duplicate centering can be avoided.+data DoCenter+ = -- | Perform centering.+ DoCenter+ | -- | Do not perform centering; assume the data matrix is already centered.+ AssumeCentered