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`
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)
mean( simlist )
Please copy and paste your answers (output) under each code chunk.
n <-10000# Number of repetitions
simlist <- numeric(n)# Initialize vector
for(i in1:n){
trial <- sample(0:1,4, replace=TRUE)
success <-if(sum(trial)==1)1else0
simlist[i]<- success }
mean(simlist)
[1]0.2528
#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.
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!
[1]0.2478
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 in1:n)# Loop through 10000 trails
{
trial <- sample(0:1,4, replace=TRUE)# new trail of four flips
success <-if(sum(trial)==1)1else0# 1 if exactly one head, 0 otherwise
simlist[i]<- success # keep record of successes
}
mean(simlist)# Proportion of trials with one head
[1]0.2491
#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)
[1]0.4201
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 in1:n)# Loop through 10000 trails
{
trial <- sample(1:6,2, replace=TRUE)# new trail of two rolls
success <-if(sum(trial)>=8)1else0# 1 if sum >= 8, 0 otherwise
simlist[i]<- success # keep record of successes
}
mean(simlist)# Proportion of trials with one head
[1]0.4215
#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?