Cumulative standard deviation: Difference between revisions

m
{{out}}
m ({{out}})
Line 1:
{{task|Probability and statistics}}
Write a stateful function, class, generator or coroutine that takes a series of floating point numbers, ''one at a time'', and returns the running [[wp:Standard Deviation|standard deviation]] of the series. The task implementation should use the most natural programming style of those listed for the function in the implementation language; the task ''must'' state which is being used. Do not apply [[wp:Bessel's correction|Bessel's correction]]; the returned standard deviation should always be computed as if the sample seen so far is the entire population.
The task implementation should use the most natural programming style of those listed for the function in the implementation language; the task ''must'' state which is being used. Do not apply [[wp:Bessel's correction|Bessel's correction]]; the returned standard deviation should always be computed as if the sample seen so far is the entire population.
 
Use this to compute the standard deviation of this demonstration set, <math>\{2, 4, 4, 4, 5, 5, 7, 9\}</math>, which is <math>2</math>.
Line 35 ⟶ 36:
Put_Line ("Deviation" & Float'Image (Deviation (Data)));
end Test_Deviation;</lang>
{{out}}
Sample output:
<pre>
Deviation 2.00000E+00</pre>
 
=={{header|ALGOL 68}}==
{{trans|C}}
Line 98 ⟶ 100:
printf(($"standard dev := "g(0,6)l$, sd))
)</lang>Output:
{{out}}
<pre>
standard dev := 2.000000
</pre>
{{trans|python}}
 
{{trans|python}}
{{works with|ALGOL 68|Standard - no extensions to language used}}
 
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
 
<!-- {{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8.8d.fc9.i386]}} -->
 
A code sample in an object oriented style:
<lang Algol68>MODE STAT = STRUCT(
Line 179 ⟶ 181:
printf(($"variance = "g(0,6)l$, (variance OF class stat)(stat)));
printf(($"count = "g(0,6)l$, (count OF class stat)(stat)))
)</lang>Output:
{{out}}
<pre>
standard deviation operator = 2.000000
Line 187 ⟶ 190:
count = 8.000000
</pre>
{{trans|python}}
 
{{trans|python}}
{{works with|ALGOL 68|Standard - no extensions to language used}}
 
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
 
<!-- {{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8.8d.fc9.i386]}} -->
 
A simple - but "unpackaged" - code example, useful if the standard deviation is required on only one set of concurrent data:
<lang Algol68>LONG REAL sum, sum2;
Line 210 ⟶ 212:
LONG REAL value = values[i];
printf(($2(xg(0,6))l$, value, sd(value)))
OD</lang>Output:
{{out}}
<pre>
2.000000 .000000
Line 257 ⟶ 260:
}
</lang>
{{out}}
<p>output:</p>
<pre>
2 0
Line 268 ⟶ 271:
9 2
</pre>
 
=={{header|Axiom}}==
We implement a domain with dependent type T with the operation + and identity 0:<lang Axiom>)abbrev package TESTD TestDomain
Line 318 ⟶ 322:
list(i%) = n
= SQR(MOD(list())^2/i% - (SUM(list())/i%)^2)</lang>
{{out}}
Output:
<pre>
Value = 2, running SD = 0
Line 569 ⟶ 573:
</lang>
 
{{out}}
Output:
<pre>
Values: 2
Line 695 ⟶ 699:
</lang>
Execute: ^Q StandardDeviation.Do 2 4 4 4 5 5 7 9 ~<br/>
{{out}}
Output:
<pre>
1 :> 0.0
Line 735 ⟶ 739:
}
}</lang>
{{out}}
Output:
<pre>0.000000e+00
1.000000e+00
Line 763 ⟶ 767:
=={{header|E}}==
 
This implementation produces two (function) objects sharing state. It is idiomatic in E to separate input from output (read from write) rather than combining them into one object.
It is idiomatic in E to separate input from output (read from write)
rather than combining them into one object.
 
