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.

  • 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...

  • 6. [R Programming| In order to complete this question, we will need to use a fundamen-...

    6. [R Programming| In order to complete this question, we will need to use a fundamen- tal theorem in probabilty theory known as the (Weak) Law of Large Numbers (WLLN) The WLLN will be covered in greater detail in Chapter 8, however we will use this key result throughout the course to approximate probabilities that are otherwise difficult to calculate theoretically. A simplified version of the theorem goes as follows. Assume we are interested in com- puting the probability of...

  • Can Dogs Understand Human Cues? EXPLORATION Dogs have been domesticated for about 14,000 years. In that...

    Can Dogs Understand Human Cues? EXPLORATION Dogs have been domesticated for about 14,000 years. In that time, have they been able to develop an understanding of human gestures such as pointing or glancing? How about simi lar nonhuman cues? Researchers Udell, Giglio, and Wynne tested a small number of dogs in order to answer these questions. In this exploration, we wll first see whether dogs can understand human gestures as well as nonhuman gestures. To test this, the researchers positioned...

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
ADVERTISEMENT