calc-stat.el 18 KB

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