(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / calc / calc-stat.el
blobc1b8ebc307ce2c484a84b31fad14ab4ccb50284f
1 ;;; calc-stat.el --- statistical functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2005 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
25 ;;; Commentary:
27 ;;; Code:
29 ;; This file is autoloaded from calc-ext.el.
31 (require 'calc-ext)
32 (require 'calc-macs)
34 ;;; Statistical operations on vectors.
36 (defun calc-vector-count (arg)
37 (interactive "P")
38 (calc-slow-wrapper
39 (calc-vector-op "coun" 'calcFunc-vcount arg)))
41 (defun calc-vector-sum (arg)
42 (interactive "P")
43 (calc-slow-wrapper
44 (if (calc-is-hyperbolic)
45 (calc-vector-op "vprd" 'calcFunc-vprod arg)
46 (calc-vector-op "vsum" 'calcFunc-vsum arg))))
48 (defun calc-vector-product (arg)
49 (interactive "P")
50 (calc-hyperbolic-func)
51 (calc-vector-sum arg))
53 (defun calc-vector-max (arg)
54 (interactive "P")
55 (calc-slow-wrapper
56 (if (calc-is-inverse)
57 (calc-vector-op "vmin" 'calcFunc-vmin arg)
58 (calc-vector-op "vmax" 'calcFunc-vmax arg))))
60 (defun calc-vector-min (arg)
61 (interactive "P")
62 (calc-invert-func)
63 (calc-vector-max arg))
65 (defun calc-vector-mean (arg)
66 (interactive "P")
67 (calc-slow-wrapper
68 (if (calc-is-hyperbolic)
69 (if (calc-is-inverse)
70 (calc-vector-op "harm" 'calcFunc-vhmean arg)
71 (calc-vector-op "medn" 'calcFunc-vmedian arg))
72 (if (calc-is-inverse)
73 (calc-vector-op "meae" 'calcFunc-vmeane arg)
74 (calc-vector-op "mean" 'calcFunc-vmean arg)))))
76 (defun calc-vector-mean-error (arg)
77 (interactive "P")
78 (calc-invert-func)
79 (calc-vector-mean arg))
81 (defun calc-vector-median (arg)
82 (interactive "P")
83 (calc-hyperbolic-func)
84 (calc-vector-mean arg))
86 (defun calc-vector-harmonic-mean (arg)
87 (interactive "P")
88 (calc-invert-func)
89 (calc-hyperbolic-func)
90 (calc-vector-mean arg))
92 (defun calc-vector-geometric-mean (arg)
93 (interactive "P")
94 (calc-slow-wrapper
95 (if (calc-is-hyperbolic)
96 (calc-binary-op "geom" 'calcFunc-agmean arg)
97 (calc-vector-op "geom" 'calcFunc-vgmean arg))))
99 (defun calc-vector-sdev (arg)
100 (interactive "P")
101 (calc-slow-wrapper
102 (if (calc-is-hyperbolic)
103 (if (calc-is-inverse)
104 (calc-vector-op "pvar" 'calcFunc-vpvar arg)
105 (calc-vector-op "var" 'calcFunc-vvar arg))
106 (if (calc-is-inverse)
107 (calc-vector-op "psdv" 'calcFunc-vpsdev arg)
108 (calc-vector-op "sdev" 'calcFunc-vsdev arg)))))
110 (defun calc-vector-pop-sdev (arg)
111 (interactive "P")
112 (calc-invert-func)
113 (calc-vector-sdev arg))
115 (defun calc-vector-variance (arg)
116 (interactive "P")
117 (calc-hyperbolic-func)
118 (calc-vector-sdev arg))
120 (defun calc-vector-pop-variance (arg)
121 (interactive "P")
122 (calc-invert-func)
123 (calc-hyperbolic-func)
124 (calc-vector-sdev arg))
126 (defun calc-vector-covariance (arg)
127 (interactive "P")
128 (calc-slow-wrapper
129 (let ((n (if (eq arg 1) 1 2)))
130 (if (calc-is-hyperbolic)
131 (calc-enter-result n "corr" (cons 'calcFunc-vcorr
132 (calc-top-list-n n)))
133 (if (calc-is-inverse)
134 (calc-enter-result n "pcov" (cons 'calcFunc-vpcov
135 (calc-top-list-n n)))
136 (calc-enter-result n "cov" (cons 'calcFunc-vcov
137 (calc-top-list-n n))))))))
139 (defun calc-vector-pop-covariance (arg)
140 (interactive "P")
141 (calc-invert-func)
142 (calc-vector-covariance arg))
144 (defun calc-vector-correlation (arg)
145 (interactive "P")
146 (calc-hyperbolic-func)
147 (calc-vector-covariance arg))
149 (defun calc-vector-op (name func arg)
150 (setq calc-aborted-prefix name
151 arg (prefix-numeric-value arg))
152 (if (< arg 0)
153 (error "Negative arguments not allowed"))
154 (calc-enter-result arg name (cons func (calc-top-list-n arg))))
159 ;;; Useful statistical functions
161 ;;; Sum, product, etc., of one or more values or vectors.
162 ;;; Each argument must be either a number or a vector. Vectors
163 ;;; are flattened, but variables inside are assumed to represent
164 ;;; non-vectors.
166 (defun calcFunc-vsum (&rest vecs)
167 (math-reduce-many-vecs 'calcFunc-add 'calcFunc-vsum vecs 0))
169 (defun calcFunc-vprod (&rest vecs)
170 (math-reduce-many-vecs 'calcFunc-mul 'calcFunc-vprod vecs 1))
172 (defun calcFunc-vmax (&rest vecs)
173 (if (eq (car-safe (car vecs)) 'sdev)
174 '(var inf var-inf)
175 (if (eq (car-safe (car vecs)) 'intv)
176 (nth 3 (math-fix-int-intv (car vecs)))
177 (math-reduce-many-vecs 'calcFunc-max 'calcFunc-vmax vecs
178 '(neg (var inf var-inf))))))
180 (defun calcFunc-vmin (&rest vecs)
181 (if (eq (car-safe (car vecs)) 'sdev)
182 '(neg (var inf var-inf))
183 (if (eq (car-safe (car vecs)) 'intv)
184 (nth 2 (math-fix-int-intv (car vecs)))
185 (math-reduce-many-vecs 'calcFunc-min 'calcFunc-vmin vecs
186 '(var inf var-inf)))))
188 (defun math-reduce-many-vecs (func whole-func vecs ident)
189 (let ((const-part nil)
190 (symb-part nil)
191 val vec)
192 (let ((calc-internal-prec (+ calc-internal-prec 2)))
193 (while vecs
194 (setq val (car vecs))
195 (and (eq (car-safe val) 'var)
196 (eq (car-safe (calc-var-value (nth 2 val))) 'vec)
197 (setq val (symbol-value (nth 2 val))))
198 (cond ((Math-vectorp val)
199 (setq vec (append (and const-part (list const-part))
200 (math-flatten-vector val)))
201 (setq const-part (if vec
202 (calcFunc-reducer
203 (math-calcFunc-to-var func)
204 (cons 'vec vec))
205 ident)))
206 ((or (Math-objectp val) (math-infinitep val))
207 (setq const-part (if const-part
208 (funcall func const-part val)
209 val)))
211 (setq symb-part (nconc symb-part (list val)))))
212 (setq vecs (cdr vecs))))
213 (if const-part
214 (progn
215 (setq const-part (math-normalize const-part))
216 (if symb-part
217 (funcall func const-part (cons whole-func symb-part))
218 const-part))
219 (if symb-part (cons whole-func symb-part) ident))))
222 ;;; Return the number of data elements among the arguments.
223 (defun calcFunc-vcount (&rest vecs)
224 (let ((count 0))
225 (while vecs
226 (setq count (if (Math-vectorp (car vecs))
227 (+ count (math-count-elements (car vecs)))
228 (if (Math-objectp (car vecs))
229 (1+ count)
230 (if (and (eq (car-safe (car vecs)) 'var)
231 (eq (car-safe (calc-var-value
232 (nth 2 (car vecs))))
233 'vec))
234 (+ count (math-count-elements
235 (symbol-value (nth 2 (car vecs)))))
236 (math-reject-arg (car vecs) 'numvecp))))
237 vecs (cdr vecs)))
238 count))
240 (defun math-count-elements (vec)
241 (let ((count 0))
242 (while (setq vec (cdr vec))
243 (setq count (if (Math-vectorp (car vec))
244 (+ count (math-count-elements (car vec)))
245 (1+ count))))
246 count))
249 (defun math-flatten-many-vecs (vecs)
250 (let ((p vecs)
251 (vec (list 'vec)))
252 (while p
253 (setq vec (nconc vec
254 (if (Math-vectorp (car p))
255 (math-flatten-vector (car p))
256 (if (Math-objectp (car p))
257 (list (car p))
258 (if (and (eq (car-safe (car p)) 'var)
259 (eq (car-safe (calc-var-value
260 (nth 2 (car p)))) 'vec))
261 (math-flatten-vector (symbol-value
262 (nth 2 (car p))))
263 (math-reject-arg (car p) 'numvecp)))))
264 p (cdr p)))
265 vec))
267 (defun calcFunc-vflat (&rest vecs)
268 (math-flatten-many-vecs vecs))
270 (defun math-split-sdev-vec (vec zero-ok)
271 (let ((means (list 'vec))
272 (wts (list 'vec))
273 (exact nil)
274 (p vec))
275 (while (and (setq p (cdr p))
276 (not (and (consp (car p))
277 (eq (car (car p)) 'sdev)))))
278 (if (null p)
279 (list vec nil)
280 (while (setq vec (cdr vec))
281 (if (and (consp (setq p (car vec)))
282 (eq (car p) 'sdev))
283 (or exact
284 (setq means (cons (nth 1 p) means)
285 wts (cons (nth 2 p) wts)))
286 (if zero-ok
287 (setq means (cons (nth 1 p) means)
288 wts (cons 0 wts))
289 (or exact
290 (setq means (list 'vec)
291 wts nil
292 exact t))
293 (setq means (cons p means)))))
294 (list (nreverse means)
295 (and wts (nreverse wts))))))
298 ;;; Return the arithmetic mean of the argument numbers or vectors.
299 ;;; (If numbers are error forms, computes the weighted mean.)
300 (defun calcFunc-vmean (&rest vecs)
301 (let* ((split (math-split-sdev-vec (math-flatten-many-vecs vecs) nil))
302 (means (car split))
303 (wts (nth 1 split))
304 (len (1- (length means))))
305 (if (= len 0)
306 (math-reject-arg nil "*Must be at least 1 argument")
307 (if (and (= len 1) (eq (car-safe (nth 1 means)) 'intv))
308 (let ((x (math-fix-int-intv (nth 1 means))))
309 (calcFunc-vmean (nth 2 x) (nth 3 x)))
310 (math-with-extra-prec 2
311 (if (and wts (> len 1))
312 (let* ((sqrwts (calcFunc-map '(var mul var-mul) wts wts))
313 (suminvsqrwts (calcFunc-reduce
314 '(var add var-add)
315 (calcFunc-map '(var div var-div)
316 1 sqrwts))))
317 (math-div (calcFunc-reduce '(var add var-add)
318 (calcFunc-map '(var div var-div)
319 means sqrwts))
320 suminvsqrwts))
321 (math-div (calcFunc-reduce '(var add var-add) means) len)))))))
323 (defun math-fix-int-intv (x)
324 (if (math-floatp x)
326 (list 'intv 3
327 (if (memq (nth 1 x) '(2 3)) (nth 2 x) (math-add (nth 2 x) 1))
328 (if (memq (nth 1 x) '(1 3)) (nth 3 x) (math-sub (nth 3 x) 1)))))
330 ;;; Compute the mean with an error estimate.
331 (defun calcFunc-vmeane (&rest vecs)
332 (let* ((split (math-split-sdev-vec (math-flatten-many-vecs vecs) nil))
333 (means (car split))
334 (wts (nth 1 split))
335 (len (1- (length means))))
336 (if (= len 0)
337 (math-reject-arg nil "*Must be at least 1 argument")
338 (math-with-extra-prec 2
339 (if wts
340 (let* ((sqrwts (calcFunc-map '(var mul var-mul) wts wts))
341 (suminvsqrwts (calcFunc-reduce
342 '(var add var-add)
343 (calcFunc-map '(var div var-div)
344 1 sqrwts))))
345 (math-make-sdev
346 (math-div (calcFunc-reduce '(var add var-add)
347 (calcFunc-map '(var div var-div)
348 means sqrwts))
349 suminvsqrwts)
350 (list 'calcFunc-sqrt (math-div 1 suminvsqrwts))))
351 (let ((mean (math-div (calcFunc-reduce '(var add var-add) means)
352 len)))
353 (math-make-sdev
354 mean
355 (list 'calcFunc-sqrt
356 (math-div (calcFunc-reducer
357 '(var add var-add)
358 (calcFunc-map '(var pow var-pow)
359 (calcFunc-map '(var abs var-abs)
360 (calcFunc-map
361 '(var add var-add)
362 means
363 (math-neg mean)))
365 (math-mul len (1- len)))))))))))
368 ;;; Compute the median of a list of values.
369 (defun calcFunc-vmedian (&rest vecs)
370 (let* ((flat (copy-sequence (cdr (math-flatten-many-vecs vecs))))
371 (p flat)
372 (len (length flat))
373 (hlen (/ len 2)))
374 (if (= len 0)
375 (math-reject-arg nil "*Must be at least 1 argument")
376 (if (and (= len 1) (memq (car-safe (car flat)) '(sdev intv)))
377 (calcFunc-vmean (car flat))
378 (while p
379 (if (eq (car-safe (car p)) 'sdev)
380 (setcar p (nth 1 (car p))))
381 (or (Math-anglep (car p))
382 (math-reject-arg (car p) 'anglep))
383 (setq p (cdr p)))
384 (setq flat (sort flat 'math-lessp))
385 (if (= (% len 2) 0)
386 (math-div (math-add (nth (1- hlen) flat) (nth hlen flat)) 2)
387 (nth hlen flat))))))
390 (defun calcFunc-vgmean (&rest vecs)
391 (let* ((flat (math-flatten-many-vecs vecs))
392 (len (1- (length flat))))
393 (if (= len 0)
394 (math-reject-arg nil "*Must be at least 1 argument")
395 (math-with-extra-prec 2
396 (let ((x (calcFunc-reduce '(var mul math-mul) flat)))
397 (if (= len 2)
398 (math-sqrt x)
399 (math-pow x (list 'frac 1 len))))))))
402 (defun calcFunc-agmean (a b)
403 (cond ((Math-equal a b) a)
404 ((math-zerop a) a)
405 ((math-zerop b) b)
406 (calc-symbolic-mode (math-inexact-result))
407 ((not (Math-realp a)) (math-reject-arg a 'realp))
408 ((not (Math-realp b)) (math-reject-arg b 'realp))
410 (math-with-extra-prec 2
411 (setq a (math-float (math-abs a))
412 b (math-float (math-abs b)))
413 (let (mean)
414 (while (not (math-nearly-equal-float a b))
415 (setq mean (math-mul-float (math-add-float a b) '(float 5 -1))
416 b (math-sqrt-float (math-mul-float a b))
417 a mean))
418 a)))))
421 (defun calcFunc-vhmean (&rest vecs)
422 (let* ((flat (math-flatten-many-vecs vecs))
423 (len (1- (length flat))))
424 (if (= len 0)
425 (math-reject-arg nil "*Must be at least 1 argument")
426 (math-with-extra-prec 2
427 (math-div len
428 (calcFunc-reduce '(var add math-add)
429 (calcFunc-map '(var inv var-inv) flat)))))))
433 ;;; Compute the sample variance or standard deviation of numbers or vectors.
434 ;;; (If the numbers are error forms, only the mean part of them is used.)
435 (defun calcFunc-vvar (&rest vecs)
436 (if (and (= (length vecs) 1)
437 (memq (car-safe (car vecs)) '(sdev intv)))
438 (if (eq (car-safe (car vecs)) 'intv)
439 (math-intv-variance (car vecs) nil)
440 (math-sqr (nth 2 (car vecs))))
441 (math-covariance vecs nil nil 0)))
443 (defun calcFunc-vsdev (&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 (if (math-floatp (car vecs))
448 (math-div (math-sub (nth 3 (car vecs)) (nth 2 (car vecs)))
449 (math-sqrt-12))
450 (math-sqrt (calcFunc-vvar (car vecs))))
451 (nth 2 (car vecs)))
452 (math-sqrt (math-covariance vecs nil nil 0))))
454 ;;; Compute the population variance or std deviation of numbers or vectors.
455 (defun calcFunc-vpvar (&rest vecs)
456 (if (and (= (length vecs) 1)
457 (memq (car-safe (car vecs)) '(sdev intv)))
458 (if (eq (car-safe (car vecs)) 'intv)
459 (math-intv-variance (car vecs) t)
460 (math-sqr (nth 2 (car vecs))))
461 (math-covariance vecs nil t 0)))
463 (defun calcFunc-vpsdev (&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 (if (math-floatp (car vecs))
468 (math-div (math-sub (nth 3 (car vecs)) (nth 2 (car vecs)))
469 (math-sqrt-12))
470 (math-sqrt (calcFunc-vpvar (car vecs))))
471 (nth 2 (car vecs)))
472 (math-sqrt (math-covariance vecs nil t 0))))
474 (defun math-intv-variance (x pop)
475 (or (math-constp x) (math-reject-arg x 'constp))
476 (if (math-floatp x)
477 (math-div (math-sqr (math-sub (nth 3 x) (nth 2 x))) 12)
478 (let* ((x (math-fix-int-intv x))
479 (len (math-sub (nth 3 x) (nth 2 x)))
480 (hlen (math-quotient len 2)))
481 (math-div (if (math-evenp len)
482 (calcFunc-sum '(^ (var X var-X) 2) '(var X var-X)
483 (math-neg hlen) hlen)
484 (calcFunc-sum '(^ (- (var X var-X) (/ 1 2)) 2)
485 '(var X var-X)
486 (math-neg hlen) (math-add hlen 1)))
487 (if pop (math-add len 1) len)))))
489 ;;; Compute the covariance and linear correlation coefficient.
490 (defun calcFunc-vcov (vec1 &optional vec2)
491 (math-covariance (list vec1) (list vec2) nil 1))
493 (defun calcFunc-vpcov (vec1 &optional vec2)
494 (math-covariance (list vec1) (list vec2) t 1))
496 (defun calcFunc-vcorr (vec1 &optional vec2)
497 (math-covariance (list vec1) (list vec2) nil 2))
500 (defun math-covariance (vec1 vec2 pop mode)
501 (or (car vec2) (= mode 0)
502 (progn
503 (if (and (eq (car-safe (car vec1)) 'var)
504 (eq (car-safe (calc-var-value (nth 2 (car vec1)))) 'vec))
505 (setq vec1 (symbol-value (nth 2 (car vec1))))
506 (setq vec1 (car vec1)))
507 (or (math-matrixp vec1) (math-dimension-error))
508 (or (= (length (nth 1 vec1)) 3) (math-dimension-error))
509 (setq vec2 (list (math-mat-col vec1 2))
510 vec1 (list (math-mat-col vec1 1)))))
511 (math-with-extra-prec 2
512 (let* ((split1 (math-split-sdev-vec (math-flatten-many-vecs vec1) nil))
513 (means1 (car split1))
514 (wts1 (nth 1 split1))
515 split2 means2 (wts2 nil)
516 (sqrwts nil)
517 suminvsqrwts
518 (len (1- (length means1))))
519 (if (< len (if pop 1 2))
520 (math-reject-arg nil (if pop
521 "*Must be at least 1 argument"
522 "*Must be at least 2 arguments")))
523 (if (or wts1 wts2)
524 (setq sqrwts (math-add
525 (if wts1
526 (calcFunc-map '(var mul var-mul) wts1 wts1)
528 (if wts2
529 (calcFunc-map '(var mul var-mul) wts2 wts2)
531 suminvsqrwts (calcFunc-reduce
532 '(var add var-add)
533 (calcFunc-map '(var div var-div) 1 sqrwts))))
534 (or (= mode 0)
535 (progn
536 (setq split2 (math-split-sdev-vec (math-flatten-many-vecs vec2)
537 nil)
538 means2 (car split2)
539 wts2 (nth 2 split1))
540 (or (= len (1- (length means2))) (math-dimension-error))))
541 (let* ((diff1 (calcFunc-map
542 '(var add var-add)
543 means1
544 (if sqrwts
545 (math-div (calcFunc-reduce
546 '(var add var-add)
547 (calcFunc-map '(var div var-div)
548 means1 sqrwts))
549 (math-neg suminvsqrwts))
550 (math-div (calcFunc-reducer '(var add var-add) means1)
551 (- len)))))
552 (diff2 (if (= mode 0)
553 diff1
554 (calcFunc-map
555 '(var add var-add)
556 means2
557 (if sqrwts
558 (math-div (calcFunc-reduce
559 '(var add var-add)
560 (calcFunc-map '(var div var-div)
561 means2 sqrwts))
562 (math-neg suminvsqrwts))
563 (math-div (calcFunc-reducer '(var add var-add) means2)
564 (- len))))))
565 (covar (calcFunc-map '(var mul var-mul) diff1 diff2)))
566 (if sqrwts
567 (setq covar (calcFunc-map '(var div var-div) covar sqrwts)))
568 (math-div
569 (calcFunc-reducer '(var add var-add) covar)
570 (if (= mode 2)
571 (let ((var1 (calcFunc-map '(var mul var-mul) diff1 diff1))
572 (var2 (calcFunc-map '(var mul var-mul) diff2 diff2)))
573 (if sqrwts
574 (setq var1 (calcFunc-map '(var div var-div) var1 sqrwts)
575 var2 (calcFunc-map '(var div var-div) var2 sqrwts)))
576 (math-sqrt
577 (math-mul (calcFunc-reducer '(var add var-add) var1)
578 (calcFunc-reducer '(var add var-add) var2))))
579 (if sqrwts
580 (if pop
581 suminvsqrwts
582 (math-div (math-mul suminvsqrwts (1- len)) len))
583 (if pop len (1- len)))))))))
585 (provide 'calc-stat)
587 ;;; arch-tag: 423858e9-8513-489c-9f35-710cd9d9c307
588 ;;; calc-stat.el ends here