<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 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
Step1:  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
<ASSISTANT_TASK:>
Python Code:
try:
    flip
except:
    assert False
else:
    assert True
np.testing.assert_allclose(flip(1.0), 1.0, rtol = 0.01)
np.testing.assert_allclose(flip(0.0), 0.0, rtol = 0.01)
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)
Explanation: 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.
End of explanation
try:
    nflips
except:
    assert False
else:
    assert True
import numpy as np
assert (np.array(nflips(1000, 1.0)) == 1).all()
assert (np.array(nflips(1000, 0.0)) == 0).all()
results = np.array(nflips(10000, 0.5))
np.testing.assert_allclose(results.mean(), 0.5, rtol = 0.1)
Explanation: 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.
End of explanation
<END_TASK>