~ubuntu-branches/ubuntu/utopic/haskell-hmatrix/utopic

« back to all changes in this revision

Viewing changes to lib/Data/Packed/Random.hs

  • Committer: Package Import Robot
  • Author(s): Denis Laxalde
  • Date: 2013-07-06 15:37:50 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130706153750-wxxplc788jedqvv5
Tags: 0.15.0.0-1
* New upstream release.
* Make it clear in copyright that the license is GPL-3 (as stated by the
  author in <https://github.com/albertoruiz/hmatrix/issues/45>) although
  there is no proper license file yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
-----------------------------------------------------------------------------
13
13
 
14
14
module Data.Packed.Random (
 
15
    Seed,
15
16
    RandDist(..),
16
17
    randomVector,
17
18
    gaussianSample,
18
 
    uniformSample,
19
 
    meanCov,
 
19
    uniformSample
20
20
) where
21
21
 
22
22
import Numeric.GSL.Vector
25
25
import Numeric.LinearAlgebra.Algorithms
26
26
 
27
27
 
 
28
type Seed = Int
 
29
 
28
30
-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate
29
31
-- Gaussian distribution.
30
 
gaussianSample :: Int -- ^ seed
 
32
gaussianSample :: Seed
31
33
               -> Int -- ^ number of rows
32
34
               -> Vector Double -- ^ mean vector
33
35
               -> Matrix Double -- ^ covariance matrix
40
42
 
41
43
-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate
42
44
-- uniform distribution.
43
 
uniformSample :: Int -- ^ seed
 
45
uniformSample :: Seed
44
46
               -> Int -- ^ number of rows
45
47
               -> [(Double,Double)] -- ^ ranges for each column
46
48
               -> Matrix Double -- ^ result
53
55
    am = konst 1 n `outer` a
54
56
    m = fromColumns (zipWith scale cs dat) `add` am
55
57
 
56
 
------------ utilities -------------------------------
57
 
 
58
 
meanCov :: Matrix Double -> (Vector Double, Matrix Double)
59
 
meanCov x = (med,cov) where
60
 
    r    = rows x
61
 
    k    = 1 / fromIntegral r
62
 
    med  = konst k r `vXm` x
63
 
    meds = konst 1 r `outer` med
64
 
    xc   = x `sub` meds
65
 
    cov  = scale (recip (fromIntegral (r-1))) (trans xc `mXm` xc)