1 ;;; calc-stat.el --- statistical functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2016 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 <http://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
31 ;;; Statistical operations on vectors.
33 (defun calc-vector-count (arg)
36 (calc-vector-op "coun" 'calcFunc-vcount arg)))
38 (defun calc-vector-sum (arg)
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)
47 (calc-hyperbolic-func)
48 (calc-vector-sum arg))
50 (defun calc-vector-max (arg)
54 (calc-vector-op "vmin" 'calcFunc-vmin arg)
55 (calc-vector-op "vmax" 'calcFunc-vmax arg))))
57 (defun calc-vector-min (arg)
60 (calc-vector-max arg))
62 (defun calc-vector-mean (arg)
65 (if (calc-is-hyperbolic)
67 (calc-vector-op "harm" 'calcFunc-vhmean arg)
68 (calc-vector-op "medn" 'calcFunc-vmedian arg))
70 (calc-vector-op "meae" 'calcFunc-vmeane arg)
71 (calc-vector-op "mean" 'calcFunc-vmean arg)))))
73 (defun calc-vector-rms (arg)
76 (calc-vector-op "rms" 'calcFunc-rms arg)))
78 (defun calc-vector-mean-error (arg)
81 (calc-vector-mean arg))
83 (defun calc-vector-median (arg)
85 (calc-hyperbolic-func)
86 (calc-vector-mean arg))
88 (defun calc-vector-harmonic-mean (arg)
91 (calc-hyperbolic-func)
92 (calc-vector-mean arg))
94 (defun calc-vector-geometric-mean (arg)
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)
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)
115 (calc-vector-sdev arg))
117 (defun calc-vector-variance (arg)
119 (calc-hyperbolic-func)
120 (calc-vector-sdev arg))
122 (defun calc-vector-pop-variance (arg)
125 (calc-hyperbolic-func)
126 (calc-vector-sdev arg))
128 (defun calc-vector-covariance (arg)
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)
144 (calc-vector-covariance arg))
146 (defun calc-vector-correlation (arg)
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))
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
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)
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)
194 (let ((calc-internal-prec (+ calc-internal-prec 2)))
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
205 (math-calcFunc-to-var func)
208 ((or (Math-objectp val) (math-infinitep val))
209 (setq const-part (if const-part
210 (funcall func const-part val)
213 (setq symb-part (nconc symb-part (list val)))))
214 (setq vecs (cdr vecs))))
217 (setq const-part (math-normalize const-part))
219 (funcall func const-part (cons whole-func symb-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)
228 (setq count (if (Math-vectorp (car vecs))
229 (+ count (math-count-elements (car vecs)))
230 (if (Math-objectp (car vecs))
232 (if (and (eq (car-safe (car vecs)) 'var)
233 (eq (car-safe (calc-var-value
236 (+ count (math-count-elements
237 (symbol-value (nth 2 (car vecs)))))
238 (math-reject-arg (car vecs) 'numvecp))))
242 (defun math-count-elements (vec)
244 (while (setq vec (cdr vec))
245 (setq count (if (Math-vectorp (car vec))
246 (+ count (math-count-elements (car vec)))
251 (defun math-flatten-many-vecs (vecs)
256 (if (Math-vectorp (car p))
257 (math-flatten-vector (car p))
258 (if (Math-objectp (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
265 (math-reject-arg (car p) 'numvecp)))))
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))
277 (while (and (setq p (cdr p))
278 (not (and (consp (car p))
279 (eq (car (car p)) 'sdev)))))
282 (while (setq vec (cdr vec))
283 (if (and (consp (setq p (car vec)))
286 (setq means (cons (nth 1 p) means)
287 wts (cons (nth 2 p) wts)))
289 (setq means (cons (nth 1 p) means)
292 (setq means (list 'vec)
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))
306 (len (1- (length means))))
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
317 (calcFunc-map '(var div var-div)
319 (math-div (calcFunc-reduce '(var add var-add)
320 (calcFunc-map '(var div var-div)
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."
329 (calcFunc-map '(var abssqr var-abssqr) a))))
331 (defun math-fix-int-intv (x)
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))
343 (len (1- (length means))))
345 (math-reject-arg nil "*Must be at least 1 argument")
346 (math-with-extra-prec 2
348 (let* ((sqrwts (calcFunc-map '(var mul var-mul) wts wts))
349 (suminvsqrwts (calcFunc-reduce
351 (calcFunc-map '(var div var-div)
354 (math-div (calcFunc-reduce '(var add var-add)
355 (calcFunc-map '(var div var-div)
358 (list 'calcFunc-sqrt (math-div 1 suminvsqrwts))))
359 (let ((mean (math-div (calcFunc-reduce '(var add var-add) means)
364 (math-div (calcFunc-reducer
366 (calcFunc-map '(var pow var-pow)
367 (calcFunc-map '(var abs var-abs)
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))))
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))
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))
392 (setq flat (sort flat 'math-lessp))
394 (math-div (math-add (nth (1- hlen) flat) (nth hlen flat)) 2)
398 (defun calcFunc-vgmean (&rest vecs)
399 (let* ((flat (math-flatten-many-vecs vecs))
400 (len (1- (length flat))))
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)))
407 (math-pow x (list 'frac 1 len))))))))
410 (defun calcFunc-agmean (a b)
411 (cond ((Math-equal a b) a)
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)))
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))
429 (defun calcFunc-vhmean (&rest vecs)
430 (let* ((flat (math-flatten-many-vecs vecs))
431 (len (1- (length flat))))
433 (math-reject-arg nil "*Must be at least 1 argument")
434 (math-with-extra-prec 2
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)))
458 (math-sqrt (calcFunc-vvar (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)))
478 (math-sqrt (calcFunc-vpvar (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))
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)
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)
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)
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")))
532 (setq sqrwts (math-add
534 (calcFunc-map '(var mul var-mul) wts1 wts1)
537 (calcFunc-map '(var mul var-mul) wts2 wts2)
539 suminvsqrwts (calcFunc-reduce
541 (calcFunc-map '(var div var-div) 1 sqrwts))))
544 (setq split2 (math-split-sdev-vec (math-flatten-many-vecs vec2)
548 (or (= len (1- (length means2))) (math-dimension-error))))
549 (let* ((diff1 (calcFunc-map
553 (math-div (calcFunc-reduce
555 (calcFunc-map '(var div var-div)
557 (math-neg suminvsqrwts))
558 (math-div (calcFunc-reducer '(var add var-add) means1)
560 (diff2 (if (= mode 0)
566 (math-div (calcFunc-reduce
568 (calcFunc-map '(var div var-div)
570 (math-neg suminvsqrwts))
571 (math-div (calcFunc-reducer '(var add var-add) means2)
573 (covar (calcFunc-map '(var mul var-mul) diff1 diff2)))
575 (setq covar (calcFunc-map '(var div var-div) covar sqrwts)))
577 (calcFunc-reducer '(var add var-add) covar)
579 (let ((var1 (calcFunc-map '(var mul var-mul) diff1 diff1))
580 (var2 (calcFunc-map '(var mul var-mul) diff2 diff2)))
582 (setq var1 (calcFunc-map '(var div var-div) var1 sqrwts)
583 var2 (calcFunc-map '(var div var-div) var2 sqrwts)))
585 (math-mul (calcFunc-reducer '(var add var-add) var1)
586 (calcFunc-reducer '(var add var-add) var2))))
590 (math-div (math-mul suminvsqrwts (1- len)) len))
591 (if pop len (1- len)))))))))
595 ;;; calc-stat.el ends here