1/36
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Simulation
Simulation is the process of using computer to mimic a physical experiment
The process
The process: Step 1: What to Simulate
Decide which quantity you want to simulate. For example, you might decide that you want to simulate the outcomes of tosses of a coin. Each Simulated value will be Head or a Tail
The process: Step 2: Simulating one Value
Figure out how to simulate one value of the quantity you specified in Step 1. In our example, you have to figure out how to simulate the outcome of one toss of a coin. If your quantity is more complicated, you might need several lines of code to come up with one simulated value. Typically we will define a function that returns the simulated value
The process: Step 3: Number of Repitions
Decide how many times you want to simulate the quantity. You will have to repeat the simulation in Step 2 that many times.
The process: Step 4: Simulating Multiple Values
Finally, put it all together as follows:
create and empty array in which to collect the simulated values. We will call this the collection array
crete ārepetitions sequence,ā that is, a sequence whose length is the number of repetitions you specified in Step 3. For n repetitions we will always youse the sequence np.arange(n)
Create a for loop. For each element of the repetitions sequence:
Simulate one value by using the function you wrote in Step 2
Augment the collection array with this simulated value
At this point, you can use the collection array as you would any other array. You can count how many simulated values fall into a particular category, place the array in the column of a table of visualize the distribution of the simulated values, and so on
Example: number of Heads in 100 Tosses
In this example we will simulate the number if heads in 100 tosses of a coin. The histogram of our results will give us some insight into how man heads are likely
Number of Heads in 100 Tosses : Step 1: What to simulate
The quantity we are going to simulate is the number if heads in 100 tosses
Number of Heads in 100 Tosses : Step 2: Simulating one Value
We have to figure out how to make one set of 100 tosses and count the number of heads. Letās start by creating a coin
coin = make_array( āHeadsā, āTailsā)
outcome = np.random.choice (coin,100)
num_of_heads = np.nonzero(outcome == āHeadsā)
Output:
46
Since we want to do this multiple times, lets define a function that returns the simulated value of the number of heads. We can do this using the code developed in the code above:
def one_simulated_value():
outcome = np.random.choice(coin,100)
return np.non_zero(outcome == āHeadsā)
Number of Heads in 100 Tosses : Step 3: Number of Repetitions
How many repetitions we will use is up to us. The more we use, the more reliable our simulations will be, but the longer it will take to run the code.
Letās go for 20,000 repetitions. That means we are going to do the following 20,000 times:
tossing a coin 100 times and count the number of heads
Number of Heads in 100 Tosses : Step 4: Simulating Multiple Values
We are ready to create create an array of 20,000 simulated values of the number of heads in 100 tosses of a coin
num_repetitions = 20,000 #number of repetitions
heads = make_array() #empty collection array
for i in np.arange(num_repetitions): # repeat the process num_repetitons times
heads = once_simulated_value() #simulated one value sing the function defined
heads = np.append(heads, new_value) # augment the collection array wit the simulated value
Check that the array heads contains 20,000 entries, one for each repetition of the experiment.
len(heads)
Output:
20,000
To get a sense of the variability in the number of heads in 100 tosses, we can collect the results in a table and draw a histogram
Simulating a table:
simulation_results = Table().with_columns(
'Repetition', np.arange(1, num_repetitions + 1),
'Number of Heads', heads
)Simulating Histogram:
simulation_results.hist('Number of Heads', bins = np.arange(30.5, 69.6, 1))
Interpreting the Histogram
Each bin has a width 1 and is centered at each value of the the number of heads
The histogram looks symmetrical around 50 heads. The height of the bar at 50 is about 8%
Since each bar is 1 unit wide, this is the same as saying that abut 8% of the receptions produced exactly 50 heads. That is not a huge percent, but itās the largest compared to the percent at every other number of heads
the histogram also shows that in almost all of the repetitions, the number of heads in 100 tosses was somewhere between 35 and 65. Indeed, the bulk of the repetitions produced numbers of heads in the range 45 o 55.
Example: Moves in Monopoly
Each move in the game Monopoly is determined by the total number of spots of two rolls of a die.If you play Monopoly, what should you expect to get when you roll the die two times?
We can explore this by simulating the sum of two rolls of a die. We will run the simulation 10,000 times. Weāve already completed Steps 1 and 3 of out simulation process s
Step 2 is the one in which we write a function to simulate the total number of spots in one pair of rolls. First, letās plan our code, We will create an array containing number 1 through 6, draw two times from the array at random with replacement, and add the two numbers drawn
Step 2: define function
die = np. arange(1,7)
sum(np.random.choice(die, 2))
we can use the array die and the expression above to define that simulates one move in Monopoly
def one_simulation_move()
return sum(np.random.choice(die,2))
Step 4: Simulating Multiple Values
num_repetitions = 10,000
moves = make_array()
for i in np.arrange(num_repetitions):
new_moves = one_simulation_move()
moves = np. append( moves, new_move)
Visualization: Histogram
results = Table().with_columns(
'Repetition', np.arange(1, num_repetitions + 1),
'Sum of Two Rolls', moves
)
results.hist('Sum of Two Rolls', bins = np.arange(1.5, 12.6, 1))
Chance
Chance: Basics
Lowest value: 0
chance of events that is impossible
Highest value: 1 (or 100%)
chance of event that is certain
Complement: if an event has a chance of 70%, then the chance that it doesn't
100% - 70% = 30%
1- 0.7 = 0.3
Equally likely Outcomes
Assuming: all outcomes are equally likely, the chance of an event A is:
P(A) = (number of outcomes that makes A happen) / (total number of outcomes)
I have three cards: ace of hearts, king of diamonds, and a queen of spades
I shuffle them and draw two cards at random without replacement
What is the chance that I get the Queens followed by the King?
First solution:
AK, AQ, KQ, KA, QK, QA
Answer: 1/6
Alternative solution:
first draw one card, hope it is a Queen ā 1/3
if we got Queen in the fist step, proceed to the second
draw one card, hope it is a King
chance of drawing a King GIVEN that we have already drawn the Queen ā 1/2
Product Rules of probability: if these steps happen in order, then you can compute the probability:
(1/3) x (1/2) = 1/6
Multiplication Rule
chance that two events A and B both happen
= P(A happens) x P( B happens given that A has happened)
The answer is less than or equal to each of the two chances being multiplied
The more conditions you have to satisfy, the less likely you are to satisfy them all
Question: What is the chance that one of the cards I draw is a King and the other is Queen
AK, AQ, KQ, KA, QK, QA
Chance of QK or KQ: 2/6
Alternative:
KQ : 1/6
QK: 1/6
= 1/6 + 1/6 = 2/6 = 1/3
Addition Rule
If event A can happen in exactly one of two ways, then
P(A) = P(first way) + P(second way)
The answer is greater than or equal to thee chance of each individual way
Complement: At Least one Head
In 3 tosses:
any outcome except TTT
P(TTT) = (1/2) x (1/2) x (1/2) = 1/8
P(at least one head) = 1 - P(TTT) = 1 - (1/8) = 87.5%
In 10 tosses:
1 - (1/2)**10 =0.999 = 99.9%
If there is a clear answer (e.g., ānot a sixā) whose probability you know, you can useā¦
Multiplication rule
If there is no clear answer (ācould be K or Q, but then the next one would have to be Q or K) list all the distant ways your event could occur, useā¦
Addition Rule
If the sir above is log and complicated, look around the complement. If the complement is simple (e.g. the complement of āat least oneā is ānoneā), you can find its chance and subtract that from 1. USEā¦
Compliment Rule
A population has 100 people, including Riya and Jordan. We sample two people at random without replacement
a. P(both Riya and Jordan are in the sample)
b. P(neither Riya nor Jordan is in the sample)
a.
P( first Riya, then Jordan) + P(Jordan first, then Riya)
(1/100) x (1/99) + (1/100) x (1/99) =0.000202
B.
P(neither Riya and Jordan is in the sample)
Step 1: Probability the first person is neither Riya nor Jordan
There are 98 other people out of 100.
P(neither first) = 98/100
Step 2: Probability the second person is neither Riya nor Jordan
Since the first person was someone else, 99 people remain. Of those, 97 are people other than Riya and Jordan.
P(neither first | neither second) = 97/99
Multiply the probability:
(98/100) x (97/99) =0.96
When an Event Doesnāt Happen
P(an event doesnāt happen) = 1 - P(the event happens)
When All Events are Equally Likely
if you are rolling an ordinary die, a natural assumption is that all six faces are equally likely. Under this assumption, the probabilities of how one roll came out can be easily calculated as a ratio.
ex: the chat that the die shows an even number is
(number of even faces) / (number of all faces) =
#{ 2, 4, 6} / # {1,2,3,4,5,6} = 3/6
Similarly:
P( die shows a multiple of 3) =
#{3,6} / #{1,2,3,4,5,6} = 2/6
General formula:
P(an event happens) =
#{outcomes that make the event happen} / #{all outcomes}
When Two Events Must Both Happen
Scenerio:
Suppose you have a box that contains three tickets: one red, one blue, and one green. Suppose you draw two tickets at random without replacement; that is, you shuffle the three tickets, draw one, shuffle the remaining two, and draw another from those two. What is the chance you get the green ticket first, followed by the red one?
Consider:
There are six possible pairs of color: RB, BR, RG, GR, BG, GB. All of these re equally likely by the sampling scheme, and only one of them (GR) makes the event happen
P(Green first, then Red) =
#{GR} / # {RB, BR, RG, GR, BG, GB} = 1/6
Alternative: Thinking about the event in two stages
First the Green ticker has to be drawn ā that has a chance of 1/3
The Red has to be drawn next ā this happens in about ½ of those repetitions
P(green first, then red) = (1/3) x (1/2) = 1/6
The factor of ½ is called āthe conditional chance that the red ticket appears second, given that the green ticket appears first
Multiplication Rule
P(two events both happen) = P(one event happens) x P(the other event happens)
Thus, when there are two conditions - one event must happen, as well as another ā the chance is a fraction of a fraction, which is smaller than either of the two component fractions. the more conditions that have to be satisfied, the less likely they are all to be satisfied
When an Event Can Happen in Two Different Ways:
Suppose instead we want the chance that one of the two tickets is GREEN and the other is RED. This event doesnāt specify the order in which the colors must appear. So they can appear in either way
A good way to tackle problems like this is to partition the event so that it can happen in exactly one of several differnt ways. The natural partition of āone green and one rdā is: GR, RG
Each of GR and RG has chance 1/6 by the calculation above. So you can calculate the chancee of āone green and one redā by adding them up
P(one Green and one red) = P(GR) + P(RG) = 1/6 + 1/6 = 2/6
Addition Rule
P(an event happens) = P(first way it can happen) + P(second way it can happen)
hen an event can happen in one of two different ways, the chance that it happens is a sum of chances, and hence bigger than the chance of either of the individual ways
At Least One Success:
A question that sometimes arises is about the likelihood that a particular individual in the population is selected to be in the sample. To work out the chance, that individuals is called a āsuccessā and the problem is to find the chance that the sample contains a success
tossing a coin TWICE:
if you toss a coin twice, there are four equally likely outcomes: HH. HT, TH, and TT.
The chance of getting at least one head in two tosses is ¾
Another way of coming up with this answer is to work out what happens if you donāt get at least one head This is when both the tosses lands tails
P(at least one head in two tosses) = 1 - P(both tails) = 1 - ¼ = ¾
What is the probability of getting at least one six is:
For a fair six-sided die:
Probability of rolling a six: 1/6
Probability of not rolling a six: 1- 1/5 =5/6
If you roll the die twice, the probability of never getting a six is:
P(no six in 2 rolls) = (5/6)2
probability of getting at least one six is:
P( at least one six) = 1 - (5/6)2