Random Walks

See all articles from SYNC v2 n6
Author(s): Eric Chandler
Publication: SYNC
Pub Details: SYNC v2 n6 (Volume 2, Issue 6)
Date: November/December 1982
Pages: 100-101
screen display: START: RANDOM WALKS, PROBABILITY OF RIGHT(>) TURN=0.7, with the sieve of doors and the column scale
screen display: START: RANDOM WALKS, PROBABILITY OF RIGHT(>) TURN=0.7, with the sieve of doors and the column scale

Most students of statistics are familiar with the “Random Walk” problem: A person passes through a series of doors and after each he is presented with the choice of turning right (with fixed probability P) or turning left, and thereafter encounters another door. If, say, there are 9 levels of doors, the possible outcomes may be characterized by the number of times X he turns right, from X=0 to X=8. We can find the probability of each of these outcomes by the formula

         ( 8 )  X      8-X
Pr(X) =  (   ) P (1-P)
         ( X )

and the resulting distribution is called “binomial.”

The most commonly seen of these distributions result when P=½: The so-called “bell shaped” carve which can be approximated by the normal function

          1         -((X-μ)/σ)²/2
Y = ──────────── e
      σ √(2π)

where the mean μ=P and the standard deviation

σ = √P (1-P).

An exceedingly clever device called the “Hexstat” illustrates this distribution. A large number of small steel balls in a transparent casing roll down through a cascade of bifurcated channels and collect in 9 columns in the familiar bell shape.

A program “Random Walks” for the ZX81 duplicates the Hexstat. It is an eye-catching, pretty display and very effective as an instructional device. Over a period of 15 minutes or so a series of “balls” falls down through a cascade or sieve to form 9 columns whose heights are proportional to the respective probabilities of the outcomes. When the tallest column reaches the bottom of the sieve the process stops. By entering different values of P at the beginning of the program, distributions skewed right or left may be produced. A typical display at this point is shown in Figure 1.

After a short pause (whose duration may be adjusted at line 114) the program continues. The top part of the display is eaten up by a little gobbler who finishes it off with a humorous embellishment. The total number of balls involved is calculated and the expected number of balls in each outcome is computed by the binomial formula and displayed in a contrasting histogram alongside the actual outcomes.

To execute the program after it is entered or loaded, hit RUN, and the fixed part of the display will be drawn. The program then asks for the probability of a right turn (your right) and a number between 0 and 1 inclusive is entered. The rest of the program runs without interruption.

   1 REM "RANDOM WALKS".LINE 3 USES GRAPHICS E.
