Question

In: Computer Science

For this problem, complete the function stub to simulate the following random variable: Suppose we have...

For this problem, complete the function stub to simulate the following random variable: Suppose we have a (possibly) unfair coin where the probability of Head is ? and we flip this coin ? times. Let X = "the number of heads showing."

Demonstrate your solution by generating 105 random variates for the parameters shown and displaying them using show_distribution(...).

STARTER CODE:

# First, create a function which simulates the coin, where
# you return 1 with probability p and 0 with probability 1-p.

N = 10
p = 0.25

num_trials = 10**5

def coinFlip(p):
return 0 # Just to get it to compile
  
def coinFlips(N,p):
return 0 # Just to get it to compile

print("Demonstration:")

seed(0)

Solutions

Expert Solution

I have provided the implementation for these 2 functions. 
Now, you can use show_distribution() method to show the distribution.



SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.


from random import seed, random
N = 10
p = 0.25
num_trials = 10 ** 5

# First, create a function which simulates the coin, where
# you return 1 with probability p and 0 with probability 1-p.
def coinFlip(p):
    # get random number and return 1 if it is less than p
    if random() < p:
        return 1
    else:
        # 0 if it's greater
        return 0

def coinFlips(N, p):
    # take count as 0
    head_count = 0
    # flip coins for N times
    for i in range(N):
        # flip coin
        toss = coinFlip(p)
        # if it's 1, then we have got a head
        if toss == 1:
            head_count += 1

    # return the count
    return head_count

# TEST
print("Demonstration:")
seed(0)
# num_heads = coinFlips(N, p)
# print('Number of heads, X=', num_heads)

=========


Related Solutions

Suppose we have a random variable X that is uniformly distributed between a = 0 and...
Suppose we have a random variable X that is uniformly distributed between a = 0 and b = 100. What is σ X? a. 0.913 b. 0.833 c. 50 d. 7.071
Suppose we have the following data on a dependent variable (Y) and an explanatory variable (X):...
Suppose we have the following data on a dependent variable (Y) and an explanatory variable (X): X          Y 0          140 1          140 4             0 0          180 3           80 2          120 4             0 1          200 2          120             3            40 Calculate the simple linear regression equation by hand. Show all your work. Using your result, predict the value of Y when X = 3.5. (15 points) Calculate the R2and the adjusted R2measures (by hand) and provide an interpretation of what they tell us. (12 points) Test (by hand) to see whether this is...
(a) Suppose that Y is a random variable with moment generating function H(t). Suppose further that...
(a) Suppose that Y is a random variable with moment generating function H(t). Suppose further that X is a random variable with moment generating function M(t) given by M(t) = 1/3 (2e ^(3t) + 1)H(t). Given that the mean of Y is 10 and the variance of Y is 12, then determine the mean and variance of X (Use H(0) = 1). (b) Suppose that the Moment generating function for X is M(t) = e^t/( 3 − 2e^t) . Find...
(a) Suppose that Y is a random variable with moment generating function H(t). Suppose further that...
(a) Suppose that Y is a random variable with moment generating function H(t). Suppose further that X is a random variable with moment generating function M(t) given by M(t) = 1 3 (2e 3t + 1)H(t). Given that the mean of Y is 10 and the variance of Y is 12, then determine the mean and variance of X (Use H(0) = 1). (b) Suppose that the Moment generating function for X is M(t) = e t 3 − 2e...
Suppose we have the following data on variable X (independent) and variable Y (dependent): X Y...
Suppose we have the following data on variable X (independent) and variable Y (dependent): X Y 2 70 0 70 4 130 a.) By hand, determine the simple regression equation relating Y and X. b.) Calculate the R-Square measure and interpret the result. c.) Calculate the adjusted R-Square. d.) Test to see whether X and Y are significantly related using a test on the population correlation. Test this at the 0.05 level. e.) Test to see whether X and Y...
Suppose we have the following data on variable X (independent) and variable Y (dependent): X         Y...
Suppose we have the following data on variable X (independent) and variable Y (dependent): X         Y 2          70 0          70 4          130 Test to see whether X and Y are significantly related using a t-test on the slope of X. Test this at the 0.05 level. Test to see whether X and Y are significantly related using an F-test on the slope of X. Test this at the 0.05 level.
Suppose we have the following data on variable X (independent) and variable Y (dependent): X Y...
Suppose we have the following data on variable X (independent) and variable Y (dependent): X Y 2 70 0 70 4 130 a.) By hand, determine the simple regression equation relating Y and X. b.) Calculate the R-Square measure and interpret the result. c.) Calculate the adjusted R-Square. d.) Test to see whether X and Y are significantly related using a test on the population correlation. Test this at the 0.05 level. e.) Test to see whether X and Y...
Suppose we have the following data on variable X (independent) and variable Y (dependent): X         Y...
Suppose we have the following data on variable X (independent) and variable Y (dependent): X         Y 2          70 0          70 4          130 By hand, determine the simple regression equation relating Y and X. Calculate the R-Square measure and interpret the result. Calculate the adjusted R-Square. Test to see whether X and Y are significantly related using a test on the population correlation. Test this at the 0.05 level. Test to see whether X and Y are significantly related using a...
Suppose we have the following data on variable X (independent) and variable Y (dependent): X Y...
Suppose we have the following data on variable X (independent) and variable Y (dependent): X Y 2 70 0 70 4 130 (SOLVE ALL BY HAND, NOT BY USING EXCEL) By hand, determine the simple regression equation relating Y and X. Calculate the R-Square measure and interpret the result. Calculate the adjusted R-Square. Test to see whether X and Y are significantly related using a test on the population correlation. Test this at the 0.05 level. Test to see whether...
Problem 1: Simulating Blackjack In this problem we will use classes and functions to simulate a...
Problem 1: Simulating Blackjack In this problem we will use classes and functions to simulate a simplified game of Blackjack (21). The game begins with a standard deck of 52 playing cards (no jokers). Each player is dealt two cards to start with. The winner of a hand of Blackjack is the player whose hand has the highest value without going over 21. When calculating the value of a hand, we add up the rank of each card in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT