Часть 2. Критерий Колмогорова:
Критерий Пирсона:
Проверка однородности дисперсии:
Текст программы.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, TeeProcs, TeEngine, Chart, Series, ComCtrls,
Grids;
type
TForm1 = class(TForm)
Memo1: TMemo;
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Edit9: TEdit;
Edit10: TEdit;
Label11: TLabel;
Label12: TLabel;
Label13: TLabel;
Label14: TLabel;
Label15: TLabel;
Label16: TLabel;
Edit11: TEdit;
Edit12: TEdit;
Edit13: TEdit;
Edit14: TEdit;
Edit15: TEdit;
Edit16: TEdit;
Label17: TLabel;
Label18: TLabel;
Label19: TLabel;
PageControl1: TPageControl;
TabSheet4: TTabSheet;
TabSheet5: TTabSheet;
sg1: TStringGrid;
Button2: TButton;
sg2: TStringGrid;
Label21: TLabel;
Edit18: TEdit;
Button3: TButton;
Memo2: TMemo;
Memo3: TMemo;
GroupBox2: TGroupBox;
Memo4: TMemo;
Label22: TLabel;
TabSheet3: TTabSheet;
Button4: TButton;
sg3: TStringGrid;
Label4: TLabel;
Edit4: TEdit;
Label2: TLabel;
Label3: TLabel;
Label20: TLabel;
Edit2: TEdit;
Label23: TLabel;
Label24: TLabel;
Label25: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
x: array [1..12000] of real;
D:array [1..100] of real;
N,M,i,j,t,k,l,count: integer;
A1,B1,y1,A2,B2,y2,A3,B3,y3,li,Dp,M_oj: extended;
k_ser: array [1..12000] of Integer;
r, r_max,U: Integer;
function SPRN(A,B,y: Extended; N,fl: Integer): Extended;
implementation
{$R *.dfm}
// Функция расчета ППСЧ---------------------------------------------------------
function SPRN(A,B,y: Extended; N,fl: Integer): Extended;
begin
for i := 1 to N do begin
y := (A * y + B);
while y >= k do
y := y - k;
x[i] := strtofloat(formatfloat('0.000',y));
if fl = 1 then begin
M_oj := (0 + k) / 2;
Form1.memo1.Lines.Add(inttostr(i)+') '+formatfloat('0.000',y));
if i>=1 then begin
if x[i]>M_oj then begin
Form1.memo3.Lines.Add(inttostr(i)+') +');
k_ser[i] := 1;
end
else begin
Form1.memo3.Lines.Add(inttostr(i)+') -');
k_ser[i] := 0;
end;
end;
end;
end;
// Критерий серий-------------------------------------------------------------
if fl = 1 then begin
i := 2;
r_max := 0;
U := 0;
while i <= N do begin
if k_ser[i] = k_ser[i-1] then begin
r := 1;
while k_ser[i] = k_ser[i-1] do begin
r := r + 1;
inc(i);
end;
inc(U);
end
else
inc(i);
if r > r_max then begin
r_max := r;
end ;
end;
if (r_max < 1.43*ln(N+1)) and (U > 0.5*(N+2-(1.96*sqrt(N-1)))) then
Form1.Label22.Caption := 'Гипотеза принимается'
else
Form1.Label22.Caption := 'Гипотеза не принимается';
Form1.Label22.Visible := True;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// Входные данные---------------------------------------------------------------
N := strtoint(edit1.Text);
M := strtoint(form1.edit16.Text);
T := strtoint(edit15.Text);
A1 := strtofloat(edit5.Text);
B1 := strtofloat(edit8.Text);
y1 := strtofloat(edit11.Text);
A2 := strtofloat(edit6.Text);
B2 := strtofloat(edit9.Text);
y2 := strtofloat(edit12.Text);
A3 := strtofloat(edit7.Text);
B3 := strtofloat(edit10.Text);
y3 := strtofloat(edit13.Text);
k := strtoint(edit14.Text);
// ППСЧ-----------------------------------------------------------------------
Form1.memo1.Lines.Clear;
Form1.memo3.Lines.Clear;
Form1.memo2.Visible:=True;
Form1.memo4.Visible:=True;
Form1.memo1.Visible:=False;
Form1.memo3.Visible:=False;
Form1.Memo1.Lines.Add('A1 B1 X1');
Form1.Memo1.Lines.Add('');
Form1.Memo3.Lines.Add('');
Form1.Memo3.Lines.Add('');
// Form1.Memo3.Lines.Add('1) _');
SPRN(A1,B1,y3,N,6);
SPRN(A1,B1,y2,N,5);
SPRN(A1,B1,y1,N,4);
SPRN(A3,B3,y1,N,3);
SPRN(A2,B2,y1,N,2);
SPRN(A1,B1,y1,N,1);
Form1.Memo1.Visible := True;
Form1.Memo3.Visible := True;
Form1.Memo2.Visible := false;
Form1.Memo4.Visible := false;
Button2.Enabled := true;
Button3.Enabled := true;
Button4.Enabled := true;
end;
// Критерий Колмогорова---------------------------------------------------------
procedure TForm1.Button2Click(Sender: TObject);
begin
SG1.RowCount:=M+1;
SG1.Cells[0,0]:='Последовательность';
SG1.Cells[1,0]:='D расчетное';
i := 1;
j := 1;
while i <= N do begin
SG1.Cells[0,j]:= floattostr(i-1) + ' - ' + floattostr(i-1 + N/M);
count := 1;
Dp := 0;
while count < N/M do begin
if Dp < (count/(N/M) - (x[i]/k)) then
Dp := (count/(N/M) - (x[i]/k));
inc(count);
inc(i);
end;
SG1.Cells[1,j]:=floattostr(Dp);
inc(i);
inc(j);
end;
if (Dp < 0.95) then begin
Label3.Caption := 'Удовлетворяет критерию Колмогорова';
end
else begin
Label3.Caption := 'Не удовлетворяет критерию Колмогорова';
end;
end;
// Однородность дисперсии-------------------------------------------------------
procedure TForm1.Button3Click(Sender: TObject);
var
a,b,i,k,m,n,j:integer;
D:array [1..100] of real;
Dt,Hi2,sr,q,s:real;
begin
m:=strtoint(form1.edit16.Text);
n:=strtoint(form1.edit1.Text);
k:=n div m;
a:=1;
b:=k;
sg2.ColCount:=m+1;
sg2.Cells[0,0]:='№';
sg2.Cells[0,1]:='D(i)';
for i:=1 to m do begin
s:=0;
for j:=a to b do begin
s:=s+unit1.x[j];
end;
sr:=s/k;
q:=0;
for j:=a to b do begin
q:=q+sqr(x[j]-sr);
end;
D[i]:=q/k;
sg2.Cells[i,0]:=inttostr(i);
sg2.Cells[i,1]:=format('%2.5f',[d[i]]);
a:=a+k;
b:=b+k;
end;
s:=0;
for i:=1 to m do begin
s:=s+12*(sqr(D[i]-1/12));
end;
Hi2:=d[i-1];
edit18.Text:=format('%2.5f',[Hi2]);
if (Hi2 > 0.95)and(Hi2 < 2.0) then
Label25.Caption := 'Дисперсия однородна'
else
Label25.Caption := 'Дисперсия не однородна'
end;
// Критерий Пирсона-------------------------------------------------------------
procedure TForm1.Button4Click(Sender: TObject);
var
i,m,n,j,count:integer;
k,l,maxD,y,p,Lr,x_max,x_min,Ni_count,Pi_count,Phi_count, Hi2:real;
Gz,Fs,D,Ft:array [1..100] of real;
begin
m:=strtoint(form1.edit16.Text);
n:=strtoint(form1.edit1.Text);
y:=0;
SG3.RowCount:=m+3;
SG3.Cells[0,0]:='Z(i-1)';
SG3.Cells[1,0]:='Z(i)';
SG3.Cells[2,0]:='n(i)';
SG3.Cells[3,0]:='P(i)';
SG3.Cells[4,0]:='F(Z(i))';
SG3.Cells[5,0]:='Ph(i)';
SG3.Cells[6,0]:='((Ph(i)-Pi)^2)/Phi';
maxD:=0;
x_max:=x[1];
x_min:=x[1];
for i:=1 to n do begin
if x_max < x[i] then
x_max:=x[i];
if x_min > x[i] then
x_min:=x[i];
end;
l:=(x_max-x_min)/m;
Ni_count := 0;
Pi_count := 0;
Phi_count := 0;
Hi2 := 0;
for j:=1 to m do begin
count:=0;
for i:=1 to 300 do begin
if (x[i]>=y)and(x[i]<l)and(not(j=m)) then
inc(count);
if (((x[i]>=y)and(x[i]<l))or(x[i]=5))and(j=m) then
inc(count);
end;
Gz[j]:=count;
sg3.Cells[0,j]:=format('%2.5f',[y]);
sg3.Cells[1,j]:=format('%2.5f',[l]);
sg3.Cells[2,j]:=floattostr(Gz[j]);
Ni_count := Ni_count + Gz[j];
sg3.Cells[3,j]:=floattostr(strtoint(sg3.Cells[2,j])/300);
Pi_count := Pi_count + strtofloat(sg3.Cells[3,j]);
sg3.Cells[4,j]:=floattostr(strtofloat(sg3.Cells[1,j])/5);
y:=l;
l:=l+(x_max-x_min)/m;
end;
sg3.Cells[2,j+1]:=floattostr(Ni_count);
sg3.Cells[3,j+1]:=floattostr(Pi_count);
sg3.Cells[5,1]:=sg3.Cells[4,1];
for j:=1 to m-1 do begin
sg3.Cells[5,j+1]:=floattostr(strtofloat(sg3.Cells[4,j+1]) - strtofloat(sg3.Cells[4,j]));
Phi_count := Phi_count + strtofloat(sg3.Cells[5,j]);
end;
sg3.Cells[5,j] := floattostr(1 - (Phi_count - strtofloat(sg3.Cells[5,j-1])));
sg3.Cells[5,j+2] := floattostr(Phi_count - strtofloat(sg3.Cells[5,j-1]) + strtofloat(sg3.Cells[5,j]) );
for j:=1 to m do begin
sg3.Cells[6,j]:=floattostr(sqr(strtofloat(sg3.Cells[5,j]) - strtofloat(sg3.Cells[3,j])) / strtofloat(sg3.Cells[5,j]));
Hi2 := Hi2 + strtofloat(sg3.Cells[6,j]);
end;
Edit4.Text := floattostr(Hi2);
if (Hi2 < StrToFloat(Edit2.text)) then
Label23.Caption := 'Принимается'
else
Label23.Caption := 'Отвергается'
end;
end.
Вывод
Выполнив лабораторную работу №1 «Генерация последовательностей псевдослучайных чисел (ППСЧ)» по предмету «моделирование систем» мною были изучены методы создания генераторов случайных чисел, в частности конгруэнтный мультипликативный алгоритм. Для расчётов мною была написана программа на языке Delphi 7, текст основного модуля которой представлен выше.