Elementary Probability in STELLA

MATH 315
Fall 2016
Elementary Probability in STELLA I
The basic starting point in the computer simulation of probabilistic events is a
random number generator. STELLA provides a built-in function RANDOM(0,1)
which generates numbers between 0 and 1 in a "random" fashion in such a manner
that the likelihood of a number falling within a specific subinterval of (0,1) is
proportional to the length of that subinterval. This “equal” distribution of values is
called a uniform distribution.
FLIPPING A COIN
1)
We will begin by creating a STELLA model for the repeated flipping of a fair
coin. We define two variables Heads and Tails that keep track of the total number
of heads and tails recorded so far.
Set up Heads and Tails as Stocks with Add Head and Add Tail as flows. Set r
as a converter with value Random(0,1). The initial values of Heads and Tails will be
0.
A flip of the coin will be simulated by calling the function Random(0,1). If the
number produced, r, is less than 1/2, then we'll say that the flip resulted in a Head;
otherwise, the flip will be counted as a Tail. The probability that the number r lands
in the interval [0,0.5) will be 1/2 and the probability that r lies in the interval [0.5,1)
will also be 1/2.
We use IF...THEN...ELSE logic to define the flows Add Head and Add Tail:
Add Head = IF r < 0.5 THEN 1 ELSE 0
Add Tail = IF r >= 0.5 THEN 1 ELSE 0
The STELLA diagram should look like this:
Heads
Add Head
Tails
r
Add Tail
To simulate 10 flips of the coin, set DT =1 and let Time run from 0 to 10. Create
a table with columns for r, Add Head, Add Tail, Heads, and Tails. Run the simulation
and check the values of all the entries in the table to verify that the simulation is
working correctly.
2)
Once you have verified the correctness of your model, you can delete the
columns for r, Add Head, and Add Tail so that future simulations will run more
quickly. Now simulate 1000 flips of the coin, using a Report Interval of 100 to
display the values of Heads and Tails . Do several runs of 1000 flips and record the
results.
Our intuition (and understanding of probability as a measure of relative
frequency) suggests that in 1000 flips, the number of Heads should be about equal to
the number of Tails. Does the data you observed confirm this result?
3)
It's instructive to examine the difference Heads - Tails and consider its values
during the simulation. This difference will be positive at the nth flip if we've obtained
more Heads than Tails in the first n flips and negative if we've seen more Tails than
Heads. After 1000 flips, Heads - Tails should be about equal to 0.
Our intuition tells us that Heads - Tails should always be close to 0 and should
switch from positive to negative to positive many times during the 1000 flips. Let's
investigate whether this actually happens.
Heads Leads
Heads
Add Lead
Add Head
Tails
Heads Over Tails
r
Add Tail
Construct a converter Heads Over Tails whose value is Heads - Tails and plot
the graph of this variable over time. Construct also a stock called Heads Leads that
keeps track of the number of times in which more Heads than Tails were seen. The
initial value of Heads Leads should be 0 and the value of the flow into this stock
should be 1 if Heads Over Tails is positive and 0 otherwise.
In my simulation, I had 502 Heads and 498 Tails but Heads Leads had a value
of 914. Thus in 1000 flips of a fair coin, more than 90 percent of the time I had
accumulated more Heads than Tails. Is this unusual? Here's a graph of Heads Over
Tails:
4)
Now let's simulate a biased coin. Suppose a coin was been weighted so there is
a probability of .6 of getting a Head and .4 of getting a Tail. How can we change the
STELLA model to simulate such a coin? Hint: We need only make the slightest
change in the equations for Add Head and Add Tail.
MATH 315
Spring 2013
Elementary Probability in STELLA II
1)
The RANDOM function has other options besides generating a uniform distribution
of values between 0 and 1. The command RANDOM(a,b), for example, creates a uniformly
distributed stream of numbers between a and b. STELLA also has a built-in greatest integer
function INT. Here INT(x) returns the largest integer less than or equal to x.
Thus the combination INT( RANDOM(1,3) ) will return the integer 1 or the integer 2
in a random fashion with each value occurring with probability 1/2.
Test this option out by setting up a STELLA program that uses this converter INT(
RANDOM(1,3) ) to generate 500 integers and counts the number the number of 1’s and 2’s.
(Mimic what you did to count HEADS and TAILS earlier for a fair coin.
NOTE: We could modify the initial fair coin simulation by letting r = INT(
RANDOM(1,3)) and defining Add Head = IF (r = 1) THEN 1 ELSE 0 and Add Tail = IF (r =
2) THEN 1 ELSE 0. We will, however, pursue a different idea that will be very useful when
we want to simulate the roll of a pair of fair dice.
ROLLING DICE
2)
Modify the command INT( RANDOM(1,3) ) to create a converter that produces one of
the integers 1, 2, ,3, 4, 5, 6 each with equal likelihood. Then create two such converters r and
s and a third converter t which sums r and s. Generate 120 values of t and examine the
results. What is the most frequent value of t? The least frequent values?
3)
Create a STELLA program to simulate the rolling of a pair of dice. Do 3000 rolls and
count the number of times the sum of the dice is 7 and the number of times the sum is 12. Do
the relative frequencies of these two sums reflect the probabilities you calculated in earlier
homework assignments?
4)
In carrying out the previous exercise, you most likely created two stocks, one of which
counted the number of times you rolled a 7 and one which counted occurrences of 12’s. We
would like to build a STELLA program that keep track of how many times you rolled each of
the sums 2 through 12. One way to do this is to create 11 different stocks each with its own
flow. This is a rather cumbersome procedure, but can be done if you are patient, not easily
bored and have a lot of time.
Instead, we will use STELLA’s ability to make use ARRAYS (also known as vectors or
1-dimensional matrices). We will build an array DICE of stocks with 12 components where
DICE[i] for 1 ≤ i ≤ 12 keeps track of the number of times a roll of the pair of dice resulted in
an i. Thus, for example, DICE[9] counts the number of times you rolled a 9.
We want each DICE[i] to be 0 initially and to increase by 1 (using a FLOW) whenever
a sum of i is rolled.
Start up a new STELLA file. Under the Model menu at the drop of the screen, select
Array Editor…
You will see a window that looks like this:
Replace (type over) Dimension 1 with SumOfDice and Size with 12 and click OK.
Now go back to the Model page, create a stock Pair of Dice and a flow into it called Increase
Count. Build converters r, s, and t as in Exercise 2 and make Increase Count a function of t.
Your STELLA diagram should like something like this:
We are now going to turn Pair of Dice into an array with components indexed by the variable
Sum. Double click on the stock Pair of Dice to obtain the usual window for initializing a
stock. Near the bottom of the that window you will see a bar with the option c2 highlight:
Click on the middle icon that looks like a stack of circles.
The Properties window changes to
Click on the Arrayed box, then on SumOfDice in the Available Dimensions window and
then select that with the right arrow between Available Dimensions and Chosen
Dimensions. The resulting Properties window should now look like this:
If you click on the green check mark in the lower right hand corner and then return
to the Model page, Pair of Dice will look like a stack of stocks and Inrease Count like a stack
of plates.
Now it’s time to initialize all of the sums to 0. Double click on Pair of Dice and it will
return you to the previous Properties page. Go to the bottom and click on the c2 icon. The
lower part of the Properties window will now look more familiar:
Type in 0 where it says Enter Initial Value.
Finally, we need to define the flow(s). Double click on Increase Count. Enter
If t = SumOfDice THEN 1 ELSE 0 so the lower part of Properties window looks like:
We are now ready to run the model and examine the results of simulating rolling a
fair pair of dice numerous times. Create a table to display the output. When you select
Pair_Of_Dice, STELLA provides a new window which gives you the option (which we want)
to display the all 12 associated stocks:
Click OK to display all. [If you only want to know how many times a 7 was enrolled, click on
line 7.]
Set DT =1 and run the model from 0 to 600 to simulate 600 rolls. I set the precision to
0 and had the totals reported after each 100 rolls. Here is a typical outcome:
Try several runs of the program and discuss how well the simulated outcomes compare
to what the theory predicts.
MATH 315
Spring 2013
Elementary Probability in STELLA III
EXPECTED VALUE
Consider an experiment with three possible outcomes: you win $5, $3 or $1
with respective probabilities .2, .3 and .5. What is the expected value of this
experiment?
Create a STELLA model to simulate 100 repetitions of this experiment,
keeping track of the total winnings and average win. Note that you do not have to
record the number of times you win $5 or $3 or $1. Observe that if r is a random
number between 0 and 1, then we can simulate the win on each experiment by
win = IF r < 0.2 THEN 5 ELSE IF r < .5 THEN 3 ELSE 1.
COLLECTING HEROES
Build a STELLA model to simulate the problem of collecting SuperHero cards.
There are six different Heroes in the set, and there is a probability of 1/6 that any
particular Hero will be in any particular box of cereal.
Your model should display the number of different Heroes collected as a
function of the number of boxes of cereal purchased. Run the model 20 times and
determine the average number of cereal boxes you needed to buy to collect all six
heroes.
RANDOM WALK
Construct a STELLA model for random walk in the plane. A particle starts at
(0,0) and at each successive moment, chooses at random a horizontal motion of 1 unit
to the right or left (each with probability 0.5) and independently chooses a vertical
motion of 1 unit up or down (each also with probability 0.5).
Use a Scatter Diagram to display the position of the particle for 20 successive
moments.
Run the model until the particle revisits the origin. How long does it take the
particle to return to the origin?
Note that by setting the vertical motion always to be 0, you have a STELLA
model of random walk on the line. How long does it take the particle to return to the
origin?