module Factory.Math.Statistics(
getMean,
getWeightedMean,
getVariance,
getStandardDeviation,
getAverageAbsoluteDeviation,
getCoefficientOfVariance,
nCr,
nPr
) where
import Control.Arrow((***))
import Control.Parallel(par, pseq)
import qualified Data.Foldable
import qualified Data.List
import qualified Factory.Math.Factorial as Math.Factorial
import qualified Factory.Math.Implementations.Factorial as Math.Implementations.Factorial
import qualified Factory.Math.Power as Math.Power
getMean :: (
Data.Foldable.Foldable foldable,
Fractional result,
Real value
)
=> foldable value
-> result
getMean foldable
| denominator == 0 = error "Factory.Math.Statistics.getMean:\tno data => undefined result."
| otherwise = realToFrac numerator / fromIntegral denominator
where
(numerator, denominator) = Data.Foldable.foldr (\s -> (+ s) *** succ) (0, 0 :: Int) foldable
getWeightedMean :: (
Data.Foldable.Foldable foldable,
Fractional result,
Real value,
Real weight
)
=> foldable (value, weight)
-> result
getWeightedMean foldable
| denominator == 0 = error "Factory.Math.Statistics.getWeightedMean:\tzero weight => undefined result."
| otherwise = numerator / realToFrac denominator
where
(numerator, denominator) = Data.Foldable.foldr (
\(value, weight) -> if weight == 0
then id
else (+ realToFrac value * realToFrac weight) *** (+ weight)
) (0, 0) foldable
getDispersionFromMean :: (
Data.Foldable.Foldable foldable,
Fractional result,
Functor foldable,
Real value
) => (Rational -> Rational) -> foldable value -> result
getDispersionFromMean weight foldable = getMean $ fmap (weight . (+ negate mean) . toRational) foldable where
mean :: Rational
mean = getMean foldable
getVariance :: (
Data.Foldable.Foldable foldable,
Fractional variance,
Functor foldable,
Real value
) => foldable value -> variance
getVariance = getDispersionFromMean Math.Power.square
getStandardDeviation :: (
Data.Foldable.Foldable foldable,
Floating result,
Functor foldable,
Real value
) => foldable value -> result
getStandardDeviation = sqrt . getVariance
getAverageAbsoluteDeviation :: (
Data.Foldable.Foldable foldable,
Fractional result,
Functor foldable,
Real value
) => foldable value -> result
getAverageAbsoluteDeviation = getDispersionFromMean abs
getCoefficientOfVariance :: (
Data.Foldable.Foldable foldable,
Eq result,
Floating result,
Functor foldable,
Real value
) => foldable value -> result
getCoefficientOfVariance l
| mean == 0 = error "Factory.Math.Statistics.getCoefficientOfVariance:\tundefined if mean is zero."
| otherwise = getStandardDeviation l / abs mean
where
mean = getMean l
nCr :: (Math.Factorial.Algorithmic factorialAlgorithm, Integral i, Show i)
=> factorialAlgorithm
-> i
-> i
-> i
nCr _ 0 _ = 1
nCr _ _ 0 = 1
nCr factorialAlgorithm n r
| n < 0 = error $ "Factory.Math.Statistics.nCr:\tinvalid n; " ++ show n
| r < 0 = error $ "Factory.Math.Statistics.nCr:\tinvalid r; " ++ show r
| n < r = 0
| otherwise = numerator `par` (denominator `pseq` numerator `div` denominator)
where
[smaller, bigger] = Data.List.sort [r, n r]
numerator = Math.Implementations.Factorial.risingFactorial (succ bigger) (n bigger)
denominator = Math.Factorial.factorial factorialAlgorithm smaller
nPr :: (Integral i, Show i)
=> i
-> i
-> i
nPr 0 _ = 1
nPr _ 0 = 1
nPr n r
| n < 0 = error $ "Factory.Math.Statistics.nPr:\tinvalid n; " ++ show n
| r < 0 = error $ "Factory.Math.Statistics.nPr:\tinvalid r; " ++ show r
| n < r = 0
| otherwise = Math.Implementations.Factorial.fallingFactorial n r