Cumulative standard deviation: Difference between revisions

Content added Content deleted
m (Fixed lang tags.)
Line 7: Line 7:


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
<lang ada>
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;


Line 35: Line 34:
end loop;
end loop;
Put_Line ("Deviation" & Float'Image (Deviation (Data)));
Put_Line ("Deviation" & Float'Image (Deviation (Data)));
end Test_Deviation;
end Test_Deviation;</lang>
</lang>
Sample output:
Sample output:
<pre>
<pre>
Line 400: Line 398:


J is block-oriented; it expresses algorithms with the semantics of all the data being available at once. It does not have native lexical closure or coroutine semantics. It is possible to implement these semantics in J; see [[Moving Average]] for an example. We will not reprise that here.
J is block-oriented; it expresses algorithms with the semantics of all the data being available at once. It does not have native lexical closure or coroutine semantics. It is possible to implement these semantics in J; see [[Moving Average]] for an example. We will not reprise that here.
mean=: +/ % #
<lang j> mean=: +/ % #
dev=: - mean
dev=: - mean
sumsqdev=: +/@:*:@dev
sumsqdev=: +/@:*:@dev
stddevP=: [: %: sumsqdev % #
stddevP=: [: %: sumsqdev % #


stddevP\ 2 4 4 4 5 5 7 9
stddevP\ 2 4 4 4 5 5 7 9
0 1 0.942809 0.866025 0.979796 1 1.39971 2
0 1 0.942809 0.866025 0.979796 1 1.39971 2</lang>


{{trans|R}}<BR>
{{trans|R}}<BR>
Or we could take a cue from the [[#R|R implementation]] and reverse the Bessel correction to stddev:
Or we could take a cue from the [[#R|R implementation]] and reverse the Bessel correction to stddev:


require'stats'
<lang j> require'stats'
(%:@:(%~<:)@:# * stddev)\ 2 4 4 4 5 5 7 9
(%:@:(%~<:)@:# * stddev)\ 2 4 4 4 5 5 7 9
0 1 0.942809 0.866025 0.979796 1 1.39971 2
0 1 0.942809 0.866025 0.979796 1 1.39971 2</lang>


=={{header|Java}}==
=={{header|Java}}==
Line 687: Line 685:
(7, 1.3997084244475311)
(7, 1.3997084244475311)
(9, 2.0)
(9, 2.0)
>>> </lang>
>>></lang>


===Using a class instance===
===Using a class instance===
Line 734: Line 732:
=={{header|R}}==
=={{header|R}}==
===Built-in Std Dev fn===
===Built-in Std Dev fn===
<lang R>#The built-in standard deviation function applies the Bessel correction. To reverse this, we can apply an uncorrection.
<lang R>
#The built-in standard deviation function applies the Bessel correction. To reverse this, we can apply an uncorrection.
#If na.rm is true, missing data points (NA values) are removed.
#If na.rm is true, missing data points (NA values) are removed.
reverseBesselCorrection <- function(x, na.rm=FALSE)
reverseBesselCorrection <- function(x, na.rm=FALSE)
Line 745: Line 742:
}
}
testdata <- c(2,4,4,4,5,5,7,9)
testdata <- c(2,4,4,4,5,5,7,9)
reverseBesselCorrection(testdata)*sd(testdata) #2
reverseBesselCorrection(testdata)*sd(testdata) #2</lang>
</lang>
===From scratch===
===From scratch===
<lang R>#Again, if na.rm is true, missing data points (NA values) are removed.
<lang R>
#Again, if na.rm is true, missing data points (NA values) are removed.
uncorrectedsd <- function(x, na.rm=FALSE)
uncorrectedsd <- function(x, na.rm=FALSE)
{
{
Line 759: Line 754:
usd
usd
}
}
uncorrectedsd(testdata) #2
uncorrectedsd(testdata) #2</lang>
</lang>


=={{header|REXX}}==
=={{header|REXX}}==
Line 767: Line 761:
This is indeed Object REXX.
This is indeed Object REXX.


<lang orexx>sdacc = .SDAccum~new
<lang rexx>sdacc = .SDAccum~new
x = .array~of(2,4,4,4,5,5,7,9)
x = .array~of(2,4,4,4,5,5,7,9)
sd = 0
sd = 0