root/trunk/statistics/covariance.lisp

Revision 34, 1.7 kB (checked in by lhealy, 9 months ago)

The classes/types in the different contexts are now gathered together
in one place, in *type-names* for the types and in *data-class-name*
for data classes, populated by #'add-data-class. Both defdata and
defmfun-all use the table and so mapping between various names is
consistent. The data class names are now different, *-double-float
and *-single-float replaces *-double and *-single. The regression
tests give the same results as before.

  • Property svn:keywords set to Id
Line 
1;; Covariance
2;; Liam Healy, Sun Dec 31 2006 - 13:19
3;; Time-stamp: <2008-03-09 19:21:49EDT covariance.lisp>
4;; $Id$
5
6(in-package :gsl)
7
8;;; To do: stride other than 1 when that information is availble from
9;;; the vector.
10
11(defmfun covariance-nom (data1 data2)
12  "gsl_stats_covariance"
13  (((gsl-array data1) :pointer) (1 :int)
14   ((gsl-array data2) :pointer) (1 :int) ((dim0 data2) size))
15  :c-return :double
16  :index covariance
17  :export nil)
18
19(defmfun covariance-m (data1 data2 mean1 mean2)
20  "gsl_stats_covariance_m"
21  (((gsl-array data1) :pointer) (1 :int)
22   ((gsl-array data2) :pointer) (1 :int) ((dim0 data2) size)
23   (mean1 :double) (mean2 :double))
24  :c-return :double
25  :index covariance
26  :export nil)
27
28(export 'covariance)
29(defun-optionals covariance (data1 data2 &optional mean1 mean2)
30  -nom -m
31  ;; FDL
32  "The covariance of the datasets data1 and data2 which must
33   be of the same length,
34   covar = {1 \over (n - 1)} \sum_{i = 1}^{n}
35      (x_{i} - \Hat x) (y_{i} - \Hat y).")
36
37;;; Examples and unit test
38
39#|
40(make-tests covariance
41  (letm ((vec1 (vector-double-float #(-3.21d0 1.0d0 12.8d0)))
42           (vec2 (vector-double-float #(1.15d0 -1.0d0 0.5d0))))
43      (let ((mean1 (mean vec1))
44            (mean2 (mean vec2)))
45        (list
46         (covariance vec1 vec2)
47         (covariance vec1 vec2 mean1 mean2)))))
48|#
49
50(LISP-UNIT:DEFINE-TEST COVARIANCE
51  (LISP-UNIT::ASSERT-NUMERICAL-EQUAL
52   (LIST
53    (LIST -0.2929999999999998d0 -0.2929999999999998d0))
54   (MULTIPLE-VALUE-LIST
55    (LETM
56        ((VEC1 (VECTOR-DOUBLE-FLOAT #(-3.21d0 1.0d0 12.8d0)))
57         (VEC2 (VECTOR-DOUBLE-FLOAT #(1.15d0 -1.0d0 0.5d0))))
58      (LET ((MEAN1 (MEAN VEC1)) (MEAN2 (MEAN VEC2)))
59        (LIST (COVARIANCE VEC1 VEC2)
60              (COVARIANCE VEC1 VEC2 MEAN1 MEAN2)))))))
61
Note: See TracBrowser for help on using the browser.