module Network.Mux.DeltaQ.TraceStatsSupport where

-- This module is making use of hackage statistical libraries. They
-- are not the most efficient approaches for this particular use
-- case, and they may increase the package dependencies for the final
-- binaries (they have a lot of dependencies).
--
-- It may well be worthwhile constructing specialised version for the
-- specific use case, but building those and creating the associated
-- test suite was not deemed a good use of time (at the time of
-- creation).
--
-- Definite space/time optimisation task here.

import           Network.Mux.DeltaQ.TraceTypes

import qualified Data.Vector.Unboxed as V
import           Statistics.LinearRegression

estimateGS :: [(Int, SISec)] -> (Double, Double, Double)
estimateGS :: [(Int, SISec)] -> (Double, Double, Double)
estimateGS [(Int, SISec)]
xys
  = let ([Int]
xs', [SISec]
ys') = forall a b. [(a, b)] -> ([a], [b])
unzip [(Int, SISec)]
xys
        xs :: Vector Double
xs = forall a. Unbox a => [a] -> Vector a
V.fromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (Integral a, Num b) => a -> b
fromIntegral [Int]
xs'
        ys :: Vector Double
ys = forall a. Unbox a => [a] -> Vector a
V.fromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\(S Float
x) -> forall a. Fractional a => Rational -> a
fromRational forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Real a => a -> Rational
toRational forall a b. (a -> b) -> a -> b
$ Float
x) [SISec]
ys'
    in forall (v :: * -> *).
Vector v Double =>
v Double -> v Double -> (Double, Double, Double)
linearRegressionRSqr Vector Double
xs Vector Double
ys