Implementing Probability Distributions to Python
Binomial Distribution
The Binomial distribution models the probability of getting exactly k successes in n independent trials, each with probability p of success.
123456789101112131415161718from scipy.stats import binom import matplotlib.pyplot as plt # number of trials n = 100 # probability of success p = 0.02 # number of successes k = 3 binom_prob = binom.pmf(k, n, p) # Vizualization x_vals = range(0, 15) y_vals = binom.pmf(x_vals, n, p) plt.bar(x_vals, y_vals, color='skyblue') plt.title(f'Binomial probability: {binom_prob:.4f}') plt.show()
n = 100
- we're testing 100 rods;p = 0.02
- 2% chance a rod is defective;k = 3
- probability of exactly 3 defectives;binom.pmf()
computes the probability mass function.
Uniform Distribution
The Uniform distribution models a continuous variable where all values between $a$ and $b$ are equally likely.
1234567891011121314151617from scipy.stats import uniform import matplotlib.pyplot as plt import numpy as np a = 49.5 b = 50.5 low, high = 49.8, 50.2 uniform_prob = uniform.cdf(high, a, b - a) - uniform.cdf(low, a, b - a) # Vizualization x = np.linspace(a, b, 100) pdf = uniform.pdf(x, a, b - a) plt.plot(x, pdf, color='black') plt.fill_between(x, pdf, where=(x >= low) & (x <= high), color='lightgreen', alpha=0.5) plt.title(f'Uniform probability: {uniform_prob:.1f}') plt.show()
a, b
- total range of rod lengths;low, high
- interval of interest;- Subtracting CDF values gives probability inside interval.
Normal Distribution
The Normal distribution describes values clustering around a mean $\mu$ with spread measured by standard deviation $\sigma$.
1234567891011121314151617181920import numpy as np import matplotlib.pyplot as plt from scipy.stats import norm mu = 200 sigma = 5 lower, upper = 195, 205 norm_prob = norm.cdf(upper, mu, sigma) - norm.cdf(lower, mu, sigma) z1 = (lower - mu) / sigma z2 = (upper - mu) / sigma # Vizualization x = np.linspace(mu - 4*sigma, mu + 4*sigma, 200) pdf = norm.pdf(x, mu, sigma) plt.plot(x, pdf, color='black') plt.fill_between(x, pdf, where=(x >= lower) & (x <= upper), color='plum', alpha=0.5) plt.title(f'Normal probability: {norm_prob:.4f}\nZ-scores: {z1}, {z2}') plt.show()
mu
- mean rod weight;sigma
- standard deviation;- Probability - CDF difference;
- Z-scores show how far bounds are from mean.
Real-World Application
- Binomial - how likely is a certain number of defective rods?
- Uniform - are rod lengths within tolerance?
- Normal - are rod weights within expected variability?
By combining these, quality control targets defects, ensures precision, and maintains product consistency.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 1.96
Implementing Probability Distributions to Python
Swipe to show menu
Binomial Distribution
The Binomial distribution models the probability of getting exactly k successes in n independent trials, each with probability p of success.
123456789101112131415161718from scipy.stats import binom import matplotlib.pyplot as plt # number of trials n = 100 # probability of success p = 0.02 # number of successes k = 3 binom_prob = binom.pmf(k, n, p) # Vizualization x_vals = range(0, 15) y_vals = binom.pmf(x_vals, n, p) plt.bar(x_vals, y_vals, color='skyblue') plt.title(f'Binomial probability: {binom_prob:.4f}') plt.show()
n = 100
- we're testing 100 rods;p = 0.02
- 2% chance a rod is defective;k = 3
- probability of exactly 3 defectives;binom.pmf()
computes the probability mass function.
Uniform Distribution
The Uniform distribution models a continuous variable where all values between $a$ and $b$ are equally likely.
1234567891011121314151617from scipy.stats import uniform import matplotlib.pyplot as plt import numpy as np a = 49.5 b = 50.5 low, high = 49.8, 50.2 uniform_prob = uniform.cdf(high, a, b - a) - uniform.cdf(low, a, b - a) # Vizualization x = np.linspace(a, b, 100) pdf = uniform.pdf(x, a, b - a) plt.plot(x, pdf, color='black') plt.fill_between(x, pdf, where=(x >= low) & (x <= high), color='lightgreen', alpha=0.5) plt.title(f'Uniform probability: {uniform_prob:.1f}') plt.show()
a, b
- total range of rod lengths;low, high
- interval of interest;- Subtracting CDF values gives probability inside interval.
Normal Distribution
The Normal distribution describes values clustering around a mean $\mu$ with spread measured by standard deviation $\sigma$.
1234567891011121314151617181920import numpy as np import matplotlib.pyplot as plt from scipy.stats import norm mu = 200 sigma = 5 lower, upper = 195, 205 norm_prob = norm.cdf(upper, mu, sigma) - norm.cdf(lower, mu, sigma) z1 = (lower - mu) / sigma z2 = (upper - mu) / sigma # Vizualization x = np.linspace(mu - 4*sigma, mu + 4*sigma, 200) pdf = norm.pdf(x, mu, sigma) plt.plot(x, pdf, color='black') plt.fill_between(x, pdf, where=(x >= lower) & (x <= upper), color='plum', alpha=0.5) plt.title(f'Normal probability: {norm_prob:.4f}\nZ-scores: {z1}, {z2}') plt.show()
mu
- mean rod weight;sigma
- standard deviation;- Probability - CDF difference;
- Z-scores show how far bounds are from mean.
Real-World Application
- Binomial - how likely is a certain number of defective rods?
- Uniform - are rod lengths within tolerance?
- Normal - are rod weights within expected variability?
By combining these, quality control targets defects, ensures precision, and maintains product consistency.
Thanks for your feedback!