'\begin{verbatim} 'declarations of SUBs DECLARE FUNCTION PoissFac! (l!, n!) DECLARE SUB CenterPrint (Text$) 'This program simulates averages of binomials as function of the sample size ON ERROR GOTO ErrTrap 'make sure graphics card is there 'prepare screen CLS SCREEN 9 LOCATE 1, 1 CenterPrint "Limit Theorems for Binomial Distribution" 'get parameters INPUT "Binomial Bin(n,p) with number of trials n="; n INPUT "Probability of success p="; p CLS xscale = n IF p * (1 - p) * n = 0 THEN PRINT "Are you joking?": END 'no trials requested yscale = 1' / SQR(n)'would work better WINDOW (0, 0)-(2 * xscale, yscale): VIEW (150, 100)-(600, 300) LINE (0, 0)-(xscale, yscale), 10, B: LINE (xscale, yscale)-(2 * xscale, 0), 11, B hst = INT(n) IF hst > 30 THEN hst = 30 DIM Empir(2 * hst + 1) GOSUB makeGrid LOCATE 5, 1 PRINT "Empirical probabilities for Bin(n="; n; ",p="; p; ")" LOCATE 2, 50 PRINT "Histrograms" COLOR 2 LOCATE 3, 52 PRINT "actual" LOCATE 4, 52 COLOR 4 PRINT "Normal" LOCATE 5, 52 COLOR 6 PRINT "Poisson" LOCATE 12, 1 COLOR 2 PRINT "Press:" PRINT " 'S' to stop" PRINT " 'H' for histogram" PRINT " 'N' for Normal" PRINT " 'P' for Poisson" PRINT " 'T' for Ticking" 'PRINT "' 'A for Alarm" DO 'process user key K$ = RIGHT$(K$, 5) + INKEY$ K$ = UCASE$(K$) IF INSTR(K$, "S") THEN EXIT DO IF INSTR(K$, "H") THEN GOSUB Histogram IF INSTR(K$, "N") THEN GOSUB NormalPlot IF INSTR(K$, "P") THEN GOSUB PoissonPlot IF INSTR(K$, "T") THEN K$ = "": noise = NOT noise ' end of user key processing Path = Path + 1 LOCATE 10, 1 PRINT "Path #"; Path x = 0 Y = 0 col = 15 * RND(1) IF noise THEN SOUND 100, .1 'go through the single sample of 1 to n FOR T = 1 TO n X0 = x y0 = Y x = x + 1 Y = Y - (RND(1) < p) '- p IF T > 5 THEN LINE (X0, y0 / T)-(x, Y / T), col NEXT T 'record final output for histogram Empir(Y / n * hst) = Empir(hst * Y / n) + 1 IF noise THEN SOUND 100 + 1000 * Y / n, .1 LOOP END ErrTrap: 'if there are errors then quit BEEP SELECT CASE ERR CASE 6 LOCATE 23, 1 PRINT SPACE$(70); LOCATE 23, 1 PRINT "Too large numbers to cumpute probabilities!" j = n RESUME NEXT CASE ELSE CLS PRINT "This error requires graphics card VGA" PRINT "Error running program" PRINT "Press any key ...'" WHILE INKEY$ = "" WEND END END SELECT Histogram: 'make new histograms 'clear display LINE (xscale, yscale)-(2 * xscale, 0), 0, BF LINE (xscale, yscale)-(2 * xscale, 0), 11, B K$ = "" FOR j = 0 TO hst LINE (xscale, yscale * (j - .5) / hst)-(xscale * (1 + Empir(j) / Path), yscale * (j + .5) / hst), 2, BF NEXT j GOSUB makeGrid RETURN makeGrid: 'RETURN FOR j = 1 TO hst LINE (xscale, yscale * j / hst)-(2 * xscale, yscale * j / hst), 2 NEXT j LINE (0, yscale * p)-(2 * xscale, yscale * p), 15 RETURN NormalPlot: 'normal e^-(x-np)/sqr(npq) 'CLEAR K$ K$ = "" res = 100 FOR j = 0 TO res f = 1 / hst * SQR(n / 6.28 / p / (1 - p)) * EXP(-((j) / res - p) ^ 2 * n / (p * (1 - p)) / 2) 'f = EXP(-((j + .5) / hst) ^ 2 * SQR(n / p / (1 - p)) / 2) LINE (xscale, yscale * (j - .5) / res)-(xscale * (1 + f), yscale * (j + .5) / res), 4, B NEXT j RETURN PoissonPlot: 'CLEAR K$ K$ = "" la = n * p f = 0 FOR j = 0 TO n 'Poisson probability f = n / hst * EXP(-la) * PoissFac(la, j) LINE (xscale, yscale * (j - .5) / n)-(xscale * (1 + f), yscale * ((j + .5) / n)), 6, B NEXT j RETURN SUB CenterPrint (Text$) ' Print text centered in 80 column screen offset = 41 - LEN(Text$) \ 2 IF offset < 1 THEN offset = 1 LOCATE , offset PRINT Text$ END SUB FUNCTION PoissFac (la, n) 'compute la^n/n! f = 1 FOR j = 1 TO n f = la * f / j NEXT j PoissFac = f '\end{verbatim} END FUNCTION