module Arithmetic where
import qualified Algebra.Field as Field
import qualified Algebra.Ring as Ring
import NumericPrelude.Numeric
import NumericPrelude.Base hiding (id)
import Prelude ()
det2 :: (Ring.C a) => ((a, a), (a, a)) -> a
det2 ((a00,a01), (a10,a11)) = a00*a11 - a10*a01
solve2 ::
(Field.C a) =>
((a, a), (a, a)) -> (a, a) -> (a, a)
solve2 a@(a0,a1) b =
let d = det2 a
in (det2 (b,a1) / d, det2 (a0,b) / d)
linearRegression :: (Field.C a) => [(a,a)] -> (a, a)
linearRegression xys =
let (xs,ys) = unzip xys
s = sum xs
s2 = sum $ map (^2) xs
in solve2
((fromIntegral $ length xs, s), (s, s2))
(sum ys, sum $ zipWith (*) xs ys)