Q1

In this question, you'll work with parametric distributions.

A

Recall from earlier lectures when we discussed flipping a coin. There is an actual distribution for this: the Bernoulli distribution. It measures probability of success or failure, 1 or 0. The only parameter to this distribution is $p$, or the probability of success.

Write a function flip() which accepts one argument $p$ between 0 and 1, indicating the probability of Heads. Your function should return 1 on success, 0 otherwise.

The only outside function you can use inside flip is numpy.random.random() with no arguments. This will return a random floating-point number between 0 and 1.


In [ ]:


In [ ]:
try:
    flip
except:
    assert False
else:
    assert True

In [ ]:
np.testing.assert_allclose(flip(1.0), 1.0, rtol = 0.01)
np.testing.assert_allclose(flip(0.0), 0.0, rtol = 0.01)

In [ ]:
results = np.zeros(10000, dtype = np.int)
for i in range(10000):
    results[i] = flip(0.5)
np.testing.assert_allclose(results.mean(), 0.5, rtol = 0.1)

B

A generalization of the Bernoulli distribution is the Binomial distribution, which also has a probability of success $p$, but also has a number of experiments $n$. Using the coin-flip analogy, $n$ is the number of times you want to flip the coin and record its result.

Write a function nflips() which takes two arguments: $n$, the number of times to flip the coin, and $p$, the probability of Heads (1). Your function should return an array of $n$ 1s and 0s, the outcome of each coin flip.

Hint: you can make use of your flip() method from earlier.


In [ ]:


In [ ]:
try:
    nflips
except:
    assert False
else:
    assert True

In [ ]:
import numpy as np
assert (np.array(nflips(1000, 1.0)) == 1).all()
assert (np.array(nflips(1000, 0.0)) == 0).all()

In [ ]:
results = np.array(nflips(10000, 0.5))
np.testing.assert_allclose(results.mean(), 0.5, rtol = 0.1)

C

I use your nflips function from part B and set n = 1000. Let $X$ be the number of heads from part B, and $Y$ the number of tails. Are $X$ and $Y$ dependent or independent random variables? Why or why not?