home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / engineer / ksprob21.zip / KSMISC.EXE / KSGOF1.PAS < prev    next >
Pascal/Delphi Source File  |  1993-05-19  |  4KB  |  137 lines

  1. program ksgof1; {simulates the chi-square goodness of fit test
  2.                  when expected frequencies are completely
  3.                  determined by sample size and the null hypothesis}
  4.  
  5. {This program simulates the chi-square goodness of fit test for
  6.          H0: The observed data comes from the distribution
  7.              specified by the expected frequencies.
  8.  against H1: The observed data comes from some other distribution.
  9.  
  10.   The input file must contain the number of cells on the first
  11.   line.
  12.  
  13.   Succeeding lines, one per cell, must contain the observed
  14.   count and the cumulative expected frequency for that cell.
  15.  
  16.   Note that the cumulative expected frequency for the last cell
  17.   must be 1.0.
  18.  
  19.   ksgof1.dat is a sample data set.
  20.  
  21.   The results written to screen are:
  22.  
  23.     the number of tables simulated with chi-square greater than
  24.     the chi-square for the observed data.
  25.  
  26.     the number of tables simulated.
  27.  
  28.     the upper and lower limits of a 95% confidence interval for
  29.     the pvalue of the chi-square goodness of fit test.
  30.  
  31.  Program limits:
  32.  
  33.    number of cells <= 100
  34.    sample size n <= 32767
  35.    number of simulated samples <= 2147483647
  36.  
  37. }
  38.  
  39. {$B-,D+,E+,F+,L+,N+,R-,V-}
  40.  
  41. uses dos,crt,ksutils;
  42.  
  43. type
  44.   real = double;
  45.  
  46. var
  47.   observed : array [1..100] of word;
  48.   expected,cumef : array [1..100] of real;
  49.   n,ncells : word;
  50.   numlarger,ntosim : longint;
  51.   obschisq,critz : real;
  52.   ifiln : string[80];
  53.   ifil : text;
  54.  
  55. procedure findchisq(var chi : real);
  56. var
  57.   chis : real;
  58.   i : word;
  59. begin
  60.   chis := 0.0;
  61.   for i := 1 to ncells do
  62.     chis := chis + sqr(observed[i] - expected[i]) / expected[i];
  63.   chi := chis
  64. end; {findchisq}
  65.  
  66. procedure getdata;
  67. var
  68.   i : word;
  69. begin
  70.   clrscr;
  71.   gotoxy(1,1); write('input file: ');
  72.   readln(ifiln);
  73.   assign(ifil,ifiln); {$I-} reset(ifil); {$I+}
  74.   if ioresult <> 0 then halt;
  75.   readln(ifil,ncells);
  76.   if ncells > 100 then begin close(ifil); halt end;
  77.   for i := 1 to ncells do readln(ifil,observed[i],cumef[i]);
  78.   close(ifil)
  79. end; {getdata}
  80.  
  81. procedure getstarted;
  82. var
  83.   i : word;
  84. begin
  85.   numlarger := 0;
  86.   for i := 1 to 100 do observed[i] := 0;
  87.   for i := 1 to 100 do expected[i] := 0;
  88.   for i := 1 to 100 do cumef[i] := 0;
  89.   ifiln := '';
  90.   critz := zinv(1.0 - 0.025,10);
  91.   getdata;
  92.   clrscr;
  93.   gotoxy(1,1); write('number of tables to simulate: ');
  94.   read(ntosim);
  95.   gotoxy(1,1); write(' ':40);
  96.   n := 0;
  97.   for i := 1 to ncells do n := n + observed[i];
  98.   expected[1] := n * cumef[1];
  99.   for i := 2 to ncells do expected[i] := (cumef[i] - cumef[i-1]) * n;
  100.   findchisq(obschisq)
  101. end; {getstarted}
  102.  
  103. procedure generatesamples;
  104. var
  105.   i,cellid : word;
  106.   k : longint;
  107.   ranum,cs,cumprob,halfwidth,ll,ul : real;
  108. begin
  109.   for k := 1 to ntosim do
  110.   begin
  111.     for i := 1 to ncells do observed[i] := 0;
  112.     for i := 1 to n do
  113.     begin
  114.       ranum := randnum1;
  115.       cumprob := 0.0;
  116.       cellid := 0;
  117.       repeat inc(cellid) until (ranum <= cumef[cellid]) or (cellid = ncells);
  118.       inc(observed[cellid]);
  119.     end;
  120.     findchisq(cs);
  121.     if cs >= obschisq then inc(numlarger);
  122.     gotoxy(1,1); write(' ':14);
  123.     gotoxy(1,1); write(numlarger, ' ',k)
  124.   end;
  125.   halfwidth := sqrt((numlarger/k)*((k - numlarger)/k)/k)*critz;
  126.   ul := numlarger / k + halfwidth;
  127.   ll := numlarger / k - halfwidth;
  128.   clrscr; gotoxy(1,1); write(numlarger, ' ',k,' ',ll:3:10,' ',ul:3:10)
  129. end; {generatesamples}
  130.  
  131. begin {main}
  132.   clrscr;
  133.   initrand1;
  134.   getstarted;
  135.   generatesamples;
  136. end.
  137.