Cumulative standard deviation: Difference between revisions

m
m (sntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 5 users not shown)
Line 904:
No attempt to handle different types -- standard deviation is intrinsically a real number.
<syntaxhighlight lang="cpp">
#include <assert.hcassert>
#include <cmath>
#include <vector>
Line 1,348:
1.3997084244475297
2.0</syntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|Pascal}}
<syntaxhighlight lang="easylang">
global sum sum2 n .
proc sd x . r .
sum += x
sum2 += x * x
n += 1
r = sqrt (sum2 / n - sum * sum / n / n)
.
v[] = [ 2 4 4 4 5 5 7 9 ]
for v in v[]
sd v r
print v & " " & r
.
</syntaxhighlight>
 
=={{header|Elixir}}==
Line 3,623 ⟶ 3,640:
=={{header|Raku}}==
(formerly Perl 6)
 
{{works with|Rakudo Star|2010.08}}
Using a closure:
<syntaxhighlight lang="raku" line>sub sd (@a) {
my $mean = @a R/ [+] @a;
sqrt @a R/ [+] map (* - $mean)**2², @a;
}
Line 3,638 ⟶ 3,655:
say f $_ for 2, 4, 4, 4, 5, 5, 7, 9;</syntaxhighlight>
 
Using a state variable (remember that <tt><(x-<x>)²> = <x²> - <x>²</tt>):
<syntaxhighlight lang="raku" line>#sub remember that <stddev($x-<x>)²> = <x²> - <x>²{
sub stddev($x) {
sqrt
( .[2] += $x**2²) / ++.[0] -
- ((.[1] += $x ) / .[0])**2²
given state @;
}
 
say .&stddev $_ for <2 4 4 4 5 5 7 9>;</syntaxhighlight>
 
{{out}}
Line 3,747 ⟶ 3,763:
7 value in = 7 Stand Dev = 1.399708
8 value in = 9 Stand Dev = 2
</pre>
 
=={{header|RPL}}==
===Basic RPL===
≪ CL∑ { } SWAP
1 OVER SIZE '''FOR''' j
DUP j GET ∑+
'''IF''' j 1 > '''THEN'''
SDEV ∑DAT SIZE 1 GET DUP 1 - SWAP / √ *
ROT SWAP + SWAP '''END'''
'''NEXT'''
DROP CL∑
≫ '<span style="color:blue>CSDEV</span>' STO
===RPL 1993===
≪ CL∑
1 ≪ ∑+ PSDEV ≫ DOSUBS CL∑
≫ '<span style="color:blue>CSDEV</span>' STO
{{out}}
<pre>
1: { 0 1 0.942809041582 0.866025403784 0.979795897113 1 1.39970842445 2 }
</pre>
 
Line 4,005 ⟶ 4,041:
(let loop ((f (standart-deviation-generator))
(input '(2 4 4 4 5 5 7 9)))
(if (notunless (null? input))
(begin
(display (f (car input)))
(newline)
(loop f (cdr input)))))
</syntaxhighlight>
 
Line 4,407 ⟶ 4,442:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Nums
 
var cumStdDev = Fiber.new { |a|
Line 4,422 ⟶ 4,457:
var sd = cumStdDev.call(a)
if (cumStdDev.isDone) return
SystemFmt.print("Std Dev : %(Fmt$10.f(108f\n", sd, 8))\n")
}</syntaxhighlight>
 
9,483

edits