2 ;;; Copyright (c) 2005--2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;; XLisp-ism's removed to focus on Common Lisp. Original source from:
7 ;;;; statistics.lsp XLISP-STAT statistics functions
8 ;;;; XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney
9 ;;;; Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz
10 ;;;; You may give out copies of this software; for conditions see the file
11 ;;;; COPYING included with this distribution.
15 (defpackage :lisp-stat-descriptive-statistics
19 :lisp-stat-compound-data
21 :lisp-stat-linalg-data
24 (:shadowing-import-from
:lisp-stat-math
25 expt
+ -
* / ** mod rem abs
1+ 1- log exp sqrt sin cos tan
26 asin acos atan sinh cosh tanh asinh acosh atanh float random
27 truncate floor ceiling round minusp zerop plusp evenp oddp
28 < <= = /= >= > complex conjugate realpart imagpart phase
29 min max logand logior logxor lognot ffloor fceiling
30 ftruncate fround signum cis
)
31 (:export standard-deviation quantile median interquartile-range
34 (in-package :lisp-stat-descriptive-statistics
)
37 ;;; Basic Summary Statistics
40 (defun standard-deviation (x)
42 Returns the standard deviation of the elements x. Vector reducing."
43 (let ((n (count-elements x
))
45 (sqrt (* (mean (* r r
)) (/ n
(- n
1))))))
48 ;; FIXME the following assume that we are using the vector based functions
51 Returns the P-th quantile(s) of sequence X. P can be a number or a sequence."
52 (let* ((x (sort-data x
))
57 (/ (+ (select x low
) (select x high
)) 2)))
61 Returns the median of the elements of X."
64 (defun interquartile-range (x)
66 Returns the interquartile range of the elements of X."
67 (reduce #'-
(quantile x
'(0.75
0.25))))
71 Returns the five number summary (min, 1st quartile, medinan, 3rd quartile,
72 max) of the elements X."
73 (quantile x
'(0 .25 .5 .75 1)))
75 (defun covariance-matrix (&rest args
)
77 Returns the sample covariance matrix of the data columns in ARGS. ARGS may
78 consist of lists, vectors or matrices."
79 (let ((columns (apply #'append
81 (if (matrixp x
) (column-list x
) (list x
)))
83 (/ (cross-product (apply #'bind-columns
84 (- columns
(mapcar #'mean columns
))))
85 (- (length (car columns
)) 1))))
87 ;;;; Sampling / Resampling
89 (defun sample (x ssize
&optional replace
)
90 "Args: (x n &optional (replace nil))
91 Returns a list of a random sample of size N from sequence X drawn with or
95 (x (if (consp x
) (coerce x
'vector
) (copy-vector x
)))
98 (dotimes (i ssize result
)
99 (let ((j (if replace
(random n
) (+ i
(random (- n i
))))))
100 (setf result
(cons (aref x j
) result
))
101 (unless replace
;; swap elements i and j
102 (let ((temp (aref x i
)))
103 (setf (aref x i
) (aref x j
))
104 (setf (aref x j
) temp
))))))))
110 Returns the mean of the elements x. Vector reducing."
113 (labels ((add-to-mean (x)
114 (let ((count+1 (+ count
1.0)))
115 (setf mean
(+ (* (/ count count
+1) mean
) (* (/ count
+1) x
)))
116 (setf count count
+1)))
120 (let ((seq (compound-data-seq x
)))
123 (if (numberp x
) (add-to-mean x
) (find-mean x
)))
124 (let ((n (length seq
)))
127 (let ((x (aref seq i
)))
130 (find-mean x
))))))))))