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
29 conjugate realpart imagpart phase
30 min max logand logior logxor lognot ffloor fceiling
31 ftruncate fround signum cis
)
32 (:export standard-deviation quantile median interquartile-range
35 (in-package :lisp-stat-descriptive-statistics
)
38 ;;; Basic Summary Statistics
41 (defun standard-deviation (x)
43 Returns the standard deviation of the elements x. Vector reducing."
44 (let ((n (count-elements x
))
46 (sqrt (* (mean (* r r
)) (/ n
(- n
1))))))
49 ;; FIXME the following assume that we are using the vector based functions
52 Returns the P-th quantile(s) of sequence X. P can be a number or a sequence."
53 (let* ((x (sort-data x
))
58 (/ (+ (select x low
) (select x high
)) 2)))
62 Returns the median of the elements of X."
65 (defun interquartile-range (x)
67 Returns the interquartile range of the elements of X."
68 (reduce #'-
(quantile x
'(0.75
0.25))))
72 Returns the five number summary (min, 1st quartile, medinan, 3rd quartile,
73 max) of the elements X."
74 (quantile x
'(0 .25 .5 .75 1)))
76 (defun covariance-matrix (&rest args
)
78 Returns the sample covariance matrix of the data columns in ARGS. ARGS may
79 consist of lists, vectors or matrices."
80 (let ((columns (apply #'append
82 (if (matrixp x
) (column-list x
) (list x
)))
84 (/ (cross-product (apply #'bind-columns
85 (- columns
(mapcar #'mean columns
))))
86 (- (length (car columns
)) 1))))
88 ;;;; Sampling / Resampling
90 (defun sample (x ssize
&optional replace
)
91 "Args: (x n &optional (replace nil))
92 Returns a list of a random sample of size N from sequence X drawn with or
96 (x (if (consp x
) (coerce x
'vector
) (copy-vector x
)))
99 (dotimes (i ssize result
)
100 (let ((j (if replace
(random n
) (+ i
(random (- n i
))))))
101 (setf result
(cons (aref x j
) result
))
102 (unless replace
;; swap elements i and j
103 (let ((temp (aref x i
)))
104 (setf (aref x i
) (aref x j
))
105 (setf (aref x j
) temp
))))))))
111 Returns the mean of the elements x. Vector reducing."
114 (labels ((add-to-mean (x)
115 (let ((count+1 (+ count
1.0)))
116 (setf mean
(+ (* (/ count count
+1) mean
) (* (/ count
+1) x
)))
117 (setf count count
+1)))
121 (let ((seq (compound-data-seq x
)))
124 (if (numberp x
) (add-to-mean x
) (find-mean x
)))
125 (let ((n (length seq
)))
128 (let ((x (aref seq i
)))
131 (find-mean x
))))))))))