Question

Conducting a Simulation For example, say we want to simulate the probability of getting “heads” exactly...

Conducting a Simulation

For example, say we want to simulate the probability of getting “heads” exactly 4 times in 10 flips of a fair coin.

One way to generate a flip of the coin is to create a vector in R with all of the possible outcomes and then randomly select one of those outcomes. The sample function takes a vector of elements (in this case heads or tails) and chooses a random sample of size elements.

coin <- c("heads","tails")
sample(coin, size = 1, replace = TRUE)

We can do this with or without replacement. Since we are interested in the number of heads in 10 flips of a coin, we need to do this with replacement. Recall that sampling with replacement means that if I get some outcome (e.g., “tails”) I can get that outcome again on a subsequent trial (coin flip).

sample(coin, size = 10, replace = TRUE)

Another approach that will work for any simulation is as follows. In order to conduct a simulation, we need to (1) describe all possible outcomes, (2) connect these outcomes to a random variable(s), (3) choose a source of random numbers, (4) generate a number and note the outcome, (5) repeat step 4 until the generated numbers show a stable pattern, (6) analyze the simulated outcomes.

In the coin flip example, (1) there are two possible outcomes: heads or tails. (2) We are interested in heads, so we will let a 1 represent “heads” and a 0 represent “tails”. (3) For a source of random numbers, we will use R to generate draws from the appropriate distribution. Since there are exactly two outcomes and “heads” (or 1) occurs with 50% probability, we know that we are working with a bernoulli distribution. For repeated flips of the coin, this is a binomial distribution. Finally, (4) we can generate a single flip of the coin using

rbinom(n=1, size=1, prob=0.5)

where n is the number of observations (or experimental repetitions) we want to generate and size is the number of trials in our binomial experiment (see ?rbinom for more information). To generate the number of 1s (heads) in 10 flips of the coin (1 experimental repetition), we write

rbinom(n=1, size=10, prob=0.5)

Note: if we wanted to generate 10 flips and count the number of 1s ourselves, we could write

rbinom(n=10, size=1, prob=0.5)

If in doubt, test with a small number like 5 or 10!

  1. Generate the number of heads in 10 flips of the coin for 1000 experimental repetitions. Store these values as nheads.

Our goal is to simulate the probability of getting “heads” exactly 4 times in 10 flips of a fair coin. In (1), you asked R to flip a coin 10 times and record the number of heads… and then to repeat this process 1000 times. In order to count the number of times there were exactly 4 heads, we need to figure out how often nheads = 4.

Internally, R stores TRUE and FALSE as 1 and 0, respectively. This means that we can tally up the number of times that something happens by asking if it happens and then summing over all of the TRUE/FALSE values.

sum(nheads == 4)
  1. Use the number of 4s in your simulated data to estimate the probability that exactly 4 in 10 coin flips result in heads.

Using RStudio

Homework Answers

Answer #1

R code with comments

---

#set a random seed
set.seed(123)

#Generate the number of heads in 10 flips of the coin
#for 1000 experimental repetitions. Store these values as nheads.
nheads<- rbinom(n=1000, size=10, prob=0.5)

# count the number of times there were exactly 4 heads
count<- sum(nheads==4)
#the probability that exactly 4 in 10 coin flips result in heads.
prob<- count/1000
sprintf('the estimated probability that exactly 4 in 10 coin flips result in heads is %.4f',prob)

---

get this

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider two coins, one fair and one unfair. The probability of getting heads on a given...
Consider two coins, one fair and one unfair. The probability of getting heads on a given flip of the unfair coin is 0.10. You are given one of these coins and will gather information about your coin by flipping it. Based on your flip results, you will infer which of the coins you were given. At the end of the question, which coin you were given will be revealed. When you flip your coin, your result is based on a...
You flip a coin until getting heads. Let X be the number of coin flips. a....
You flip a coin until getting heads. Let X be the number of coin flips. a. What is the probability that you flip the coin at least 8 times? b. What is the probability that you flip the coin at least 8 times given that the first, third, and fifth flips were all tails? c. You flip three coins. Let X be the total number of heads. You then roll X standard dice. Let Y be the sum of those...
In a situation where we have a biased coin that is tails with probability 0.7 and...
In a situation where we have a biased coin that is tails with probability 0.7 and we independently flip it 10 times. Find the following probabilities. 1. getting the sequence HTHHHTHTTH? 2. exactly 4 tails? 3. at least 4 tails? 4. expected number of tails? expected number of heads?
When coin 1 is flipped, it lands on heads with probability   3 5  ; when coin...
When coin 1 is flipped, it lands on heads with probability   3 5  ; when coin 2 is flipped it lands on heads with probability   4 5  . (a) If coin 1 is flipped 11 times, find the probability that it lands on heads at least 9 times. (b) If one of the coins is randomly selected and flipped 10 times, what is the probability that it lands on heads exactly 7 times? (c) In part (b), given that the...
Deriving fair coin flips from biased coins: From coins with uneven heads/tails probabilities construct an experiment...
Deriving fair coin flips from biased coins: From coins with uneven heads/tails probabilities construct an experiment for which there are two disjoint events, with equal probabilities, that we call "heads" and "tails". a. given c1 and c2, where c1 lands heads up with probability 2/3 and c2 lands heads up with probability 1/4, construct a "fair coin flip" experiment. b. given one coin with unknown probability p of landing heads up, where 0 < p < 1, construct a "fair...
1. what is the empirical probability of getting 4 heads in 10 flips? 2. Find the...
1. what is the empirical probability of getting 4 heads in 10 flips? 2. Find the empirical probability for getting 7 heads in 10 coin flip.
When coin 1 is flipped, it lands on heads with probability   3/5  ; when coin 2...
When coin 1 is flipped, it lands on heads with probability   3/5  ; when coin 2 is flipped it lands on heads with probability  4/5 . (a) If coin 1 is flipped 12 times, find the probability that it lands on heads at least 10 times. (b) If one of the coins is randomly selected and flipped 10 times, what is the probability that it lands on heads exactly 7 times? (c) In part (b), given that the first of...
Language: Python Write a program to simulate an experiment of tossing a fair coin 16 times...
Language: Python Write a program to simulate an experiment of tossing a fair coin 16 times and counting the number of heads. Repeat this experiment 10**5 times to obtain the number of heads for every 16 tosses; save the number of heads in a vector of size 10**5 (call it headCounts). You should be able to do this in 1-3 lines of numpy code. (Use np.random.uniform1 to generate a 2d array of 10**5 x 16 random numbers between 0 and...
3. A fair coin is flipped 4 times. (a) What is the probability that the third...
3. A fair coin is flipped 4 times. (a) What is the probability that the third flip is tails? (b) What is the probability that we never get the same outcome (heads or tails) twice in a row? (c) What is the probability of tails appearing on at most one of the four flips? (d) What is the probability of tails appearing on either the first or the last flip (or both)? (e) What is the probability of tails appearing...
An unfair coin is such that on any given toss, the probability of getting heads is...
An unfair coin is such that on any given toss, the probability of getting heads is 0.6 and the probability of getting tails is 0.4. The coin is tossed 8 times. Let the random variable X be the number of times heads is tossed. 1. Find P(X=5). 2. Find P(X≥3). 3. What is the expected value for this random variable? E(X) = 4. What is the standard deviation for this random variable? (Give your answer to 3 decimal places) SD(X)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT