Anonymous user
Cumulative standard deviation: Difference between revisions
→{{header|Common Lisp}}
m (→{{header|REXX}}: changed/add comments and whitespace, changed section header comment.) |
|||
Line 813:
=={{header|Common Lisp}}==
</lang>▼
<lang lisp>CL-USER> (loop with deviator = (make-deviator)▼
(9 2.0d0))</lang>▼
Since we don't care about the sample values once std dev is computed, we only need to keep track of their sum and square sums, hence:<lang lisp>(defun running-stddev ()
(let ((sum 0) (sq 0) (n 0))
Line 860 ⟶ 819:
(/ (sqrt (- (* n sq) (* sum sum))) n))))
CL-USER> (loop with f = (running-stddev) for i in '(2 4 4 4 5 5 7 9) do
(format t "~a ~a~%" i (funcall f i)))
NIL
2 0.0
4 1.0
4 0.94280905
4 0.8660254
5 0.97979593
5 1.0
7 1.3997085
In the REPL, one step at a time:
#<Interpreted Closure (:INTERNAL RUNNING-STDDEV) @ #x21b9a492>
CL-USER> (funcall fn 2)
0.0
CL-USER> (funcall fn 4)
1.0
CL-USER> (funcall fn 4)
0.94280905
CL-USER> (funcall fn 4)
0.8660254
CL-USER> (funcall fn 5)
0.97979593
CL-USER> (funcall fn 5)
1.0
CL-USER> (funcall fn 7)
1.3997085
CL-USER> (funcall fn 9)
2.0
▲</lang>
=={{header|Component Pascal}}==
|