LazStats: Refactor RunsTestUnit. Add pdf help to chm.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7405 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-04-25 21:39:27 +00:00
parent 0e48719e6b
commit f3982ea95b
4 changed files with 223 additions and 210 deletions

View File

@ -54,6 +54,7 @@ object runstestform: Trunstestform
BorderSpacing.Right = 8 BorderSpacing.Right = 8
ItemHeight = 0 ItemHeight = 0
MultiSelect = True MultiSelect = True
OnSelectionChange = VarListSelectionChange
TabOrder = 0 TabOrder = 0
end end
object InBtn: TBitBtn object InBtn: TBitBtn
@ -100,6 +101,7 @@ object runstestform: Trunstestform
BorderSpacing.Left = 8 BorderSpacing.Left = 8
BorderSpacing.Right = 8 BorderSpacing.Right = 8
BorderSpacing.Bottom = 12 BorderSpacing.Bottom = 12
ReadOnly = True
TabOrder = 3 TabOrder = 3
Text = 'TestVarEdit' Text = 'TestVarEdit'
end end
@ -220,6 +222,8 @@ object runstestform: Trunstestform
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Right = 8 BorderSpacing.Right = 8
BorderSpacing.Bottom = 4 BorderSpacing.Bottom = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True ReadOnly = True
TabOrder = 4 TabOrder = 4
Text = 'MeanEdit' Text = 'MeanEdit'
@ -239,9 +243,11 @@ object runstestform: Trunstestform
BorderSpacing.Left = 8 BorderSpacing.Left = 8
BorderSpacing.Right = 8 BorderSpacing.Right = 8
BorderSpacing.Bottom = 4 BorderSpacing.Bottom = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True ReadOnly = True
TabOrder = 5 TabOrder = 5
Text = 'Edit1' Text = 'StdDevEdit'
end end
object NUpEdit: TEdit object NUpEdit: TEdit
AnchorSideLeft.Control = StdDevEdit AnchorSideLeft.Control = StdDevEdit
@ -257,9 +263,11 @@ object runstestform: Trunstestform
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Right = 8 BorderSpacing.Right = 8
BorderSpacing.Bottom = 4 BorderSpacing.Bottom = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True ReadOnly = True
TabOrder = 6 TabOrder = 6
Text = 'Edit1' Text = 'NUpEdit'
end end
object NDownEdit: TEdit object NDownEdit: TEdit
AnchorSideLeft.Control = StdDevEdit AnchorSideLeft.Control = StdDevEdit
@ -274,9 +282,11 @@ object runstestform: Trunstestform
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Right = 8 BorderSpacing.Right = 8
BorderSpacing.Bottom = 4 BorderSpacing.Bottom = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True ReadOnly = True
TabOrder = 7 TabOrder = 7
Text = 'Edit1' Text = 'NDownEdit'
end end
object NRunsEdit: TEdit object NRunsEdit: TEdit
AnchorSideLeft.Control = StdDevEdit AnchorSideLeft.Control = StdDevEdit
@ -291,9 +301,11 @@ object runstestform: Trunstestform
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Right = 8 BorderSpacing.Right = 8
BorderSpacing.Bottom = 4 BorderSpacing.Bottom = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True ReadOnly = True
TabOrder = 8 TabOrder = 8
Text = 'Edit1' Text = 'NRunsEdit'
end end
object StatEdit: TEdit object StatEdit: TEdit
AnchorSideLeft.Control = StdDevEdit AnchorSideLeft.Control = StdDevEdit
@ -307,9 +319,11 @@ object runstestform: Trunstestform
Alignment = taRightJustify Alignment = taRightJustify
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Bottom = 4 BorderSpacing.Bottom = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True ReadOnly = True
TabOrder = 9 TabOrder = 9
Text = 'Edit1' Text = 'StatEdit'
end end
object ProbEdit: TEdit object ProbEdit: TEdit
AnchorSideLeft.Control = StdDevEdit AnchorSideLeft.Control = StdDevEdit
@ -323,9 +337,11 @@ object runstestform: Trunstestform
Alignment = taRightJustify Alignment = taRightJustify
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True ReadOnly = True
TabOrder = 10 TabOrder = 10
Text = 'Edit1' Text = 'ProbEdit'
end end
object ConclusionEdit: TEdit object ConclusionEdit: TEdit
AnchorSideLeft.Control = Label10 AnchorSideLeft.Control = Label10
@ -340,83 +356,70 @@ object runstestform: Trunstestform
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Left = 8 BorderSpacing.Left = 8
BorderSpacing.Right = 8 BorderSpacing.Right = 8
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True ReadOnly = True
TabOrder = 11 TabOrder = 11
Text = 'Edit1' Text = 'ConclusionEdit'
end end
object ResetBtn: TButton object ResetBtn: TButton
AnchorSideRight.Control = CancelBtn AnchorSideRight.Control = ComputeBtn
AnchorSideBottom.Control = Owner AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 79 Left = 167
Height = 25 Height = 25
Top = 405 Top = 405
Width = 54 Width = 54
Anchors = [akRight, akBottom] Anchors = [akRight, akBottom]
AutoSize = True AutoSize = True
BorderSpacing.Left = 8 BorderSpacing.Left = 8
BorderSpacing.Right = 12 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Caption = 'Reset' Caption = 'Reset'
OnClick = ResetBtnClick OnClick = ResetBtnClick
TabOrder = 12 TabOrder = 12
end end
object CancelBtn: TButton
AnchorSideRight.Control = ComputeBtn
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 145
Height = 25
Top = 405
Width = 62
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Right = 12
BorderSpacing.Bottom = 8
Cancel = True
Caption = 'Cancel'
ModalResult = 2
TabOrder = 13
end
object ComputeBtn: TButton object ComputeBtn: TButton
AnchorSideRight.Control = ReturnBtn AnchorSideRight.Control = CloseBtn
AnchorSideBottom.Control = Owner AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 219 Left = 229
Height = 25 Height = 25
Top = 405 Top = 405
Width = 76 Width = 76
Anchors = [akRight, akBottom] Anchors = [akRight, akBottom]
AutoSize = True AutoSize = True
BorderSpacing.Right = 12 BorderSpacing.Left = 8
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Caption = 'Compute' Caption = 'Compute'
OnClick = ComputeBtnClick OnClick = ComputeBtnClick
TabOrder = 14 TabOrder = 13
end end
object ReturnBtn: TButton object CloseBtn: TButton
AnchorSideRight.Control = Owner AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Owner AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 307 Left = 313
Height = 25 Height = 25
Top = 405 Top = 405
Width = 61 Width = 55
Anchors = [akRight, akBottom] Anchors = [akRight, akBottom]
AutoSize = True AutoSize = True
BorderSpacing.Left = 8
BorderSpacing.Top = 8 BorderSpacing.Top = 8
BorderSpacing.Right = 8 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Caption = 'Return' Caption = 'Close'
ModalResult = 1 ModalResult = 11
TabOrder = 15 TabOrder = 14
end end
object Bevel1: TBevel object Bevel1: TBevel
AnchorSideLeft.Control = Owner AnchorSideLeft.Control = Owner
AnchorSideRight.Control = Owner AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = ReturnBtn AnchorSideBottom.Control = CloseBtn
Left = 0 Left = 0
Height = 8 Height = 8
Top = 389 Top = 389

View File

@ -1,3 +1,5 @@
// File for testing: RunTest.laz, use VAR1
unit RunsTestUnit; unit RunsTestUnit;
{$mode objfpc}{$H+} {$mode objfpc}{$H+}
@ -7,21 +9,20 @@ interface
uses uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, ExtCtrls, StdCtrls, Buttons, ExtCtrls,
MainUnit, OutputUnit, Globals, DataProcs; MainUnit, Globals, DataProcs;
type type
{ Trunstestform } { TRunstestform }
Trunstestform = class(TForm) Trunstestform = class(TForm)
Bevel1: TBevel; Bevel1: TBevel;
Bevel2: TBevel; Bevel2: TBevel;
CancelBtn: TButton;
ComputeBtn: TButton; ComputeBtn: TButton;
MeanEdit: TEdit; MeanEdit: TEdit;
Memo1: TLabel; Memo1: TLabel;
ResetBtn: TButton; ResetBtn: TButton;
ReturnBtn: TButton; CloseBtn: TButton;
StdDevEdit: TEdit; StdDevEdit: TEdit;
NUpEdit: TEdit; NUpEdit: TEdit;
NDownEdit: TEdit; NDownEdit: TEdit;
@ -49,9 +50,11 @@ type
procedure InBtnClick(Sender: TObject); procedure InBtnClick(Sender: TObject);
procedure OutBtnClick(Sender: TObject); procedure OutBtnClick(Sender: TObject);
procedure ResetBtnClick(Sender: TObject); procedure ResetBtnClick(Sender: TObject);
procedure VarListSelectionChange(Sender: TObject; User: boolean);
private private
{ private declarations } { private declarations }
FAutoSized: Boolean; FAutoSized: Boolean;
procedure UpdateBtnStates;
public public
{ public declarations } { public declarations }
end; end;
@ -62,168 +65,162 @@ var
implementation implementation
uses uses
Math; Math, Utils;
{ Trunstestform } { Trunstestform }
procedure Trunstestform.ResetBtnClick(Sender: TObject); procedure Trunstestform.ResetBtnClick(Sender: TObject);
VAR i : integer; var
i: integer;
begin begin
VarList.Clear; VarList.Clear;
for i := 1 to NoVariables do for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]); VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
TestVarEdit.Text := ''; TestVarEdit.Text := '';
MeanEdit.Text := ''; MeanEdit.Text := '';
StdDevEdit.Text := ''; StdDevEdit.Text := '';
NUpEdit.Text := ''; NUpEdit.Text := '';
NDownEdit.Text := ''; NDownEdit.Text := '';
StatEdit.Text := ''; StatEdit.Text := '';
ProbEdit.Text := ''; ProbEdit.Text := '';
ConclusionEdit.Text := ''; ConclusionEdit.Text := '';
NRunsEdit.Text := ''; NRunsEdit.Text := '';
InBtn.Enabled := true; UpdateBtnStates;
OutBtn.Enabled := false;
end; end;
procedure Trunstestform.ComputeBtnClick(Sender: TObject); procedure Trunstestform.ComputeBtnClick(Sender: TObject);
VAR var
a, i, col, N, N1, N2, NLess, Nmore, R : integer; a, i, col, N, N1, N2, NLess, Nmore, R: integer;
Mean, ExpMean, SD1, SD2, SD3, SD4, SD, z1, z2, z, t, p1, p : double; Mean, ExpMean, SD1, SD2, SD3, SD4, SD, z1, z2, z, t, p1, p: double;
strvalue, outstr, astring : string; strvalue: string;
values : DblDyneVec; values: DblDyneVec;
begin begin
col := 0; col := 0;
N := 0; N := 0;
N1 := 0; N1 := 0;
N2 := 0; N2 := 0;
Nless := 0; Nless := 0;
Nmore := 0; Nmore := 0;
R := 1; R := 1;
Mean := 0.0; Mean := 0.0;
for i := 1 to NoVariables do for i := 1 to NoVariables do
begin begin
strvalue := Trim(OS3MainFrm.DataGrid.Cells[i,0]); strvalue := Trim(OS3MainFrm.DataGrid.Cells[i,0]);
if TestVarEdit.Text = strvalue then col := i; if TestVarEdit.Text = strvalue then col := i;
end; end;
if col = 0 then if col = 0 then
begin begin
ShowMessage('No variable was selected. Returning.'); MessageDlg('No variable was selected.', mtError, [mbOK], 0);
exit; exit;
end; end;
SetLength(values,NoCases);
for i := 1 to NoCases do SetLength(Values, NoCases);
begin for i := 1 to NoCases do
if not ValidValue(i,col) then continue; begin
values[i-1] := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[col,i])); if not ValidValue(i, col) then continue;
N := N + 1; Values[i-1] := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[col,i]));
end; N := N + 1;
if N <= 10 then end;
begin
ShowMessage('Insufficient data. You must have at least 11 values.'); if N <= 10 then
values := nil; begin
exit; MessageDlg('Insufficient data. You must have at least 11 values.', mtError, [mbOK], 0);
end; Values := nil;
for i := 0 to N-1 do Mean := Mean + values[i]; exit;
Mean := Mean / N; end;
// run through each value and compare with the mean
for i := 0 to N-1 do for i := 0 to N-1 do
begin // check and discard the ties with the mean Mean := Mean + values[i];
if Mean <> values[i] then Mean := Mean / N;
begin // check if it is greater than the mean
if values[i] > mean then // run through each value and compare with the mean
begin for i := 0 to N-1 do
N1 := N1 + 1; begin // check and discard the ties with the mean
a := i; if Mean <> Values[i] then
while a > 0 do begin // check if it is greater than the mean
begin if Values[i] > mean then
a := a - 1; begin
if values[a] <> Mean then break; N1 := N1 + 1;
end; a := i;
if values[a] < Mean then while a > 0 do
begin begin
R := R + 1; a := a - 1;
NLess := NLess + 1; if Values[a] <> Mean then break;
end; end;
end if Values[a] < Mean then
// check to see if it is less than the mean begin
else if values[i] < Mean then R := R + 1;
begin NLess := NLess + 1;
N2 := N2 + 1; end;
a := i; end
while a > 0 do else // check to see if it is less than the mean
begin if Values[i] < Mean then
a := a - 1; begin
if values[a] <> Mean then break; N2 := N2 + 1;
end; a := i;
if values[a] > Mean then while a > 0 do
begin begin
R := R + 1; a := a - 1;
Nmore := Nmore + 1; if Values[a] <> Mean then break;
end; end;
end; // close of else i if Values[a] > Mean then
end; // end of if values[i] not equal to the mean begin
end; // end of i loop R := R + 1;
// compute the expected mean and variance of R Nmore := Nmore + 1;
ExpMean := 1.0 + ((2 * N1 * N2) / (N1 + N2)); // mean mu end;
SD1 := 2 * N1 * N2 * (2 * N1 * N2 - N1 - N2); end; // close of else i
SD2 := power((N1 + N2),2); end; // end of if values[i] not equal to the mean
SD3 := N1 + N2 - 1; end; // end of i loop
SD4 := SD1 / (SD2 * SD3); // standard deviation "sigma"
SD := sqrt(SD4); // compute the expected mean and variance of R
// calculating P Value ExpMean := 1.0 + ((2 * N1 * N2) / (N1 + N2)); // mean mu
z1 := (R - ExpMean) / SD; SD1 := 2 * N1 * N2 * (2 * N1 * N2 - N1 - N2);
z2 := abs(z1); SD2 := power((N1 + N2), 2);
z := z2; SD3 := N1 + N2 - 1;
if z > 0 then t := z else t := -z; SD4 := SD1 / (SD2 * SD3); // standard deviation "sigma"
p1 := power((1 + t * (0.049867347 + t * (0.0211410061 + t * (0.0032776283 + SD := sqrt(SD4);
t * (0.0000380036 + t * (0.0000488906 + t * (0.000005383))))))), -16);
p := 1.0 - p1 / 2.0; // calculating P Value
if z > 0.0 then t := 1.0 - p z1 := (R - ExpMean) / SD;
else t := 1.0 - (1.0 - p); // this is P value z2 := abs(z1);
// show results z := z2;
outstr := format('%8.3f',[Mean]); if z > 0 then
MeanEdit.Text := outstr; t := z
outstr := format('%8.3f',[SD]); else
StdDevEdit.Text := outstr; t := -z;
NUpEdit.Text := IntToStr(N1); p1 := power(
NDownEdit.Text := IntToStr(N2); (1 + t * (0.049867347 + t * (0.0211410061 + t * (0.0032776283 +
NRunsEdit.Text := IntToStr(R); t * (0.0000380036 + t * (0.0000488906 + t * (0.000005383))))))),
outstr := format('%8.3f',[z]); -16
StatEdit.Text := outstr; );
// if t < 0.0001 then astring := 'Almost Zero' p := 1.0 - p1 / 2.0;
// else if z > 0.0 then
// begin t := 1.0 - p
outstr := format('%6.4f',[t]); else
ProbEdit.Text := outstr; t := 1.0 - (1.0 - p); // this is P value
// end;
// determine the conclusion // show results
if t < 0.01 then MeanEdit.Text := Format('%.3f', [Mean]);
begin StdDevEdit.Text := Format('%.3f', [SD]);
astring := 'Very strong evidence against randomness (trend or seasonality'; NUpEdit.Text := IntToStr(N1);
ConclusionEdit.Text := astring; NDownEdit.Text := IntToStr(N2);
end NRunsEdit.Text := IntToStr(R);
else if (t < 0.05) and (t >= 0.01) then StatEdit.Text := Format('%.3f', [z]);
begin ProbEdit.Text := Format('%.3f', [z]);
astring := 'Moderate evidence against randomness';
ConclusionEdit.Text := astring; // determine the conclusion
end if t < 0.01 then
else if (t < 0.10) and (t >= 0.05) then ConclusionEdit.Text := 'Very strong evidence against randomness (trend or seasonality'
begin else if (t < 0.05) and (t >= 0.01) then
astring := 'Suggestive evidence against normality'; ConclusionEdit.Text := 'Moderate evidence against randomness'
ConclusionEdit.Text := astring; else if (t < 0.10) and (t >= 0.05) then
end ConclusionEdit.Text := 'Suggestive evidence against normality'
else if t >= 0.10 then else if t >= 0.10 then
begin ConclusionEdit.Text := 'Little or no real evidence against randomness'
astring := 'Little or no real evidence against randomness'; else
ConclusionEdit.Text := astring; ConclusionEdit.Text := 'Strong evidence against randomness (trend or seasonality exists)';
end
else Values := nil;
begin
astring := 'Strong evidence against randomness (trend or seasonality exists)';
ConclusionEdit.Text := astring;
end;
values := nil;
end; end;
procedure Trunstestform.FormActivate(Sender: TObject); procedure Trunstestform.FormActivate(Sender: TObject);
@ -233,11 +230,10 @@ begin
if FAutoSized then if FAutoSized then
exit; exit;
w := MaxValue([ResetBtn.Width, CancelBtn.Width, ComputeBtn.Width, ReturnBtn.Width]); w := MaxValue([ResetBtn.Width, ComputeBtn.Width, CloseBtn.Width]);
ResetBtn.Constraints.MinWidth := w; ResetBtn.Constraints.MinWidth := w;
CancelBtn.Constraints.MinWidth := w;
ComputeBtn.Constraints.MinWidth := w; ComputeBtn.Constraints.MinWidth := w;
ReturnBtn.Constraints.MinWidth := w; CloseBtn.Constraints.MinWidth := w;
Constraints.MinWidth := Width; Constraints.MinWidth := Width;
Constraints.MinHeight := Height; Constraints.MinHeight := Height;
@ -251,23 +247,37 @@ begin
end; end;
procedure Trunstestform.InBtnClick(Sender: TObject); procedure Trunstestform.InBtnClick(Sender: TObject);
VAR index : integer; var
index: integer;
begin begin
index := VarList.ItemIndex; index := VarList.ItemIndex;
if index < 0 then exit; if (index > -1) and (TestVarEdit.Text = '') then
TestVarEdit.Text := VarList.Items.Strings[index]; begin
VarList.Items.Delete(index); TestVarEdit.Text := VarList.Items[index];
InBtn.Enabled := false; VarList.Items.Delete(index);
OutBtn.Enabled := true; UpdateBtnStates;
end;
end; end;
procedure Trunstestform.OutBtnClick(Sender: TObject); procedure Trunstestform.OutBtnClick(Sender: TObject);
begin begin
if TestVarEdit.Text = '' then exit; if TestVarEdit.Text <> '' then
VarList.Items.Add(TestVarEdit.Text); begin
TestVarEdit.Text := ''; VarList.Items.Add(TestVarEdit.Text);
InBtn.Enabled := true; TestVarEdit.Text := '';
OutBtn.Enabled := false; UpdateBtnStates;
end;
end;
procedure TRunsTestForm.UpdateBtnStates;
begin
InBtn.Enabled := AnySelected(VarList) and (TestVarEdit.Text = '');
OutBtn.Enabled := TestVarEdit.Text <> '';
end;
procedure Trunstestform.VarListSelectionChange(Sender: TObject; User: boolean);
begin
UpdateBtnStates;
end; end;
initialization initialization