summaryrefslogtreecommitdiff
path: root/examples/pinv2.hs
blob: 7423540e9fdfe947066ca54713a68c5bb000924c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-- MSE polynomial model using the pseudoinverse
import Numeric.LinearAlgebra
import Graphics.Plot

expand :: Int -> Vector Double -> Matrix Double
expand n x = fromColumns $ constant 1 (dim x): map (x^) [1 .. n]

polynomialModel :: Matrix Double -> Int -> (Vector Double -> Vector Double)
polynomialModel d n = f where
    f z = expand n z <> ws 
    ws  = pinv a <> b
    [x,b] = toColumns d 
    a = expand n x

mk fv = f where
    f x = fv (fromList [x]) @> 0

main = do
    d <- readMatrix `fmap` readFile "data.txt"
    let [x,y] = toColumns d
    let pol = polynomialModel d
    let view = [x, y, pol 1 x, pol 2 x, pol 3 x]
    dispR 2 $ fromColumns view
    mplot view
    let f = mk (pol 2)
    print (f 2.5)