summaryrefslogtreecommitdiff
path: root/examples
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
parentd4cb2692f9dae748da23371057a983deca4b2f80 (diff)
add examples
Diffstat (limited to 'examples')
-rw-r--r--examples/data.txt10
-rw-r--r--examples/deriv.hs8
-rw-r--r--examples/error.hs16
-rw-r--r--examples/integrate.hs17
-rw-r--r--examples/kalman.hs56
-rw-r--r--examples/listlike.hs32
-rw-r--r--examples/minimize.hs43
-rw-r--r--examples/pca1.hs45
-rw-r--r--examples/pca2.hs67
-rw-r--r--examples/pinv1.hs18
-rw-r--r--examples/pinv2.hs26
-rw-r--r--examples/plot.hs20
-rw-r--r--examples/testmnist.m29
13 files changed, 387 insertions, 0 deletions
diff --git a/examples/data.txt b/examples/data.txt
new file mode 100644
index 0000000..7031462
--- /dev/null
+++ b/examples/data.txt
@@ -0,0 +1,10 @@
1 1 1.1
2 2 3.9
3 3 9.2
4 4 15.8
5 5 25.3
6 6 35.7
7 7 49.4
8 8 63.6
9 9 81.5
1010 99.5 \ No newline at end of file
diff --git a/examples/deriv.hs b/examples/deriv.hs
new file mode 100644
index 0000000..472a284
--- /dev/null
+++ b/examples/deriv.hs
@@ -0,0 +1,8 @@
1-- Numerical differentiation
2
3import GSL
4
5d :: (Double -> Double) -> (Double -> Double)
6d f x = fst $ derivCentral 0.01 f x
7
8main = print $ d (\x-> x * d (\y-> x+y) 1) 1
diff --git a/examples/error.hs b/examples/error.hs
new file mode 100644
index 0000000..bcfe224
--- /dev/null
+++ b/examples/error.hs
@@ -0,0 +1,16 @@
1import GSL
2import Prelude hiding (catch)
3import Control.Exception
4
5test x = catch
6 (print x)
7 (\e -> putStrLn $ "captured ["++ show e++"]")
8
9main = do
10 setErrorHandlerOff
11
12 test $ log_e (-1)
13 test $ 5 + (fst.exp_e) 1000
14 test $ bessel_zero_Jnu_e (-0.3) 2
15
16 putStrLn "Bye" \ No newline at end of file
diff --git a/examples/integrate.hs b/examples/integrate.hs
new file mode 100644
index 0000000..6da88ad
--- /dev/null
+++ b/examples/integrate.hs
@@ -0,0 +1,17 @@
1-- Numerical integration
2import GSL
3
4quad f a b = fst $ integrateQAGS 1E-9 100 f a b
5
6-- A multiple integral can be easily defined using partial application
7quad2 f a b g1 g2 = quad h a b
8 where h x = quad (f x) (g1 x) (g2 x)
9
10volSphere r = 8 * quad2 (\x y -> sqrt (r*r-x*x-y*y))
11 0 r (const 0) (\x->sqrt (r*r-x*x))
12
13main = do
14 print $ quad (\x -> 4/(x^2+1)) 0 1
15 print pi
16 print $ volSphere 2.5
17 print $ 4/3*pi*2.5**3
diff --git a/examples/kalman.hs b/examples/kalman.hs
new file mode 100644
index 0000000..ccb8083
--- /dev/null
+++ b/examples/kalman.hs
@@ -0,0 +1,56 @@
1import LinearAlgebra
2import Graphics.Plot
3import LinearAlgebra.Instances
4
5--import GSLHaskell
6
7vector l = fromList l :: Vector Double
8matrix ls = fromLists ls :: Matrix Double
9diagl = diag . vector
10
11f = matrix [[1,0,0,0],
12 [1,1,0,0],
13 [0,0,1,0],
14 [0,0,0,1]]
15
16h = matrix [[0,-1,1,0],
17 [0,-1,0,1]]
18
19q = diagl [1,1,0,0]
20
21r = diagl [2,2]
22
23s0 = State (vector [0, 0, 10, -10]) (diagl [10,0, 100, 100])
24
25data System = System {kF, kH, kQ, kR :: Matrix Double}
26data State = State {sX :: Vector Double , sP :: Matrix Double}
27type Measurement = Vector Double
28
29kalman :: System -> State -> Measurement -> State
30kalman (System f h q r) (State x p) z = State x' p' where
31 px = f <> x -- prediction
32 pq = f <> p <> trans f -- its covariance
33 y = z - h <> px -- residue
34 cy = h <> pq <> trans h + r -- its covariance
35 k = pq <> trans h <> inv cy -- kalman gain
36 x' = px + k <> y -- new state
37 p' = (ident (dim x) - k <> h) <> pq -- its covariance
38
39sys = System f h q r
40
41zs = [vector [15-k,-20-k] | k <- [0..]]
42
43xs = s0 : zipWith (kalman sys) xs zs
44
45des = map (sqrt.takeDiag.sP) xs
46
47evolution n (xs,des) =
48 vector [1.. fromIntegral n]:(toColumns $ fromRows $ take n (zipWith (-) (map sX xs) des)) ++
49 (toColumns $ fromRows $ take n (zipWith (+) (map sX xs) des))
50
51main = do
52 print $ fromRows $ take 10 (map sX xs)
53 mapM_ (print . sP) $ take 10 xs
54 mplot (evolution 20 (xs,des))
55
56--(<>) = multiply \ No newline at end of file
diff --git a/examples/listlike.hs b/examples/listlike.hs
new file mode 100644
index 0000000..6c54f17
--- /dev/null
+++ b/examples/listlike.hs
@@ -0,0 +1,32 @@
1{-# OPTIONS_GHC -fglasgow-exts #-}
2
3import qualified Data.ListLike as LL
4import LinearAlgebra
5import Data.Monoid
6import Data.Packed.Internal.Vector
7import Foreign
8
9instance (Storable a) => Monoid (Vector a) where
10 mempty = V { dim = 0, fptr = undefined, ptr = undefined }
11 mappend a b = mconcat [a,b]
12 mconcat = j . filter ((>0).dim)
13 where j [] = mempty
14 j l = join l
15
16instance Storable a => LL.FoldableLL (Vector a) a where
17 foldl f x v = foldl f x (toList v)
18 foldr f x v = foldr f x (toList v)
19
20instance Storable a => LL.ListLike (Vector a) a where
21 singleton a = fromList [a]
22 head a = a @> 0
23 tail a | dim a == 1 = mempty
24 | otherwise = subVector 1 (dim a -1) a
25 genericLength = fromIntegral.dim
26
27
28v k = fromList [1..k] :: Vector Double
29
30f k = k+(3::Double)
31
32main = print $ (LL.map f [1..5] :: Vector Double) \ No newline at end of file
diff --git a/examples/minimize.hs b/examples/minimize.hs
new file mode 100644
index 0000000..0429a24
--- /dev/null
+++ b/examples/minimize.hs
@@ -0,0 +1,43 @@
1-- the multidimensional minimization example in the GSL manual
2import GSL
3import LinearAlgebra
4import Graphics.Plot
5
6-- the function to be minimized
7f [x,y] = 10*(x-1)^2 + 20*(y-2)^2 + 30
8
9-- its gradient
10df [x,y] = [20*(x-1), 40*(y-2)]
11
12-- the conjugate gradient method
13minimizeCG = minimizeConjugateGradient 1E-2 1E-4 1E-3 30
14
15-- a minimization algorithm which does not require the gradient
16minimizeS f xi = minimizeNMSimplex f xi (replicate (length xi) 1) 1E-2 100
17
18-- Numerical estimation of the gradient
19gradient f v = [partialDerivative k f v | k <- [0 .. length v -1]]
20
21partialDerivative n f v = fst (derivCentral 0.01 g (v!!n)) where
22 g x = f (concat [a,x:b])
23 (a,_:b) = splitAt n v
24
25main = do
26 -- conjugate gradient with true gradient
27 let (s,p) = minimizeCG f df [5,7]
28 print s -- solution
29 dispR 2 p -- evolution of the algorithm
30 let [x,y] = drop 2 (toColumns p)
31 mplot [x,y] -- path from the starting point to the solution
32
33 -- conjugate gradient with estimated gradient
34 let (s,p) = minimizeCG f (gradient f) [5,7]
35 print s
36 dispR 2 p
37 mplot $ drop 2 (toColumns p)
38
39 -- without gradient, using the NM Simplex method
40 let (s,p) = minimizeS f [5,7]
41 print s
42 dispR 2 p
43 mplot $ drop 3 (toColumns p)
diff --git a/examples/pca1.hs b/examples/pca1.hs
new file mode 100644
index 0000000..2c5074d
--- /dev/null
+++ b/examples/pca1.hs
@@ -0,0 +1,45 @@
1-- Principal component analysis
2
3import LinearAlgebra
4import System.Directory(doesFileExist)
5import System(system)
6import Control.Monad(when)
7
8type Vec = Vector Double
9type Mat = Matrix Double
10
11sumColumns m = constant 1 (rows m) <> m
12
13-- Vec with the mean value of the columns of a Mat
14mean x = sumColumns x / fromIntegral (rows x)
15
16-- covariance Mat of a list of observations as rows of a Mat
17cov x = (trans xc <> xc) / fromIntegral (rows x -1)
18 where xc = center x
19 center m = m - constant 1 (rows m) `outer` mean m
20
21-- creates the compression and decompression functions from the desired number of components
22pca :: Int -> Mat -> (Vec -> Vec , Vec -> Vec)
23pca n dataSet = (encode,decode)
24 where
25 encode x = vp <> (x - m)
26 decode x = x <> vp + m
27 m = mean dataSet
28 c = cov dataSet
29 (_,v) = eigS c
30 vp = takeRows n (trans v)
31
32main = do
33 ok <- doesFileExist ("mnist.txt")
34 when (not ok) $ do
35 putStrLn "\nTrying to download test datafile..."
36 system("wget -nv http://dis.um.es/~alberto/material/sp/mnist.txt.gz")
37 system("gunzip mnist.txt.gz")
38 return ()
39 m <- fromFile "mnist.txt" (5000,785)
40 let xs = takeColumns (cols m -1) m -- the last column is the digit type (class label)
41 let x = toRows xs !! 4 -- an arbitrary test Vec
42 let (pe,pd) = pca 10 xs
43 let y = pe x
44 print y -- compressed version
45 print $ norm (x - pd y) / norm x --reconstruction quality
diff --git a/examples/pca2.hs b/examples/pca2.hs
new file mode 100644
index 0000000..bd498e4
--- /dev/null
+++ b/examples/pca2.hs
@@ -0,0 +1,67 @@
1-- Improved PCA, including illustrative graphics
2
3import LinearAlgebra
4import Graphics.Plot
5import System.Directory(doesFileExist)
6import System(system)
7import Control.Monad(when)
8
9type Vec = Vector Double
10type Mat = Matrix Double
11
12sumColumns m = constant 1 (rows m) <> m
13
14-- Vector with the mean value of the columns of a Mat
15mean x = sumColumns x / fromIntegral (rows x)
16
17-- covariance matrix of a list of observations as rows of a matrix
18cov x = (trans xc <> xc) / fromIntegral (rows x -1)
19 where xc = center x
20 center m = m - constant 1 (rows m) `outer` mean m
21
22type Stat = (Vec, [Double], Mat)
23-- 1st and 2nd order statistics of a dataset (mean, eigenvalues and eigenvectors of cov)
24stat :: Mat -> Stat
25stat x = (m, toList s, trans v) where
26 m = mean x
27 (s,v) = eigS (cov x)
28
29-- creates the compression and decompression functions from the desired reconstruction
30-- quality and the statistics of a data set
31pca :: Double -> Stat -> (Vec -> Vec , Vec -> Vec)
32pca prec (m,s,v) = (encode,decode)
33 where
34 encode x = vp <> (x - m)
35 decode x = x <> vp + m
36 vp = takeRows n v
37 n = 1 + (length $ fst $ span (< (prec'*sum s)) $ cumSum s)
38 cumSum = tail . scanl (+) 0.0
39 prec' = if prec <=0.0 || prec >= 1.0
40 then error "the precision in pca must be 0<prec<1"
41 else prec
42
43shdigit :: Vec -> IO ()
44shdigit v = imshow (reshape 28 (-v))
45
46-- shows the effect of a given reconstruction quality on a test vector
47test :: Stat -> Double -> Vec -> IO ()
48test st prec x = do
49 let (pe,pd) = pca prec st
50 let y = pe x
51 print $ dim y
52 shdigit (pd y)
53
54main = do
55 ok <- doesFileExist ("mnist.txt")
56 when (not ok) $ do
57 putStrLn "\nTrying to download test datafile..."
58 system("wget -nv http://dis.um.es/~alberto/material/sp/mnist.txt.gz")
59 system("gunzip mnist.txt.gz")
60 return ()
61 m <- fromFile "mnist.txt" (5000,785)
62 let xs = takeColumns (cols m -1) m
63 let x = toRows xs !! 4 -- an arbitrary test vector
64 shdigit x
65 let st = stat xs
66 test st 0.90 x
67 test st 0.50 x
diff --git a/examples/pinv1.hs b/examples/pinv1.hs
new file mode 100644
index 0000000..76fa0a9
--- /dev/null
+++ b/examples/pinv1.hs
@@ -0,0 +1,18 @@
1-- initial check for the polynomial model example
2import LinearAlgebra
3
4
5prepSyst :: Int -> Matrix Double -> (Matrix Double, Vector Double)
6prepSyst n d = (a,b) where
7 [x,b] = toColumns d
8 a = fromColumns $ 1+0*x : map (x^) [1 .. n]
9
10main = do
11 dat <- readMatrix `fmap` readFile "data.txt"
12 let (a,b) = prepSyst 3 dat
13 putStr "Coefficient matrix:\n"
14 dispR 3 a
15 putStr "Desired values:\n"
16 print b
17 putStr "\nLeast Squares Solution:\n"
18 print $ pinv a <> b -- equivalent to: linearSolveLSR a (fromColumns [b])
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)
diff --git a/examples/plot.hs b/examples/plot.hs
new file mode 100644
index 0000000..1177c11
--- /dev/null
+++ b/examples/plot.hs
@@ -0,0 +1,20 @@
1import LinearAlgebra
2import Graphics.Plot
3import GSL(erf_Z, erf)
4
5sombrero n = f x y where
6 (x,y) = meshdom range range
7 range = linspace n (-2,2)
8 f x y = exp (-r2) * cos (2*r2) where
9 r2 = x*x+y*y
10
11f x = sin x + 0.5 * sin (5*x)
12
13gaussianPDF = erf_Z
14cumdist x = 0.5 * (1+ erf (x/sqrt 2))
15
16main = do
17 let x = linspace 1000 (-4,4)
18 mplot [f x]
19 mplot [x, liftVector cumdist x, liftVector gaussianPDF x]
20 mesh (sombrero 40) \ No newline at end of file
diff --git a/examples/testmnist.m b/examples/testmnist.m
new file mode 100644
index 0000000..38625a7
--- /dev/null
+++ b/examples/testmnist.m
@@ -0,0 +1,29 @@
1#! /usr/bin/octave -qf
2% measuring Octave computing times
3
4t0=time();
5load mnist.txt
6disp("load");
7disp(time()-t0)
8
9
10x = mnist(:,1:784);
11d = mnist(:,785);
12
13
14t0=time();
15xc = x - repmat(mean(x),rows(x),1);
16disp("x - repmat(mean(x),rows(x),1)");
17disp(time()-t0)
18
19t0=time();
20mc = (xc'*xc)/rows(x);
21disp("(xc'*xc)/rows(x)");
22disp(time()-t0)
23
24t0=time();
25[v,l]=eig(mc);
26disp("eig");
27disp(time()-t0)
28
29disp(flipud(diag(l))(1:10)); \ No newline at end of file