toss1 <-rbernoulli(1, 0.7)Warning: `rbernoulli()` was deprecated in purrr 1.0.0.
toss2 <-rbernoulli(1, 0.7)
toss3 <-rbernoulli(1, 0.7)Intro to simulations
In this lab, you will actually conduct the simulations that you wrote psuedocode for in 2.1 Group Work.
Recall our random experiment of flipping a weighted coin 3 times, where each toss has a 70% probability of landing on heads. This could be used to model the # of shots a 70% free throw shooter will make if they shoot 3 free throws (we are considering the shots to be independent).
We are interested in the following three random variables:
Simulate 1 run of this experiment by creating three objects toss1, toss2, and toss3. You can make use of the function rbernoulli(), which takes two arguments:
toss1 <-rbernoulli(1, 0.7)Warning: `rbernoulli()` was deprecated in purrr 1.0.0.
toss2 <-rbernoulli(1, 0.7)
toss3 <-rbernoulli(1, 0.7)Use the toss1, toss2, toss3 objects to create your random variables X, Y, and Z.
X <- toss1+toss2+toss3
Y <- toss2
Z <- X-(3-X)Adapt your code from Tasks 1 and 2 to simulate 100,000 values of X, Y, and Z
toss1 <-rbernoulli(100000, 0.7)
toss2 <-rbernoulli(100000, 0.7)
toss3 <-rbernoulli(100000, 0.7)
X <- toss1+toss2+toss3
Y <- toss2
Z <- X-(3-X)The following code combines the vectors into a data frame, and creates summary tables and a visualization for the distribution of X. Remove eval: false so the output will be in your html.
# create data frame
sims <- data.frame(toss1, toss2, toss3, X, Y, Z)
# check dimensions - should be 100000 x 6
dim(sims)
# create frequency table
table(sims$X)
# create relative frequency table
proportions(table(sims$X))
# create visualization
ggplot(sims, aes(x = X)) +
geom_bar()Interpret these results in the context of the free throw shooting example.
There is a pretty likely chance that 2 out of the 3 shots will make it and there is a more unlikely chance that 0 will make it.
Create the frequency table, relative frequency table, and bar plot for Y. Do the same for Z.
# create data frame
sims <- data.frame(toss1, toss2, toss3, X, Y, Z)
# check dimensions - should be 100000 x 6
dim(sims)
# create frequency table
table(sims$Y)
# create relative frequency table
proportions(table(sims$Y))
# create visualization
ggplot(sims, aes(y = Y)) +
geom_bar()
#| eval: false
# create data frame
sims <- data.frame(toss1, toss2, toss3, X, Y, Z)
# check dimensions - should be 100000 x 6
dim(sims)
# create frequency table
table(sims$Z)
# create relative frequency table
proportions(table(sims$Z))
# create visualization
ggplot(sims, aes(z = Z)) +
geom_bar()Comprehension check: for each random variable, what should the counts in the frequency table add to? What should the relative frequencies add to?
Counts in the frequency table add to how many times are heads verses how many times are tales. The realative frequencies is the probabilities of these outcomes.
Comment on the distributions of Y and Z. Do they fit your intuition? What do you notice about distributions of X and Z?
Yes, I think that they do in fact fit my intuition. I think that it would make a lot of sense
A great benefit of simulations is we can approximate the expected value of a random variable by calculating the mean of many simulated observations of that random variable. For example, we can get an approximate value of \(E(X)\) by calculating mean(X).
Compute the approximate expected value for X, Y, and Z.
mean(X)[1] 2.10006
mean(Y)[1] 0.69799
mean(Z)[1] 1.20012
How do these compare to the true expected values you computed in Group Work 2.2?
I think these values did compare well to what we expected in our group work. With a 70% probability of heads, we would expect that the majority would be a value of heads. a little over 2/3 heads makes sense as that would be about 70% of the time. Having a heads on the second flip should also have around a 70% chance which we got a 0.699 which is very close. The mean of Z should be heads minus tails should be lower than 2 which it is.
Let’s assume that these 3 free throws take place with <1 second left on the clock, when the shooting team is down by 2. Use your simulations to find the approximate probability that:
There is about a 0.343 chance that the shooting team will win, about a 0.216 chance the shooting team looses, and about a 0.441 chance that the game goes into over time.