Question

You just picked up a coin and is wondering if the coin is truly unbiased. To...

You just picked up a coin and is wondering if the coin is truly unbiased. To determine if this coin is unbiased, you decide to do an experiment by flipping this coin 100 times and record how many times it returns head. Let us say this is flipping experiment No.1, and its result is the no. of heads. Then you decide to repeat this flipping experiment 999 more times (for a total of 1000 experiments, in each experiment you flip the coin 100 times).

  • Write a function called simulation_coinflip(p) returns a numpy array result with shape (1000,) where the ?i-th element (result[i-1]) in this array is the result of the ?i-th flipping test (the no. of heads in a given flip test).
  • Plot the histograms of this array using 50 bins using the resulting array of simulation_coinflip(p) for ?=0.2,0.5,0.7p=0.2,0.5,0.7.
  • Remark: using for loops is okay, the code for this problem can be formulated using completely vectorized routines without any for loops, and you MAY or MAY NOT find np.vectorize() (vectorize a function if there is if-else conditionals), np.mean(), np.sum(), np.apply_along_axis, np.count_nonzero(), and np.random.random() useful.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

All the explanation is in the code comments. Hope this helps!

Code:

# imports
import numpy as np
import random
import matplotlib.pyplot as plt

# the required function
def simulation_coinflip(p):
  
# number of experiments
n = 1000
result = np.zeros(n)
  
# for 1000 experiments
for i in range(n):
# each experiment is for 100 times
for j in range(100):
  
# check if it is a head
if(random.random() <= p):
result[i] = result[i] + 1
  
# return result
return result

# call the simulation_coinflip() function
result1 = simulation_coinflip(0.2)
result2 = simulation_coinflip(0.5)
result3 = simulation_coinflip(0.7)
plt.hist([result1, result2, result3], bins=50)
plt.xlabel('Number of heads')
plt.ylabel('Number of times')
plt.legend(['p=0.2', 'p=0.5', 'p=0.7'])
plt.show()

Sample run:

Code screenshots:

Add a comment
Know the answer?
Add Answer to:
You just picked up a coin and is wondering if the coin is truly unbiased. To...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 2 Suppose you have a fair coin (a coin is considered fair if there is...

    Question 2 Suppose you have a fair coin (a coin is considered fair if there is an equal probability of being heads or tails after a flip). In other words, each coin flip i follows an independent Bernoulli distribution X Ber(1/2). Define the random variable X, as: i if coin flip i results in heads 10 if coin flip i results in tails a. Suppose you flip the coin n = 10 times. Define the number of heads you observe...

  • Suppose you and your roommate use a coin-flipping app to decide who has to take out...

    Suppose you and your roommate use a coin-flipping app to decide who has to take out the trash: heads you take out the trash, tails your roommate does. After losing a number of flips, you start to wonder if the coin-flipping app really is totally random, or if it is biased in one direction or the other. To be fair to your roommate, you wish to test whether the app is biased in either direction, and thus a two-tailed test...

  • Suppose you just flipped a fair coin 8 times in a row and you got heads...

    Suppose you just flipped a fair coin 8 times in a row and you got heads each time! What is the probability that the next coin flip will result in a heads? Write answer as a decimal and round to 1 place after the decimal point.

  • The Coin Tossing simulation Coin.java The Coin class must be the enum class, defining the instances...

    The Coin Tossing simulation Coin.java The Coin class must be the enum class, defining the instances HEADS and TAILS. CoinTossing.java The CoinTossing class should be a simple class (with a main method) that uses the values provided by a Coin class, and performs a simulation of the tossing of a coin. There must be a separate (possibly static) flip method that takes no arguments and returns a value of the Coin class. The result returned from the flip method is...

  • Do the following in MATLAB: trials = 100; flip = rand(trials,1); heads = (flip >= 0.5);...

    Do the following in MATLAB: trials = 100; flip = rand(trials,1); heads = (flip >= 0.5); percentheads = sum(heads)/trials a.We can use a random number generator to estimate probabilities that might otherwise be difficult to evaluate. Consider the probability of obtaining four heads on four flips. Generate four separate random experiments of 0s and 1s representing N trials each. (You can do this by generating an Nx4 random array rather than an Nx1 array as in #1.) Now look at...

  • 3. (25 pts) A Truly FAIR COIN: Because actual coins are not truly balanced, P -...

    3. (25 pts) A Truly FAIR COIN: Because actual coins are not truly balanced, P - the ACTUAL probability of HEAD for our old, battered coin - may differ substantially from 1/2. The famous Mathematician John Von-Neumann came up with the following proposal for using our possibly unfair coin to simulate a truly fair coin that always has PROB(HEAD)=PROB(TAIL) = 1/2, as follows: • (i) toss the UNFAIR coin twice. This is the experiment E. • (ii) IF you got...

  • A box contains five coins. For each coin there is a different probability that a head...

    A box contains five coins. For each coin there is a different probability that a head will be obtained when the coin is tossed. (Some of the coins are not fair coins!) Let pi denote the probability of a head when the i th coin is tossed (i = 1, . . . , 5), and suppose that p1 = 0, p2 =1/4, p3 =1/2, p4 =3/4, p5 =1. The experiment we are interested in consists in selecting at random...

  • Using R-studio 2. Consider an experiment where we flip a fair coin six times in a row, and i is the number of heads toss...

    Using R-studio 2. Consider an experiment where we flip a fair coin six times in a row, and i is the number of heads tossed:             a.         Calculate the probability mass function for i = 0. . . 6 using the equation from Ross section 2.8 for Binomial Random Variables             b.         Conduct a simulation of this experiment in R, with T trials of the experiment – pick several values of T from 10 to 10,000.             c.         Create a plot of the...

  • For this question, you will flip fair coin to take some samples and analyze them. First,...

    For this question, you will flip fair coin to take some samples and analyze them. First, take any fair coin and flip it 12 times. Count the number of heads out of the 12 flips. This is your first sample. Do this 4 more times and count the number of heads out of the 12 flips in each sample. Thus, you should have 5 samples of 12 flips each. The important number is the number of heads in each sample...

  • I'm working on probability HW and working on word problems. Do you have any tips as...

    I'm working on probability HW and working on word problems. Do you have any tips as to how to differentiate between Poisson distributions from Binomials, Geometric and Negative Binomials when doing word problems...i provided an examples for everything but Possion, please explain Poisson distribution using the same example structures Binomial is simplest one in this we have number of trials given 'n' and probability of success 'p' and they ask for probability of getting success 'r' times. For example: When...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT