Question

In: Math

Consider flipping nn times a coin. The probability for heads is given by pp where pp...

Consider flipping nn times a coin. The probability for heads is given by pp where pp is some parameter which can be chosen from the interval (0,1)(0,1).

Write a Python code to simulate nn coin flips with heads probability pp and compute the running proportion of heads X¯nX¯n for nn running from 1 to 1,000 trials. Plot your results. Your plot should illustrate how the proportion of heads appears to converge to pp as nn approaches 1,000.

In [ ]:

### Insert your code here for simulating the coin flips and for computing the average

In [2]:

### Complete the plot commands accordingly for also plotting the computed running averages in the graph below
p = 0.25 # just an example
plt.figure(figsize=(10,5))
plt.title("Proportion of heads in 1,000 coin flips")
plt.plot(np.arange(1000),p*np.ones(1000),'-',color="red",label="true probability") 
plt.xlabel("Number of coin flips")
plt.ylabel("Running average")
plt.legend(loc="upper right")

Solutions

Expert Solution

The R code for simulating the coin toss is given below. The probability of heads is set at p = 0.25.

You can change the value as you want. The plot shows the probability converges to p = 0.25 as sample size increases.

import matplotlib.pyplot as plt
import numpy as np
def proportion(seed,n, p):
        np.random.seed(seed)
        h = 0
        for i in range(n):
             r = np.random.random_sample(1)
             if(r<p):
                h = h+1
        return(h/n)
N = 1000
p = 0.25
PH = []
seed = 154
for i in range(1,N):
   ph = proportion(seed,i, p)
   PH.append(ph)
if __name__ == "__main__":
    plt.figure(figsize=(10,5))
    plt.title("Proportion of heads in 1,000 coin flips")
    plt.plot(range(1,N),PH,'-',color="red",label="True probability")
    plt.xlabel("Number of coin flips")
    plt.ylabel("Running average")
    plt.legend(loc="upper right")
    plt.show()

Kindly upvote.


Related Solutions

What’s the probability of flipping a coin 25 times and getting between 32% and 50% heads....
What’s the probability of flipping a coin 25 times and getting between 32% and 50% heads. Please assume that this clearly discrete distribution is, in fact, continuous, and use the normal approximation.
What’s the probability of getting no heads after flipping a fair coin 10 times? What’s the...
What’s the probability of getting no heads after flipping a fair coin 10 times? What’s the probability of getting no 3’s after rolling a fair 6-sided die 9 times? What’s the probability of getting a 4 at least once after rolling a fair 4-sided die 5 times? What’s the probability of getting a 5 exactly once after rolling a fair 8-sided die 7 times?
[Counting and Probability] Consider the experiment of flipping a coin four times. a. Using a tree,...
[Counting and Probability] Consider the experiment of flipping a coin four times. a. Using a tree, determine the probability of one or two tails, with a biased coin with P(H) = 2/3. Compare to the probability with an unbiased coin b. [Bayes’ Rule] Using the results of the part a suppose we have two coins, one unbiased, and a biased coin with P(H) = 2/3. We select a coin at random, flip it three times, and observe either one or...
Consider flipping a fair coin 10 times. What is the probability that (a) you get exactly...
Consider flipping a fair coin 10 times. What is the probability that (a) you get exactly 7 heads? (b) you get at least 4 heads? (c) you get two more heads than tails?
I have a coin and I know that the probability p of flipping heads is either...
I have a coin and I know that the probability p of flipping heads is either 1/2 or 1/4. I flip the coin 3 times and I get THT. Use the maximum likelihood method to determine the actual value of p.
- Your friend claims he has a fair coin; that is, the probability of flipping heads...
- Your friend claims he has a fair coin; that is, the probability of flipping heads or tails is equal to 0.5. You believe the coin is weighted. Suppose a coin toss turns up 15 heads out of 20 trials. At α = 0.05, can we conclude that the coin is fair (i.e., the probability of flipping heads is 0.5)? You may use the traditional method or P-value method.
A biased coin has probability p = 3/7 of flipping heads. In a certain game, one...
A biased coin has probability p = 3/7 of flipping heads. In a certain game, one flips this coin repeatedly until flipping a total of four heads. (a) What is the probability a player finishes in no more than 10 flips? (b) If five players independently play this game, what is the probability that exactly two of them finish in no more than ten flips?
If a fair coin is tossed 25 times, the probability distribution for the number of heads,...
If a fair coin is tossed 25 times, the probability distribution for the number of heads, X, is given below. Find the mean and the standard deviation of the probability distribution using Excel Enter the mean and round the standard deviation to two decimal places. x   P(x) 0   0 1   0 2   0 3   0.0001 4   0.0004 5   0.0016 6   0.0053 7   0.0143 8   0.0322 9   0.0609 10   0.0974 11   0.1328 12   0.155 13   0.155 14   0.1328 15   0.0974 16  ...
A coin that lands on heads with a probability of p is tossed multiple times. Each...
A coin that lands on heads with a probability of p is tossed multiple times. Each toss is independent. X is the number of heads in the first m tosses and Y is the number of heads in the first n tosses. m and n are fixed integers where 0 < m < n. Find the joint distribution of X and Y.
What is the probability that you would get heads-up when flipping a coin ONE time? What...
What is the probability that you would get heads-up when flipping a coin ONE time? What is the probability that you would get heads-up when flipping a coin TEN times? What is the probability that you would get heads-up on two coins flipped at the same time? What is the probability that you would roll a 5 on a single dice if you rolled it five times? What is the probability that you would roll a 3 on a single...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT