Cumulative standard deviation: Difference between revisions

Content added Content deleted
Line 127: Line 127:
{{incomplete|Ada|It does not return the ''running'' standard deviation of the series.}}
{{incomplete|Ada|It does not return the ''running'' standard deviation of the series.}}
<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.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;


procedure Test_Deviation is
procedure Test_Deviation is
type Sample is record
type Sample is record
N : Natural := 0;
N : Natural := 0;
Mean : Float := 0.0;
Sum : Float := 0.0;
Squares : Float := 0.0;
SumOfSquares : Float := 0.0;
end record;
end record;
procedure Add (Data : in out Sample; Point : Float) is
procedure Add (Data : in out Sample; Point : Float) is
begin
begin
Data.N := Data.N + 1;
Data.N := Data.N + 1;
Data.Mean := Data.Mean + Point;
Data.Sum := Data.Sum + Point;
Data.Squares := Data.Squares + Point ** 2;
Data.SumOfSquares := Data.SumOfSquares + Point ** 2;
end Add;
end Add;
function Deviation (Data : Sample) return Float is
function Deviation (Data : Sample) return Float is
begin
begin
return Sqrt (Data.Squares / Float (Data.N) - (Data.Mean / Float (Data.N)) ** 2);
return Sqrt (Data.SumOfSquares / Float (Data.N) - (Data.Sum / Float (Data.N)) ** 2);
end Deviation;
end Deviation;


Data : Sample;
Data : Sample;
Test : array (1..8) of Float := (2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0);
Test : array (1..8) of Integer := (2, 4, 4, 4, 5, 5, 7, 9);
begin
begin
for Item in Test'Range loop
for Index in Test'Range loop
Add (Data, Test (Item));
Add (Data, Float(Test(Index)));
Put("N="); Put(Item => Index, Width => 1);
Put(" ITEM="); Put(Item => Test(Index), Width => 1);
Put(" AVG="); Put(Item => Float(Data.Sum)/Float(Index), Fore => 1, Aft => 3, Exp => 0);
Put(" STDDEV="); Put(Item => Deviation (Data), Fore => 1, Aft => 3, Exp => 0);
New_line;
end loop;
end loop;
Put_Line ("Deviation" & Float'Image (Deviation (Data)));
end Test_Deviation;</lang>
end Test_Deviation;</lang>
{{out}}
{{out}}