First pass at a way to compare lists as almost=
[CommonLispStat.git] / statistics.lsp
blob5ace130beb3b74b13197ad1b0c27f7abed705b96
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 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
32 fivnum sample))
34 (in-package :lisp-stat-descriptive-statistics)
36 ;;;
37 ;;; Basic Summary Statistics
38 ;;;
40 (defun standard-deviation (x)
41 "Args: (x)
42 Returns the standard deviation of the elements x. Vector reducing."
43 (let ((n (count-elements x))
44 (r (- x (mean x))))
45 (sqrt (* (mean (* r r)) (/ n (- n 1))))))
48 ;; FIXME the following assume that we are using the vector based functions
49 (defun quantile (x p)
50 "Args: (x p)
51 Returns the P-th quantile(s) of sequence X. P can be a number or a sequence."
52 (let* ((x (sort-data x))
53 (n (length x))
54 (np (* p (- n 1)))
55 (low (floor np))
56 (high (ceiling np)))
57 (/ (+ (select x low) (select x high)) 2)))
59 (defun median (x)
60 "Args: (x)
61 Returns the median of the elements of X."
62 (quantile x 0.5))
64 (defun interquartile-range (x)
65 "Args: (number-data)
66 Returns the interquartile range of the elements of X."
67 (reduce #'- (quantile x '(0.75 0.25))))
69 (defun fivnum (x)
70 "Args: (number-data)
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)
76 "Args: (&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
80 (mapcar #'(lambda (x)
81 (if (matrixp x) (column-list x) (list x)))
82 args))))
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
92 without replacement."
93 (check-sequence x)
94 (let ((n (length x))
95 (x (if (consp x) (coerce x 'vector) (copy-vector x)))
96 (result nil))
97 (if (< 0 n)
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))))))))
108 (defun mean (x)
109 "Args: (x)
110 Returns the mean of the elements x. Vector reducing."
111 (let ((mean 0.0)
112 (count 0.0))
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)))
117 (find-mean (x)
118 (if (numberp x)
119 (add-to-mean x)
120 (let ((seq (compound-data-seq x)))
121 (if (consp seq)
122 (dolist (x seq)
123 (if (numberp x) (add-to-mean x) (find-mean x)))
124 (let ((n (length seq)))
125 (dotimes (i n)
126 (declare (fixnum i))
127 (let ((x (aref seq i)))
128 (if (numberp x)
129 (add-to-mean x)
130 (find-mean x))))))))))
131 (find-mean x)
132 mean)))