home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Current Shareware 1994 January
/
SHAR194.ISO
/
engineer
/
ksprob21.zip
/
KSMISC.EXE
/
KSGOF2.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-05-19
|
5KB
|
160 lines
program ksgof2; {simulates the chi-square goodness of fit test
for a Poisson distribution with µ estimated
from the sample}
{X is a random variable with µ. A sample of size n is taken from
the distribution of X. This program simulates the chi-square
goodness of fit test for
H0: The distribution of X is Poisson
against H1: The distribution of X is not Poisson.
The input file must be an ascii file with the following
information in successive lines:
1. The sample mean.
2. The number of cells.
3. one line for each cell, containing
for all but the last cell, the largest value of x included
in the cell and the observed cell count.
for the last cell, the smallest value of x included in the
cell and the observed cell count.
When there are two numbers in one line, separate them by spaces.
This cell structure allows adjacent values of x to be combined
into one cell. Since the sample mean cannot be computed from
cell information, it must be supplied separately.
ksgof2.dat is a sample data set.
The results written to screen are:
the number of tables simulated with chi-square greater than
the chi-square for the observed data.
the number of tables simulated.
the upper and lower limits of a 95% confidence interval for
the pvalue of the chi-square goodness of fit test.
Program limits:
number of cells <= 100
sample size n <= 32767
number of simulated samples <= 2147483647
}
{$B-,D+,E+,F+,L+,N+,R-,V-}
uses dos,crt,ksutils;
type
real = double;
var
origobs,simobs : array [1..100] of word;
origexp,simexp : array [1..100] of real;
maxx : array [1..100] of word;
n,ncells : word;
numlarger,ntosim : longint;
origchisq,simchisq,critz,origxbar,simxbar : real;
ifiln : string[80];
ifil : text;
procedure getdata;
var
i : word;
begin
clrscr;
gotoxy(1,1); write('input file: ');
readln(ifiln);
assign(ifil,ifiln); {$I-} reset(ifil); {$I+}
if ioresult <> 0 then halt;
readln(ifil,origxbar);
readln(ifil,ncells);
if ncells > 100 then begin close(ifil); halt end;
for i := 1 to ncells do readln(ifil,maxx[i],origobs[i]);
close(ifil)
end; {getdata}
procedure getstarted;
var
i : word;
begin
clrscr;
for i := 1 to 100 do origobs[i] := 0;
for i := 1 to 100 do simobs[i] := 0;
for i := 1 to 100 do origexp[i] := 0.0;
for i := 1 to 100 do simexp[i] := 0.0;
for i := 1 to 100 do maxx[i] := 0;
getdata;
n := 0;
for i := 1 to ncells do n := n + origobs[i];
origexp[1] := n * poiscdf(maxx[1],origxbar);
for i := 2 to ncells - 1 do
origexp[i] := n * (poiscdf(maxx[i],origxbar) - poiscdf(maxx[i-1],origxbar));
origexp[ncells] := n * (1.0 - poiscdf(maxx[ncells] - 1,origxbar));
origchisq := 0.0;
for i := 1 to ncells do
origchisq := origchisq + sqr(origobs[i] - origexp[i]) / origexp[i];
numlarger := 0;
critz := zinv(1.0 - 0.025,10);
clrscr;
gotoxy(1,1); write('number of tables to simulate: ');
readln(ntosim);
gotoxy(1,1); write(' ':40);
end; {getstarted}
procedure generatesamples;
var
i,j : word;
k : longint;
ival : integer;
ranum,cs,cumprob,halfwidth,ll,ul,pval,tmpr1,tmpr2,xbar : real;
begin
for k := 1 to ntosim do
begin
for i := 1 to ncells do simobs[i] := 0;
simxbar := 0.0;
for i := 1 to n do
repeat
pval := randnum1;
ival := poisinv(pval,origxbar,tmpr1,tmpr2);
if ival <> 32767 then
begin
inc(ival);
simxbar := simxbar + ival;
j := 0;
repeat inc(j) until (ival <= maxx[j]) or (j = ncells);
inc(simobs[j])
end
until ival <> 32767;
simxbar := simxbar / n;
simexp[1] := n * poiscdf(maxx[1],simxbar);
for i := 2 to ncells - 1 do
simexp[i] := n * (poiscdf(maxx[i],simxbar) - poiscdf(maxx[i-1],simxbar));
simexp[ncells] := n * (1.0 - poiscdf(maxx[ncells] - 1,simxbar));
simchisq := 0.0;
for i := 1 to ncells do
simchisq := simchisq + sqr(simobs[i] - simexp[i]) / simexp[i];
if simchisq >= origchisq then inc(numlarger);
gotoxy(1,1); write(' ':14);
gotoxy(1,1); write(numlarger, ' ',k)
end;
halfwidth := sqrt((numlarger/k)*((k - numlarger)/k)/k)*critz;
ul := numlarger / k + halfwidth;
ll := numlarger / k - halfwidth;
clrscr; gotoxy(1,1); write(numlarger, ' ',k,' ',ll:3:10,' ',ul:3:10)
end; {generatesamples}
begin {main}
clrscr;
initrand1;
getstarted;
generatesamples
end.