How does np.random.binomial work in numpy python
Question:
I have the following code but I’m unsure how it works even after reading documentation..
print(np.random.binomial(n = 1, [0.1,0.9]))
I want to produce either 0 or 1 values with probability 10% being 1 and 90% being 0. Is the code above how you would do it?
What if I changed the code to the following? What does that mean for the distributions when I have 3 values in the p argument? Does n being 2 mean the possible values could be 0,1,2?
print(np.random.binomial(n = 2, p = [0.1,0.5,0.4]))
Answers:
I took a look at the function implementation.
The parameter n
in the binomial distribution is how many Bernoulli experiments are we going to perform. In the binomial distribution, p
would be the equal probability in every Bernoulli experiment.
So what does a list of p
mean for a Binomial distribution? Nothing. In this function a list of probabilities list_of_p
used np.random.binomial(n, list_of_p)
will produce as many samples as len(list_of_p)
. The function will check every element in list_of_p
is a number between 0 and 1.
In the particular case all of the elements in list_of_p
are the same, we can use the third parameter: size
. That is, this both lines are equivalent:
np.random.binomial(n = 1, [0.1, 0.1])
np.random.binomial(n = 1, 0.1, 2)
So, if we can tell the function how many samples we want with two different arguments, we can give the function contradictory information, don’t we? Yes, but the function will raise an error if we do so.
# Works fine
np.random.binomial(n = 1, [0.1, 0.3], 2)
# Raises an exception
np.random.binomial(n = 1, [0.1, 0.3], 1)
# Works fine also if the list has length 1
np.random.binomial(n = 1, [0.1], 20)
To answer your question. A binomial distribution with n = 2
produces numbers that go from 0 to 2. Take a look at the Binomial distribution definition. I will not elaborate as this would be math, not coding.
The function call
np.random.binomial(n = 2, p = [0.1, 0.5, 0.4])
will produce three samples of Binomial distribution each of them B(2, 0.1)
, B(2, 0.5)
and B(2, 0.4)
.
And the answer to your main question I think would be clear now. If you would use the Binomial function to produce a Bernoulli experiment with probability 0.1 of taking the value 1, you should write:
np.random.binomial(n = 1, p = 0.1)
I have the following code but I’m unsure how it works even after reading documentation..
print(np.random.binomial(n = 1, [0.1,0.9]))
I want to produce either 0 or 1 values with probability 10% being 1 and 90% being 0. Is the code above how you would do it?
What if I changed the code to the following? What does that mean for the distributions when I have 3 values in the p argument? Does n being 2 mean the possible values could be 0,1,2?
print(np.random.binomial(n = 2, p = [0.1,0.5,0.4]))
I took a look at the function implementation.
The parameter n
in the binomial distribution is how many Bernoulli experiments are we going to perform. In the binomial distribution, p
would be the equal probability in every Bernoulli experiment.
So what does a list of p
mean for a Binomial distribution? Nothing. In this function a list of probabilities list_of_p
used np.random.binomial(n, list_of_p)
will produce as many samples as len(list_of_p)
. The function will check every element in list_of_p
is a number between 0 and 1.
In the particular case all of the elements in list_of_p
are the same, we can use the third parameter: size
. That is, this both lines are equivalent:
np.random.binomial(n = 1, [0.1, 0.1])
np.random.binomial(n = 1, 0.1, 2)
So, if we can tell the function how many samples we want with two different arguments, we can give the function contradictory information, don’t we? Yes, but the function will raise an error if we do so.
# Works fine
np.random.binomial(n = 1, [0.1, 0.3], 2)
# Raises an exception
np.random.binomial(n = 1, [0.1, 0.3], 1)
# Works fine also if the list has length 1
np.random.binomial(n = 1, [0.1], 20)
To answer your question. A binomial distribution with n = 2
produces numbers that go from 0 to 2. Take a look at the Binomial distribution definition. I will not elaborate as this would be math, not coding.
The function call
np.random.binomial(n = 2, p = [0.1, 0.5, 0.4])
will produce three samples of Binomial distribution each of them B(2, 0.1)
, B(2, 0.5)
and B(2, 0.4)
.
And the answer to your main question I think would be clear now. If you would use the Binomial function to produce a Bernoulli experiment with probability 0.1 of taking the value 1, you should write:
np.random.binomial(n = 1, p = 0.1)