The algorithm is {{trans|Perl}} and the results were checked against [[#Python]].
Line 1,114 ⟶ 1,120:
}
}</lang>
{{out}}
Output:
<pre>
0
Line 1,138 ⟶ 1,144:
}</lang>
 
{{out}}
Output:
<pre>running std.dev.: 0
running std.dev.: 1
Line 1,256 ⟶ 1,262:
}
end</lang>
{{out}}
Sample output:<pre>stddev (so far) := 0.0
stddev (so far) := 1.0
stddev (so far) := 0.9428090415820626
Line 1,346 ⟶ 1,353:
WScript.Echo(stddev.join(', ');</lang>
 
{{out}}
output:
<pre>0, 1, 0.942809041582063, 0.866025403784439, 0.979795897113273, 1, 1.39970842444753, 2</pre>
 
=={{header|jq}}==
====Observations from a file or array====
We first define a filter, "simulate", that, if given a file of
observations, will emit the standard deviations of the observations
seen so far. The current state is stored in a JSON object, with
The current state is stored in a JSON object, with this structure:
 
{ "n": _, "ssd": _, "mean": _ }
Line 1,588 ⟶ 1,596:
for value in [2,4,4,4,5,5,7,9]:
echo value, " ", formatFloat(sd(value), precision = 0)</lang>
{{out}}
Output:
<pre>
2 0
Line 1,747 ⟶ 1,755:
Printf.printf "\nStandard deviation: %g\n" (stddev l)</lang>
 
{{out}}
Sample output:
<pre>
List: 2 4 4 4 5 5 7 9
Line 2,022 ⟶ 2,030:
(for N (2.0 4.0 4.0 4.0 5.0 5.0 7.0 9.0)
(prinl (format N *Scl) " -> " (format (Fun N) *Scl)) ) )</lang>
{{out}}
Output:
<pre>2.00 -> 0.00
4.00 -> 1.00
Line 2,033 ⟶ 2,041:
 
=={{header|PowerShell}}==
This implementation takes the form of an advanced function which can act like a cmdlet and receive input from the pipeline.
which can act like a cmdlet and receive input from the pipeline.
<lang powershell>function Get-StandardDeviation {
begin {
Line 2,089 ⟶ 2,098:
EndDataSection</lang>
 
{{out}}
'''Outputs
<pre>
0.0000000000
1.0000000000
Line 2,098 ⟶ 2,108:
1.3997084244
2.0000000000
</pre>
 
=={{header|Python}}==
===Using a function with attached properties===
The program should work with Python 2.x and 3.x, although the output would not be a tuple in 3.x
although the output would not be a tuple in 3.x
<lang python>>>> from math import sqrt
>>> def sd(x):
Line 2,252 ⟶ 2,264:
do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k; g=.5*(g+x/g); end
numeric digits d; return g/1</lang>
'''output'''{{out}} using the default input
<pre>
item 1: 2 average= 2 standard deviation= 0
Line 2,411 ⟶ 2,423:
var n sd /*mean*/;
run;
</lang>
 
{{out}}
Output:
<pre>
N SD
Line 2,494 ⟶ 2,505:
}
var aa = stdDev()</lang>
{{out}}
Output:
<pre>
value 2 SD = 0.0
Line 2,542 ⟶ 2,553:
}
puts "the standard deviation is: $sd"</lang>
{{out}}
which produces the output:
<pre>the standard deviation is: 2.0</pre>
 
Line 2,625 ⟶ 2,636:
End Sub</lang>
 
{{out}}
Output:
<pre>
0
1
Line 2,634 ⟶ 2,646:
1.39970842444753
2
</pre>
 
=={{header|XPL0}}==
Line 2,650 ⟶ 2,663:
]</lang>
 
{{out}}
Output:
<pre>
0.00000 1.00000 0.94281 0.86603 0.97980 1.00000 1.39971 2.00000
Line 2,661 ⟶ 2,674:
}.fp1(L())
}</lang>
{{out}}
<pre>
zkl: T(2,4,4,4,5,5,7,9).pump(Void,sdf())
Anonymous user