diff options
author | Alberto Ruiz <aruiz@um.es> | 2009-11-14 12:57:52 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2009-11-14 12:57:52 +0000 |
commit | dc39c11d2d2e75428e6394cf8542c8c3ff2cd887 (patch) | |
tree | 24c5bac27258b5752691316e4d6736a5e0c1d2e5 /lib | |
parent | 6bdf5355a26da547b775f29926c131d539e86e7c (diff) |
added module Random.hs
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Data/Packed/Random.hs | 52 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 3 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra.hs | 2 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests.hs | 11 |
4 files changed, 66 insertions, 2 deletions
diff --git a/lib/Data/Packed/Random.hs b/lib/Data/Packed/Random.hs new file mode 100644 index 0000000..56d038e --- /dev/null +++ b/lib/Data/Packed/Random.hs | |||
@@ -0,0 +1,52 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- | | ||
3 | -- Module : Data.Packed.Vector | ||
4 | -- Copyright : (c) Alberto Ruiz 2009 | ||
5 | -- License : GPL | ||
6 | -- | ||
7 | -- Maintainer : Alberto Ruiz <aruiz@um.es> | ||
8 | -- Stability : provisional | ||
9 | -- | ||
10 | -- Random vectors and matrices. | ||
11 | -- | ||
12 | ----------------------------------------------------------------------------- | ||
13 | |||
14 | module Data.Packed.Random ( | ||
15 | RandDist(..), | ||
16 | randomVector, | ||
17 | gaussianSample, | ||
18 | meanCov, | ||
19 | ) where | ||
20 | |||
21 | import Numeric.GSL.Vector | ||
22 | import Data.Packed | ||
23 | import Numeric.LinearAlgebra.Algorithms | ||
24 | import Numeric.LinearAlgebra.Instances() | ||
25 | import Numeric.LinearAlgebra.Interface | ||
26 | |||
27 | -- | Obtains a matrix whose rows are pseudorandom samples from a multivariante | ||
28 | -- Gaussian distribution. | ||
29 | gaussianSample :: Int -- ^ seed | ||
30 | -> Int -- ^ number of rows | ||
31 | -> Vector Double -- ^ mean vector | ||
32 | -> Matrix Double -- ^ covariance matrix | ||
33 | -> Matrix Double -- ^ result | ||
34 | gaussianSample seed n med cov = m where | ||
35 | (l,v) = eigSH' cov | ||
36 | c = dim l | ||
37 | meds = constant 1 n `outer` med | ||
38 | rs = reshape c $ randomVector seed Gaussian (c * n) | ||
39 | ds = sqrt (abs l) | ||
40 | m = rs <> (diag ds <> trans v) + meds | ||
41 | |||
42 | ------------ utilities ------------------------------- | ||
43 | |||
44 | -- | Compute mean vector and covariance matrix of the rows of a matrix. | ||
45 | meanCov :: Matrix Double -> (Vector Double, Matrix Double) | ||
46 | meanCov x = (med,cov) where | ||
47 | r = rows x | ||
48 | k = 1 / fromIntegral r | ||
49 | med = constant k r <> x | ||
50 | meds = constant 1 r `outer` med | ||
51 | xc = x - meds | ||
52 | cov = (trans xc <> xc) / fromIntegral (r-1) | ||
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs index 836f11a..21f51e5 100644 --- a/lib/Data/Packed/Vector.hs +++ b/lib/Data/Packed/Vector.hs | |||
@@ -21,8 +21,7 @@ module Data.Packed.Vector ( | |||
21 | vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, | 21 | vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, |
22 | mapVector, zipVector, | 22 | mapVector, zipVector, |
23 | fscanfVector, fprintfVector, freadVector, fwriteVector, | 23 | fscanfVector, fprintfVector, freadVector, fwriteVector, |
24 | foldLoop, foldVector, foldVectorG, | 24 | foldLoop, foldVector, foldVectorG |
25 | RandDist(..), randomVector | ||
26 | ) where | 25 | ) where |
27 | 26 | ||
28 | import Data.Packed.Internal | 27 | import Data.Packed.Internal |
diff --git a/lib/Numeric/LinearAlgebra.hs b/lib/Numeric/LinearAlgebra.hs index f92c40d..337c007 100644 --- a/lib/Numeric/LinearAlgebra.hs +++ b/lib/Numeric/LinearAlgebra.hs | |||
@@ -16,12 +16,14 @@ This module reexports the most comon functions (including "Numeric.LinearAlgebra | |||
16 | ----------------------------------------------------------------------------- | 16 | ----------------------------------------------------------------------------- |
17 | module Numeric.LinearAlgebra ( | 17 | module Numeric.LinearAlgebra ( |
18 | module Data.Packed, | 18 | module Data.Packed, |
19 | module Data.Packed.Random, | ||
19 | module Numeric.LinearAlgebra.Linear, | 20 | module Numeric.LinearAlgebra.Linear, |
20 | module Numeric.LinearAlgebra.Algorithms, | 21 | module Numeric.LinearAlgebra.Algorithms, |
21 | module Numeric.LinearAlgebra.Interface | 22 | module Numeric.LinearAlgebra.Interface |
22 | ) where | 23 | ) where |
23 | 24 | ||
24 | import Data.Packed | 25 | import Data.Packed |
26 | import Data.Packed.Random | ||
25 | import Numeric.LinearAlgebra.Linear | 27 | import Numeric.LinearAlgebra.Linear |
26 | import Numeric.LinearAlgebra.Algorithms | 28 | import Numeric.LinearAlgebra.Algorithms |
27 | import Numeric.LinearAlgebra.Instances() | 29 | import Numeric.LinearAlgebra.Instances() |
diff --git a/lib/Numeric/LinearAlgebra/Tests.hs b/lib/Numeric/LinearAlgebra/Tests.hs index d7c3f99..097756e 100644 --- a/lib/Numeric/LinearAlgebra/Tests.hs +++ b/lib/Numeric/LinearAlgebra/Tests.hs | |||
@@ -131,6 +131,16 @@ rootFindingTest = TestList [ utest "root Hybrids" (fst sol1 ~~ [1,1]) | |||
131 | 131 | ||
132 | --------------------------------------------------------------------- | 132 | --------------------------------------------------------------------- |
133 | 133 | ||
134 | randomTest = c :~1~: snd (meanCov dat) where | ||
135 | a = (3><3) [1,2,3, | ||
136 | 2,4,0, | ||
137 | -2,2,1] | ||
138 | m = 3 |> [1,2,3] | ||
139 | c = a <> trans a | ||
140 | dat = gaussianSample 7 (10^6) m c | ||
141 | |||
142 | --------------------------------------------------------------------- | ||
143 | |||
134 | rot :: Double -> Matrix Double | 144 | rot :: Double -> Matrix Double |
135 | rot a = (3><3) [ c,0,s | 145 | rot a = (3><3) [ c,0,s |
136 | , 0,1,0 | 146 | , 0,1,0 |
@@ -227,6 +237,7 @@ runTests n = do | |||
227 | , utest "polySolve" (polySolveProp [1,2,3,4]) | 237 | , utest "polySolve" (polySolveProp [1,2,3,4]) |
228 | , minimizationTest | 238 | , minimizationTest |
229 | , rootFindingTest | 239 | , rootFindingTest |
240 | , utest "random" randomTest | ||
230 | ] | 241 | ] |
231 | return () | 242 | return () |
232 | 243 | ||