Question

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 1, then convert values less than 0.5 to True - or “head”, and count the number of heads for each simulated coin.)

Here is my code, but I am having difficulty understanding the question


import numpy as np
import scipy.stats as stats
import numpy.random as rn

m = 10**5; # number of coins
N = 16; # number of tosses each coin

sims = np.empty(N)
for j in range(N):
flips = np.random.randint(0, 2, m)
sims[j] = np.sum(flips)

print(sims[:])

Homework Answers

Answer #1

import numpy as np
import scipy.stats as stats
import numpy.random as rn

m = 10**5; # number of coins
N = 16; # number of tosses each coin
simulations=rn.random(size=(m,N))
#each row represents one experiment, each column represents one coin
headCounts=np.sum(simulations<.5,1)#sum all the places where values is <.5, row wise
#headCounts now stores number of heads obtained in each of the 10^5 experiments

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 an experiment of tossing two coins three times. Coin A is fair but coin B...
Consider an experiment of tossing two coins three times. Coin A is fair but coin B is not with P(H)= 1/4 and P(T)= 3/4. Consider a bivariate random variable (X,Y) where X denotes the number of heads resulting from coin A and Y denotes the number of heads resulting from coin B. (a) Find the range of (X,Y) (b) Find the joint probability mass function of (X,Y). (c) Find P(X=Y), P(X>Y), P(X+Y<=4). (d) Find the marginal distributions of X and...
An experiment consists of tossing a coin 6 times. Let X be the random variable that...
An experiment consists of tossing a coin 6 times. Let X be the random variable that is the number of heads in the outcome. Find the mean and variance of X.
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")...
java beginner level NO ARRAYS in program Flip a coin (Assignment) How many times in a...
java beginner level NO ARRAYS in program Flip a coin (Assignment) How many times in a row can you flip a coin and gets heads? Use the random number generator to simulate the flipping of the coin. 0 means heads, 1 means tails. Start a loop, flip it, if heads, count it and keep flipping. If tails, stop the loop. Display the number of times in a row that heads came up. Once this is working, wrap a while loop...
In need of assistance. Please show your work -----> Given: Chance Experiment involving tossing a biased...
In need of assistance. Please show your work -----> Given: Chance Experiment involving tossing a biased coin. Probability of heads: p = .20 A) The coin is tossed 12 times. Let z = the # of heads tossed. i) What type of distribution would you use to find probabilities in this case? What is the general distribution function, p(z), you would use to find probabilities? ii) Find the probability of tossing exactly 5 heads iii) Probability of tossing at least...
If a binomial experiment involves flipping a fair coin four times and counting the number of...
If a binomial experiment involves flipping a fair coin four times and counting the number of heads that result, the probability of at least three heads turning up in four flips is what? A mobile provider believes that its new promotion will convince 20% of their competitors' customers to switch to their own service. If the company's forecasts are correct, what is the approximate probability that one out of four randomly selected competitors' customers will switch after exposure to the...
Suppose you are conducting an experiment where you flip a coin four times and count the...
Suppose you are conducting an experiment where you flip a coin four times and count the number of heads flips. For such an experiment, you can either get 0/4, 1/4, 2/4, 3/4, or 4/4 heads. a. Explain why there are five bars in the sampling distribution for this experiment. b. Draw a sampling distribution for this problem. c. Use the equation √p * (1-p)/n  to determine the mean and standard deviation of the sampling distribution of sample proportions with samples of...
Question 2 Tossing a coin 15 times, let  be the number of tails obtained. (a) The mean...
Question 2 Tossing a coin 15 times, let  be the number of tails obtained. (a) The mean of this binomial distribution is . (b) The standard deviation (to the neatest tenth) of this binomial distribution is . (c) The the probability of getting 6 heads is  (round to the nearest thousandth). (d) The probability of getting at least 2 tails is  (round to 6 decimal places). (e) The probability that the number of tails is between 5 and 10, exclusive, is  (round to the...
PROJECT B 1. Flip a coin 120 times. In order to be organized, please record the...
PROJECT B 1. Flip a coin 120 times. In order to be organized, please record the results of this experiment in 5 rows, with 24 flips per row. For example, the first row may be HTHTTTHTTHTTHHHTTHTHHHHT If you do not desire to flip a coin manually, STATDISK can be used to simulate the process. To use STATDISK, go to “Data” at the top of the STATDISK window, and then choose “Coins Generator”. The “Coin Toss Simulator” window will appear. Then...
Think about the following game: A fair coin is tossed 10 times. Each time the toss...
Think about the following game: A fair coin is tossed 10 times. Each time the toss results in heads, you receive $10; for tails, you get nothing. What is the maximum amount you would pay to play the game? Define a success as a toss that lands on heads. Then the probability of a success is 0.5, and the expected number of successes after 10 tosses is 10(0.5) = 5. Since each success pays $10, the total amount you would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT