unit HyperGeoUnit; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, functionsLib, OutPutUnit; type { THyperGeoForm } THyperGeoForm = class(TForm) ResetBtn: TButton; ComputeBtn: TButton; ReturnBtn: TButton; SampSizeEdit: TEdit; SampObsEdit: TEdit; PopSizeEdit: TEdit; PopObsEdit: TEdit; ProbXEdit: TEdit; ProbGTEdit: TEdit; ProbLEEdit: TEdit; ProbGEEdit: TEdit; ProbLTEdit: TEdit; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; Label8: TLabel; Label9: TLabel; procedure ComputeBtnClick(Sender: TObject); procedure ResetBtnClick(Sender: TObject); procedure FisherTable(A,B,C,D, p, SumP : double); private { private declarations } public { public declarations } end; var HyperGeoForm: THyperGeoForm; implementation { THyperGeoForm } procedure THyperGeoForm.ResetBtnClick(Sender: TObject); begin SampSizeEdit.Text := ''; SampObsEdit.Text := ''; PopSizeEdit.Text := ''; PopObsEdit.Text := ''; ProbXEdit.Text := ''; ProbGEEdit.Text := ''; ProbLTEdit.Text := ''; ProbLEEdit.Text := ''; ProbGTEdit.Text := ''; label5.Visible := false; label6.Visible := false; label7.Visible := false; label8.Visible := false; label9.Visible := false; ProbXEdit.Visible := false; ProbGEEdit.Visible := false; ProbLTEdit.Visible := false; ProbLEEdit.Visible := false; ProbGTEdit.Visible := false; end; procedure THyperGeoForm.ComputeBtnClick(Sender: TObject); VAR SampObs, PopObs, SampSize, PopSize, N : double; A, B, C, D, APlusC, BPlusD, APlusB, CPlusD : double; largest : integer; ProbX, Prob, SumProb, ProbGE, ProbGT, ProbLT, ProbLE : double; done : boolean; outvalue : string; begin done := false; SumProb := 0.0; label5.Visible := true; label6.Visible := true; label7.Visible := true; label8.Visible := true; label9.Visible := true; ProbXEdit.Visible := true; ProbGEEdit.Visible := true; ProbLTEdit.Visible := true; ProbLEEdit.Visible := true; ProbGTEdit.Visible := 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; initialization {$I hypergeounit.lrs} end.