Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Implementing Probability Distributions to Python | Probability & Statistics
Mathematics for Data Science

bookImplementing Probability Distributions to Python

Binomial Distribution

The Binomial distribution models the probability of getting exactly kk successes in nn independent trials, each with probability pp of success.

123456789101112131415161718
from 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()
copy
  • 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.

1234567891011121314151617
from 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()
copy
  • 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$.

1234567891011121314151617181920
import 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()
copy
  • 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.

question mark

Which function calculates the probability of exactly k defective rods?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 11

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 1.96

bookImplementing Probability Distributions to Python

Swipe to show menu

Binomial Distribution

The Binomial distribution models the probability of getting exactly kk successes in nn independent trials, each with probability pp of success.

123456789101112131415161718
from 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()
copy
  • 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.

1234567891011121314151617
from 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()
copy
  • 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$.

1234567891011121314151617181920
import 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()
copy
  • 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.

question mark

Which function calculates the probability of exactly k defective rods?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 11
some-alt