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/>.
28 ;; This file is autoloaded from calc-ext.el.
33 ;;; Statistical operations on vectors.
35 (defun calc-vector-count (arg)
38 (calc-vector-op "coun" 'calcFunc-vcount arg
)))
40 (defun calc-vector-sum (arg)
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)
49 (calc-hyperbolic-func)
50 (calc-vector-sum arg
))
52 (defun calc-vector-max (arg)
56 (calc-vector-op "vmin" 'calcFunc-vmin arg
)
57 (calc-vector-op "vmax" 'calcFunc-vmax arg
))))
59 (defun calc-vector-min (arg)
62 (calc-vector-max arg
))
64 (defun calc-vector-mean (arg)
67 (if (calc-is-hyperbolic)
69 (calc-vector-op "harm" 'calcFunc-vhmean arg
)
70 (calc-vector-op "medn" 'calcFunc-vmedian arg
))
72 (calc-vector-op "meae" 'calcFunc-vmeane arg
)
73 (calc-vector-op "mean" 'calcFunc-vmean arg
)))))
75 (defun calc-vector-mean-error (arg)
78 (calc-vector-mean arg
))
80 (defun calc-vector-median (arg)
82 (calc-hyperbolic-func)
83 (calc-vector-mean arg
))
85 (defun calc-vector-harmonic-mean (arg)
88 (calc-hyperbolic-func)
89 (calc-vector-mean arg
))
91 (defun calc-vector-geometric-mean (arg)
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)
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)
112 (calc-vector-sdev arg
))
114 (defun calc-vector-variance (arg)
116 (calc-hyperbolic-func)
117 (calc-vector-sdev arg
))
119 (defun calc-vector-pop-variance (arg)
122 (calc-hyperbolic-func)
123 (calc-vector-sdev arg
))
125 (defun calc-vector-covariance (arg)
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)
141 (calc-vector-covariance arg
))
143 (defun calc-vector-correlation (arg)
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
))
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
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
)
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
)
191 (let ((calc-internal-prec (+ calc-internal-prec
2)))
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
202 (math-calcFunc-to-var func
)
205 ((or (Math-objectp val
) (math-infinitep val
))
206 (setq const-part
(if const-part
207 (funcall func const-part val
)
210 (setq symb-part
(nconc symb-part
(list val
)))))
211 (setq vecs
(cdr vecs
))))
214 (setq const-part
(math-normalize const-part
))
216 (funcall func const-part
(cons whole-func symb-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
)
225 (setq count
(if (Math-vectorp (car vecs
))
226 (+ count
(math-count-elements (car vecs
)))
227 (if (Math-objectp (car vecs
))
229 (if (and (eq (car-safe (car vecs
)) 'var
)
230 (eq (car-safe (calc-var-value
233 (+ count
(math-count-elements
234 (symbol-value (nth 2 (car vecs
)))))
235 (math-reject-arg (car vecs
) 'numvecp
))))
239 (defun math-count-elements (vec)
241 (while (setq vec
(cdr vec
))
242 (setq count
(if (Math-vectorp (car vec
))
243 (+ count
(math-count-elements (car vec
)))
248 (defun math-flatten-many-vecs (vecs)
253 (if (Math-vectorp (car p
))
254 (math-flatten-vector (car p
))
255 (if (Math-objectp (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
262 (math-reject-arg (car p
) 'numvecp
)))))
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
))
274 (while (and (setq p
(cdr p
))
275 (not (and (consp (car p
))
276 (eq (car (car p
)) 'sdev
)))))
279 (while (setq vec
(cdr vec
))
280 (if (and (consp (setq p
(car vec
)))
283 (setq means
(cons (nth 1 p
) means
)
284 wts
(cons (nth 2 p
) wts
)))
286 (setq means
(cons (nth 1 p
) means
)
289 (setq means
(list 'vec
)
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
))
303 (len (1- (length means
))))
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
314 (calcFunc-map '(var div var-div
)
316 (math-div (calcFunc-reduce '(var add var-add
)
317 (calcFunc-map '(var div var-div
)
320 (math-div (calcFunc-reduce '(var add var-add
) means
) len
)))))))
322 (defun math-fix-int-intv (x)
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
))
334 (len (1- (length means
))))
336 (math-reject-arg nil
"*Must be at least 1 argument")
337 (math-with-extra-prec 2
339 (let* ((sqrwts (calcFunc-map '(var mul var-mul
) wts wts
))
340 (suminvsqrwts (calcFunc-reduce
342 (calcFunc-map '(var div var-div
)
345 (math-div (calcFunc-reduce '(var add var-add
)
346 (calcFunc-map '(var div var-div
)
349 (list 'calcFunc-sqrt
(math-div 1 suminvsqrwts
))))
350 (let ((mean (math-div (calcFunc-reduce '(var add var-add
) means
)
355 (math-div (calcFunc-reducer
357 (calcFunc-map '(var pow var-pow
)
358 (calcFunc-map '(var abs var-abs
)
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
))))
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
))
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
))
383 (setq flat
(sort flat
'math-lessp
))
385 (math-div (math-add (nth (1- hlen
) flat
) (nth hlen flat
)) 2)
389 (defun calcFunc-vgmean (&rest vecs
)
390 (let* ((flat (math-flatten-many-vecs vecs
))
391 (len (1- (length flat
))))
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
)))
398 (math-pow x
(list 'frac
1 len
))))))))
401 (defun calcFunc-agmean (a b
)
402 (cond ((Math-equal a b
) a
)
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
)))
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
))
420 (defun calcFunc-vhmean (&rest vecs
)
421 (let* ((flat (math-flatten-many-vecs vecs
))
422 (len (1- (length flat
))))
424 (math-reject-arg nil
"*Must be at least 1 argument")
425 (math-with-extra-prec 2
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
)))
449 (math-sqrt (calcFunc-vvar (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
)))
469 (math-sqrt (calcFunc-vpvar (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
))
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)
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)
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
)
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")))
523 (setq sqrwts
(math-add
525 (calcFunc-map '(var mul var-mul
) wts1 wts1
)
528 (calcFunc-map '(var mul var-mul
) wts2 wts2
)
530 suminvsqrwts
(calcFunc-reduce
532 (calcFunc-map '(var div var-div
) 1 sqrwts
))))
535 (setq split2
(math-split-sdev-vec (math-flatten-many-vecs vec2
)
539 (or (= len
(1- (length means2
))) (math-dimension-error))))
540 (let* ((diff1 (calcFunc-map
544 (math-div (calcFunc-reduce
546 (calcFunc-map '(var div var-div
)
548 (math-neg suminvsqrwts
))
549 (math-div (calcFunc-reducer '(var add var-add
) means1
)
551 (diff2 (if (= mode
0)
557 (math-div (calcFunc-reduce
559 (calcFunc-map '(var div var-div
)
561 (math-neg suminvsqrwts
))
562 (math-div (calcFunc-reducer '(var add var-add
) means2
)
564 (covar (calcFunc-map '(var mul var-mul
) diff1 diff2
)))
566 (setq covar
(calcFunc-map '(var div var-div
) covar sqrwts
)))
568 (calcFunc-reducer '(var add var-add
) covar
)
570 (let ((var1 (calcFunc-map '(var mul var-mul
) diff1 diff1
))
571 (var2 (calcFunc-map '(var mul var-mul
) diff2 diff2
)))
573 (setq var1
(calcFunc-map '(var div var-div
) var1 sqrwts
)
574 var2
(calcFunc-map '(var div var-div
) var2 sqrwts
)))
576 (math-mul (calcFunc-reducer '(var add var-add
) var1
)
577 (calcFunc-reducer '(var add var-add
) var2
))))
581 (math-div (math-mul suminvsqrwts
(1- len
)) len
))
582 (if pop len
(1- len
)))))))))
586 ;; arch-tag: 423858e9-8513-489c-9f35-710cd9d9c307
587 ;;; calc-stat.el ends here