1.41 Modify the code in the R script CoinFlip.R to simulate the probability of getting exactly
one head in four coin tosses.
1.43 Use R to simulate the probability of getting at least one 8 in the sum of two dice rolls.
Two dice are rolled ➡️ sample (_ , 2, _______) Values of the dice are 1 to 6 (inclusive) ➡️ sample (1:6 , _ , _______) trial <- sample(1:6, 2, replace=TRUE) OK so the probability of getting exactly one 8 in the sum of two dice rolls would be different from checking at least 8, where
(𝑋 = 8)->>>>> `success <- if (sum(trial) == 8) 1 else 0`
would be ≅ 0.1442❌❌❌
P(𝑋 ≥ 8) ->>>> `success <- if (sum(trial) >= 8) 1 else 0`
would be ≅ 0.4119✅✅✅
I think the trick to understanding this question would be to recognize that, “…getting at least one 8 in the sum of two dice rolls.” Is the same as saying “… sum of the two rolls is greater than or equal to 8.”
P(𝑋 ≥ 8) we want to calculate the sum of exactly 8, as well as accounting for getting the sum of 9, 10, 11, 12.
1.44 Use R to simulate the probability in Exercise 1.30.
(1.30 A tetrahedron dice is four-sided and labeled with 1, 2, 3, and 4. When rolled it lands on the base of a pyramid and the number rolled is the number on the base. In five rolls, what is the probability of rolling at least one 2?)
See image regarding problem 2 (1.43)
Please copy and paste your answers (output) under each code chunk.
n <- 10000 # Number of repetitions
simlist <- numeric(n) # Initialize vector
for (i in 1:n) {
trial <- sample(0:1, 4, replace=TRUE)
success <- if (sum(trial)==1) 1 else 0
simlist[i] <- success }
mean(simlist)
#1.41
Modify the code in the R script CoinFlip.R to simulate the probability of getting exactly one head in four coin tosses.
Let’s Try Mine :
Sure! Here is the reformatted code:
# We can simulate the flips using the sample line, where heads=0 and tails=1, and the number of flips is 4.
trial <- sample(0:1, 4, replace = TRUE)
# Routine to decide if we get one-and-only one head in a toss.
# We dump this into the value 'success' and replicate it 4 times.
#if (sum(trial) == 1) 1 else 0
# Using the code above, we can simulate replication over 10,000 trials.
n <- 10000 # Number of repetitions
simlist <- numeric(n) # Initialize vector "simlist"
for (i in 1:n) {
trial <- sample(0:1, 4, replace = TRUE)
success <- if (sum(trial) == 1) 1 else 0
simlist[i] <- success
}
mean(simlist) # Proportion of trials with 3 heads in 10,000 samples
Let me know if there's anything else I can help you with!
We can see that the resulting estimate is fairly close to the exact solution 1/4=0.25
Increase n to 100,000 or even a million to get more precise estimates.
n <- 10000 # Number of repetitions
simlist <- numeric(n) # Initialize vector
for (i in 1:n) # Loop through 10000 trails
{
trial <- sample(0:1, 4, replace=TRUE) # new trail of four flips
success <- if (sum(trial)==1) 1 else 0 # 1 if exactly one head, 0 otherwise
simlist[i] <- success # keep record of successes
}
mean(simlist) # Proportion of trials with one head
#1.43
Use R to simulate the probability of getting at least one 8 in the sum of two dice rolls.
We want to estimate the probability of getting a sum of 8 when rolling two dice. Instead of taking the basic "for-loop approach, we can make things functionally better:
we can create 10000 columns with 2 rows, simply put-> we run 10,000 trials of rolling 2 dice then we can sum the columns, metaphorically adding the two dice together and the last like finds the average of sums that are equal to 8 ntrials = 10000
x = matrix(sample(1:6, 2*ntrials, replace=TRUE), nrow=2, ncol=ntrials)
y = colSums(x)
mean(y >= 8)
We can see that the resulting estimate is fairly close to the exact solution 5/36= 0.1389
n <- 10000 # Number of repetitions
simlist <- numeric(n) # Initialize vector
for (i in 1:n) # Loop through 10000 trails
{
trial <- sample(1:6, 2, replace=TRUE) # new trail of two rolls
success <- if (sum(trial)>=8) 1 else 0 # 1 if sum >= 8, 0 otherwise
simlist[i] <- success # keep record of successes
}
mean(simlist) # Proportion of trials with one head
#1.44
Use R to simulate the probability in Exercise 1.30:
1.30) A tetrahedron dice is four-sided and labeled with 1, 2, 3, and 4. When rolled it lands on the base of a pyramid and the number rolled is the number on the base. In five rolls, what is the probability of rolling at least one 2?
Sure! Here is the reformatted code:
# Let's create a matrix map of a 5x10 array.
# Each column represents 5 rolls.
# The columns represent the trials.
# We will simulate rolling a 4-sided die.
sides <- 1:4 # Sides of the die
ntrials <- 10000 # Number of trials
x <- matrix(sample(sides, 4 * ntrials, replace = TRUE), nrow = 5, ncol = ntrials)
y <- (x == 2) # Check which rolls are 2's. The results in y are TRUE/FALSE
z <- colSums(y) # Sum the columns to get the number of 2's in each trial
mean(z > 0) # Proportion of trials with at least one 2
Let me know if there's anything else I can help you with!
we can see that the resulting estimate is fairly close to the exact solution = .763
Sure! Here is the reformatted code:
n <- 10000 # Number of repetitions
simlistp <- numeric(n) # Initialize vector
for (i in 1:n) { # Loop through 10000 trails
trial <- sample(1:4, 5, replace=TRUE) # new trail of 5 rolls
success <- if (sum(trial==2) >= 1) 1 else 0 # 1 if at least one 2, 0 otherwise
simlistp[i] <- success # keep record of successes
}
mean(simlistp) # Proportion of trials with one head
Let me know if there's anything else I can help you with!