2 FOR N=0 TO 11
3 PRINT AT 21-N,0;"▒";2*(N+1)
4 NEXT N
5 PRINT AT 0,10;"START: RANDOM"
6 FOR N=44 TO 55
7 PLOT N,41
8 NEXT N
9 PRINT AT 2,25;"WALKS"
10 FOR N=50 TO 59
11 PLOT N,37
12 NEXT N
13 FOR N=1 TO 9
14 LET Y=43-2*N
16 LET X=28-3*N
18 FOR M=1 TO N+1
20 FOR T=1 TO 3
22 LET X=X+1
24 UNPLOT X,Y
26 NEXT T
28 FOR T=1 TO 3
30 LET X=X+1
32 PLOT X,Y
34 NEXT T
36 NEXT M
38 NEXT N
40 FOR N=0 TO 8
42 PRINT AT 21,5+3*N;N
44 NEXT N
45 PRINT AT 20,30;"X"
46 PRINT AT 1,0;"ENTER PROB."
48 PRINT AT 2,0;"OF RIGHT(>)"
50 PRINT AT 3,0;"TURN"
52 INPUT P
54 PRINT AT 1,0;"PROBABILITY"
56 PRINT AT 2,0;"OF RIGHT(>)"
58 PRINT AT 3,0;"TURN=";P
59 LET PR=1-P
60 DIM A(9)
61 FOR N=1 TO 9
62 LET A(N)=24
64 NEXT N
65 RAND 0
66 LET X=33
67 LET Y=42
68 PLOT X,Y
69 GOTO 84
70 LET S=SGN (RND-PR)
72 IF S=0 THEN GOTO 70
74 FOR N=1 TO 3
76 UNPLOT X,Y
78 LET X=X+S
80 PLOT X,Y
82 NEXT N
84 FOR N=1 TO 2
86 UNPLOT X,Y
88 LET Y=Y-1
90 PLOT X,Y
92 NEXT N
94 IF Y=24 THEN GOTO 98
96 GOTO 70
98 FOR N=1 TO A((X-3)/6)
100 UNPLOT X,Y
102 LET Y=Y-1
104 PLOT X,Y
106 NEXT N
108 LET A((X-3)/6)=A((X-3)/6)-1
110 IF A((X-3)/6)=0 THEN GOTO 114
112 GOTO 66
114 FOR N=1 TO 100
116 NEXT N
120 FOR L=0 TO 8 STEP 2
122 FOR C=0 TO 29
123 REM NEXT 2 LINES USE GRAPHICS:INV.VID.P,7 AND SPACE,6.
124 PRINT AT L,C;" [graphics]"
126 PRINT AT L+1,C;" [graphics]"
128 FOR N=1 TO 3
130 NEXT N
135 REM NEXT LINE USES GRAPHICS T.
140 PRINT AT L+1,C+2;"▒"
142 NEXT C
144 PRINT AT L,30;" "
146 PRINT AT L+1,30;" "
148 NEXT L
149 PRINT AT 7,18;"[graphics]"
150 PRINT AT 8,18;"[graphics]"
151 FOR N=1 TO 50
152 NEXT N
153 PRINT AT 8,19;" "
154 PRINT AT 6,22;"[graphics]"
155 PRINT AT 7,22;"[graphics]"
156 PRINT AT 8,22;"[graphics]"
162 LET A=0
164 FOR M=1 TO 9
168 LET A=24-A(M)+A
172 NEXT M
176 PRINT AT 0,0;"THERE WERE ";A;" BALLS IN ALL."
178 PRINT "FOR EACH OUTCOME X WE COMPUTE"
180 PRINT "THE EXPECTED NUMBER OF BALLS "
182 PRINT "E(X) BY THE BINOMIAL FORMULA AND"
183 REM NEXT LINE USES GRAPHICS A.
184 PRINT "GRAPH WITH THE HISTOGRAM ▒."
188 FOR X=0 TO 8
190 LET N=8
192 GOSUB 300
194 LET EF=F
196 LET N=X
198 GOSUB 300
200 LET XF=F
202 LET N=8-X
204 GOSUB 300
206 LET QF=F
208 LET E=INT (A*(EF/(XF*QF))*(P**X)*((1-P)**(8-X))+.5)
210 IF E=0 THEN GOTO 236
212 IF E=1 THEN GOTO 234
214 IF E/2<>INT (E/2) THEN GOTO 224
216 FOR Y=21 TO 22-E/2 STEP -1
217 REM NEXT LINE USES GRAPHICS A
218 PRINT AT Y,3*(X+1);"▒"
220 NEXT Y
222 GOTO 236
224 FOR Y=21 TO 22-(E-1)/2 STEP -1
225 REM NEXT LINE USES GRAPHICS A.
226 PRINT AT Y,3*(X+1);"▒"
228 NEXT Y
230 PRINT AT 22-(E+1)/2,3*(X+1);"▒"
232 GOTO 236
233 REM NEXT LINE USES GRAPHICS D
234 PRINT AT 21,3*(X+1);"▒"
236 NEXT X
286 STOP
300 LET F=1
301 IF N=0 THEN RETURN
302 FOR M=1 TO N
303 LET F=F*M
304 NEXT M
305 RETURN
Figure 1. Screen display: THERE WERE 73 BALLS IN ALL. FOR EACH OUTCOME X WE COMPUTE THE EXPECTED NUMBER OF BALLS E(X) BY THE BINOMIAL FORMULA AND GRAPH WITH THE HISTOGRAM, with the gobbler and "URP!" and the two histograms
Figure 1. Screen display: THERE WERE 73 BALLS IN ALL. FOR EACH OUTCOME X WE COMPUTE THE EXPECTED NUMBER OF BALLS E(X) BY THE BINOMIAL FORMULA AND GRAPH WITH THE HISTOGRAM, with the gobbler and "URP!" and the two histograms

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.