Show Negative Binomial Discrete Distribution in Statistics using Python


In this problem statement we have to show the negative binomial discrete distribution in statistics with the help of Python. So for showing this statistic we will be using the numpy and matplotlib libraries of Python.

What is Negative Binomial Discrete Distribution?

In statistics, the Negative Binomial distribution represents the number of trials required to get the number of failures. In this the trial can result in success or failure. So we can say that the number of failures occurs before the number of successes is achieved in the trials. It is related to the geometric distribution.

The Negative Binomial Distribution is defined with the help of three parameters 'r', 'p' and X. In this parameter r shows the required number of successes and parameter p shows the probability of success for every trial. And X displays the number of trials necessary to get the fixed number of successes.

Algorithm

  • Step 1 − Importing the essential libraries in this step. In our program we will use Numpy and matplotlib libraries.

# Import the necessary libraries
import numpy as nmp
import matplotlib.pyplot as plot
  • Step 2 − The parameters r and p will be defined and initiated in the second step. Here r is the number of failures and p is the probability of success.

r = 6
p = 0.5  
  • Step 3 − Now we will generate the random samples using the random.negative_binomial in this step.

samples = nmp.random.negative_binomial(r, p, size=1000)
  • Step 4 − At the end we will plot the distribution by defining the x and y axis and show the distribution using the show method.

plot.hist(samples, bins='auto', density=True)
plot.xlabel('Number of Trials')
plot.ylabel('Probability')
plot.title('Negative Binomial Distribution')
plot.show()

Example

# Import the necessary libraries
import numpy as nmp
import matplotlib.pyplot as plot

r = 6
p = 0.5  

samples = nmp.random.negative_binomial(r, p, size=1000)

plot.hist(samples, bins='auto', density=True)
plot.xlabel('Number of Trials')
plot.ylabel('Probability')
plot.title('Negative Binomial Distribution')
plot.show()

Output

Complexity

The time complexity for showing the Negative Binomial Discrete Distribution using the Numpy and Matplotlib library is O(n), here n is the number of samples

Conclusion

In this article we have explored how to show the negative binomial distribution using Python. We have also discussed the logic and algorithm to get the required Output.

Updated on: 18-Oct-2023

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements