summaryrefslogtreecommitdiff
path: root/examples/pinv2.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-09-21 18:28:08 +0000
committerAlberto Ruiz <aruiz@um.es>2007-09-21 18:28:08 +0000
commit0198366bba7a5f2d67338633f9eb90889ffc31b2 (patch)
tree4897d90233b333ee2092e63a4b74c7bcb2d22577 /examples/pinv2.hs
parentd4cb2692f9dae748da23371057a983deca4b2f80 (diff)
add examples
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)