cl-random
I failed to fully install cl-random
on Mac OS X,
although it is available via quicklisp
,
which makes things way easier. I mainly failed to get cl-rmath
(a dependency of cl-random
) working properly with
libRmath
. Although I succeded in compiling and
installing libRmath.dylib
for my architecture,
CFFI gives me the message Unable to load foreign library
(LIBRMATH). / Could not register handle for external module
LIBRMATH: / no suitable image found. Did find: /
/usr/lib/libRmath.dylib: mach-o, but wrong architecture.
I have no clue why... Here is what I have achieved so far:
cl-rmath
seems not to be needed to correctly draw
samples from a multivariate normal distribution. Here is what I
did:
quicklisp
:
alexandria
, anaphora
, array-operations
,
cl-num-utils
, cl-slice
, let-plus
,
lla
lla
needs blas
and lapack
to be installed. This blog entry describes how to install itlla
,
you may have to give the path to its directory like this: (push #P"/usr/lib/"
cffi:*foreign-library-directories*)
cl-random
src/
folder: internals.lisp
, package.lisp
,
random.lisp
, multivariate.lisp
as
well as the function definition draw-standard-normal
from univariate.lisp
You SHOULD now be able to draw random samples from a multivariate normal distribution:
CL-USER 1 > (setf distribution (cl-random::r-multivariate-normal #(1 2)
(cl-random::mm t #2a((1 2)
(3 4)))))
#S(CL-RANDOM::R-MULTIVARIATE-NORMAL :N 2 :MEAN #(1 2) :VARIANCE-LEFT-SQRT #<CL-NUM-UTILS.MATRIX:LOWER-TRIANGULAR-MATRIX element-type DOUBLE-FLOAT
3.16228 .
4.42719 0.63246>)
CL-USER 2 > (cl-random:draw distribution)
#(0.20293392998246396D0 0.28948779023461224D0)
But if we generate some data and try to re-construct its mean and co-variance matrix, it doesn't fit...
CL-USER 3 > (setf data (loop repeat 1000 collect (coerce (cl-random:draw distribution) 'list)))
((2.777448778345402D0 5.218260204218044D0) (2.4401058735625983D0 4.318675829774026D0) (-3.1800525694534816D0 -2.5749326526079886D0) (3.289998442578751D0 6.533266966322055D0) ...)
CL-USER 4 > (setf lik (make-instance 'mlep:max-likelihood :data-set data))
#<MLEP:MAX-LIKELIHOOD 227B4C83>
CL-USER 5 > (mlep:run lik)
(#(0.7370068251772093D0 1.6190530720731782D0) #2A((9.490688560267179D0 13.240753178128449D0) (13.240753178128449D0 18.8576808919066D0)))
So, the co-variance matrix is clearly not wat we wanted to
achieve. I've tested that against numpy.cov
from
Pythons numpy
and got the same results. So mlep:max-likelihood
works fine. The samples are drawn wrongly... If you succeed in
getting this to work all right and/or know what I've done wrong,
please report!