{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE AllowAmbiguousTypes #-}

{-# OPTIONS_GHC -Wall #-}
-- {-# OPTIONS_GHC -Wno-unused-imports #-} -- TEMP

-- -- Does this flag make any difference?
-- {-# OPTIONS_GHC -fexpose-all-unfoldings #-}

-- | Multi-dimensional regression
module ConCat.Regress where

import Data.Key (Zip(..))

import ConCat.Misc ((:*)) -- ,Binop
import ConCat.AltCat (Ok)
import qualified ConCat.Free.VectorSpace as V
import ConCat.Free.VectorSpace hiding ((^+^),distSqr)
import ConCat.Free.LinearRow
-- import ConCat.Free.Affine

-- There's not much here now. See ConCat.Free.Affine.

-- Square of distance between predicted and observed datum
sqErrF :: forall s a g. (Num s, Foldable g, Zip g)
       => (a -> g s) -> a :* g s -> s
sqErrF :: forall s a (g :: * -> *).
(Num s, Foldable g, Zip g) =>
(a -> g s) -> (a :* g s) -> s
sqErrF a -> g s
h (a
x,g s
y) = a -> g s
h a
x g s -> g s -> s
forall s (f :: * -> *).
(Zip f, Foldable f, Num s) =>
f s -> f s -> s
`V.distSqr` g s
y
{-# INLINE sqErrF #-}

-- Square of distance between predicted and observed datum
sqErr :: forall s a b. Ok (L s) b => (a -> b) -> a :* b -> s
sqErr :: forall s a b. Ok (L s) b => (a -> b) -> (a :* b) -> s
sqErr a -> b
h (a
x,b
y) = a -> b
h a
x b -> b -> s
forall s a.
(HasV s a, Zip (V s a), Foldable (V s a), Num s) =>
a -> a -> s
`distSqr` b
y
{-# INLINE sqErr #-}

-- add :: forall s a. (HasV s a, Zip (V s a), Num s) => Binop a
-- add = onV2 @s (V.^+^)
-- {-# INLINE add #-}

-- | Distance squared
distSqr :: forall s a. (HasV s a, Zip (V s a), Foldable (V s a), Num s)
        => a -> a -> s
distSqr :: forall s a.
(HasV s a, Zip (V s a), Foldable (V s a), Num s) =>
a -> a -> s
distSqr a
a a
b = V s a s -> V s a s -> s
forall s (f :: * -> *).
(Zip f, Foldable f, Num s) =>
f s -> f s -> s
V.distSqr (a -> V s a s
forall s a. HasV s a => a -> V s a s
toV a
a) (a -> V s a s
forall s a. HasV s a => a -> V s a s
toV a
b)
{-# INLINE distSqr #-}