summaryrefslogtreecommitdiff
path: root/examples/pinv2.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/pinv2.hs')
-rw-r--r--examples/pinv2.hs26
1 files changed, 26 insertions, 0 deletions
diff --git a/examples/pinv2.hs b/examples/pinv2.hs
new file mode 100644
index 0000000..c1038d1
--- /dev/null
+++ b/examples/pinv2.hs
@@ -0,0 +1,26 @@
1-- MSE polynomial model using the pseudoinverse
2import LinearAlgebra
3import Graphics.Plot
4
5expand :: Int -> Vector Double -> Matrix Double
6expand n x = fromColumns $ constant 1 (dim x): map (x^) [1 .. n]
7
8polynomialModel :: Matrix Double -> Int -> (Vector Double -> Vector Double)
9polynomialModel d n = f where
10 f z = expand n z <> ws
11 ws = pinv a <> b
12 [x,b] = toColumns d
13 a = expand n x
14
15mk fv = f where
16 f x = fv (fromList [x]) @> 0
17
18main = do
19 d <- readMatrix `fmap` readFile "data.txt"
20 let [x,y] = toColumns d
21 let pol = polynomialModel d
22 let view = [x, y, pol 1 x, pol 2 x, pol 3 x]
23 dispR 2 $ fromColumns view
24 mplot view
25 let f = mk (pol 2)
26 print (f 2.5)