23 Apr 2013 11:44

## Automated Differentiation Type Question

```Can anyone tell me why I get a type error with testGrad2? What are my options? Clearly I would like to be able
find the gradient of my cost function for different sets of observations.

Thanks, Dominic.

> {-# LANGUAGE NoMonomorphismRestriction #-}
>
>
> default()
>
> costFn :: Floating a => [a] -> [[a]] -> [a] -> a
> costFn ys xss thetas = (/ (2*m)) \$ sum \$ map (^ (2 :: Int)) \$
>                        zipWith (\y xs -> costFnAux y xs thetas) ys xss
>   where
>     m = fromIntegral \$ length xss
>     costFnAux :: Floating a => a -> [a] -> [a] -> a
>     costFnAux y xs thetas = y - head thetas - sum (zipWith (*) xs (tail thetas))
>
> ys :: Floating a => [a]
> ys = [1.0, 2.0, 3.0]
>
> xss :: Floating a => [[a]]
> xss = [[1.0], [2.0], [3.0]]
>
> thetas :: Floating a => [a]
> thetas = [0.0, 1.0]
>
> test :: Floating a => a
> test = costFn ys xss thetas
```

23 Apr 2013 15:00

### Re: Automated Differentiation Type Question

```Answering my own question, what I needed was:

testGrad2 :: (Fractional a, Num a) =>
(forall s . Mode s => [AD s a]) ->
(forall s . Mode s => [[AD s a]]) ->
[a] -> [a]

On 23 Apr 2013, at 10:44, Dominic Steinitz <dominic <at> steinitz.org> wrote:

> Can anyone tell me why I get a type error with testGrad2? What are my options? Clearly I would like to be able
find the gradient of my cost function for different sets of observations.
>
> Thanks, Dominic.
>
>> {-# LANGUAGE NoMonomorphismRestriction #-}
>>
>>
>> default()
>>
>> costFn :: Floating a => [a] -> [[a]] -> [a] -> a
>> costFn ys xss thetas = (/ (2*m)) \$ sum \$ map (^ (2 :: Int)) \$
>>                       zipWith (\y xs -> costFnAux y xs thetas) ys xss
>>  where
>>    m = fromIntegral \$ length xss
>>    costFnAux :: Floating a => a -> [a] -> [a] -> a
>>    costFnAux y xs thetas = y - head thetas - sum (zipWith (*) xs (tail thetas))
>>
>> ys :: Floating a => [a]
```