LazStats: Inherit HyperGeoUnit from BasicStatsReportFormUnit

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7889 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-11-16 15:46:13 +00:00
parent 5b9d42bcba
commit 25f1fc4f27
3 changed files with 469 additions and 476 deletions

View File

@ -1342,7 +1342,7 @@ procedure TOS3MainFrm.mnuSimHyperGeomProbClick(Sender: TObject);
begin
if HyperGeoForm = nil then
Application.CreateForm(THyperGeoForm, HyperGeoForm);
HyperGeoForm.ShowModal;
HyperGeoForm.Show;
end;
// Menu "Simulations" > "z for a given cum. Probability"

View File

@ -1,355 +1,314 @@
object HyperGeoForm: THyperGeoForm
inherited HyperGeoForm: THyperGeoForm
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 303
Height = 310
Top = 156
Width = 339
Width = 693
Anchors = [akTop]
AutoSize = True
BorderStyle = bsSingle
Caption = 'Hypergeometric Probabilities'
ClientHeight = 310
ClientWidth = 339
OnActivate = FormActivate
OnCreate = FormCreate
OnShow = ResetBtnClick
Position = poMainFormCenter
LCLVersion = '2.1.0.0'
object Label1: TLabel
AnchorSideTop.Control = SampSizeEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 134
Height = 15
Top = 12
Width = 68
Anchors = [akTop, akRight]
Caption = 'Sample Size: '
ParentColor = False
ClientWidth = 693
inherited ParamsPanel: TPanel
Height = 294
ClientHeight = 294
inherited CloseBtn: TButton
Top = 269
TabOrder = 12
end
inherited ComputeBtn: TButton
Top = 269
TabOrder = 11
end
inherited ResetBtn: TButton
Top = 269
TabOrder = 10
end
inherited HelpBtn: TButton
Top = 269
TabOrder = 9
end
inherited ButtonBevel: TBevel
Top = 253
end
object SampSizeLabel: TLabel[5]
AnchorSideTop.Control = SampSizeEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 116
Height = 15
Top = 4
Width = 68
Anchors = [akTop, akRight]
Caption = 'Sample Size: '
ParentColor = False
end
object SampObsLabel: TLabel[6]
AnchorSideTop.Control = SampObsEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 32
Height = 15
Top = 31
Width = 152
Anchors = [akTop, akRight]
Caption = 'Number observed in sample:'
ParentColor = False
end
object PopObsLabel: TLabel[7]
AnchorSideTop.Control = PopObsEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 12
Height = 15
Top = 85
Width = 172
Anchors = [akTop, akRight]
Caption = 'Number observed in population:'
ParentColor = False
end
object PopSizeLabel: TLabel[8]
AnchorSideTop.Control = PopSizeEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 103
Height = 15
Top = 58
Width = 81
Anchors = [akTop, akRight]
Caption = 'Population Size'
ParentColor = False
end
object SampSizeEdit: TEdit[9]
AnchorSideLeft.Control = ProbGEEdit
AnchorSideTop.Control = ParamsPanel
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
Left = 192
Height = 23
Top = 0
Width = 99
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
TabOrder = 0
Text = 'SampSizeEdit'
end
object SampObsEdit: TEdit[10]
AnchorSideLeft.Control = SampSizeEdit
AnchorSideTop.Control = SampSizeEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
Left = 192
Height = 23
Top = 27
Width = 99
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
TabOrder = 1
Text = 'SampObsEdit'
end
object PopSizeEdit: TEdit[11]
AnchorSideLeft.Control = SampSizeEdit
AnchorSideTop.Control = SampObsEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
Left = 192
Height = 23
Top = 54
Width = 99
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
TabOrder = 2
Text = 'PopSizeEdit'
end
object PopObsEdit: TEdit[12]
AnchorSideLeft.Control = SampSizeEdit
AnchorSideTop.Control = PopSizeEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
Left = 192
Height = 23
Top = 81
Width = 99
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
TabOrder = 3
Text = 'PopObsEdit'
end
object Label5: TLabel[13]
AnchorSideTop.Control = ProbXEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 100
Height = 15
Top = 124
Width = 84
Anchors = [akTop, akRight]
Caption = 'Probability of X:'
ParentColor = False
end
object Label6: TLabel[14]
AnchorSideTop.Control = ProbGTEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 47
Height = 15
Top = 151
Width = 137
Anchors = [akTop, akRight]
Caption = 'Probability greater than X:'
ParentColor = False
end
object Label7: TLabel[15]
AnchorSideTop.Control = ProbLEEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 32
Height = 15
Top = 178
Width = 152
Anchors = [akTop, akRight]
Caption = 'Probability less or equal to X:'
ParentColor = False
end
object Label8: TLabel[16]
AnchorSideTop.Control = ProbGEEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = ProbGEEdit
Left = 14
Height = 15
Top = 205
Width = 170
Anchors = [akTop, akRight]
BorderSpacing.Left = 16
Caption = 'Probability greater or equal to X:'
ParentColor = False
end
object Label9: TLabel[17]
AnchorSideTop.Control = ProbLTEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 65
Height = 15
Top = 232
Width = 119
Anchors = [akTop, akRight]
Caption = 'Probability less than X:'
ParentColor = False
end
object ProbXEdit: TEdit[18]
AnchorSideLeft.Control = SampSizeEdit
AnchorSideTop.Control = PopObsEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
Left = 192
Height = 23
Top = 120
Width = 99
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 16
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True
TabOrder = 4
Text = 'ProbXEdit'
end
object ProbGTEdit: TEdit[19]
AnchorSideLeft.Control = SampSizeEdit
AnchorSideTop.Control = ProbXEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
Left = 192
Height = 23
Top = 147
Width = 99
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True
TabOrder = 5
Text = 'ProbGTEdit'
end
object ProbLEEdit: TEdit[20]
AnchorSideLeft.Control = ProbGEEdit
AnchorSideTop.Control = ProbGTEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
Left = 192
Height = 23
Top = 174
Width = 99
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True
TabOrder = 6
Text = 'ProbLEEdit'
end
object ProbGEEdit: TEdit[21]
AnchorSideLeft.Control = Label8
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = ProbLEEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
Left = 192
Height = 23
Top = 201
Width = 99
Alignment = taRightJustify
Anchors = [akTop, akRight]
BorderSpacing.Left = 8
BorderSpacing.Top = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True
TabOrder = 7
Text = 'ProbGEEdit'
end
object ProbLTEdit: TEdit[22]
AnchorSideLeft.Control = ProbGEEdit
AnchorSideTop.Control = ProbGEEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
Left = 192
Height = 23
Top = 228
Width = 99
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True
TabOrder = 8
Text = 'ProbLTEdit'
end
end
object Label2: TLabel
AnchorSideTop.Control = SampObsEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 50
Height = 15
Top = 39
Width = 152
Anchors = [akTop, akRight]
Caption = 'Number observed in sample:'
ParentColor = False
end
object Label3: TLabel
AnchorSideTop.Control = PopObsEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 118
Height = 15
Top = 93
Width = 84
Anchors = [akTop, akRight]
Caption = 'Population Size:'
ParentColor = False
end
object Label4: TLabel
AnchorSideTop.Control = PopSizeEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 30
Height = 15
Top = 66
Width = 172
Anchors = [akTop, akRight]
Caption = 'Number observed in population:'
ParentColor = False
end
object Label5: TLabel
AnchorSideTop.Control = ProbXEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 118
Height = 15
Top = 132
Width = 84
Anchors = [akTop, akRight]
Caption = 'Probability of X:'
ParentColor = False
end
object Label6: TLabel
AnchorSideTop.Control = ProbGTEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 65
Height = 15
Top = 159
Width = 137
Anchors = [akTop, akRight]
Caption = 'Probability greater than X:'
ParentColor = False
end
object Label7: TLabel
AnchorSideTop.Control = ProbLEEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 50
Height = 15
Top = 186
Width = 152
Anchors = [akTop, akRight]
Caption = 'Probability less or equal to X:'
ParentColor = False
end
object Label8: TLabel
AnchorSideLeft.Control = Owner
AnchorSideTop.Control = ProbGEEdit
AnchorSideTop.Side = asrCenter
Left = 32
Height = 15
Top = 213
Width = 170
BorderSpacing.Left = 32
Caption = 'Probability greater or equal to X:'
ParentColor = False
end
object Label9: TLabel
AnchorSideTop.Control = ProbLTEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = Label8
AnchorSideRight.Side = asrBottom
Left = 83
Height = 15
Top = 240
Width = 119
Anchors = [akTop, akRight]
Caption = 'Probability less than X:'
ParentColor = False
end
object ResetBtn: TButton
AnchorSideTop.Control = ReturnBtn
AnchorSideRight.Control = ComputeBtn
Left = 65
Height = 25
Top = 276
Width = 54
Anchors = [akTop, akRight]
AutoSize = True
BorderSpacing.Left = 16
BorderSpacing.Right = 12
BorderSpacing.Bottom = 8
Caption = 'Reset'
OnClick = ResetBtnClick
TabOrder = 0
end
object ComputeBtn: TButton
AnchorSideLeft.Control = Owner
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = ReturnBtn
Left = 131
Height = 25
Top = 276
Width = 76
AutoSize = True
Caption = 'Compute'
OnClick = ComputeBtnClick
TabOrder = 1
end
object ReturnBtn: TButton
AnchorSideLeft.Control = ComputeBtn
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = Bevel1
AnchorSideTop.Side = asrBottom
Left = 219
Height = 25
Top = 276
Width = 61
AutoSize = True
BorderSpacing.Left = 12
BorderSpacing.Top = 8
BorderSpacing.Right = 16
BorderSpacing.Bottom = 8
Caption = 'Return'
ModalResult = 1
TabOrder = 2
end
object SampSizeEdit: TEdit
AnchorSideLeft.Control = ProbGEEdit
AnchorSideTop.Control = Owner
AnchorSideRight.Control = ProbGEEdit
AnchorSideRight.Side = asrBottom
Left = 210
Height = 23
Top = 8
Width = 82
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 8
TabOrder = 3
Text = 'SampSizeEdit'
end
object SampObsEdit: TEdit
AnchorSideLeft.Control = SampSizeEdit
AnchorSideTop.Control = SampSizeEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ProbGEEdit
AnchorSideRight.Side = asrBottom
Left = 210
Height = 23
Top = 35
Width = 82
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
TabOrder = 4
Text = 'Edit1'
end
object PopSizeEdit: TEdit
AnchorSideLeft.Control = SampSizeEdit
AnchorSideTop.Control = SampObsEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ProbGEEdit
AnchorSideRight.Side = asrBottom
Left = 210
Height = 23
Top = 62
Width = 82
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
TabOrder = 5
Text = 'Edit1'
end
object PopObsEdit: TEdit
AnchorSideLeft.Control = SampSizeEdit
AnchorSideTop.Control = PopSizeEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ProbGEEdit
AnchorSideRight.Side = asrBottom
Left = 210
Height = 23
Top = 89
Width = 82
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
TabOrder = 6
Text = 'Edit1'
end
object ProbXEdit: TEdit
AnchorSideLeft.Control = SampSizeEdit
AnchorSideTop.Control = PopObsEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ProbLEEdit
AnchorSideRight.Side = asrBottom
Left = 210
Height = 23
Top = 128
Width = 82
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 16
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True
TabOrder = 7
Text = 'Edit1'
end
object ProbGTEdit: TEdit
AnchorSideLeft.Control = SampSizeEdit
AnchorSideTop.Control = ProbXEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ProbGEEdit
AnchorSideRight.Side = asrBottom
Left = 210
Height = 23
Top = 155
Width = 82
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True
TabOrder = 8
Text = 'Edit1'
end
object ProbLEEdit: TEdit
AnchorSideLeft.Control = ProbGEEdit
AnchorSideTop.Control = ProbGTEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ProbGEEdit
AnchorSideRight.Side = asrBottom
Left = 210
Height = 23
Top = 182
Width = 82
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True
TabOrder = 9
Text = 'Edit1'
end
object ProbGEEdit: TEdit
AnchorSideLeft.Control = Label8
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = ProbLEEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = SampSizeEdit
AnchorSideRight.Side = asrBottom
Left = 210
Height = 23
Top = 209
Width = 82
Alignment = taRightJustify
BorderSpacing.Left = 8
BorderSpacing.Top = 4
BorderSpacing.Right = 32
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True
TabOrder = 10
Text = 'Edit1'
end
object ProbLTEdit: TEdit
AnchorSideLeft.Control = ProbGEEdit
AnchorSideTop.Control = ProbGEEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ProbGEEdit
AnchorSideRight.Side = asrBottom
Left = 210
Height = 23
Top = 236
Width = 82
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 4
Font.Style = [fsBold]
ParentFont = False
ReadOnly = True
TabOrder = 11
Text = 'Edit1'
end
object Bevel1: TBevel
AnchorSideLeft.Control = Owner
AnchorSideTop.Control = ProbLTEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
Left = 0
Height = 9
Top = 259
Width = 339
Anchors = [akTop, akLeft, akRight]
Shape = bsBottomLine
inherited ParamsSplitter: TSplitter
Height = 310
end
end

View File

@ -5,19 +5,14 @@ unit HyperGeoUnit;
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, ExtCtrls,
FunctionsLib, OutputUnit;
Classes, SysUtils, Forms, Controls, Graphics, StdCtrls, ExtCtrls,
FunctionsLib, BasicStatsReportFormUnit;
type
{ THyperGeoForm }
THyperGeoForm = class(TForm)
Bevel1: TBevel;
ResetBtn: TButton;
ComputeBtn: TButton;
ReturnBtn: TButton;
THyperGeoForm = class(TBasicStatsReportForm)
SampSizeEdit: TEdit;
SampObsEdit: TEdit;
PopSizeEdit: TEdit;
@ -27,39 +22,157 @@ type
ProbLEEdit: TEdit;
ProbGEEdit: TEdit;
ProbLTEdit: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
SampSizeLabel: TLabel;
SampObsLabel: TLabel;
PopObsLabel: TLabel;
PopSizeLabel: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
procedure ComputeBtnClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ResetBtnClick(Sender: TObject);
procedure FisherTable(A,B,C,D, p, SumP : double);
private
{ private declarations }
procedure FisherTable(A,B,C,D, p, SumP: double; AReport: TStrings);
protected
procedure AdjustConstraints; override;
procedure Compute; override;
function Validate(out AMsg: String; out AControl: TWinControl): Boolean; override;
public
{ public declarations }
procedure Reset; override;
end;
var
HyperGeoForm: THyperGeoForm;
implementation
{$R *.lfm}
uses
Math;
{ THyperGeoForm }
procedure THyperGeoForm.ResetBtnClick(Sender: TObject);
procedure THyperGeoForm.AdjustConstraints;
begin
inherited;
ParamsPanel.Constraints.MinHeight := ProbLTEdit.Top + ProbLTEdit.Height +
ButtonBevel.Height + CloseBtn.BorderSpacing.Top + CloseBtn.Height;
ParamsPanel.Constraints.MinWidth := Max(
Max(PopSizeLabel.Width, Label8.Width) + Label8.BorderSpacing.Left + ProbGEEdit.BorderSpacing.Left + ProbGEEdit.Width,
4*CloseBtn.Width + 3*CloseBtn.BorderSpacing.Left
);;
end;
procedure THyperGeoForm.Compute;
var
SampObs, PopObs, SampSize, PopSize: double;
A, B, C, D, APlusB, CPlusD: double;
ProbX, Prob, SumProb, ProbGE, ProbGT, ProbLT, ProbLE: double;
done: boolean;
lReport: TStrings;
begin
lReport := TStringList.Create;
try
lReport.Add('HYPERGEOMETRIC DISTRIBUTION CALCULATIONS');
lReport.Add('Accumulating Values of the distribution');
lReport.Add('');
label5.Enabled := true;
label6.Enabled := true;
label7.Enabled := true;
label8.Enabled := true;
label9.Enabled := true;
ProbXEdit.Enabled := true;
ProbGEEdit.Enabled := true;
ProbLTEdit.Enabled := true;
ProbLEEdit.Enabled := true;
ProbGTEdit.Enabled := true;
SampObs := StrToFloat(SampObsEdit.Text);
PopObs := StrToFloat(PopObsEdit.Text);
SampSize := StrToFloat(SampSizeEdit.Text);
PopSize := StrToFloat(PopSizeEdit.Text);
done := false;
SumProb := 0.0;
A := SampObs;
B := SampSize - A;
C := PopObs;
D := PopSize - C;
APlusB := A + B;
CPlusD := C + D;
ProbX := combos(A,C) * combos(B,D) / combos(APlusB,CPlusD);
ProbXEdit.Text := Format('%.4f', [ProbX]);
SumProb := SumProb + ProbX;
FisherTable(A,B,C,D,ProbX,SumProb, lReport);
// get more extreme probabilities
while not done do
begin
if A = APlusB then
done := true
else
begin
A := A + 1;
B := B - 1;
if (A < 0) or (B < 0) or (C < 0) or (D < 0) then done := true;
end;
if not done then
begin
Prob := combos(A,C) * combos(B,D) / combos(APlusB,CPlusD);
SumProb := SumProb + Prob;
FisherTable(A,B,C,D,Prob,SumProb, lReport);
lReport.Add('');
end;
end; // end while not done
ProbGE := SumProb;
ProbGT := SumProb - ProbX;
ProbLT := 1.0 - ProbGE;
ProbLE := ProbLT + ProbX;
ProbGEEdit.Text := Format('%.4f', [ProbGE]);
ProbLEEdit.Text := Format('%.4f', [ProbLE]);
ProbGTEdit.Text := Format('%.4f', [ProbGT]);
ProbLTEdit.Text := Format('%.4f', [ProbLT]);
FReportFrame.DisplayReport(lReport);
finally
lReport.Free;
end;
end;
procedure THyperGeoForm.FisherTable(A,B,C,D, p, SumP: double; AReport: TStrings);
begin
AReport.Add('Table for Hypergeometric Probabilities');
AReport.Add('----------------------------------');
AReport.Add(' Row Column ');
AReport.Add(' 1 2 ');
AReport.Add(' --- ---------- -----------');
AReport.Add(' 1 %8.0f %8.0f', [A, B]);
AReport.Add(' 2 %8.0f %8.0f', [C, D]);
AReport.Add('----------------------------------');
AReport.Add('Probability: %8.4f', [p]);
AReport.Add('Cumulative Probability: %8.4f', [SumP]);
AReport.Add('');
end;
procedure THyperGeoForm.Reset;
begin
inherited;
SampSizeEdit.Text := '';
SampObsEdit.Text := '';
PopSizeEdit.Text := '';
@ -81,122 +194,43 @@ begin
ProbGTEdit.Enabled := false;
end;
procedure THyperGeoForm.ComputeBtnClick(Sender: TObject);
VAR
SampObs, PopObs, SampSize, PopSize, N : double;
A, B, C, D, APlusC, BPlusD, APlusB, CPlusD : double;
ProbX, Prob, SumProb, ProbGE, ProbGT, ProbLT, ProbLE : double;
done : boolean;
outvalue : string;
begin
done := false;
SumProb := 0.0;
label5.Enabled := true;
label6.Enabled := true;
label7.Enabled := true;
label8.Enabled := true;
label9.Enabled := true;
ProbXEdit.Enabled := true;
ProbGEEdit.Enabled := true;
ProbLTEdit.Enabled := true;
ProbLEEdit.Enabled := true;
ProbGTEdit.Enabled := true;
SampObs := StrToFloat(SampObsEdit.Text);
PopObs := StrToFloat(PopObsEdit.Text);
SampSize := StrToFloat(SampSizeEdit.Text);
PopSize := StrToFloat(PopSizeEdit.Text);
A := SampObs;
B := SampSize - A;
C := PopObs;
D := PopSize - C;
APlusC := A + C;
BPlusD := B + D;
APlusB := A + B;
CPlusD := C + D;
N := A + B + C + D;
// largest := 1;
OutputFrm.RichEdit.Clear;
OutputFrm.RichEdit.Lines.Add('Hypergeometric Distribution Calculations');
OutputFrm.RichEdit.Lines.Add('');
OutputFrm.RichEdit.Lines.Add('Accumulating Values of the distribution');
OutputFrm.RichEdit.Lines.Add('');
ProbX := combos(A,C) * combos(B,D) / combos(APlusB,CPlusD);
outvalue := format('%6.4f',[ProbX]);
ProbXEdit.Text := outvalue;
SumProb := SumProb + ProbX;
FisherTable(A,B,C,D,ProbX,SumProb);
// get more extreme probabilities
while not done do
begin
if A = APlusB then done := true
else begin
A := A + 1;
B := B - 1;
if (A < 0) or (B < 0) or (C < 0) or (D < 0) then done := true;
end;
if not done then
begin
Prob := combos(A,C) * combos(B,D) / combos(APlusB,CPlusD);
SumProb := SumProb + Prob;
FisherTable(A,B,C,D,Prob,SumProb);
end;
end; // end while not done
OutputFrm.ShowModal;
ProbGE := SumProb;
ProbGT := SumProb - ProbX;
ProbLT := 1.0 - ProbGE;
ProbLE := ProbLT + ProbX;
outvalue := format('%6.4f',[ProbGE]);
ProbGEEdit.Text := outvalue;
outvalue := format('%6.4f',[ProbLE]);
ProbLEEdit.Text := outvalue;
outvalue := format('%6.4f',[ProbGT]);
ProbGTEdit.Text := outvalue;
outvalue := format('%6.4f',[ProbLT]);
ProbLTEdit.Text := outvalue;
OutputFrm.RichEdit.Clear;
end;
procedure THyperGeoForm.FisherTable(A,B,C,D, p, SumP : double);
VAR
outline : string;
begin
OutputFrm.RichEdit.Lines.Add('');
OutputFrm.RichEdit.Lines.Add('Table for Hypergeometric Probabilities');
OutputFrm.RichEdit.Lines.Add(' Column');
OutputFrm.RichEdit.Lines.Add('Row 1 2');
outline := format(' 1 %10.0f %10.0f',[A,B]);
OutputFrm.RichEdit.Lines.Add(outline);
outline := format(' 2 %10.0f %10.0f',[C,D]);
OutputFrm.RichEdit.Lines.Add(outline);
outline := format('Probability = %6.4f',[p]);
OutputFrm.RichEdit.Lines.Add(outline);
OutputFrm.RichEdit.Lines.Add('');
outline := format('Cumulative Probability = %6.4f',[SumP]);
OutputFrm.RichEdit.Lines.Add(outline);
OutputFrm.RichEdit.Lines.Add('');
end;
procedure THyperGeoForm.FormActivate(Sender: TObject);
function THyperGeoForm.Validate(out AMsg: String; out AControl: TWinControl): Boolean;
var
w: Integer;
n: Integer;
begin
w := MaxValue([ResetBtn.Width, ComputeBtn.Width, ReturnBtn.Width]);
ResetBtn.Constraints.MinWidth := w;
ComputeBtn.Constraints.MinWidth := w;
ReturnBtn.Constraints.MinWidth := w;
end;
Result := false;
procedure THyperGeoForm.FormCreate(Sender: TObject);
begin
if OutputFrm = nil then
Application.CreateForm(TOutputFrm, OutputFrm);
end;
if (SampSizeEdit.Text = '') or not TryStrToInt(SampSizeEdit.Text, n) or (n <= 0) then
begin
AMsg := 'Valid positive integer number required.';
AControl := SampSizeEdit;
exit;
end;
initialization
{$I hypergeounit.lrs}
if (SampObsEdit.Text = '') or not TryStrToInt(SampObsEdit.Text, n) or (n <= 0) then
begin
AMsg := 'Valid positive integer number required.';
AControl := SampObsEdit;
exit;
end;
if (PopSizeEdit.Text = '') or not TryStrToInt(PopSizeEdit.Text, n) or (n <= 0) then
begin
AMsg := 'Valid positive integer number required.';
AControl := PopSizeEdit;
exit;
end;
if (PopObsEdit.Text = '') or not TryStrToInt(PopObsEdit.Text, n) or (n <= 0) then
begin
AMsg := 'Valid positive integer number required.';
AControl := PopObsEdit;
exit;
end;
Result := true;
end;
end.