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