Cumulative standard deviation: Difference between revisions

(+ МК-61/52)
Line 591:
1.3997084244475297
2.0</lang>
 
 
=={{header|Emacs Lisp}}==
 
This implementation uses a temporary buffer (the central data structure of emacs) to have simply local variables.
 
<lang lisp>
(defun running-std (x)
; ensure that we have a float to avoid potential integer math errors.
(setq x (float x))
; define variables to use
(defvar running-sum 0 "the running sum of all known values")
(defvar running-len 0 "the running number of all known values")
(defvar running-squared-sum 0 "the running squared sum of all known values")
; and make them local to this buffer
(make-local-variable 'running-sum)
(make-local-variable 'running-len)
(make-local-variable 'running-squared-sum)
; now process the new value
(setq running-sum (+ running-sum x))
(setq running-len (1+ running-len))
(setq running-squared-sum (+ running-squared-sum (* x x)))
; and calculate the new standard deviation
(sqrt (- (/ running-squared-sum
running-len) (/ (* running-sum running-sum)
(* running-len running-len )))))
 
</lang>
 
<lang lisp>
(with-temp-buffer
(loop for i in '(2 4 4 4 5 5 7 9) do
(insert (number-to-string (running-std i)))
(newline))
(message (buffer-substring (point-min) (1- (point-max)))))
 
"0.0
1.0
0.9428090415820636
0.8660254037844386
0.9797958971132716
1.0
1.399708424447531
2.0"
</lang>
 
=={{header|F_Sharp|F#}}==
25

edits