LazStats: More general usability of the calculations in DescriptiveUnit

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7739 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-10-03 21:49:09 +00:00
parent a38794b4f8
commit 6f5659920f
5 changed files with 359 additions and 309 deletions

View File

@ -7,7 +7,8 @@ unit MathUnit;
interface
uses
Classes, SysUtils;
Classes, SysUtils,
Globals;
const
TWO_PI = 2.0 * PI;
@ -43,6 +44,13 @@ function FactorialLn(n: Integer): Double;
function PoissonPDF(n: integer; a: double): Double;
function PoissonCDF(n: Integer; a: double): Double;
procedure Calc_MaxMin(const AData: DblDyneVec; out AMax, AMin: Double);
procedure Calc_MeanStdDev(const AData: DblDyneVec; out AMean, AStdDev: Double);
procedure Calc_MeanVarStdDev(const AData: DblDyneVec; out AMean, AVariance, AStdDev: Double);
procedure Calc_SumSS(const AData: DblDyneVec; out Sum, SS: Double);
implementation
uses
@ -474,6 +482,70 @@ begin
end;
{===============================================================================
* Vector-based calculations
===============================================================================}
procedure Calc_MaxMin(const AData: DblDyneVec; out AMax, AMin: Double);
var
i: Integer;
begin
AMin := Infinity;
AMax := -Infinity;
for i := Low(AData) to High(AData) do
begin
if AData[i] < AMin then AMin := AData[i];
if AData[i] > AMax then AMax := AData[i];
end;
end;
procedure Calc_MeanStdDev(const AData: DblDyneVec; out AMean, AStdDev: Double);
var
variance: Double;
begin
Calc_MeanVarStdDev(AData, AMean, variance, AStdDev);
end;
procedure Calc_MeanVarStdDev(const AData: DblDyneVec; out AMean, AVariance, AStdDev: Double);
var
sum, ss: Double;
n: Integer;
begin
AMean := NaN;
AVariance := NaN;
AStdDev := NaN;
n := Length(AData);
if n = 0 then
exit;
Calc_SumSS(AData, sum, ss);
AMean := sum / n;
if n = 1 then
exit;
AVariance := ((ss - sqr(AMean)) / n) / (n - 1);
AStdDev := sqrt(AVariance);
end;
procedure Calc_SumSS(const AData: DblDyneVec; out Sum, SS: Double);
var
i: Integer;
begin
Sum := 0;
SS := 0;
for i := Low(AData) to High(AData) do
begin
Sum := Sum + AData[i];
SS := SS + sqr(AData[i]);
end;
end;
initialization
InitFactLn();