Fix typos.
[emacs.git] / lisp / calc / calc-stat.el
blob500620f3cc6eb9e104eaa0006e99a0a324a94e2a
1 ;;; calc-stat.el --- statistical functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 ;; This file is autoloaded from calc-ext.el.
30 (require 'calc-ext)
31 (require 'calc-macs)
33 ;;; Statistical operations on vectors.
35 (defun calc-vector-count (arg)
36 (interactive "P")
37 (calc-slow-wrapper
38 (calc-vector-op "coun" 'calcFunc-vcount arg)))
40 (defun calc-vector-sum (arg)
41 (interactive "P")
42 (calc-slow-wrapper
43 (if (calc-is-hyperbolic)
44 (calc-vector-op "vprd" 'calcFunc-vprod arg)
45 (calc-vector-op "vsum" 'calcFunc-vsum arg))))
47 (defun calc-vector-product (arg)
48 (interactive "P")
49 (calc-hyperbolic-func)
50 (calc-vector-sum arg))
52 (defun calc-vector-max (arg)
53 (interactive "P")
54 (calc-slow-wrapper
55 (if (calc-is-inverse)
56 (calc-vector-op "vmin" 'calcFunc-vmin arg)
57 (calc-vector-op "vmax" 'calcFunc-vmax arg))))
59 (defun calc-vector-min (arg)
60 (interactive "P")
61 (calc-invert-func)
62 (calc-vector-max arg))
64 (defun calc-vector-mean (arg)
65 (interactive "P")
66 (calc-slow-wrapper
67 (if (calc-is-hyperbolic)
68 (if (calc-is-inverse)
69 (calc-vector-op "harm" 'calcFunc-vhmean arg)
70 (calc-vector-op "medn" 'calcFunc-vmedian arg))
71 (if (calc-is-inverse)
72 (calc-vector-op "meae" 'calcFunc-vmeane arg)
73 (calc-vector-op "mean" 'calcFunc-vmean arg)))))
75 (defun calc-vector-mean-error (arg)
76 (interactive "P")
77 (calc-invert-func)
78 (calc-vector-mean arg))
80 (defun calc-vector-median (arg)
81 (interactive "P")
82 (calc-hyperbolic-func)
83 (calc-vector-mean arg))
85 (defun calc-vector-harmonic-mean (arg)
86 (interactive "P")
87 (calc-invert-func)
88 (calc-hyperbolic-func)
89 (calc-vector-mean arg))
91 (defun calc-vector-geometric-mean (arg)
92 (interactive "P")
93 (calc-slow-wrapper
94 (if (calc-is-hyperbolic)
95 (calc-binary-op "geom" 'calcFunc-agmean arg)
96 (calc-vector-op "geom" 'calcFunc-vgmean arg))))
98 (defun calc-vector-sdev (arg)
99 (interactive "P")
100 (calc-slow-wrapper
101 (if (calc-is-hyperbolic)
102 (if (calc-is-inverse)
103 (calc-vector-op "pvar" 'calcFunc-vpvar arg)
104 (calc-vector-op "var" 'calcFunc-vvar arg))
105 (if (calc-is-inverse)
106 (calc-vector-op "psdv" 'calcFunc-vpsdev arg)
107 (calc-vector-op "sdev" 'calcFunc-vsdev arg)))))
109 (defun calc-vector-pop-sdev (arg)
110 (interactive "P")
111 (calc-invert-func)
112 (calc-vector-sdev arg))
114 (defun calc-vector-variance (arg)
115 (interactive "P")
116 (calc-hyperbolic-func)
117 (calc-vector-sdev arg))
119 (defun calc-vector-pop-variance (arg)
120 (interactive "P")
121 (calc-invert-func)
122 (calc-hyperbolic-func)
123 (calc-vector-sdev arg))
125 (defun calc-vector-covariance (arg)
126 (interactive "P")
127 (calc-slow-wrapper
128 (let ((n (if (eq arg 1) 1 2)))
129 (if (calc-is-hyperbolic)
130 (calc-enter-result n "corr" (cons 'calcFunc-vcorr
131 (calc-top-list-n n)))
132 (if (calc-is-inverse)
133 (calc-enter-result n "pcov" (cons 'calcFunc-vpcov
134 (calc-top-list-n n)))
135 (calc-enter-result n "cov" (cons 'calcFunc-vcov
136 (calc-top-list-n n))))))))
138 (defun calc-vector-pop-covariance (arg)
139 (interactive "P")
140 (calc-invert-func)
141 (calc-vector-covariance arg))
143 (defun calc-vector-correlation (arg)
144 (interactive "P")
145 (calc-hyperbolic-func)
146 (calc-vector-covariance arg))
148 (defun calc-vector-op (name func arg)
149 (setq calc-aborted-prefix name
150 arg (prefix-numeric-value arg))
151 (if (< arg 0)
152 (error "Negative arguments not allowed"))
153 (calc-enter-result arg name (cons func (calc-top-list-n arg))))
158 ;;; Useful statistical functions
160 ;;; Sum, product, etc., of one or more values or vectors.
161 ;;; Each argument must be either a number or a vector. Vectors
162 ;;; are flattened, but variables inside are assumed to represent
163 ;;; non-vectors.
165 (defun calcFunc-vsum (&rest vecs)
166 (math-reduce-many-vecs 'calcFunc-add 'calcFunc-vsum vecs 0))
168 (defun calcFunc-vprod (&rest vecs)
169 (math-reduce-many-vecs 'calcFunc-mul 'calcFunc-vprod vecs 1))
171 (defun calcFunc-vmax (&rest vecs)
172 (if (eq (car-safe (car vecs)) 'sdev)
173 '(var inf var-inf)
174 (if (eq (car-safe (car vecs)) 'intv)
175 (nth 3 (math-fix-int-intv (car vecs)))
176 (math-reduce-many-vecs 'calcFunc-max 'calcFunc-vmax vecs
177 '(neg (var inf var-inf))))))
179 (defun calcFunc-vmin (&rest vecs)
180 (if (eq (car-safe (car vecs)) 'sdev)
181 '(neg (var inf var-inf))
182 (if (eq (car-safe (car vecs)) 'intv)
183 (nth 2 (math-fix-int-intv (car vecs)))
184 (math-reduce-many-vecs 'calcFunc-min 'calcFunc-vmin vecs
185 '(var inf var-inf)))))
187 (defun math-reduce-many-vecs (func whole-func vecs ident)
188 (let ((const-part nil)
189 (symb-part nil)
190 val vec)
191 (let ((calc-internal-prec (+ calc-internal-prec 2)))
192 (while vecs
193 (setq val (car vecs))
194 (and (eq (car-safe val) 'var)
195 (eq (car-safe (calc-var-value (nth 2 val))) 'vec)
196 (setq val (symbol-value (nth 2 val))))
197 (cond ((Math-vectorp val)
198 (setq vec (append (and const-part (list const-part))
199 (math-flatten-vector val)))
200 (setq const-part (if vec
201 (calcFunc-reducer
202 (math-calcFunc-to-var func)
203 (cons 'vec vec))
204 ident)))
205 ((or (Math-objectp val) (math-infinitep val))
206 (setq const-part (if const-part
207 (funcall func const-part val)
208 val)))
210 (setq symb-part (nconc symb-part (list val)))))
211 (setq vecs (cdr vecs))))
212 (if const-part
213 (progn
214 (setq const-part (math-normalize const-part))
215 (if symb-part
216 (funcall func const-part (cons whole-func symb-part))
217 const-part))
218 (if symb-part (cons whole-func symb-part) ident))))
221 ;;; Return the number of data elements among the arguments.
222 (defun calcFunc-vcount (&rest vecs)
223 (let ((count 0))
224 (while vecs
225 (setq count (if (Math-vectorp (car vecs))
226 (+ count (math-count-elements (car vecs)))
227 (if (Math-objectp (car vecs))
228 (1+ count)
229 (if (and (eq (car-safe (car vecs)) 'var)
230 (eq (car-safe (calc-var-value
231 (nth 2 (car vecs))))
232 'vec))
233 (+ count (math-count-elements
234 (symbol-value (nth 2 (car vecs)))))
235 (math-reject-arg (car vecs) 'numvecp))))
236 vecs (cdr vecs)))
237 count))
239 (defun math-count-elements (vec)
240 (let ((count 0))
241 (while (setq vec (cdr vec))
242 (setq count (if (Math-vectorp (car vec))
243 (+ count (math-count-elements (car vec)))
244 (1+ count))))
245 count))
248 (defun math-flatten-many-vecs (vecs)
249 (let ((p vecs)
250 (vec (list 'vec)))
251 (while p
252 (setq vec (nconc vec
253 (if (Math-vectorp (car p))
254 (math-flatten-vector (car p))
255 (if (Math-objectp (car p))
256 (list (car p))
257 (if (and (eq (car-safe (car p)) 'var)
258 (eq (car-safe (calc-var-value
259 (nth 2 (car p)))) 'vec))
260 (math-flatten-vector (symbol-value
261 (nth 2 (car p))))
262 (math-reject-arg (car p) 'numvecp)))))
263 p (cdr p)))
264 vec))
266 (defun calcFunc-vflat (&rest vecs)
267 (math-flatten-many-vecs vecs))
269 (defun math-split-sdev-vec (vec zero-ok)
270 (let ((means (list 'vec))
271 (wts (list 'vec))
272 (exact nil)
273 (p vec))
274 (while (and (setq p (cdr p))
275 (not (and (consp (car p))
276 (eq (car (car p)) 'sdev)))))
277 (if (null p)
278 (list vec nil)
279 (while (setq vec (cdr vec))
280 (if (and (consp (setq p (car vec)))
281 (eq (car p) 'sdev))
282 (or exact
283 (setq means (cons (nth 1 p) means)
284 wts (cons (nth 2 p) wts)))
285 (if zero-ok
286 (setq means (cons (nth 1 p) means)
287 wts (cons 0 wts))
288 (or exact
289 (setq means (list 'vec)
290 wts nil
291 exact t))
292 (setq means (cons p means)))))
293 (list (nreverse means)
294 (and wts (nreverse wts))))))
297 ;;; Return the arithmetic mean of the argument numbers or vectors.
298 ;;; (If numbers are error forms, computes the weighted mean.)
299 (defun calcFunc-vmean (&rest vecs)
300 (let* ((split (math-split-sdev-vec (math-flatten-many-vecs vecs) nil))
301 (means (car split))
302 (wts (nth 1 split))
303 (len (1- (length means))))
304 (if (= len 0)
305 (math-reject-arg nil "*Must be at least 1 argument")
306 (if (and (= len 1) (eq (car-safe (nth 1 means)) 'intv))
307 (let ((x (math-fix-int-intv (nth 1 means))))
308 (calcFunc-vmean (nth 2 x) (nth 3 x)))
309 (math-with-extra-prec 2
310 (if (and wts (> len 1))
311 (let* ((sqrwts (calcFunc-map '(var mul var-mul) wts wts))
312 (suminvsqrwts (calcFunc-reduce
313 '(var add var-add)
314 (calcFunc-map '(var div var-div)
315 1 sqrwts))))
316 (math-div (calcFunc-reduce '(var add var-add)
317 (calcFunc-map '(var div var-div)
318 means sqrwts))
319 suminvsqrwts))
320 (math-div (calcFunc-reduce '(var add var-add) means) len)))))))
322 (defun math-fix-int-intv (x)
323 (if (math-floatp x)
325 (list 'intv 3
326 (if (memq (nth 1 x) '(2 3)) (nth 2 x) (math-add (nth 2 x) 1))
327 (if (memq (nth 1 x) '(1 3)) (nth 3 x) (math-sub (nth 3 x) 1)))))
329 ;;; Compute the mean with an error estimate.
330 (defun calcFunc-vmeane (&rest vecs)
331 (let* ((split (math-split-sdev-vec (math-flatten-many-vecs vecs) nil))
332 (means (car split))
333 (wts (nth 1 split))
334 (len (1- (length means))))
335 (if (= len 0)
336 (math-reject-arg nil "*Must be at least 1 argument")
337 (math-with-extra-prec 2
338 (if wts
339 (let* ((sqrwts (calcFunc-map '(var mul var-mul) wts wts))
340 (suminvsqrwts (calcFunc-reduce
341 '(var add var-add)
342 (calcFunc-map '(var div var-div)
343 1 sqrwts))))
344 (math-make-sdev
345 (math-div (calcFunc-reduce '(var add var-add)
346 (calcFunc-map '(var div var-div)
347 means sqrwts))
348 suminvsqrwts)
349 (list 'calcFunc-sqrt (math-div 1 suminvsqrwts))))
350 (let ((mean (math-div (calcFunc-reduce '(var add var-add) means)
351 len)))
352 (math-make-sdev
353 mean
354 (list 'calcFunc-sqrt
355 (math-div (calcFunc-reducer
356 '(var add var-add)
357 (calcFunc-map '(var pow var-pow)
358 (calcFunc-map '(var abs var-abs)
359 (calcFunc-map
360 '(var add var-add)
361 means
362 (math-neg mean)))
364 (math-mul len (1- len)))))))))))
367 ;;; Compute the median of a list of values.
368 (defun calcFunc-vmedian (&rest vecs)
369 (let* ((flat (copy-sequence (cdr (math-flatten-many-vecs vecs))))
370 (p flat)
371 (len (length flat))
372 (hlen (/ len 2)))
373 (if (= len 0)
374 (math-reject-arg nil "*Must be at least 1 argument")
375 (if (and (= len 1) (memq (car-safe (car flat)) '(sdev intv)))
376 (calcFunc-vmean (car flat))
377 (while p
378 (if (eq (car-safe (car p)) 'sdev)
379 (setcar p (nth 1 (car p))))
380 (or (Math-anglep (car p))
381 (math-reject-arg (car p) 'anglep))
382 (setq p (cdr p)))
383 (setq flat (sort flat 'math-lessp))
384 (if (= (% len 2) 0)
385 (math-div (math-add (nth (1- hlen) flat) (nth hlen flat)) 2)
386 (nth hlen flat))))))
389 (defun calcFunc-vgmean (&rest vecs)
390 (let* ((flat (math-flatten-many-vecs vecs))
391 (len (1- (length flat))))
392 (if (= len 0)
393 (math-reject-arg nil "*Must be at least 1 argument")
394 (math-with-extra-prec 2
395 (let ((x (calcFunc-reduce '(var mul math-mul) flat)))
396 (if (= len 2)
397 (math-sqrt x)
398 (math-pow x (list 'frac 1 len))))))))
401 (defun calcFunc-agmean (a b)
402 (cond ((Math-equal a b) a)
403 ((math-zerop a) a)
404 ((math-zerop b) b)
405 (calc-symbolic-mode (math-inexact-result))
406 ((not (Math-realp a)) (math-reject-arg a 'realp))
407 ((not (Math-realp b)) (math-reject-arg b 'realp))
409 (math-with-extra-prec 2
410 (setq a (math-float (math-abs a))
411 b (math-float (math-abs b)))
412 (let (mean)
413 (while (not (math-nearly-equal-float a b))
414 (setq mean (math-mul-float (math-add-float a b) '(float 5 -1))
415 b (math-sqrt-float (math-mul-float a b))
416 a mean))
417 a)))))
420 (defun calcFunc-vhmean (&rest vecs)
421 (let* ((flat (math-flatten-many-vecs vecs))
422 (len (1- (length flat))))
423 (if (= len 0)
424 (math-reject-arg nil "*Must be at least 1 argument")
425 (math-with-extra-prec 2
426 (math-div len
427 (calcFunc-reduce '(var add math-add)
428 (calcFunc-map '(var inv var-inv) flat)))))))
432 ;;; Compute the sample variance or standard deviation of numbers or vectors.
433 ;;; (If the numbers are error forms, only the mean part of them is used.)
434 (defun calcFunc-vvar (&rest vecs)
435 (if (and (= (length vecs) 1)
436 (memq (car-safe (car vecs)) '(sdev intv)))
437 (if (eq (car-safe (car vecs)) 'intv)
438 (math-intv-variance (car vecs) nil)
439 (math-sqr (nth 2 (car vecs))))
440 (math-covariance vecs nil nil 0)))
442 (defun calcFunc-vsdev (&rest vecs)
443 (if (and (= (length vecs) 1)
444 (memq (car-safe (car vecs)) '(sdev intv)))
445 (if (eq (car-safe (car vecs)) 'intv)
446 (if (math-floatp (car vecs))
447 (math-div (math-sub (nth 3 (car vecs)) (nth 2 (car vecs)))
448 (math-sqrt-12))
449 (math-sqrt (calcFunc-vvar (car vecs))))
450 (nth 2 (car vecs)))
451 (math-sqrt (math-covariance vecs nil nil 0))))
453 ;;; Compute the population variance or std deviation of numbers or vectors.
454 (defun calcFunc-vpvar (&rest vecs)
455 (if (and (= (length vecs) 1)
456 (memq (car-safe (car vecs)) '(sdev intv)))
457 (if (eq (car-safe (car vecs)) 'intv)
458 (math-intv-variance (car vecs) t)
459 (math-sqr (nth 2 (car vecs))))
460 (math-covariance vecs nil t 0)))
462 (defun calcFunc-vpsdev (&rest vecs)
463 (if (and (= (length vecs) 1)
464 (memq (car-safe (car vecs)) '(sdev intv)))
465 (if (eq (car-safe (car vecs)) 'intv)
466 (if (math-floatp (car vecs))
467 (math-div (math-sub (nth 3 (car vecs)) (nth 2 (car vecs)))
468 (math-sqrt-12))
469 (math-sqrt (calcFunc-vpvar (car vecs))))
470 (nth 2 (car vecs)))
471 (math-sqrt (math-covariance vecs nil t 0))))
473 (defun math-intv-variance (x pop)
474 (or (math-constp x) (math-reject-arg x 'constp))
475 (if (math-floatp x)
476 (math-div (math-sqr (math-sub (nth 3 x) (nth 2 x))) 12)
477 (let* ((x (math-fix-int-intv x))
478 (len (math-sub (nth 3 x) (nth 2 x)))
479 (hlen (math-quotient len 2)))
480 (math-div (if (math-evenp len)
481 (calcFunc-sum '(^ (var X var-X) 2) '(var X var-X)
482 (math-neg hlen) hlen)
483 (calcFunc-sum '(^ (- (var X var-X) (/ 1 2)) 2)
484 '(var X var-X)
485 (math-neg hlen) (math-add hlen 1)))
486 (if pop (math-add len 1) len)))))
488 ;;; Compute the covariance and linear correlation coefficient.
489 (defun calcFunc-vcov (vec1 &optional vec2)
490 (math-covariance (list vec1) (list vec2) nil 1))
492 (defun calcFunc-vpcov (vec1 &optional vec2)
493 (math-covariance (list vec1) (list vec2) t 1))
495 (defun calcFunc-vcorr (vec1 &optional vec2)
496 (math-covariance (list vec1) (list vec2) nil 2))
499 (defun math-covariance (vec1 vec2 pop mode)
500 (or (car vec2) (= mode 0)
501 (progn
502 (if (and (eq (car-safe (car vec1)) 'var)
503 (eq (car-safe (calc-var-value (nth 2 (car vec1)))) 'vec))
504 (setq vec1 (symbol-value (nth 2 (car vec1))))
505 (setq vec1 (car vec1)))
506 (or (math-matrixp vec1) (math-dimension-error))
507 (or (= (length (nth 1 vec1)) 3) (math-dimension-error))
508 (setq vec2 (list (math-mat-col vec1 2))
509 vec1 (list (math-mat-col vec1 1)))))
510 (math-with-extra-prec 2
511 (let* ((split1 (math-split-sdev-vec (math-flatten-many-vecs vec1) nil))
512 (means1 (car split1))
513 (wts1 (nth 1 split1))
514 split2 means2 (wts2 nil)
515 (sqrwts nil)
516 suminvsqrwts
517 (len (1- (length means1))))
518 (if (< len (if pop 1 2))
519 (math-reject-arg nil (if pop
520 "*Must be at least 1 argument"
521 "*Must be at least 2 arguments")))
522 (if (or wts1 wts2)
523 (setq sqrwts (math-add
524 (if wts1
525 (calcFunc-map '(var mul var-mul) wts1 wts1)
527 (if wts2
528 (calcFunc-map '(var mul var-mul) wts2 wts2)
530 suminvsqrwts (calcFunc-reduce
531 '(var add var-add)
532 (calcFunc-map '(var div var-div) 1 sqrwts))))
533 (or (= mode 0)
534 (progn
535 (setq split2 (math-split-sdev-vec (math-flatten-many-vecs vec2)
536 nil)
537 means2 (car split2)
538 wts2 (nth 2 split1))
539 (or (= len (1- (length means2))) (math-dimension-error))))
540 (let* ((diff1 (calcFunc-map
541 '(var add var-add)
542 means1
543 (if sqrwts
544 (math-div (calcFunc-reduce
545 '(var add var-add)
546 (calcFunc-map '(var div var-div)
547 means1 sqrwts))
548 (math-neg suminvsqrwts))
549 (math-div (calcFunc-reducer '(var add var-add) means1)
550 (- len)))))
551 (diff2 (if (= mode 0)
552 diff1
553 (calcFunc-map
554 '(var add var-add)
555 means2
556 (if sqrwts
557 (math-div (calcFunc-reduce
558 '(var add var-add)
559 (calcFunc-map '(var div var-div)
560 means2 sqrwts))
561 (math-neg suminvsqrwts))
562 (math-div (calcFunc-reducer '(var add var-add) means2)
563 (- len))))))
564 (covar (calcFunc-map '(var mul var-mul) diff1 diff2)))
565 (if sqrwts
566 (setq covar (calcFunc-map '(var div var-div) covar sqrwts)))
567 (math-div
568 (calcFunc-reducer '(var add var-add) covar)
569 (if (= mode 2)
570 (let ((var1 (calcFunc-map '(var mul var-mul) diff1 diff1))
571 (var2 (calcFunc-map '(var mul var-mul) diff2 diff2)))
572 (if sqrwts
573 (setq var1 (calcFunc-map '(var div var-div) var1 sqrwts)
574 var2 (calcFunc-map '(var div var-div) var2 sqrwts)))
575 (math-sqrt
576 (math-mul (calcFunc-reducer '(var add var-add) var1)
577 (calcFunc-reducer '(var add var-add) var2))))
578 (if sqrwts
579 (if pop
580 suminvsqrwts
581 (math-div (math-mul suminvsqrwts (1- len)) len))
582 (if pop len (1- len)))))))))
584 (provide 'calc-stat)
586 ;; arch-tag: 423858e9-8513-489c-9f35-710cd9d9c307
587 ;;; calc-stat.el ends here