Question: In this problem I only need the question in part C answered: How well did the simulation compare to the theoretical values from `BinomPMF`? Exercise

In this problem I only need the question in part C answered: How well did the simulation compare to the theoretical values from `BinomPMF`?

Exercise 4. (R, Python and Julia)

You will be simulating a series of Binomial experiments. A simple example of a Binomial experiment is the number of heads in a series of coin flips. Your simulation will require two nested `for` loops.

Part a.

First, define a vector of length 11, populated with 0s. This will represent a count of the number of times heads appear in a series of 10 flips. Call this vector `Successes`.

Part b.

Next, write two loops. The outer loop will run from 1 to 1000. This will allow use to repeat the simple experiment 1000 times. The inner loop will perform the experiment, and will run from 1 to 10. Remember to adjust the indices for Python. You might use `i` as counter for the inner loop, and `j` as a counter for the inner loop, but we won't need those variables.

Inside the outer loop, but outside the inner loop, define an accumulator variable `Count`, setting this value to 0. Inside the inner loop, perform a random coin toss by drawing from a uniform random sample (see this week's lecture notes for code to simulate a coin toss). If the random value is greater than 0.5, count this as a success, and increment `Count` by 1. Remember to set `Count` to 0 before the start of the inner loop.

After the inner loop executes, `Count` will have a value between 0 (no heads) and 10 (all heads). You can now use `Count` as an index into `Successes`. After each iteration of the inner loop, increment `Successes[Count]` by 1. (You may need to use `Count+1` as an index; remember `Count` can be 0).

We will be using `Successes` to sum the number of times each possible value of `Count` occurs in the 1000 repetitions of the simple experiment. Increment `Successes` outside the inner loop, but inside the outer loop.

Part C

In this problem I only need the question in part C answered:

Part b. Next, write two loops. The outer loop will run from 1 to 1000. This will allow usc to repeat the simple experiment 1000 times. The inner loop will perform the experiment, and will run from 1 to 10. Remember to adjust the indices for Python. You might use i as counter for the inner loop, and j as a counter for the inner loop, but we won't need those variables. Inside the outer loop, but outside the inner loop, define an accumulator variable Count, setting this value to 0. Inside the inner loop, perform a random coin toss by drawing from a uniform random sample (see this week's lecture notes for code to simulate a coin toss). If the random value is greater than 0.5, count this as a success, and increment Count by 1. Remember to set Count to 0 before the start of the inner loop. After the inner loop executes, Count will have a value between 0 (no heads) and 10 (all heads). You can now use Count as an index into Successes. After cach iteration of the inner loop, increment Successes[Count ] by 1. (You may nced to use Count+1 as an index; remember Count can be 0). We will be using Successes to sum the number of times cach possible value of Count occurs in the 1000 repetitions of the simple experiment. Increment Successes outside the inner loop, but inside the outer loop. R Python Julia Part c. Divide each clement of Successes by 1000. Successes will now represent the proportion of success out of n = 10 attempts. Plot Successes against a vector of values from 0 to 10 as points. Compare this with your BinomPMF function, where x takes values from 0 to 10, n = 10 and pi = 0.5. Add the values produced by BinomPMF to the plot of Successes as a line. How well did the simulation compare to the theoretical values from BinomPMF? R Python Julia

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!