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,12 +65,13 @@ 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
@ -81,17 +85,15 @@ begin
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;
@ -108,55 +110,60 @@ begin
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);
SetLength(Values, NoCases);
for i := 1 to NoCases do for i := 1 to NoCases do
begin begin
if not ValidValue(i, col) then continue; if not ValidValue(i, col) then continue;
values[i-1] := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[col,i])); Values[i-1] := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[col,i]));
N := N + 1; N := N + 1;
end; end;
if N <= 10 then if N <= 10 then
begin begin
ShowMessage('Insufficient data. You must have at least 11 values.'); MessageDlg('Insufficient data. You must have at least 11 values.', mtError, [mbOK], 0);
values := nil; Values := nil;
exit; exit;
end; end;
for i := 0 to N-1 do Mean := Mean + values[i];
for i := 0 to N-1 do
Mean := Mean + values[i];
Mean := Mean / N; Mean := Mean / N;
// run through each value and compare with the mean // 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 begin // check and discard the ties with the mean
if Mean <> values[i] then if Mean <> Values[i] then
begin // check if it is greater than the mean begin // check if it is greater than the mean
if values[i] > mean then if Values[i] > mean then
begin begin
N1 := N1 + 1; N1 := N1 + 1;
a := i; a := i;
while a > 0 do while a > 0 do
begin begin
a := a - 1; a := a - 1;
if values[a] <> Mean then break; if Values[a] <> Mean then break;
end; end;
if values[a] < Mean then if Values[a] < Mean then
begin begin
R := R + 1; R := R + 1;
NLess := NLess + 1; NLess := NLess + 1;
end; end;
end end
// check to see if it is less than the mean else // check to see if it is less than the mean
else if values[i] < Mean then if Values[i] < Mean then
begin begin
N2 := N2 + 1; N2 := N2 + 1;
a := i; a := i;
while a > 0 do while a > 0 do
begin begin
a := a - 1; a := a - 1;
if values[a] <> Mean then break; if Values[a] <> Mean then break;
end; end;
if values[a] > Mean then if Values[a] > Mean then
begin begin
R := R + 1; R := R + 1;
Nmore := Nmore + 1; Nmore := Nmore + 1;
@ -164,6 +171,7 @@ begin
end; // close of else i end; // close of else i
end; // end of if values[i] not equal to the mean end; // end of if values[i] not equal to the mean
end; // end of i loop end; // end of i loop
// compute the expected mean and variance of R // compute the expected mean and variance of R
ExpMean := 1.0 + ((2 * N1 * N2) / (N1 + N2)); // mean mu ExpMean := 1.0 + ((2 * N1 * N2) / (N1 + N2)); // mean mu
SD1 := 2 * N1 * N2 * (2 * N1 * N2 - N1 - N2); SD1 := 2 * N1 * N2 * (2 * N1 * N2 - N1 - N2);
@ -171,59 +179,48 @@ begin
SD3 := N1 + N2 - 1; SD3 := N1 + N2 - 1;
SD4 := SD1 / (SD2 * SD3); // standard deviation "sigma" SD4 := SD1 / (SD2 * SD3); // standard deviation "sigma"
SD := sqrt(SD4); SD := sqrt(SD4);
// calculating P Value // calculating P Value
z1 := (R - ExpMean) / SD; z1 := (R - ExpMean) / SD;
z2 := abs(z1); z2 := abs(z1);
z := z2; z := z2;
if z > 0 then t := z else t := -z; if z > 0 then
p1 := power((1 + t * (0.049867347 + t * (0.0211410061 + t * (0.0032776283 + t := z
t * (0.0000380036 + t * (0.0000488906 + t * (0.000005383))))))), -16); else
t := -z;
p1 := power(
(1 + t * (0.049867347 + t * (0.0211410061 + t * (0.0032776283 +
t * (0.0000380036 + t * (0.0000488906 + t * (0.000005383))))))),
-16
);
p := 1.0 - p1 / 2.0; p := 1.0 - p1 / 2.0;
if z > 0.0 then t := 1.0 - p if z > 0.0 then
else t := 1.0 - (1.0 - p); // this is P value t := 1.0 - p
else
t := 1.0 - (1.0 - p); // this is P value
// show results // show results
outstr := format('%8.3f',[Mean]); MeanEdit.Text := Format('%.3f', [Mean]);
MeanEdit.Text := outstr; StdDevEdit.Text := Format('%.3f', [SD]);
outstr := format('%8.3f',[SD]);
StdDevEdit.Text := outstr;
NUpEdit.Text := IntToStr(N1); NUpEdit.Text := IntToStr(N1);
NDownEdit.Text := IntToStr(N2); NDownEdit.Text := IntToStr(N2);
NRunsEdit.Text := IntToStr(R); NRunsEdit.Text := IntToStr(R);
outstr := format('%8.3f',[z]); StatEdit.Text := Format('%.3f', [z]);
StatEdit.Text := outstr; ProbEdit.Text := Format('%.3f', [z]);
// if t < 0.0001 then astring := 'Almost Zero'
// else
// begin
outstr := format('%6.4f',[t]);
ProbEdit.Text := outstr;
// end;
// determine the conclusion // determine the conclusion
if t < 0.01 then if t < 0.01 then
begin ConclusionEdit.Text := 'Very strong evidence against randomness (trend or seasonality'
astring := 'Very strong evidence against randomness (trend or seasonality';
ConclusionEdit.Text := astring;
end
else if (t < 0.05) and (t >= 0.01) then else if (t < 0.05) and (t >= 0.01) then
begin ConclusionEdit.Text := 'Moderate evidence against randomness'
astring := 'Moderate evidence against randomness';
ConclusionEdit.Text := astring;
end
else if (t < 0.10) and (t >= 0.05) then else if (t < 0.10) and (t >= 0.05) then
begin ConclusionEdit.Text := 'Suggestive evidence against normality'
astring := 'Suggestive evidence against normality';
ConclusionEdit.Text := astring;
end
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';
ConclusionEdit.Text := astring;
end
else else
begin ConclusionEdit.Text := 'Strong evidence against randomness (trend or seasonality exists)';
astring := 'Strong evidence against randomness (trend or seasonality exists)';
ConclusionEdit.Text := astring; Values := nil;
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
TestVarEdit.Text := VarList.Items[index];
VarList.Items.Delete(index); VarList.Items.Delete(index);
InBtn.Enabled := false; UpdateBtnStates;
OutBtn.Enabled := true; end;
end; end;
procedure Trunstestform.OutBtnClick(Sender: TObject); procedure Trunstestform.OutBtnClick(Sender: TObject);
begin begin
if TestVarEdit.Text = '' then exit; if TestVarEdit.Text <> '' then
begin
VarList.Items.Add(TestVarEdit.Text); VarList.Items.Add(TestVarEdit.Text);
TestVarEdit.Text := ''; TestVarEdit.Text := '';
InBtn.Enabled := true; UpdateBtnStates;
OutBtn.Enabled := false; 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