8.0k views
1 vote
Write a python program to simulate Binomial distribution the python program should at least

1) Plot the original PDF/PMF
2) Have flexible user input number of "samples" simulated and flexible number of samples generated
3) Output the histogram of the sample mean (for example, you can generate 2000 sets of sample with n= 20) to demonstrated a tendency of normal distribution with increased"sample size".

User Len Joseph
by
7.7k points

1 Answer

3 votes

Final answer:

To simulate a binomial distribution in Python and meet the provided requirements, you can use the numpy.random.binomial function. This function allows you to generate a binomial distribution using user-defined parameters for the number of samples and sample size. The program also includes plotting the original PDF/PMF and generating a histogram of the sample mean to demonstrate the tendency of a normal distribution with increased sample size.

Step-by-step explanation:

To simulate a binomial distribution in Python, you can use the numpy.random.binomial function. Here is an example program that meets the mentioned requirements:

import numpy as np
import matplotlib.pyplot as plt

# User input
samples = int(input('Number of samples: '))
sample_size = int(input('Sample size: '))

# Simulate binomial distribution
data = np.random.binomial(n=20, p=0.5, size=(samples, sample_size))

# Plot PDF/PMF
plt.hist(data.mean(axis=1), bins=20, density=True)
plt.xlabel('Sample Mean')
plt.ylabel('Probability')
plt.title('Histogram of Sample Mean')
plt.show()
User Daniel Hiller
by
7.7k points