# bos/statistics

A big old style cleanup diff

This cleans up a number of small issues that are not consistent
with my normal coding style, mostly relating to import sorting and
whitespace.

--HG--
rename : tests/Tests/NonparametricTest.hs => tests/Tests/NonParametric.hs
rename : tests/Tests/NonparametricTest/Table.hs => tests/Tests/NonParametric/Table.hs
1 parent a06b3ca commit d948e0c6d7fe1b609919ee5b6b3c8ce0b8d87093 committed Mar 4, 2014
 @@ -32,15 +32,14 @@ module Statistics.Distribution , sumProbabilities ) where -import Control.Applicative ((<\$>), Applicative(..)) +import Control.Applicative ((<\$>), Applicative(..)) import Control.Monad.Primitive (PrimMonad,PrimState) import Prelude hiding (sum) import Statistics.Sample.Internal (sum) -import System.Random.MWC +import System.Random.MWC (Gen, uniform) import qualified Data.Vector.Unboxed as U - -- | Type class common to all distributions. Only c.d.f. could be -- defined for both discrete and continous distributions. class Distribution d where
 @@ -89,12 +89,12 @@ instance D.MaybeVariance BetaDistribution where instance D.Entropy BetaDistribution where entropy (BD a b) = - logBeta a b + logBeta a b - (a-1) * digamma a - (b-1) * digamma b + (a+b-2) * digamma (a+b) {-# INLINE entropy #-} - + instance D.MaybeEntropy BetaDistribution where maybeEntropy = Just . D.entropy {-# INLINE maybeEntropy #-}
 @@ -103,7 +103,7 @@ variance (BD n p) = fromIntegral n * p * (1 - p) {-# INLINE variance #-} directEntropy :: BinomialDistribution -> Double -directEntropy d@(BD n _) = +directEntropy d@(BD n _) = negate . sum \$ takeWhile (< negate m_epsilon) \$ dropWhile (not . (< negate m_epsilon)) \$
 @@ -76,6 +76,6 @@ instance D.ContGen CauchyDistribution where instance D.Entropy CauchyDistribution where entropy (CD _ s) = log s + log (4*pi) - + instance D.MaybeEntropy CauchyDistribution where maybeEntropy = Just . D.entropy
 @@ -72,12 +72,12 @@ instance D.MaybeMean ChiSquared where instance D.MaybeVariance ChiSquared where maybeStdDev = Just . D.stdDev maybeVariance = Just . D.variance - + instance D.Entropy ChiSquared where entropy (ChiSquared ndf) = let kHalf = 0.5 * fromIntegral ndf in - kHalf - + log 2 + kHalf + + log 2 + logGamma kHalf + (1-kHalf) * digamma kHalf
 @@ -93,9 +93,9 @@ instance D.Entropy FDistribution where entropy (F n m _) = let nHalf = 0.5 * n mHalf = 0.5 * m in - log (n/m) + log (n/m) + logBeta nHalf mHalf - + (1 - nHalf) * digamma nHalf + + (1 - nHalf) * digamma nHalf - (1 + mHalf) * digamma mHalf + (nHalf + mHalf) * digamma (nHalf + mHalf)
 @@ -25,17 +25,16 @@ module Statistics.Distribution.Gamma , gdScale ) where +import Control.Applicative ((<\$>), (<*>)) import Data.Binary (Binary) +import Data.Binary (put, get) import Data.Data (Data, Typeable) import GHC.Generics (Generic) import Numeric.MathFunctions.Constants (m_pos_inf, m_NaN, m_neg_inf) -import Numeric.SpecFunctions ( - incompleteGamma, invIncompleteGamma, logGamma, digamma) -import Statistics.Distribution.Poisson.Internal as Poisson -import qualified Statistics.Distribution as D +import Numeric.SpecFunctions (incompleteGamma, invIncompleteGamma, logGamma, digamma) +import Statistics.Distribution.Poisson.Internal as Poisson +import qualified Statistics.Distribution as D import qualified System.Random.MWC.Distributions as MWC -import Data.Binary (put, get) -import Control.Applicative ((<\$>), (<*>)) -- | The gamma distribution. data GammaDistribution = GD { @@ -94,11 +93,11 @@ instance D.MaybeVariance GammaDistribution where instance D.MaybeEntropy GammaDistribution where maybeEntropy (GD a l) - | a > 0 && l > 0 = + | a > 0 && l > 0 = Just \$ - a - + log l - + logGamma a + a + + log l + + logGamma a + (1-a) * digamma a | otherwise = Nothing
 @@ -30,15 +30,15 @@ module Statistics.Distribution.Geometric , gdSuccess0 ) where -import Control.Monad (liftM) -import Data.Binary (Binary) -import Data.Data (Data, Typeable) -import GHC.Generics (Generic) -import Numeric.MathFunctions.Constants(m_pos_inf,m_neg_inf) -import qualified Statistics.Distribution as D -import qualified System.Random.MWC.Distributions as MWC -import Data.Binary (put, get) import Control.Applicative ((<\$>)) +import Control.Monad (liftM) +import Data.Binary (Binary) +import Data.Binary (put, get) +import Data.Data (Data, Typeable) +import GHC.Generics (Generic) +import Numeric.MathFunctions.Constants (m_pos_inf, m_neg_inf) +import qualified Statistics.Distribution as D +import qualified System.Random.MWC.Distributions as MWC ---------------------------------------------------------------- -- Distribution over [1..]
 @@ -67,7 +67,7 @@ instance D.MaybeVariance HypergeometricDistribution where instance D.Entropy HypergeometricDistribution where entropy = directEntropy - + instance D.MaybeEntropy HypergeometricDistribution where maybeEntropy = Just . D.entropy
 @@ -20,17 +20,16 @@ module Statistics.Distribution.Normal , standard ) where +import Control.Applicative ((<\$>), (<*>)) import Data.Binary (Binary) +import Data.Binary (put, get) import Data.Data (Data, Typeable) import GHC.Generics (Generic) import Numeric.MathFunctions.Constants (m_sqrt_2, m_sqrt_2_pi) -import Numeric.SpecFunctions (erfc, invErfc) +import Numeric.SpecFunctions (erfc, invErfc) import qualified Statistics.Distribution as D -import qualified Statistics.Sample as S +import qualified Statistics.Sample as S import qualified System.Random.MWC.Distributions as MWC -import Data.Binary (put, get) -import Control.Applicative ((<\$>), (<*>)) - -- | The normal distribution.
 @@ -14,10 +14,10 @@ module Statistics.Distribution.Poisson.Internal probability, poissonEntropy ) where -import Data.List(unfoldr) +import Data.List (unfoldr) import Numeric.MathFunctions.Constants (m_sqrt_2_pi, m_tiny, m_epsilon) import Numeric.SpecFunctions (logGamma, stirlingError, choose, logFactorial) -import Numeric.SpecFunctions.Extra (bd0) +import Numeric.SpecFunctions.Extra (bd0) -- | An unchecked, non-integer-valued version of Loader's saddle point -- algorithm. @@ -50,7 +50,7 @@ alyc k = where parity j | even (k-j) = -1 | otherwise = 1 - + -- | Returns [x, x^2, x^3, x^4, ...] powers :: Double -> [Double] powers x = unfoldr (\y -> Just (y*x,y*x)) 1 @@ -68,31 +68,31 @@ alyThm2 :: Double -> [Double] -> [Double] -> Double alyThm2 lambda upper lower = alyThm2Upper lambda upper + 0.5 * (zipCoefficients lambda lower) -zipCoefficients :: Double -> [Double] -> Double +zipCoefficients :: Double -> [Double] -> Double zipCoefficients lambda coefficients = (sum \$ map (uncurry (*)) (zip (powers \$ recip lambda) coefficients)) -- Mathematica code deriving the coefficients below: -- -- poissonMoment[0, s_] := 1 -- poissonMoment[1, s_] := 0 --- poissonMoment[k_, s_] := +-- poissonMoment[k_, s_] := -- Sum[s * Binomial[k - 1, j] * poissonMoment[j, s], {j, 0, k - 2}] -- -- upperSeries[m_] := -- Distribute[Integrate[ --- Sum[(-1)^(j - 1) * +-- Sum[(-1)^(j - 1) * -- poissonMoment[j, \[Lambda]] / (j * (j - 1)* \[Lambda]^j), -- {j, 3, 2 m - 1}], -- \[Lambda]]] -- -- lowerSeries[m_] := -- Distribute[Integrate[ -- poissonMoment[ --- 2 m + 2, \[Lambda]] / ((2 m + +-- 2 m + 2, \[Lambda]] / ((2 m + -- 1)*\[Lambda]^(2 m + 2)), \[Lambda]]] -- --- upperBound[m_] := upperSeries[m] + (Log[2*Pi*\[Lambda]] + 1)/2 +-- upperBound[m_] := upperSeries[m] + (Log[2*Pi*\[Lambda]] + 1)/2 -- -- lowerBound[m_] := upperBound[m] + lowerSeries[m] @@ -120,21 +120,21 @@ lowerCoefficients8 :: [Double] lowerCoefficients8 = [0,0,0,0,0,0,0, -2027025/8, -15315300, -105252147, -178127950, -343908565/4, -10929270, -3721149/14, -7709/15, -1/272] - + upperCoefficients10 :: [Double] upperCoefficients10 = [1/12, 1/24, 19/360, 9,80, 863/2520, 1375/1008, 33953/5040, 57281/1440, -2271071617/11880, -1483674219/176, -31714406276557/720720, -7531072742237/131040, -1405507544003/65520, -21001919627/10080, -1365808297/36720, -26059/544, -1/5814] - + lowerCoefficients10 :: [Double] lowerCoefficients10 = [0,0,0,0,0,0,0,0,0,-130945815/2, -7638505875, -438256243425/4, -435477637540, -3552526473925/6, -857611717105/3, -545654955967/12, -5794690528/3, -578334559/42, -699043/133, -1/420] - + upperCoefficients12 :: [Double] upperCoefficients12 = [1/12, 1/24, 19/360, 863/2520, 1375/1008, 33953/5040, 57281/1440, 3250433/11880, @@ -153,13 +153,13 @@ lowerCoefficients12 = [0,0,0,0,0,0,0,0,0,0,0, -347362037754732, -2205885452434521/100, -12237195698286/35, -16926981721/22, -6710881/155, -1/600] - + -- | Compute entropy directly from its definition. This is just as accurate -- as 'alyThm1' for lambda <= 1 and is faster, but is slow for large lambda, -- and produces some underestimation due to accumulation of floating point -- error. directEntropy :: Double -> Double -directEntropy lambda = +directEntropy lambda = negate . sum \$ takeWhile (< negate m_epsilon * lambda) \$ dropWhile (not . (< negate m_epsilon * lambda)) \$ @@ -175,4 +175,4 @@ poissonEntropy lambda | lambda <= 18 = alyThm2 lambda upperCoefficients6 lowerCoefficients6 | lambda <= 24 = alyThm2 lambda upperCoefficients8 lowerCoefficients8 | lambda <= 30 = alyThm2 lambda upperCoefficients10 lowerCoefficients10 - | otherwise = alyThm2 lambda upperCoefficients12 lowerCoefficients12 + | otherwise = alyThm2 lambda upperCoefficients12 lowerCoefficients12
 @@ -79,7 +79,7 @@ instance D.MaybeVariance StudentT where instance D.Entropy StudentT where entropy (StudentT ndf) = 0.5 * (ndf+1) * (digamma ((1+ndf)/2) - digamma(ndf/2)) - + log (sqrt ndf) + + log (sqrt ndf) + logBeta (ndf/2) 0.5 instance D.MaybeEntropy StudentT where
 @@ -10,19 +10,20 @@ -- Portability : portable -- -- Transformations over distributions -module Statistics.Distribution.Transform ( - LinearTransform (..) - , linTransFixedPoint - , scaleAround - ) where +module Statistics.Distribution.Transform + ( + LinearTransform (..) + , linTransFixedPoint + , scaleAround + ) where +import Control.Applicative ((<*>)) import Data.Binary (Binary) +import Data.Binary (put, get) import Data.Data (Data, Typeable) +import Data.Functor ((<\$>)) import GHC.Generics (Generic) -import Data.Functor ((<\$>)) import qualified Statistics.Distribution as D -import Data.Binary (put, get) -import Control.Applicative ((<*>)) -- | Linear transformation applied to distribution. -- @@ -77,11 +78,11 @@ instance (D.Variance d) => D.Variance (LinearTransform d) where variance (LinearTransform _ sc dist) = sc * sc * D.variance dist stdDev (LinearTransform _ sc dist) = sc * D.stdDev dist -instance (D.MaybeEntropy d, D.DiscreteDistr d) +instance (D.MaybeEntropy d, D.DiscreteDistr d) => D.MaybeEntropy (LinearTransform d) where maybeEntropy (LinearTransform _ _ dist) = D.maybeEntropy dist -instance (D.Entropy d, D.DiscreteDistr d) +instance (D.Entropy d, D.DiscreteDistr d) => D.Entropy (LinearTransform d) where entropy (LinearTransform _ _ dist) = D.entropy dist
 @@ -16,9 +16,9 @@ module Statistics.Function.Comparison within ) where -import Control.Monad.ST (runST) +import Control.Monad.ST (runST) +import Data.Int (Int64) import Data.Primitive.ByteArray (newByteArray, readByteArray, writeByteArray) -import Data.Int (Int64) -- | Compare two 'Double' values for approximate equality, using -- Dawson's method.
 @@ -15,7 +15,7 @@ -- 'Numeric.SpecFunctions.Extra' and 'Numeric.Polynomial.Chebyshev'. module Statistics.Math -{-# DEPRECATED "Use package math-function" #-} +{-# DEPRECATED "Use package math-function" #-} ( module Numeric.Polynomial.Chebyshev , module Numeric.SpecFunctions , module Numeric.SpecFunctions.Extra @@ -24,4 +24,3 @@ module Statistics.Math import Numeric.Polynomial.Chebyshev import Numeric.SpecFunctions import Numeric.SpecFunctions.Extra -
 @@ -20,16 +20,15 @@ module Statistics.Math.RootFinding -- \$references ) where -import Statistics.Function.Comparison - +import Control.Applicative (Alternative(..), Applicative(..)) +import Control.Monad (MonadPlus(..), ap) import Data.Binary (Binary) -import Control.Applicative -import Control.Monad (MonadPlus(..), ap) -import Data.Data (Data, Typeable) -import GHC.Generics (Generic) import Data.Binary (put, get) -import Data.Binary.Put (putWord8) import Data.Binary.Get (getWord8) +import Data.Binary.Put (putWord8) +import Data.Data (Data, Typeable) +import GHC.Generics (Generic) +import Statistics.Function.Comparison (within) -- | The result of searching for a root of a mathematical function.
 @@ -37,14 +37,14 @@ module Statistics.Quantile -- \$references ) where -import Data.Vector.Generic ((!)) +import Data.Vector.Generic ((!)) import Numeric.MathFunctions.Constants (m_epsilon) -import Statistics.Function (partialSort) +import Statistics.Function (partialSort) import qualified Data.Vector.Generic as G -- | O(/n/ log /n/). Estimate the /k/th /q/-quantile of a sample, -- using the weighted average method. -weightedAvg :: G.Vector v Double => +weightedAvg :: G.Vector v Double => Int -- ^ /k/, the desired quantile. -> Int -- ^ /q/, the number of quantiles. -> v Double -- ^ /x/, the sample data.