'============================================================================ ' G.H. George 1992 FEB 27 ' CI.BAS last modified 2003 SEP 04 ' An illustration of confidence intervals. '============================================================================ ' ' Procedures: ' Intro - Print introductory instructions on screen. ' Title - Print the header for a new page on screen. ' Interval - Construct and print CI from current random sample on screen. ' Update - Print updated info about proportion of CIs so far capturing mu. ' Summary - Print summary information and exit from program. DECLARE SUB Intro () DECLARE SUB Summary (misses AS INTEGER) DECLARE SUB Interval (misses AS INTEGER) DECLARE SUB Title () DECLARE SUB Update (simulation AS INTEGER, misses AS INTEGER) DECLARE SUB ClearBox (left AS INTEGER, right AS INTEGER, top AS INTEGER, bottom AS INTEGER) DECLARE SUB Number (xbar AS DOUBLE) ' Variables: DIM misses AS INTEGER ' # confidence intervals not capturing mu DIM simulation AS INTEGER ' loop counter = # random samples so far SCREEN 0: WIDTH 80: COLOR 7, 0: CLS RANDOMIZE 3 CALL Intro CALL Title LET misses = 0 FOR simulation = 1 TO 100 CALL Interval(misses) CALL Update(simulation, misses) NEXT simulation CALL Summary(misses) END SUB ClearBox (left AS INTEGER, right AS INTEGER, top AS INTEGER, bottom AS INTEGER) '---------------------------------------------------------------------------- ' Clears rectangular part of the screen, from (top,left) to (bottom, right). '---------------------------------------------------------------------------- ' No procedures. ' Local variables: DIM valid AS INTEGER ' logical variable = true iff box in bounds DIM thisline AS INTEGER ' loop counter = current line number LET valid = -1 IF left < 1 OR right > 79 THEN LET valid = 0 IF top < 1 OR bottom > 24 THEN LET valid = 0 IF left > right OR top > bottom THEN LET valid = 0 IF valid THEN LOCATE top, left FOR thisline = top TO bottom LOCATE thisline, left PRINT STRING$(right - left + 1, 32) NEXT thisline END IF END SUB SUB Interval (misses AS INTEGER) '---------------------------------------------------------------------------- ' Construct and print confidence interval from current sample on screen. '---------------------------------------------------------------------------- ' Procedure: ' Number(xbar) - returns random value, xbar, from N(30, 5^2) ' Local variables: DIM lower AS INTEGER, upper AS INTEGER ' Boundaries of confidence interval DIM xbar AS DOUBLE ' mean of random sample CALL Number(xbar) ' <-- fetch a random number N(30,5^2) lower = CINT(xbar - 10) ' 95% CI ==> half-width = 2 st. errors upper = CINT(xbar + 10) IF lower > 30 OR upper < 30 THEN ' Confidence interval misses mu COLOR 12, 0 ' red on black; CHR$(219) = filled block PRINT TAB(lower); STRING$((upper - lower + 1), 219); LET misses = misses + 1 LOCATE , 30: PRINT "*"; COLOR 7, 0 ELSE ' Confidence interval captures mu COLOR 10, 0 ' green on black PRINT TAB(lower); STRING$((upper - lower + 1), 219); COLOR 7, 0 LOCATE , 30: PRINT CHR$(179); END IF PRINT END SUB SUB Intro '---------------------------------------------------------------------------- ' Print introductory instructions on screen. '---------------------------------------------------------------------------- ' No procedures. ' Local variable: ' dummy$ - character of key pressed by the user (not needed anywhere else) CLS PRINT " Simulation for confidence intervals." PRINT "The random variable X has a normal distribution" PRINT "100 Random samples are taken and the observed sample mean from each one " PRINT "is used to estimate the population mean "; CHR$(230); "." PRINT "A "; COLOR 10, 0 PRINT "green confidence interval"; COLOR 7, 0 PRINT " captures the true value of "; CHR$(230); "." PRINT "A "; COLOR 12, 0 PRINT "red confidence interval"; COLOR 7, 0 PRINT " misses the true value of "; CHR$(230); "." PRINT PRINT "When ready, press any key." dummy$ = INPUT$(1) LOCATE 2, 1 CLS END SUB SUB Number (xbar AS DOUBLE) '---------------------------------------------------------------------------- ' Returns random value, xbar, from N(30, 5^2). '---------------------------------------------------------------------------- ' Procedure: ' Number(xbar) - returns random value, xbar, from N(30, 5^2) ' Local variables: DIM sum AS DOUBLE ' accumulator ' i% ' loop counter LET sum = 0 FOR i% = 1 TO 300 ' <-- making use of X ~ U(0,1) LET x = RND ' -> sum(X) ~ N(n/2, n/12). With n = 300, LET sum = sum + x ' sum(X) ~ N(150, 25), (Cent.limit thm.) NEXT i% LET xbar = sum - 120 ' and we require Xbar ~ N(30,5^2). END SUB SUB Summary (misses AS INTEGER) '---------------------------------------------------------------------------- ' Print summary information and exit from program. '---------------------------------------------------------------------------- ' Procedure: ' ClearBox - Clears box on screen, from (top,left) to (bottom, right). ' No local variables. CALL ClearBox(45, 79, 19, 24) LOCATE 19, 45: PRINT "In 100 samples the 95% C.I. based" LOCATE 20, 45: PRINT "on the sample mean failed to" LOCATE 21, 45: PRINT "capture the true population mean on" LOCATE 22, 52: PRINT misses; "occasions." LOCATE 24, 1: PRINT "Press any key to exit. "; DO WHILE INKEY$ = "": LOOP END SUB SUB Title '---------------------------------------------------------------------------- ' Print the header for a new page on screen. '---------------------------------------------------------------------------- ' No procedures or local variables. LOCATE 1, 10 PRINT "Simulation for Confidence Intervals; N(30,25)" LOCATE 22, 30: PRINT CHR$(230): LOCATE 2, 1 END SUB SUB Update (simulation AS INTEGER, misses AS INTEGER) '---------------------------------------------------------------------------- ' Print updated information on screen about proportion of CIs capturing mu. '---------------------------------------------------------------------------- ' Procedure: ' Title - Print the header for a new page on screen. ' ClearBox - Clears box on screen, from (top,left) to (bottom, right). ' Local variables: DIM thisline AS INTEGER ' loop counter = current line number ' dummy$ key pressed by the user CALL Title CALL ClearBox(50, 78, 4, 8) LOCATE 4, 50: PRINT "# samples: "; PRINT USING "###"; simulation LOCATE 5, 50: PRINT " # misses: "; PRINT USING "###"; misses LOCATE 6, 50: PRINT "P(type I) "; CHR$(247); PRINT USING " #.####"; (misses / simulation) LET thisline = simulation MOD 20 + 2 IF thisline = 20 THEN LOCATE 23, 1 PRINT "Press any key:"; dummy$ = INPUT$(1) LOCATE 23, 1 PRINT SPACE$(15) END IF CALL ClearBox(1, 55, thisline, thisline) LOCATE thisline, 1 END SUB