# statistics

committed 9b6dc50

Replace asserts with checks which gives meaningful error messages

# Statistics/Quantile.hs

`     -- \$references`
`     ) where`
` `
`-import Control.Exception (assert)`
`-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,`
`             -> v Double   -- ^ /x/, the sample data.`
`             -> Double`
` weightedAvg k q x`
`-    | n == 1    = G.head x`
`-    | otherwise =`
`-    assert (q >= 2) .`
`-    assert (k >= 0) .`
`-    assert (k < q) .`
`-    assert (G.all (not . isNaN) x) \$`
`-    xj + g * (xj1 - xj)`
`+  | G.any isNaN x   = modErr "weightedAvg" "Sample contains NaNs"`
`+  | n == 1          = G.head x`
`+  | q < 2           = modErr "weightedAvg" "At least 2 quantiles is needed"`
`+  | k < 0 || k >= q = modErr "weightedAvg" "Wrong quantile number"`
`+  | otherwise       = xj + g * (xj1 - xj)`
`   where`
`     j   = floor idx`
`     idx = fromIntegral (n - 1) * fromIntegral k / fromIntegral q`
`              -> Int        -- ^ /q/, the number of quantiles.`
`              -> v Double   -- ^ /x/, the sample data.`
`              -> Double`
`-continuousBy (ContParam a b) k q x =`
`-    assert (q >= 2) .`
`-    assert (k >= 0) .`
`-    assert (k <= q) .`
`-    assert (G.all (not . isNaN) x) \$`
`-    (1-h) * item (j-1) + h * item j`
`+continuousBy (ContParam a b) k q x`
`+  | q < 2          = modErr "continuousBy" "At least 2 quantiles is needed"`
`+  | k < 0 || k > q = modErr "continuousBy" "Wrong quantile number"`
`+  | G.any isNaN x  = modErr "continuousBy" "Sample contains NaNs"`
`+  | otherwise      = (1-h) * item (j-1) + h * item j`
`   where`
`     j               = floor (t + eps)`
`     t               = a + p * (fromIntegral n + 1 - a - b)`
`           -> Int        -- ^ /q/, the number of quantiles.`
`           -> v Double   -- ^ /x/, the sample data.`
`           -> Double`
`-midspread (ContParam a b) k x =`
`-    assert (G.all (not . isNaN) x) .`
`-    assert (k > 0) \$`
`-    quantile (1-frac) - quantile frac`
`+midspread (ContParam a b) k x`
`+  | G.any isNaN x = modErr "midspread" "Sample contains NaNs"`
`+  | k <= 0        = modErr "midspread" "Nonpositive number of quantiles"`
`+  | otherwise     = quantile (1-frac) - quantile frac`
`   where`
`     quantile i        = (1-h i) * item (j i-1) + h i * item (j i)`
`     j i               = floor (t i + eps) :: Int`
`     where ta = 3/8`
` {-# INLINE normalUnbiased #-}`
` `
`+modErr :: String -> String -> a`
`+modErr f err = error \$ "Statistics.Quantile." ++ f ++ ": " ++ err`
`+`
` -- \$references`
` --`
` -- * Weisstein, E.W. Quantile. /MathWorld/.`
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.