2 - Consortium for Mathematics and its Applications

1
HiMAP Pull-Out Section: Winter 1995
TI SCIENTIFIC (TI-60) CALCULATOR PROGRAM FOR
THE 143 ALGORITHM
Use the following strokes to enter
the program:
ON/C ON/C 2nd Part 2 LRN 2nd CP STO 0 x
143 = STO 1 – RCL 1 2nd Intg = R/S RST LRN
RST
To run the program: Enter an eight-digit
number from 0 to 1. The authors entered
0.1234567. Pres R/S. Record the number. The
authors got 0.6543081. Press R/S and
continue.
ZEBRA PATTERNS
The patterns of a zebra’s stripes are like
fingerprints. No two patterns are exactly
alike! Even though each zebra is different,
the ratio of dark to light is always the same.
How could you measure the dark and light
areas of a zebra’s coat?
rom The New York Times: “Monte Carlo Simulation, named for
Monaco’s famous gambling casino, can help to represent very
complex interactions in physics, chemistry, engineering,
economics, and environmental dynamics mathematically.
Mathematicians call such a representation a “model,” and if a model
is accurate enough, it produces the same responses to manipulation as
the real thing would do. But Monte Carlo modeling contains a
dangerous flaw: if the supposedly random numbers that must be
pumped into a simulation actually form some subtle, nonrandom
pattern, the entire simulation (and its predictions) may be wrong.”
F
This danger was highlighted in a recent report…. In a December 7,
1993 article in the journal Physical Review Letters, the scientists showed
that five of the most popular computer programs for generating
streams of random numbers produced errors when they were used in
a simple mathematical model of the behavior of atoms in a magnetic
crystal.” (From “Coin-Tossing Computers Found to Show Subtle
Bias,” by Malcolm W. Browne, The New York Times, 12 January, 1993,
Section 2, page 1.)
From this New York Times article we observe that:
(a) Computer programs and mathematical algorithms generate
long sequences of “random numbers.”
(b) There are mathematical tests that detect lack of randomness.
(c) Computer-generated random numbers are used in Monte Carlo
simulations to solve complex problems.
Interesting questions suggested by these observations are
considered below:
WHAT IS A RANDOM NUMBER?
PROGRAM TO GENERATE RANDOM NUMBERS IN
BASIC (PC VERSION)
10 REM PROGRAM TO GENERATE NUMBERS
FROM 1 TO 100
20 RANDOMIZE
30 MAX = 100
40 PRINT “How many random numbers do
you wish to generate?”
50 INPUT NUM
60 FOR I = 1 TO NUM
70 REM GENERATE A NUMBER FROM
1 TO 100
80 RN = INT(RND*100+1)
90 PRINT RN,
100 NEXT I
110 END
Roughly speaking, a number is chosen at random if all permissible
numbers have equal chance of being chosen. For a sequence of
random numbers, all permissible numbers have equal chance of
occupying each position in the sequence.
WHAT IS AN EXAMPLE OF A RANDOM NUMBER GENERATOR?
A random number generator is a mathematical algorithm that
generates sequences of numbers which appear random. It can be
argued that since a computer algorithm is deterministic, computergenerated numbers are not random. For this reason, some people refer
to them as “pseudo-random numbers.” Nonetheless, computergenerated random numbers are used to solve problems in science,
business, and government.
2
HiMAP Pull-Out Section: Winter 1995
Example 1
Consider the following “143 Algorithm” which is used to generate
sequences of numbers that appear random.
PROGRAM TO GENERATE RANDOM NUMBERS IN
BASIC (APPLE VERSION)
10 REM PROGRAM TO GENERATE
NUMBERS FROM 1 TO 100
20 MAX = 100
30 PRINT “How many random numbers do
you wish to generate?”
40 INPUT NUM
50 FOR I = 1 TO NUM
60 REM GENERATE A NUMBER FROM
1 TO 100
70 RN = INT(RND*100+1)
80 PRINT RN,
90 NEXT I
100 END
Step 1. Select any eight-digit
decimal from 0 to 1.
(0.1234567 )
Step 2. Multiply by 143.
(143 x 0.1234567 = 17.65430810)
Step 3. Subtract the whole number
portion of the product.
(17.65430810 – 17 = 0.65430810)
Step 4. Record the first digit to the right of the decimal point. The digit 6
is recorded.
Step 5. Use the results of each Step 3 in Steps 2 through 5 to generate
more numbers. (As you can see, this algorithm is easy to do
with a calculator and would be easy to program.)
The following sequence of 20 numbers was generated from the above
example by recording for each cycle the first digit after the decimal
point, as directed in Step 4.
65936910526501806516
For the calculator program that the authors used, see the sidebar note
entitled “TI Calculator Program for the 143 Algorithm.”
WHAT IS AN EXAMPLE OF A MONTE CARLO SIMULATION?
Example 2
The following is a very simple example of the Monte Carlo simulation
method. We will estimate the area of the shaded triangular region in
Figure 1. First we impose a numerical grid on Figure 1, as displayed.
9
8
Next, a random number generator is used to generate sixty integers
from zero to nine. They are divided into consecutive pairs as listed
below:
7
6
5
(1, 8) (6, 6) (8, 3) (1, 2) (3, 1) (4, 3) (4, 6) (6, 5) (0, 2) (9, 6) (6, 8) (2, 7)
(0, 8) (1, 9) (2, 5) (2, 0) (1, 9) (0, 9) (0, 8) (9, 0)(7, 9) (3, 6) (7, 0) (5, 6) (7, 0)
(9, 4) (2, 6) (1, 1) (9, 7) (3, 0)
4
3
Then the coordinate pairs that are inside the triangular region are
plotted on Figure 1. To estimate the area of the triangle, we count the
number of random coordinate pairs that were plotted inside or on the
__ th
triangle. There were 10. The area is about 10
30 of the 100 square units
10
__
in the square. Thus the estimate is ( 30 )100 = 33.3 square units.
2
1
0
0
1
2
3
4
5
6
7
8
Figure 1.
WHAT IS THE AREA OF
THE SHADED TRIANGULAR REGION?
9
A test of our methodology and the randomness of our numbers
would be to compare this estimate with the actual calculated area of
1 (10 x 4) – __
1 (7 x 6) – __
1 (10 x 3) = 44
the triangle, which is 100 – __
2
2
2
square units. Perhaps if we had used a larger number of points,
improved methodology, or better random numbers, we would have
gotten a more accurate estimate. (How large a sample is needed to
achieve a desired accuracy?)
3
HiMAP Pull-Out Section: Winter 1995
WHAT ARE SOME TESTS FOR LACK OF RANDOMNESS?
A RANDOM NUMBER TABLE
To generate random numbers, choose a
number and then move consecutively
along rows or move consecutively along
columns.
There is no single best test for nonrandomness. The choice of a test
usually depends on the way the sequence looks, the experience of the
analyst, and the use to be made of the random number sequence.
First, one should run the generator (algorithm) to observe whether the
numbers “look” random. Consider the following sequence of 1000
digits generated by an algorithm.
012345678901234567890123456789… 0123456789
Let us conduct some of the tests.
Frequency of Digits Test One:
For a long sequence of numbers, each digit should appear about onetenth of the time. The above sequence obviously passes this test. Its
application gives no evidence of nonrandomness.
Frequency of Digits Test Two:
If the sequence is random, it is unlikely that each digit should appear
exactly the same number of times. The given sequence fails this test.
We conclude it is nearly impossible that the numbers were chosen at
random.
Frequency of Pairs Test:
Break the sequence into consecutive ordered pairs. For the above
sequence we get: 01, 23, 45, … . With ten different digits, there are
10 x 10 = 100 possible ordered pairs. For a long random sequence,
each possible pair should appear about the same number of times.
With 500 pairs in the above sequence, each possible pair should
appear about five times. We observe that the only pairs that appear
are 01, 23, 45, 67, and 89 each appearing 100 times. The sequence fails
this test. We conclude again that it is very, very unlikely that the
numbers were chosen at random.
The Heads-Tails Test:
This test compares the relative frequencies of ordered pairs classified
as (even, even), (even, odd), (odd, even), and (odd, odd) with the
probabilities of HH, HT, TH, and TT in the flipping of two coins.
Tabulating the above consecutive ordered pairs into these categories,
we observe that (even, odd) occurs 500 times and the other
possibilities do not occur. Again we reject the hypothesis of
randomness of the sequence.
There are numerous other tests for nonrandomness. For further
information see the publications under “References” and the You Try
Its. In order to further understand the Monte Carlo simulation
method and random numbers, do the following You Try Its.
4
HiMAP Pull-Out Section: Winter 1995
You Try It #1
Make photocopies of Figure 2 and replicate Example 2 given
above. Use a random number generator to get thirty pairs of
coordinates and plot them on the grid. Count the number of
points plotted inside the shaded triangular region. Report your
estimate of the area and compare it with the actual calculated
area. To generate random numbers, you may use such devices
as a computer program, as indicated in sidebar notes, or the
random number table given in a sidebar note, or a random
number function in a graphing calculator.
9
8
7
6
5
4
3
2
1
0
Figure 2.
0
1
2
3
4
5
6
7
8
9
You Try It #2
Perhaps a larger sample will improve accuracy. Combine your results from You Try It #1 with those of other students in
your class. Your teacher may want to organize this activity. Complete the following steps: (a) Make a list from the largest
to the smallest of all of the reported estimates of the area. (b) Report the largest, smallest, range, median, and average. (c)
Combine all Monte Carlo runs in the following way: Add the total number of pairs inside the triangular region. Add the
total number of all points generated. The estimated area from the combined runs = 100 x (total number of points in the
triangular region) ÷ (total number of points). (d) Write a report of the above statistics. Did the larger simulation yield
more accurate results? Why? What could be done to improve the methodology?
You Try It #3
Sam takes chances and doesn’t study for tests. Suppose he is in a mathematics class and a true-false test with ten
questions is to be given. He doesn’t know the answers and he needs seven out of ten correct to pass the test. What are his
chances of passing the test?
Use the Monte Carlo method to model Sam’s chances. Use random coin flipping or random numbers to simulate taking
the test forty times. Record the number of tests with seven or more correct. An estimate of Sam’s chances of passing the
test is the number of tests with seven or more correct divided by the total number of tests. Write a report of the problem,
and your methodology, observations, and conclusions. To get a better estimate, average all simulations done by members
of your class. From probability calculations, his chances of passing the test are .171875.
In a course with ten such tests, what are his chances of passing the course? Answer, P = .00003. Do you think that Sam
knows this? What do you think of an educational system that finds an excuse to give Sam a passing grade in this course?
5
HiMAP Pull-Out Section: Winter 1995
You Try It #4
Write a computer or graphing calculator program to replicate Example 2
above. Use Figure 3 to do your planning. Describe the triangular region
in terms of inequalities. Use a random number function to generate and
test 2000 points. Count those that pass the test of being in the region.
Report your estimate and compare it with the actual area. Report your
program and results, and write a discussion of your program and
methodology. Why is this an improvement over Example 2?
10
9
8
7
6
5
4
3
2
1
0
Figure 3.
1
2
3
4
5
6
7
8
9
10
You Try It #5
Write a Monte Carlo simulation program to approximate the area under one arc of y = sin x. Use the sin function in radian
mode, a rectangle of height one and length π containing the arc, the resident random number function, and 2000 random
points (x, y) with 0 ≤ y ≤ 1 and 0 ≤ x ≤ π. Report your methodology, program, and results. Compare your answer with the
actual area.
You Try It #6
Design a “180 Algorithm” modeled after the “143 Algorithm” given above. Report your steps and a sequence of 20
generated numbers. Describe the results. Does this algorithm generate random numbers? Why?
You Try It #7
Use different tests to detect nonrandomness in the following array of numbers. Report your methods and results.
0936142785
3614278509
1427850936
2785093614
8509361427
6
HiMAP Pull-Out Section: Winter 1995
Some Answers to the You Try Its
4
The following is a generic program for estimating
the area of the triangle in Figure 3. Revision may
be required to run it on your computer.
Let N = 0
For I = 1 to 2000
Call RND x
(for 0 ≤ x ≤ 1)
Let x = x*10
Call RND y
(for 0 ≤ y ≤ 1)
Let y = y*10
If y < (4/10)x + 4 then 150
If y > (6/7)x + 4 then 150
If y > (-10/2)x + 100/3 then 150
Let N = N + 1
150 Next I
Print “APPROXIMATE AREA =”
Print (N/2000)*100
END
5
The area A under y = sin x for 0 ≤ x ≤ π can be
calculated as
A = ∫ π0 sin x dx = –cos x  π0 = –cos π – (–cos 0) = –(–1) – (–1) = 2.0
How accurate was your program?
7
When you divide the array into columns, you
observe that every other column contains the
same numbers. Thus the sequence is not random.
The array does pass several other tests for lack of
randomness.