doc whitespace mods.
[CommonLispStat.git] / statistics.lsp
blob6c88a976a6213019a13fc2239b1ba56f0de4fa59
1 ;;; -*- mode: lisp -*-
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.
13 (in-package :cl-user)
15 (defpackage :lisp-stat-descriptive-statistics
16 (:use :common-lisp
17 :lisp-stat-data
18 :lisp-stat-math
19 :lisp-stat-compound-data
20 :lisp-stat-matrix
21 :lisp-stat-linalg-data
22 :lisp-stat-linalg
23 :lisp-stat-basics)
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
33 fivnum sample))
35 (in-package :lisp-stat-descriptive-statistics)
37 ;;;
38 ;;; Basic Summary Statistics
39 ;;;
41 (defun standard-deviation (x)
42 "Args: (x)
43 Returns the standard deviation of the elements x. Vector reducing."
44 (let ((n (count-elements x))
45 (r (- x (mean x))))
46 (sqrt (* (mean (* r r)) (/ n (- n 1))))))
49 ;; FIXME the following assume that we are using the vector based functions
50 (defun quantile (x p)
51 "Args: (x p)
52 Returns the P-th quantile(s) of sequence X. P can be a number or a sequence."
53 (let* ((x (sort-data x))
54 (n (length x))
55 (np (* p (- n 1)))
56 (low (floor np))
57 (high (ceiling np)))
58 (/ (+ (select x low) (select x high)) 2)))
60 (defun median (x)
61 "Args: (x)
62 Returns the median of the elements of X."
63 (quantile x 0.5))
65 (defun interquartile-range (x)
66 "Args: (number-data)
67 Returns the interquartile range of the elements of X."
68 (reduce #'- (quantile x '(0.75 0.25))))
70 (defun fivnum (x)
71 "Args: (number-data)
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)
77 "Args: (&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
81 (mapcar #'(lambda (x)
82 (if (matrixp x) (column-list x) (list x)))
83 args))))
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
93 without replacement."
94 (check-sequence x)
95 (let ((n (length x))
96 (x (if (consp x) (coerce x 'vector) (copy-vector x)))
97 (result nil))
98 (if (< 0 n)
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))))))))
109 (defun mean (x)
110 "Args: (x)
111 Returns the mean of the elements x. Vector reducing."
112 (let ((mean 0.0)
113 (count 0.0))
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)))
118 (find-mean (x)
119 (if (numberp x)
120 (add-to-mean x)
121 (let ((seq (compound-data-seq x)))
122 (if (consp seq)
123 (dolist (x seq)
124 (if (numberp x) (add-to-mean x) (find-mean x)))
125 (let ((n (length seq)))
126 (dotimes (i n)
127 (declare (fixnum i))
128 (let ((x (aref seq i)))
129 (if (numberp x)
130 (add-to-mean x)
131 (find-mean x))))))))))
132 (find-mean x)
133 mean)))