You've already forked lazarus-ccr
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7880 8e941d3f-bd1b-0410-a28a-d453659cc2b4
167 lines
4.8 KiB
Plaintext
167 lines
4.8 KiB
Plaintext
unit BinomialUnit;
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
interface
|
|
|
|
uses
|
|
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
|
StdCtrls, OutPutUnit, Math, FunctionsLib, GraphLib;
|
|
|
|
type
|
|
|
|
{ TBinomialFrm }
|
|
|
|
TBinomialFrm = class(TForm)
|
|
PlotChk: TCheckBox;
|
|
ResetBtn: TButton;
|
|
CancelBtn: TButton;
|
|
ComputeBtn: TButton;
|
|
ReturnBtn: TButton;
|
|
FreqAEdit: TEdit;
|
|
FreqBEdit: TEdit;
|
|
PropAEdit: TEdit;
|
|
Label1: TLabel;
|
|
Label2: TLabel;
|
|
Label3: TLabel;
|
|
procedure ComputeBtnClick(Sender: TObject);
|
|
procedure FormShow(Sender: TObject);
|
|
procedure FreqAEditKeyPress(Sender: TObject; var Key: char);
|
|
procedure FreqBEditKeyPress(Sender: TObject; var Key: char);
|
|
procedure PropAEditKeyPress(Sender: TObject; var Key: char);
|
|
procedure ResetBtnClick(Sender: TObject);
|
|
private
|
|
{ private declarations }
|
|
public
|
|
{ public declarations }
|
|
end;
|
|
|
|
var
|
|
BinomialFrm: TBinomialFrm;
|
|
|
|
implementation
|
|
|
|
{ TBinomialFrm }
|
|
|
|
procedure TBinomialFrm.ResetBtnClick(Sender: TObject);
|
|
begin
|
|
FreqAEdit.Text := '';
|
|
FreqBEdit.Text := '';
|
|
PropAEdit.Text := '';
|
|
FreqAEdit.SetFocus;
|
|
end;
|
|
|
|
procedure TBinomialFrm.FormShow(Sender: TObject);
|
|
begin
|
|
ResetBtnClick(self);
|
|
end;
|
|
|
|
procedure TBinomialFrm.ComputeBtnClick(Sender: TObject);
|
|
var
|
|
p, Q, Probability, z, CorrectedA, SumProb : double;
|
|
A, b, N, X, i, j : integer;
|
|
outline : string;
|
|
begin
|
|
SumProb := 0.0;
|
|
A := round(StrToFloat(FreqAEdit.Text));
|
|
b := round(StrToFloat(FreqBEdit.Text));
|
|
p := StrToFloat(PropAEdit.Text);
|
|
N := A + b;
|
|
Q := 1.0 - p;
|
|
|
|
OutPutFrm.RichEdit.Clear;
|
|
OutPutFrm.RichEdit.Lines.Add('Binomial Probability Test');
|
|
OutPutFrm.RichEdit.Lines.Add('');
|
|
outline := format('Frequency of %d out of %d observed',[A, N]);
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
outline := format('The theoretical proportion expected in category A is %5.3f',
|
|
[p]);
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
outline := 'The test is for the probability of a value in category A as small or smaller';
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
outline := 'than that observed given the expected proportion.';
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
|
|
if (N > 35) then //Use normal distribution approximation
|
|
begin
|
|
CorrectedA := A;
|
|
if A < (N * p) then CorrectedA := A + 0.5;
|
|
if A > (N * p) then CorrectedA := A - 0.5;
|
|
z := (CorrectedA - N * p) / sqrt(N * p * Q);
|
|
outline := format('Z value for Normal Distribution approximation := %5.3f',
|
|
[z]);
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
Probability := probz(z);
|
|
outline := format('Probability := %6.4f',[Probability]);
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
end
|
|
else //Use binomial fomula
|
|
begin
|
|
for X := 0 to A do
|
|
begin
|
|
Probability := combos(X, N) * Power(p,X) * Power(Q,(N - X));
|
|
outline := format('Probability of %d = %6.4f',[X,Probability]);
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
SumProb := SumProb + Probability;
|
|
end;
|
|
outline := format('Binomial Probability of %d or less out of %d = %6.4f',
|
|
[A,N,SumProb]);
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
end;
|
|
OutPutFrm.ShowModal;
|
|
OutPutFrm.RichEdit.Clear;
|
|
if PlotChk.Checked then
|
|
begin
|
|
if N <= 35 then
|
|
begin
|
|
SetLength(GraphFrm.Xpoints,1,N+1);
|
|
SetLength(GraphFrm.Ypoints,1,N+1);
|
|
for i := 0 to N do GraphFrm.Xpoints[0,i] := i;
|
|
for i := 0 to N do
|
|
begin
|
|
Probability := combos(i,N) * power(p,i) * power(Q,(N-i));
|
|
GraphFrm.Ypoints[0,i] := Probability;
|
|
end;
|
|
GraphFrm.GraphType := 2;
|
|
GraphFrm.nosets := 1;
|
|
GraphFrm.nbars := N;
|
|
GraphFrm.BackColor := clYellow;
|
|
GraphFrm.WallColor := clBlue;
|
|
GraphFrm.FloorColor := clGray;
|
|
GraphFrm.Heading := 'Binomial Distribution';
|
|
GraphFrm.XTitle := 'Values';
|
|
GraphFrm.YTitle := 'Probability';
|
|
GraphFrm.barwideprop := 0.5;
|
|
GraphFrm.AutoScale := true;
|
|
GraphFrm.ShowLeftWall := true;
|
|
GraphFrm.ShowRightWall := true;
|
|
GraphFrm.ShowBottomWall := true;
|
|
GraphFrm.ShowModal;
|
|
end
|
|
else ShowMessage('Cannot plot for N > 35');
|
|
end;
|
|
GraphFrm.Xpoints := nil;
|
|
GraphFrm.Ypoints := nil;
|
|
end;
|
|
|
|
procedure TBinomialFrm.FreqAEditKeyPress(Sender: TObject; var Key: char);
|
|
begin
|
|
if Ord(Key) = 13 then FreqBEdit.SetFocus;
|
|
end;
|
|
|
|
procedure TBinomialFrm.FreqBEditKeyPress(Sender: TObject; var Key: char);
|
|
begin
|
|
if Ord(Key) = 13 then PropAEdit.SetFocus;
|
|
end;
|
|
|
|
procedure TBinomialFrm.PropAEditKeyPress(Sender: TObject; var Key: char);
|
|
begin
|
|
if Ord(Key) = 13 then ComputeBtn.SetFocus;
|
|
end;
|
|
|
|
initialization
|
|
{$I binomialunit.lrs}
|
|
|
|
end.
|
|
|