1 ;;; calc-stat.el --- statistical functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc-ext.el.
35 ;;; Statistical operations on vectors.
37 (defun calc-vector-count (arg)
40 (calc-vector-op "coun" 'calcFunc-vcount arg
)))
42 (defun calc-vector-sum (arg)
45 (if (calc-is-hyperbolic)
46 (calc-vector-op "vprd" 'calcFunc-vprod arg
)
47 (calc-vector-op "vsum" 'calcFunc-vsum arg
))))
49 (defun calc-vector-product (arg)
51 (calc-hyperbolic-func)
52 (calc-vector-sum arg
))
54 (defun calc-vector-max (arg)
58 (calc-vector-op "vmin" 'calcFunc-vmin arg
)
59 (calc-vector-op "vmax" 'calcFunc-vmax arg
))))
61 (defun calc-vector-min (arg)
64 (calc-vector-max arg
))
66 (defun calc-vector-mean (arg)
69 (if (calc-is-hyperbolic)
71 (calc-vector-op "harm" 'calcFunc-vhmean arg
)
72 (calc-vector-op "medn" 'calcFunc-vmedian arg
))
74 (calc-vector-op "meae" 'calcFunc-vmeane arg
)
75 (calc-vector-op "mean" 'calcFunc-vmean arg
)))))
77 (defun calc-vector-mean-error (arg)
80 (calc-vector-mean arg
))
82 (defun calc-vector-median (arg)
84 (calc-hyperbolic-func)
85 (calc-vector-mean arg
))
87 (defun calc-vector-harmonic-mean (arg)
90 (calc-hyperbolic-func)
91 (calc-vector-mean arg
))
93 (defun calc-vector-geometric-mean (arg)
96 (if (calc-is-hyperbolic)
97 (calc-binary-op "geom" 'calcFunc-agmean arg
)
98 (calc-vector-op "geom" 'calcFunc-vgmean arg
))))
100 (defun calc-vector-sdev (arg)
103 (if (calc-is-hyperbolic)
104 (if (calc-is-inverse)
105 (calc-vector-op "pvar" 'calcFunc-vpvar arg
)
106 (calc-vector-op "var" 'calcFunc-vvar arg
))
107 (if (calc-is-inverse)
108 (calc-vector-op "psdv" 'calcFunc-vpsdev arg
)
109 (calc-vector-op "sdev" 'calcFunc-vsdev arg
)))))
111 (defun calc-vector-pop-sdev (arg)
114 (calc-vector-sdev arg
))
116 (defun calc-vector-variance (arg)
118 (calc-hyperbolic-func)
119 (calc-vector-sdev arg
))
121 (defun calc-vector-pop-variance (arg)
124 (calc-hyperbolic-func)
125 (calc-vector-sdev arg
))
127 (defun calc-vector-covariance (arg)
130 (let ((n (if (eq arg
1) 1 2)))
131 (if (calc-is-hyperbolic)
132 (calc-enter-result n
"corr" (cons 'calcFunc-vcorr
133 (calc-top-list-n n
)))
134 (if (calc-is-inverse)
135 (calc-enter-result n
"pcov" (cons 'calcFunc-vpcov
136 (calc-top-list-n n
)))
137 (calc-enter-result n
"cov" (cons 'calcFunc-vcov
138 (calc-top-list-n n
))))))))
140 (defun calc-vector-pop-covariance (arg)
143 (calc-vector-covariance arg
))
145 (defun calc-vector-correlation (arg)
147 (calc-hyperbolic-func)
148 (calc-vector-covariance arg
))
150 (defun calc-vector-op (name func arg
)
151 (setq calc-aborted-prefix name
152 arg
(prefix-numeric-value arg
))
154 (error "Negative arguments not allowed"))
155 (calc-enter-result arg name
(cons func
(calc-top-list-n arg
))))
160 ;;; Useful statistical functions
162 ;;; Sum, product, etc., of one or more values or vectors.
163 ;;; Each argument must be either a number or a vector. Vectors
164 ;;; are flattened, but variables inside are assumed to represent
167 (defun calcFunc-vsum (&rest vecs
)
168 (math-reduce-many-vecs 'calcFunc-add
'calcFunc-vsum vecs
0))
170 (defun calcFunc-vprod (&rest vecs
)
171 (math-reduce-many-vecs 'calcFunc-mul
'calcFunc-vprod vecs
1))
173 (defun calcFunc-vmax (&rest vecs
)
174 (if (eq (car-safe (car vecs
)) 'sdev
)
176 (if (eq (car-safe (car vecs
)) 'intv
)
177 (nth 3 (math-fix-int-intv (car vecs
)))
178 (math-reduce-many-vecs 'calcFunc-max
'calcFunc-vmax vecs
179 '(neg (var inf var-inf
))))))
181 (defun calcFunc-vmin (&rest vecs
)
182 (if (eq (car-safe (car vecs
)) 'sdev
)
183 '(neg (var inf var-inf
))
184 (if (eq (car-safe (car vecs
)) 'intv
)
185 (nth 2 (math-fix-int-intv (car vecs
)))
186 (math-reduce-many-vecs 'calcFunc-min
'calcFunc-vmin vecs
187 '(var inf var-inf
)))))
189 (defun math-reduce-many-vecs (func whole-func vecs ident
)
190 (let ((const-part nil
)
193 (let ((calc-internal-prec (+ calc-internal-prec
2)))
195 (setq val
(car vecs
))
196 (and (eq (car-safe val
) 'var
)
197 (eq (car-safe (calc-var-value (nth 2 val
))) 'vec
)
198 (setq val
(symbol-value (nth 2 val
))))
199 (cond ((Math-vectorp val
)
200 (setq vec
(append (and const-part
(list const-part
))
201 (math-flatten-vector val
)))
202 (setq const-part
(if vec
204 (math-calcFunc-to-var func
)
207 ((or (Math-objectp val
) (math-infinitep val
))
208 (setq const-part
(if const-part
209 (funcall func const-part val
)
212 (setq symb-part
(nconc symb-part
(list val
)))))
213 (setq vecs
(cdr vecs
))))
216 (setq const-part
(math-normalize const-part
))
218 (funcall func const-part
(cons whole-func symb-part
))
220 (if symb-part
(cons whole-func symb-part
) ident
))))
223 ;;; Return the number of data elements among the arguments.
224 (defun calcFunc-vcount (&rest vecs
)
227 (setq count
(if (Math-vectorp (car vecs
))
228 (+ count
(math-count-elements (car vecs
)))
229 (if (Math-objectp (car vecs
))
231 (if (and (eq (car-safe (car vecs
)) 'var
)
232 (eq (car-safe (calc-var-value
235 (+ count
(math-count-elements
236 (symbol-value (nth 2 (car vecs
)))))
237 (math-reject-arg (car vecs
) 'numvecp
))))
241 (defun math-count-elements (vec)
243 (while (setq vec
(cdr vec
))
244 (setq count
(if (Math-vectorp (car vec
))
245 (+ count
(math-count-elements (car vec
)))
250 (defun math-flatten-many-vecs (vecs)
255 (if (Math-vectorp (car p
))
256 (math-flatten-vector (car p
))
257 (if (Math-objectp (car p
))
259 (if (and (eq (car-safe (car p
)) 'var
)
260 (eq (car-safe (calc-var-value
261 (nth 2 (car p
)))) 'vec
))
262 (math-flatten-vector (symbol-value
264 (math-reject-arg (car p
) 'numvecp
)))))
268 (defun calcFunc-vflat (&rest vecs
)
269 (math-flatten-many-vecs vecs
))
271 (defun math-split-sdev-vec (vec zero-ok
)
272 (let ((means (list 'vec
))
276 (while (and (setq p
(cdr p
))
277 (not (and (consp (car p
))
278 (eq (car (car p
)) 'sdev
)))))
281 (while (setq vec
(cdr vec
))
282 (if (and (consp (setq p
(car vec
)))
285 (setq means
(cons (nth 1 p
) means
)
286 wts
(cons (nth 2 p
) wts
)))
288 (setq means
(cons (nth 1 p
) means
)
291 (setq means
(list 'vec
)
294 (setq means
(cons p means
)))))
295 (list (nreverse means
)
296 (and wts
(nreverse wts
))))))
299 ;;; Return the arithmetic mean of the argument numbers or vectors.
300 ;;; (If numbers are error forms, computes the weighted mean.)
301 (defun calcFunc-vmean (&rest vecs
)
302 (let* ((split (math-split-sdev-vec (math-flatten-many-vecs vecs
) nil
))
305 (len (1- (length means
))))
307 (math-reject-arg nil
"*Must be at least 1 argument")
308 (if (and (= len
1) (eq (car-safe (nth 1 means
)) 'intv
))
309 (let ((x (math-fix-int-intv (nth 1 means
))))
310 (calcFunc-vmean (nth 2 x
) (nth 3 x
)))
311 (math-with-extra-prec 2
312 (if (and wts
(> len
1))
313 (let* ((sqrwts (calcFunc-map '(var mul var-mul
) wts wts
))
314 (suminvsqrwts (calcFunc-reduce
316 (calcFunc-map '(var div var-div
)
318 (math-div (calcFunc-reduce '(var add var-add
)
319 (calcFunc-map '(var div var-div
)
322 (math-div (calcFunc-reduce '(var add var-add
) means
) len
)))))))
324 (defun math-fix-int-intv (x)
328 (if (memq (nth 1 x
) '(2 3)) (nth 2 x
) (math-add (nth 2 x
) 1))
329 (if (memq (nth 1 x
) '(1 3)) (nth 3 x
) (math-sub (nth 3 x
) 1)))))
331 ;;; Compute the mean with an error estimate.
332 (defun calcFunc-vmeane (&rest vecs
)
333 (let* ((split (math-split-sdev-vec (math-flatten-many-vecs vecs
) nil
))
336 (len (1- (length means
))))
338 (math-reject-arg nil
"*Must be at least 1 argument")
339 (math-with-extra-prec 2
341 (let* ((sqrwts (calcFunc-map '(var mul var-mul
) wts wts
))
342 (suminvsqrwts (calcFunc-reduce
344 (calcFunc-map '(var div var-div
)
347 (math-div (calcFunc-reduce '(var add var-add
)
348 (calcFunc-map '(var div var-div
)
351 (list 'calcFunc-sqrt
(math-div 1 suminvsqrwts
))))
352 (let ((mean (math-div (calcFunc-reduce '(var add var-add
) means
)
357 (math-div (calcFunc-reducer
359 (calcFunc-map '(var pow var-pow
)
360 (calcFunc-map '(var abs var-abs
)
366 (math-mul len
(1- len
)))))))))))
369 ;;; Compute the median of a list of values.
370 (defun calcFunc-vmedian (&rest vecs
)
371 (let* ((flat (copy-sequence (cdr (math-flatten-many-vecs vecs
))))
376 (math-reject-arg nil
"*Must be at least 1 argument")
377 (if (and (= len
1) (memq (car-safe (car flat
)) '(sdev intv
)))
378 (calcFunc-vmean (car flat
))
380 (if (eq (car-safe (car p
)) 'sdev
)
381 (setcar p
(nth 1 (car p
))))
382 (or (Math-anglep (car p
))
383 (math-reject-arg (car p
) 'anglep
))
385 (setq flat
(sort flat
'math-lessp
))
387 (math-div (math-add (nth (1- hlen
) flat
) (nth hlen flat
)) 2)
391 (defun calcFunc-vgmean (&rest vecs
)
392 (let* ((flat (math-flatten-many-vecs vecs
))
393 (len (1- (length flat
))))
395 (math-reject-arg nil
"*Must be at least 1 argument")
396 (math-with-extra-prec 2
397 (let ((x (calcFunc-reduce '(var mul math-mul
) flat
)))
400 (math-pow x
(list 'frac
1 len
))))))))
403 (defun calcFunc-agmean (a b
)
404 (cond ((Math-equal a b
) a
)
407 (calc-symbolic-mode (math-inexact-result))
408 ((not (Math-realp a
)) (math-reject-arg a
'realp
))
409 ((not (Math-realp b
)) (math-reject-arg b
'realp
))
411 (math-with-extra-prec 2
412 (setq a
(math-float (math-abs a
))
413 b
(math-float (math-abs b
)))
415 (while (not (math-nearly-equal-float a b
))
416 (setq mean
(math-mul-float (math-add-float a b
) '(float 5 -
1))
417 b
(math-sqrt-float (math-mul-float a b
))
422 (defun calcFunc-vhmean (&rest vecs
)
423 (let* ((flat (math-flatten-many-vecs vecs
))
424 (len (1- (length flat
))))
426 (math-reject-arg nil
"*Must be at least 1 argument")
427 (math-with-extra-prec 2
429 (calcFunc-reduce '(var add math-add
)
430 (calcFunc-map '(var inv var-inv
) flat
)))))))
434 ;;; Compute the sample variance or standard deviation of numbers or vectors.
435 ;;; (If the numbers are error forms, only the mean part of them is used.)
436 (defun calcFunc-vvar (&rest vecs
)
437 (if (and (= (length vecs
) 1)
438 (memq (car-safe (car vecs
)) '(sdev intv
)))
439 (if (eq (car-safe (car vecs
)) 'intv
)
440 (math-intv-variance (car vecs
) nil
)
441 (math-sqr (nth 2 (car vecs
))))
442 (math-covariance vecs nil nil
0)))
444 (defun calcFunc-vsdev (&rest vecs
)
445 (if (and (= (length vecs
) 1)
446 (memq (car-safe (car vecs
)) '(sdev intv
)))
447 (if (eq (car-safe (car vecs
)) 'intv
)
448 (if (math-floatp (car vecs
))
449 (math-div (math-sub (nth 3 (car vecs
)) (nth 2 (car vecs
)))
451 (math-sqrt (calcFunc-vvar (car vecs
))))
453 (math-sqrt (math-covariance vecs nil nil
0))))
455 ;;; Compute the population variance or std deviation of numbers or vectors.
456 (defun calcFunc-vpvar (&rest vecs
)
457 (if (and (= (length vecs
) 1)
458 (memq (car-safe (car vecs
)) '(sdev intv
)))
459 (if (eq (car-safe (car vecs
)) 'intv
)
460 (math-intv-variance (car vecs
) t
)
461 (math-sqr (nth 2 (car vecs
))))
462 (math-covariance vecs nil t
0)))
464 (defun calcFunc-vpsdev (&rest vecs
)
465 (if (and (= (length vecs
) 1)
466 (memq (car-safe (car vecs
)) '(sdev intv
)))
467 (if (eq (car-safe (car vecs
)) 'intv
)
468 (if (math-floatp (car vecs
))
469 (math-div (math-sub (nth 3 (car vecs
)) (nth 2 (car vecs
)))
471 (math-sqrt (calcFunc-vpvar (car vecs
))))
473 (math-sqrt (math-covariance vecs nil t
0))))
475 (defun math-intv-variance (x pop
)
476 (or (math-constp x
) (math-reject-arg x
'constp
))
478 (math-div (math-sqr (math-sub (nth 3 x
) (nth 2 x
))) 12)
479 (let* ((x (math-fix-int-intv x
))
480 (len (math-sub (nth 3 x
) (nth 2 x
)))
481 (hlen (math-quotient len
2)))
482 (math-div (if (math-evenp len
)
483 (calcFunc-sum '(^
(var X var-X
) 2) '(var X var-X
)
484 (math-neg hlen
) hlen
)
485 (calcFunc-sum '(^
(- (var X var-X
) (/ 1 2)) 2)
487 (math-neg hlen
) (math-add hlen
1)))
488 (if pop
(math-add len
1) len
)))))
490 ;;; Compute the covariance and linear correlation coefficient.
491 (defun calcFunc-vcov (vec1 &optional vec2
)
492 (math-covariance (list vec1
) (list vec2
) nil
1))
494 (defun calcFunc-vpcov (vec1 &optional vec2
)
495 (math-covariance (list vec1
) (list vec2
) t
1))
497 (defun calcFunc-vcorr (vec1 &optional vec2
)
498 (math-covariance (list vec1
) (list vec2
) nil
2))
501 (defun math-covariance (vec1 vec2 pop mode
)
502 (or (car vec2
) (= mode
0)
504 (if (and (eq (car-safe (car vec1
)) 'var
)
505 (eq (car-safe (calc-var-value (nth 2 (car vec1
)))) 'vec
))
506 (setq vec1
(symbol-value (nth 2 (car vec1
))))
507 (setq vec1
(car vec1
)))
508 (or (math-matrixp vec1
) (math-dimension-error))
509 (or (= (length (nth 1 vec1
)) 3) (math-dimension-error))
510 (setq vec2
(list (math-mat-col vec1
2))
511 vec1
(list (math-mat-col vec1
1)))))
512 (math-with-extra-prec 2
513 (let* ((split1 (math-split-sdev-vec (math-flatten-many-vecs vec1
) nil
))
514 (means1 (car split1
))
515 (wts1 (nth 1 split1
))
516 split2 means2
(wts2 nil
)
519 (len (1- (length means1
))))
520 (if (< len
(if pop
1 2))
521 (math-reject-arg nil
(if pop
522 "*Must be at least 1 argument"
523 "*Must be at least 2 arguments")))
525 (setq sqrwts
(math-add
527 (calcFunc-map '(var mul var-mul
) wts1 wts1
)
530 (calcFunc-map '(var mul var-mul
) wts2 wts2
)
532 suminvsqrwts
(calcFunc-reduce
534 (calcFunc-map '(var div var-div
) 1 sqrwts
))))
537 (setq split2
(math-split-sdev-vec (math-flatten-many-vecs vec2
)
541 (or (= len
(1- (length means2
))) (math-dimension-error))))
542 (let* ((diff1 (calcFunc-map
546 (math-div (calcFunc-reduce
548 (calcFunc-map '(var div var-div
)
550 (math-neg suminvsqrwts
))
551 (math-div (calcFunc-reducer '(var add var-add
) means1
)
553 (diff2 (if (= mode
0)
559 (math-div (calcFunc-reduce
561 (calcFunc-map '(var div var-div
)
563 (math-neg suminvsqrwts
))
564 (math-div (calcFunc-reducer '(var add var-add
) means2
)
566 (covar (calcFunc-map '(var mul var-mul
) diff1 diff2
)))
568 (setq covar
(calcFunc-map '(var div var-div
) covar sqrwts
)))
570 (calcFunc-reducer '(var add var-add
) covar
)
572 (let ((var1 (calcFunc-map '(var mul var-mul
) diff1 diff1
))
573 (var2 (calcFunc-map '(var mul var-mul
) diff2 diff2
)))
575 (setq var1
(calcFunc-map '(var div var-div
) var1 sqrwts
)
576 var2
(calcFunc-map '(var div var-div
) var2 sqrwts
)))
578 (math-mul (calcFunc-reducer '(var add var-add
) var1
)
579 (calcFunc-reducer '(var add var-add
) var2
))))
583 (math-div (math-mul suminvsqrwts
(1- len
)) len
))
584 (if pop len
(1- len
)))))))))
588 ;;; arch-tag: 423858e9-8513-489c-9f35-710cd9d9c307
589 ;;; calc-stat.el ends here