<commit_before>-----------------------------------------------------------------------------
-- |
-- Module     : Numeric.LinearAlgebra.Statistics
-- Copyright  : Copyright (c) 2010, <NAME> <<EMAIL>>
-- License    : BSD3
-- Maintainer : <NAME> <<EMAIL>>
-- Stability  : experimental
--
-- Basic multivariate statistics.
--

module Numeric.LinearAlgebra.Statistics (
    CovMethod(..),
    defaultCovUplo,

    -- * Immutable interface
    
    -- ** Sums and means
    sumVector,
    meanVector,
    weightedSumVector,
    weightedMeanVector,

    -- ** Covariance matrices
    covMatrix,
    covMatrixWithMean,
    weightedCovMatrix,
    weightedCovMatrixWithMean,

    -- ** Covariance matrices in packed form
    covPacked,
    covPackedWithMean,
    weightedCovPacked,
    weightedCovPackedWithMean,

    -- * Mutable interface
    
    -- ** Sums and means
    sumToVector,
    meanToVector,
    weightedSumToVector,
    weightedMeanToVector,
        
    -- ** Covariance matrices
    covToMatrix,
    covToMatrixWithMean,
    weightedCovToMatrix,
    weightedCovToMatrixWithMean,
    
    -- ** Covariance matrices in packed form
    covToPacked,
    covToPackedWithMean,
    weightedCovToPacked,
    weightedCovToPackedWithMean,

    ) where

import Control.Monad( forM_ )
import Control.Monad.ST( ST )
import Data.List( foldl' )
import Text.Printf( printf )

import Numeric.LinearAlgebra.Types
import Numeric.LinearAlgebra.Vector
import Numeric.LinearAlgebra.Vector.ST
import Numeric.LinearAlgebra.Matrix
import Numeric.LinearAlgebra.Matrix.Herm
import Numeric.LinearAlgebra.Matrix.Packed
import Numeric.LinearAlgebra.Matrix.ST


-- | The method of scaling the sample covariance matrix.
data CovMethod =
      UnbiasedCov -- ^ This is the default behavior. Corresponds to a
                  -- scaling of @n/(n-1)@ in the unweighed case, and
                  -- @1/(1 - \\sum w_i^2)@ in the weighted case, where @w_i@
                  -- is the normalized weight. Note the unweighted and
                  -- weighted cases agree when @w_i = 1/n@.
                  
    | MLCov       -- ^ Returns the centered second moment matrix without
                  -- scaling the result.
    deriving (Eq, Show)

-- | Returns the default storage scheme for covariance matrices.
defaultCovUplo :: Uplo
defaultCovUplo = Upper

-- | Returns the sum of the vectors.  The first argument gives the dimension
-- of the vectors.
sumVector :: (VNum e) => Int -> [Vector e] -> Vector e
sumVector p xs = runVector $ do
    s <- newVector_ p
    sumToVector xs s
    return s

-- | Returns the mean of the vectors.  The first argument gives the dimension
-- of the vectors.
meanVector :: (VNum e, Fractional e) => Int -> [Vector e] -> Vector e
meanVector p xs = runVector $ do
      m <- newVector_ p
      meanToVector xs m
      return m

-- | Returns the weighted sum of the vectors.  The first argument gives the
-- dimension of the vectors.
weightedSumVector :: (VNum e) => Int -> [(e, Vector e)] -> Vector e
weightedSumVector p wxs = runVector $ do
    s <- newVector_ p
    weightedSumToVector wxs s<commit_msg>Change defaultCovUplo to Lower<commit_after>-----------------------------------------------------------------------------
-- |
-- Module     : Numeric.LinearAlgebra.Statistics
-- Copyright  : Copyright (c) 2010, <NAME> <<EMAIL>>
-- License    : BSD3
-- Maintainer : <NAME> <<EMAIL>>
-- Stability  : experimental
--
-- Basic multivariate statistics.
--

module Numeric.LinearAlgebra.Statistics (
    CovMethod(..),
    defaultCovUplo,

    -- * Immutable interface
    
    -- ** Sums and means
    sumVector,
    meanVector,
    weightedSumVector,
    weightedMeanVector,

    -- ** Covariance matrices
    covMatrix,
    covMatrixWithMean,
    weightedCovMatrix,
    weightedCovMatrixWithMean,

    -- ** Covariance matrices in packed form
    covPacked,
    covPackedWithMean,
    weightedCovPacked,
    weightedCovPackedWithMean,

    -- * Mutable interface
    
    -- ** Sums and means
    sumToVector,
    meanToVector,
    weightedSumToVector,
    weightedMeanToVector,
        
    -- ** Covariance matrices
    covToMatrix,
    covToMatrixWithMean,
    weightedCovToMatrix,
    weightedCovToMatrixWithMean,
    
    -- ** Covariance matrices in packed form
    covToPacked,
    covToPackedWithMean,
    weightedCovToPacked,
    weightedCovToPackedWithMean,

    ) where

import Control.Monad( forM_ )
import Control.Monad.ST( ST )
import Data.List( foldl' )
import Text.Printf( printf )

import Numeric.LinearAlgebra.Types
import Numeric.LinearAlgebra.Vector
import Numeric.LinearAlgebra.Vector.ST
import Numeric.LinearAlgebra.Matrix
import Numeric.LinearAlgebra.Matrix.Herm
import Numeric.LinearAlgebra.Matrix.Packed
import Numeric.LinearAlgebra.Matrix.ST


-- | The method of scaling the sample covariance matrix.
data CovMethod =
      UnbiasedCov -- ^ This is the default behavior. Corresponds to a
                  -- scaling of @n/(n-1)@ in the unweighed case, and
                  -- @1/(1 - \\sum w_i^2)@ in the weighted case, where @w_i@
                  -- is the normalized weight. Note the unweighted and
                  -- weighted cases agree when @w_i = 1/n@.
                  
    | MLCov       -- ^ Returns the centered second moment matrix without
                  -- scaling the result.
    deriving (Eq, Show)

-- | Returns the default storage scheme for covariance matrices.
defaultCovUplo :: Uplo
defaultCovUplo = Lower

-- | Returns the sum of the vectors.  The first argument gives the dimension
-- of the vectors.
sumVector :: (VNum e) => Int -> [Vector e] -> Vector e
sumVector p xs = runVector $ do
    s <- newVector_ p
    sumToVector xs s
    return s

-- | Returns the mean of the vectors.  The first argument gives the dimension
-- of the vectors.
meanVector :: (VNum e, Fractional e) => Int -> [Vector e] -> Vector e
meanVector p xs = runVector $ do
      m <- newVector_ p
      meanToVector xs m
      return m

-- | Returns the weighted sum of the vectors.  The first argument gives the
-- dimension of the vectors.
weightedSumVector :: (VNum e) => Int -> [(e, Vector e)] -> Vector e
weightedSumVector p wxs = runVector $ do
    s <- newVector_ p
    weightedSumToVector wxs s
{'fraction_non_alphanumeric': 0.11219746754287546, 'fraction_numerical': 0.0032056419297964416, 'mean_word_length': 2.365695792880259, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4214761', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1788, 'n_words': 570}